diff --git a/app/assets/javascripts/discourse/controllers/composer_controller.js b/app/assets/javascripts/discourse/controllers/composer_controller.js index 7b59ccc04..99805553f 100644 --- a/app/assets/javascripts/discourse/controllers/composer_controller.js +++ b/app/assets/javascripts/discourse/controllers/composer_controller.js @@ -7,10 +7,16 @@ @module Discourse **/ Discourse.ComposerController = Discourse.Controller.extend({ - needs: ['modal', 'topic'], + needs: ['modal', 'topic', 'composerMessages'], replyAsNewTopicDraft: Em.computed.equal('model.draftKey', Discourse.Composer.REPLY_AS_NEW_TOPIC_KEY), + + init: function() { + this._super(); + this.set('similarTopics', Em.A()); + }, + togglePreview: function() { this.get('model').togglePreview(); }, @@ -94,7 +100,6 @@ Discourse.ComposerController = Discourse.Controller.extend({ composerController.destroyDraft(); } - opts = opts || {}; composerController.close(); @@ -112,51 +117,27 @@ Discourse.ComposerController = Discourse.Controller.extend({ }); }, - closeEducation: function() { - this.set('educationClosed', true); - }, - - closeSimilar: function() { - this.set('similarClosed', true); - }, - - similarVisible: function() { - if (this.get('similarClosed')) return false; - if (this.get('model.composeState') !== Discourse.Composer.OPEN) return false; - return (this.get('similarTopics.length') || 0) > 0; - }.property('similarTopics.length', 'similarClosed', 'model.composeState'), - - newUserEducationVisible: function() { - if (!this.get('educationContents')) return false; - if (this.get('model.composeState') !== Discourse.Composer.OPEN) return false; - if (!this.present('model.reply')) return false; - if (this.get('educationClosed')) return false; - return true; - }.property('model.composeState', 'model.reply', 'educationClosed', 'educationContents'), - - fetchNewUserEducation: function() { + _considerNewUserEducation: function() { // We don't show education when editing a post. if (this.get('model.editingPost')) return; // If creating a topic, use topic_count, otherwise post_count var count = this.get('model.creatingTopic') ? Discourse.User.currentProp('topic_count') : Discourse.User.currentProp('reply_count'); - if (count >= Discourse.SiteSettings.educate_until_posts) { - this.set('educationClosed', true); - this.set('educationContents', ''); - return; - } + if (count >= Discourse.SiteSettings.educate_until_posts) { return; } // The user must have typed a reply if (!this.get('typedReply')) return; - this.set('educationClosed', false); - // If visible update the text - var educationKey = this.get('model.creatingTopic') ? 'new-topic' : 'new-reply'; - var composerController = this; + var educationKey = this.get('model.creatingTopic') ? 'new-topic' : 'new-reply', + messageController = this.get('controllers.composerMessages'); + Discourse.ajax("/education/" + educationKey, {dataType: 'html'}).then(function(result) { - composerController.set('educationContents', result); + messageController.popup({ + templateName: 'composer/education', + body: result + }); }); }.observes('typedReply', 'model.creatingTopic', 'currentUser.reply_count'), @@ -176,16 +157,25 @@ Discourse.ComposerController = Discourse.Controller.extend({ // We don't care about similar topics unless creating a topic if (!this.get('model.creatingTopic')) return; - var body = this.get('model.reply'); - var title = this.get('model.title'); + var body = this.get('model.reply'), + title = this.get('model.title'); // Ensure the fields are of the minimum length if (body.length < Discourse.SiteSettings.min_body_similar_length) return; if (title.length < Discourse.SiteSettings.min_title_similar_length) return; - var composerController = this; - Discourse.Topic.findSimilarTo(title, body).then(function (topics) { - composerController.set('similarTopics', topics); + var messageController = this.get('controllers.composerMessages'), + similarTopics = this.get('similarTopics'); + + Discourse.Topic.findSimilarTo(title, body).then(function (newTopics) { + similarTopics.clear(); + similarTopics.pushObjects(newTopics); + + messageController.popup({ + templateName: 'composer/similar_topics', + similarTopics: similarTopics, + extraClass: 'similar-topics' + }); }); }, @@ -211,8 +201,6 @@ Discourse.ComposerController = Discourse.Controller.extend({ var promise = opts.promise || Ember.Deferred.create(); opts.promise = promise; this.set('typedReply', false); - this.set('similarTopics', null); - this.set('similarClosed', false); if (!opts.draftKey) { alert("composer was opened without a draft key"); diff --git a/app/assets/javascripts/discourse/controllers/composer_messages_controller.js b/app/assets/javascripts/discourse/controllers/composer_messages_controller.js new file mode 100644 index 000000000..1f4b1bcf1 --- /dev/null +++ b/app/assets/javascripts/discourse/controllers/composer_messages_controller.js @@ -0,0 +1,31 @@ +/** + A controller for displaying messages as the user composes a message. + + @class ComposerMessagesController + @extends Ember.ArrayController + @namespace Discourse + @module Discourse +**/ +Discourse.ComposerMessagesController = Ember.ArrayController.extend({ + needs: ['composer'], + + init: function() { + this._super(); + this.set('messagesByTemplate', {}); + }, + + popup: function(msg) { + var messagesByTemplate = this.get('messagesByTemplate'), + existing = messagesByTemplate[msg.templateName]; + + if (!existing) { + this.pushObject(msg); + messagesByTemplate[msg.templateName] = msg; + } + }, + + closeMessage: function(message) { + this.removeObject(message); + } + +}); \ No newline at end of file diff --git a/app/assets/javascripts/discourse/templates/composer.js.handlebars b/app/assets/javascripts/discourse/templates/composer.js.handlebars index 3064aeebf..daed39384 100644 --- a/app/assets/javascripts/discourse/templates/composer.js.handlebars +++ b/app/assets/javascripts/discourse/templates/composer.js.handlebars @@ -2,23 +2,7 @@