From ca8046c7c3f3d22124a1bd8d34d81ddc6c32a9e2 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Wed, 9 Sep 2015 15:51:43 -0400 Subject: [PATCH] FIX: Switch out topic template if the user hasn't changed it --- .../discourse/models/composer.js.es6 | 50 +++++++++++++++---- 1 file changed, 39 insertions(+), 11 deletions(-) diff --git a/app/assets/javascripts/discourse/models/composer.js.es6 b/app/assets/javascripts/discourse/models/composer.js.es6 index 8e02989c878..12cba4936a7 100644 --- a/app/assets/javascripts/discourse/models/composer.js.es6 +++ b/app/assets/javascripts/discourse/models/composer.js.es6 @@ -3,6 +3,7 @@ import Topic from 'discourse/models/topic'; import { throwAjaxError } from 'discourse/lib/ajax-error'; import Quote from 'discourse/lib/quote'; import Draft from 'discourse/models/draft'; +import computed from 'ember-addons/ember-computed-decorators'; const CLOSED = 'closed', SAVING = 'saving', @@ -35,11 +36,32 @@ const CLOSED = 'closed', }; const Composer = RestModel.extend({ + _categoryId: null, archetypes: function() { return this.site.get('archetypes'); }.property(), + + @computed + categoryId: { + get() { return this._categoryId; }, + + // We wrap categoryId this way so we can fire `applyTopicTemplate` with + // the previous value as well as the new value + set(categoryId) { + const oldCategoryId = this._categoryId; + + if (Ember.isEmpty(categoryId)) { categoryId = null; } + this._categoryId = categoryId; + + if (oldCategoryId !== categoryId) { + this.applyTopicTemplate(oldCategoryId, categoryId); + } + return categoryId; + } + }, + creatingTopic: Em.computed.equal('action', CREATE_TOPIC), creatingPrivateMessage: Em.computed.equal('action', PRIVATE_MESSAGE), notCreatingPrivateMessage: Em.computed.not('creatingPrivateMessage'), @@ -56,6 +78,7 @@ const Composer = RestModel.extend({ viewOpen: Em.computed.equal('composeState', OPEN), viewDraft: Em.computed.equal('composeState', DRAFT), + composeStateChanged: function() { var oldOpen = this.get('composerOpened'); @@ -339,20 +362,24 @@ const Composer = RestModel.extend({ this.keyValueStore.set({ key: 'composer.showPreview', value: this.get('showPreview') }); }, - applyTopicTemplate: function() { + applyTopicTemplate(oldCategoryId, categoryId) { if (this.get('action') !== CREATE_TOPIC) { return; } - if (!Ember.isEmpty(this.get('reply'))) { return; } + let reply = this.get('reply'); - const categoryId = this.get('categoryId'); - const category = this.site.categories.find((c) => c.get('id') === categoryId); - if (category) { - const topicTemplate = category.get('topic_template'); - if (!Ember.isEmpty(topicTemplate)) { - this.set('reply', topicTemplate); + // If the user didn't change the template, clear it + if (oldCategoryId) { + const oldCat = this.site.categories.findProperty('id', oldCategoryId); + if (oldCat && (oldCat.get('topic_template') === reply)) { + reply = ""; } } - }.observes('categoryId'), + if (!Ember.isEmpty(reply)) { return; } + const category = this.site.categories.findProperty('id', categoryId); + if (category) { + this.set('reply', category.get('topic_template') || ""); + } + }, /* Open a composer @@ -397,14 +424,15 @@ const Composer = RestModel.extend({ } } - const categoryId = opts.categoryId || this.get('topic.category.id'); this.setProperties({ - categoryId, archetypeId: opts.archetypeId || this.site.get('default_archetype'), metaData: opts.metaData ? Em.Object.create(opts.metaData) : null, reply: opts.reply || this.get("reply") || "" }); + // We set the category id separately for topic templates on opening of composer + this.set('categoryId', opts.categoryId || this.get('topic.category.id')); + if (opts.postId) { this.set('loading', true); this.store.find('post', opts.postId).then(function(post) {