discourse/app/assets/javascripts/discourse/controllers/merge-topic.js.es6

67 lines
2.1 KiB
Plaintext
Raw Normal View History

2015-05-12 18:54:28 +08:00
import SelectedPostsCount from 'discourse/mixins/selected-posts-count';
2014-08-13 07:04:36 +08:00
import ModalFunctionality from 'discourse/mixins/modal-functionality';
2015-08-01 04:30:18 +08:00
import { movePosts, mergeTopic } from 'discourse/models/topic';
2015-08-11 05:11:27 +08:00
import DiscourseURL from 'discourse/lib/url';
2014-08-13 07:04:36 +08:00
// Modal related to merging of topics
export default Ember.Controller.extend(SelectedPostsCount, ModalFunctionality, {
topicController: Ember.inject.controller('topic'),
2014-05-09 23:57:19 +08:00
2015-08-01 04:30:18 +08:00
saving: false,
selectedTopicId: null,
2014-05-09 23:57:19 +08:00
selectedPosts: Em.computed.alias('topicController.selectedPosts'),
selectedReplies: Em.computed.alias('topicController.selectedReplies'),
allPostsSelected: Em.computed.alias('topicController.allPostsSelected'),
buttonDisabled: function() {
if (this.get('saving')) return true;
return Ember.isEmpty(this.get('selectedTopicId'));
2014-05-09 23:57:19 +08:00
}.property('selectedTopicId', 'saving'),
buttonTitle: function() {
if (this.get('saving')) return I18n.t('saving');
return I18n.t('topic.merge_topic.title');
}.property('saving'),
2015-08-01 04:30:18 +08:00
onShow() {
this.set('modal.modalClass', 'split-modal');
2014-05-09 23:57:19 +08:00
},
actions: {
2015-08-01 04:30:18 +08:00
movePostsToExistingTopic() {
const topicId = this.get('model.id');
2014-05-09 23:57:19 +08:00
this.set('saving', true);
2015-08-01 04:30:18 +08:00
let promise = null;
2014-05-09 23:57:19 +08:00
if (this.get('allPostsSelected')) {
2015-08-01 04:30:18 +08:00
promise = mergeTopic(topicId, this.get('selectedTopicId'));
2014-05-09 23:57:19 +08:00
} else {
2015-08-01 04:30:18 +08:00
const postIds = this.get('selectedPosts').map(function(p) { return p.get('id'); });
const replyPostIds = this.get('selectedReplies').map(function(p) { return p.get('id'); });
2014-05-09 23:57:19 +08:00
2015-08-01 04:30:18 +08:00
promise = movePosts(topicId, {
2014-05-09 23:57:19 +08:00
destination_topic_id: this.get('selectedTopicId'),
post_ids: postIds,
reply_post_ids: replyPostIds
});
}
2015-08-01 04:30:18 +08:00
const self = this;
2014-05-09 23:57:19 +08:00
promise.then(function(result) {
// Posts moved
2015-08-01 04:30:18 +08:00
self.send('closeModal');
self.get('topicController').send('toggleMultiSelect');
2015-08-11 05:11:27 +08:00
Em.run.next(function() { DiscourseURL.routeTo(result.url); });
2015-08-01 04:30:18 +08:00
}).catch(function() {
self.flash(I18n.t('topic.merge_topic.error'));
}).finally(function() {
self.set('saving', false);
2014-05-09 23:57:19 +08:00
});
return false;
}
}
});