2014-12-20 14:26:46 +08:00
|
|
|
import Ember from 'ember';
|
|
|
|
|
2015-01-16 14:56:10 +08:00
|
|
|
import PostStream from '../models/post-stream';
|
2015-02-02 14:27:59 +08:00
|
|
|
import ComposerReply from '../components/discussions/composer-reply';
|
|
|
|
import ActionButton from '../components/ui/controls/action-button';
|
2015-02-03 14:32:46 +08:00
|
|
|
import AlertMessage from '../components/alert-message';
|
2014-12-20 14:26:46 +08:00
|
|
|
|
|
|
|
export default Ember.ObjectController.extend(Ember.Evented, {
|
|
|
|
|
|
|
|
needs: ['application', 'composer'],
|
|
|
|
|
|
|
|
queryParams: ['start'],
|
|
|
|
start: '1',
|
|
|
|
searchQuery: '',
|
|
|
|
|
|
|
|
loaded: false,
|
2015-01-07 14:53:27 +08:00
|
|
|
stream: null,
|
2014-12-20 14:26:46 +08:00
|
|
|
|
|
|
|
setup: function(discussion) {
|
|
|
|
this.set('model', discussion);
|
|
|
|
|
|
|
|
// Set up the post stream object. It needs to know about the discussion
|
|
|
|
// its representing the posts for, and we also need to inject the Ember
|
|
|
|
// data store.
|
2015-01-16 14:56:10 +08:00
|
|
|
var stream = PostStream.create();
|
2015-01-07 14:53:27 +08:00
|
|
|
stream.set('discussion', discussion);
|
|
|
|
stream.set('store', this.get('store'));
|
|
|
|
this.set('stream', stream);
|
2014-12-20 14:26:46 +08:00
|
|
|
|
|
|
|
// Next, we need to load a list of the discussion's post IDs into the
|
|
|
|
// post stream object. If we don't already have this information, we'll
|
|
|
|
// need to reload the discussion model.
|
|
|
|
var promise = discussion.get('posts') ? Ember.RSVP.resolve(discussion) : discussion.reload();
|
|
|
|
|
|
|
|
// When we know we have the post IDs, we can set up the post stream with
|
2015-01-16 14:56:10 +08:00
|
|
|
// them. Then the view will trigger the stream to load as it sees fit.
|
2014-12-20 14:26:46 +08:00
|
|
|
var controller = this;
|
|
|
|
promise.then(function(discussion) {
|
2015-01-07 14:53:27 +08:00
|
|
|
stream.setup(discussion.get('postIds'));
|
2014-12-20 14:26:46 +08:00
|
|
|
controller.set('loaded', true);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2015-02-03 14:32:46 +08:00
|
|
|
// Save a reply. This may be called by a composer-reply component that was
|
|
|
|
// set up on a different discussion, so we require a discussion model to
|
|
|
|
// be explicitly passed rather than using the controller's implicit one.
|
2015-02-02 14:27:59 +08:00
|
|
|
saveReply: function(discussion, content) {
|
|
|
|
var controller = this;
|
|
|
|
var composer = this.get('controllers.composer');
|
|
|
|
var stream = this.get('stream');
|
|
|
|
|
|
|
|
composer.set('content.loading', true);
|
2015-02-03 14:32:46 +08:00
|
|
|
controller.get('controllers.application').send('clearAlerts');
|
2015-02-02 14:27:59 +08:00
|
|
|
|
|
|
|
var post = this.store.createRecord('post', {
|
|
|
|
content: content,
|
|
|
|
discussion: discussion
|
|
|
|
});
|
|
|
|
|
2015-02-03 14:32:46 +08:00
|
|
|
return post.save().then(function(post) {
|
|
|
|
composer.send('hide');
|
|
|
|
|
|
|
|
// If we're currently viewing the discussion which this reply was
|
|
|
|
// made in, then we can add the post to the end of the post
|
|
|
|
// stream.
|
|
|
|
discussion.set('posts', discussion.get('posts')+','+post.get('id'));
|
2015-02-02 14:27:59 +08:00
|
|
|
if (discussion == controller.get('model')) {
|
2015-02-03 14:32:46 +08:00
|
|
|
stream.set('ids', discussion.get('postIds'));
|
2015-02-02 14:27:59 +08:00
|
|
|
stream.addPostToEnd(post);
|
2015-02-03 14:32:46 +08:00
|
|
|
} else {
|
|
|
|
// Otherwise, we'll create an alert message to inform the user
|
|
|
|
// that their reply has been posted, containing a button which
|
|
|
|
// will transition to their new post when clicked.
|
|
|
|
var message = AlertMessage.create({
|
|
|
|
type: 'success',
|
|
|
|
message: 'Your reply was posted.'
|
|
|
|
});
|
|
|
|
message.on('populateControls', function(controls) {
|
|
|
|
controls.pushObjectWithTag(ActionButton.extend({
|
|
|
|
label: 'View',
|
|
|
|
action: function() {
|
|
|
|
controller.transitionToRoute('discussion', post.get('discussion'), {queryParams: {start: post.get('number')}});
|
|
|
|
message.send('dismiss');
|
|
|
|
}
|
|
|
|
}), 'view');
|
|
|
|
});
|
|
|
|
controller.get('controllers.application').send('alert', message);
|
2015-02-02 14:27:59 +08:00
|
|
|
}
|
2015-02-03 14:32:46 +08:00
|
|
|
},
|
|
|
|
function(reason) {
|
|
|
|
var message = AlertMessage.create({
|
|
|
|
type: 'warning',
|
|
|
|
message: reason.errors[0].detail
|
|
|
|
});
|
|
|
|
controller.get('controllers.application').send('alert', message);
|
|
|
|
})
|
|
|
|
.finally(function() {
|
2015-02-02 14:27:59 +08:00
|
|
|
composer.set('content.loading', false);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2014-12-20 14:26:46 +08:00
|
|
|
actions: {
|
|
|
|
reply: function() {
|
2015-02-02 14:27:59 +08:00
|
|
|
var controller = this;
|
|
|
|
var discussion = this.get('model');
|
2015-01-30 09:52:19 +08:00
|
|
|
var composer = this.get('controllers.composer');
|
2015-02-03 14:32:46 +08:00
|
|
|
|
|
|
|
// If the composer is already set up for this discussion, then we
|
|
|
|
// don't need to change its content - we can just show it.
|
2015-02-02 14:27:59 +08:00
|
|
|
if (composer.get('content.discussion') != discussion) {
|
|
|
|
composer.switchContent(ComposerReply.create({
|
|
|
|
user: controller.get('session.user'),
|
|
|
|
discussion: discussion,
|
|
|
|
submit: function(value) {
|
2015-02-03 14:32:46 +08:00
|
|
|
controller.saveReply(discussion, value);
|
2015-02-02 14:27:59 +08:00
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
2015-02-03 14:32:46 +08:00
|
|
|
|
2015-02-02 14:27:59 +08:00
|
|
|
composer.send('show');
|
2014-12-20 14:26:46 +08:00
|
|
|
},
|
|
|
|
|
2015-01-16 14:56:10 +08:00
|
|
|
// This action is called when the start position of the discussion
|
|
|
|
// currently being viewed changes (i.e. when the user scrolls up/down
|
|
|
|
// the post stream.)
|
|
|
|
updateStart: function(start) {
|
|
|
|
this.set('start', start);
|
2014-12-20 14:26:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|