framework/ember/app/controllers/discussion.js

121 lines
3.9 KiB
JavaScript
Raw Normal View History

2014-12-20 14:26:46 +08:00
import Ember from 'ember';
import ComposerReply from 'flarum/components/composer/composer-reply';
import ActionButton from 'flarum/components/ui/action-button';
import AlertMessage from 'flarum/components/ui/alert-message';
import UseComposerMixin from 'flarum/mixins/use-composer';
2014-12-20 14:26:46 +08:00
export default Ember.Controller.extend(Ember.Evented, UseComposerMixin, {
needs: ['application'],
composer: Ember.inject.controller('composer'),
alerts: Ember.inject.controller('alerts'),
2014-12-20 14:26:46 +08:00
queryParams: ['start'],
start: '1',
searchQuery: '',
2014-12-20 14:26:46 +08:00
loaded: false,
stream: null,
// 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.
// @todo break this down into bite-sized functions so that extensions can
// easily override where they please.
saveReply: function(discussion, data) {
var post = this.store.createRecord('post', {
content: data.content,
discussion: discussion
});
var controller = this;
var stream = this.get('stream');
return this.saveAndDismissComposer(post).then(function(post) {
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.
if (discussion == controller.get('model') && stream) {
stream.addPostToEnd(post);
} 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.',
buttons: [{
label: 'View',
action: function() {
controller.transitionToRoute('discussion', post.get('discussion'), {queryParams: {start: post.get('number')}});
2015-02-03 16:10:34 +08:00
}
}]
});
controller.get('alerts').send('alert', message);
}
});
},
// Whenever we transition to a different discussion or the logged-in user
// changes, we'll need the composer content to refresh next time the reply
// button is clicked.
clearComposerContent: Ember.observer('model', 'session.user', function() {
this.set('composerContent', undefined);
}),
2015-02-03 14:32:46 +08:00
actions: {
reply: function() {
var discussion = this.get('model');
var controller = this;
this.showComposer(function() {
return ComposerReply.create({
user: controller.get('session.user'),
discussion: discussion,
submit: function(data) {
controller.saveReply(discussion, data);
}
});
});
},
2015-02-03 14:32:46 +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.)
positionChanged: function(startNumber, endNumber) {
this.set('start', startNumber);
2014-12-20 14:26:46 +08:00
var discussion = this.get('model');
if (endNumber > discussion.get('readNumber')) {
discussion.set('readNumber', endNumber);
discussion.save();
}
},
postRemoved: function(post) {
this.get('stream').removePost(post);
2015-02-12 12:48:17 +08:00
},
rename: function(title) {
var discussion = this.get('model');
discussion.set('title', title);
discussion.save();
},
delete: function() {
var controller = this;
this.get('model').destroyRecord().then(function() {
controller.transitionToRoute('index');
});
2014-12-20 14:26:46 +08:00
}
}
2014-12-20 14:26:46 +08:00
});