2014-12-20 14:26:46 +08:00
|
|
|
import Ember from 'ember';
|
|
|
|
|
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
|
|
|
|
2015-02-08 13:21:40 +08:00
|
|
|
export default Ember.Controller.extend(Ember.Evented, {
|
|
|
|
needs: ['application', 'alerts', 'composer'],
|
2014-12-20 14:26:46 +08:00
|
|
|
|
|
|
|
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
|
|
|
|
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-06 11:36:22 +08:00
|
|
|
// @todo break this down into bite-sized functions so that extensions can
|
|
|
|
// easily override where they please.
|
|
|
|
saveReply: function(discussion, data) {
|
2015-02-02 14:27:59 +08:00
|
|
|
var controller = this;
|
|
|
|
var composer = this.get('controllers.composer');
|
|
|
|
var stream = this.get('stream');
|
|
|
|
|
|
|
|
composer.set('content.loading', true);
|
2015-02-08 13:21:40 +08:00
|
|
|
controller.get('controllers.alerts').send('clearAlerts');
|
2015-02-02 14:27:59 +08:00
|
|
|
|
|
|
|
var post = this.store.createRecord('post', {
|
2015-02-06 11:36:22 +08:00
|
|
|
content: data.content,
|
2015-02-02 14:27:59 +08:00
|
|
|
discussion: discussion
|
|
|
|
});
|
|
|
|
|
2015-02-03 14:32:46 +08:00
|
|
|
return post.save().then(function(post) {
|
|
|
|
composer.send('hide');
|
|
|
|
|
2015-02-03 16:11:03 +08:00
|
|
|
discussion.setProperties({
|
|
|
|
lastTime: post.get('time'),
|
|
|
|
lastUser: post.get('user'),
|
|
|
|
lastPost: post,
|
|
|
|
lastPostNumber: post.get('number'),
|
|
|
|
commentsCount: discussion.get('commentsCount') + 1,
|
|
|
|
readTime: post.get('time'),
|
|
|
|
readNumber: post.get('number')
|
|
|
|
});
|
|
|
|
|
2015-02-03 14:32:46 +08:00
|
|
|
// 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.
|
2015-02-06 08:00:38 +08:00
|
|
|
if (discussion == controller.get('model') && stream) {
|
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) {
|
2015-02-08 13:21:40 +08:00
|
|
|
controls.pushObjectWithTag(ActionButton.create({
|
2015-02-03 14:32:46 +08:00
|
|
|
label: 'View',
|
|
|
|
action: function() {
|
|
|
|
controller.transitionToRoute('discussion', post.get('discussion'), {queryParams: {start: post.get('number')}});
|
|
|
|
message.send('dismiss');
|
|
|
|
}
|
|
|
|
}), 'view');
|
|
|
|
});
|
2015-02-08 13:21:40 +08:00
|
|
|
controller.get('controllers.alerts').send('alert', message);
|
2015-02-02 14:27:59 +08:00
|
|
|
}
|
2015-02-03 14:32:46 +08:00
|
|
|
},
|
|
|
|
function(reason) {
|
2015-02-03 16:10:34 +08:00
|
|
|
var errors = reason.errors;
|
|
|
|
for (var i in reason.errors) {
|
|
|
|
var message = AlertMessage.create({
|
|
|
|
type: 'warning',
|
|
|
|
message: reason.errors[i]
|
|
|
|
});
|
2015-02-08 13:21:40 +08:00
|
|
|
controller.get('controllers.alerts').send('alert', message);
|
2015-02-03 16:10:34 +08:00
|
|
|
}
|
2015-02-03 14:32:46 +08:00
|
|
|
})
|
|
|
|
.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-08 13:21:40 +08:00
|
|
|
if (!(composer.get('content') instanceof ComposerReply) || composer.get('content.discussion') !== discussion) {
|
2015-02-02 14:27:59 +08:00
|
|
|
composer.switchContent(ComposerReply.create({
|
|
|
|
user: controller.get('session.user'),
|
|
|
|
discussion: discussion,
|
2015-02-06 11:36:22 +08:00
|
|
|
submit: function(data) {
|
|
|
|
controller.saveReply(discussion, data);
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|