discourse/app/assets/javascripts/discourse/controllers/composer-messages.js.es6

89 lines
2.0 KiB
Plaintext
Raw Normal View History

// A controller for displaying messages as the user composes a message.
export default Ember.ArrayController.extend({
needs: ['composer'],
// Whether we've checked our messages
checkedMessages: false,
2015-02-26 18:07:21 +08:00
init() {
this._super();
this.reset();
},
2013-09-17 02:08:55 +08:00
actions: {
2015-02-26 18:07:21 +08:00
closeMessage(message) {
2013-09-17 02:08:55 +08:00
this.removeObject(message);
},
2015-02-26 18:07:21 +08:00
hideMessage(message) {
this.removeObject(message);
2015-02-26 18:07:21 +08:00
// kind of hacky but the visibility depends on this
this.get('messagesByTemplate')[message.get('templateName')] = undefined;
},
2015-02-26 18:07:21 +08:00
popup(message) {
let messagesByTemplate = this.get('messagesByTemplate');
const templateName = message.get('templateName');
2015-02-26 18:07:21 +08:00
if (!messagesByTemplate[templateName]) {
this.pushObject(message);
messagesByTemplate[templateName] = message;
}
},
},
/**
Resets all active messages. For example if composing a new post.
@method reset
**/
2015-02-26 18:07:21 +08:00
reset() {
this.clear();
2015-02-26 18:07:21 +08:00
this.setProperties({
messagesByTemplate: {},
queuedForTyping: [],
checkedMessages: false
});
},
/**
Called after the user has typed a reply. Some messages only get shown after being
typed.
@method typedReply
**/
2015-02-26 18:07:21 +08:00
typedReply() {
2015-03-05 11:57:31 +08:00
var self = this;
this.get('queuedForTyping').forEach(function(msg){
if(self.popup){
2015-03-05 12:36:08 +08:00
self.popup(msg);
2015-03-05 11:57:31 +08:00
}
});
},
/**
Figure out if there are any messages that should be displayed above the composer.
@method queryFor
@params {Discourse.Composer} composer The composer model
**/
2015-02-26 18:07:21 +08:00
queryFor(composer) {
if (this.get('checkedMessages')) { return; }
2015-02-26 18:07:21 +08:00
const self = this;
let queuedForTyping = self.get('queuedForTyping');
Discourse.ComposerMessage.find(composer).then(function (messages) {
self.set('checkedMessages', true);
messages.forEach(function (msg) {
if (msg.wait_for_typing) {
queuedForTyping.addObject(msg);
} else {
self.popup(msg);
}
});
});
}
});