FIX: Switch out topic template if the user hasn't changed it

This commit is contained in:
Robin Ward 2015-09-09 15:51:43 -04:00
parent 3ceadbd5b8
commit ca8046c7c3
1 changed files with 39 additions and 11 deletions

View File

@ -3,6 +3,7 @@ import Topic from 'discourse/models/topic';
import { throwAjaxError } from 'discourse/lib/ajax-error'; import { throwAjaxError } from 'discourse/lib/ajax-error';
import Quote from 'discourse/lib/quote'; import Quote from 'discourse/lib/quote';
import Draft from 'discourse/models/draft'; import Draft from 'discourse/models/draft';
import computed from 'ember-addons/ember-computed-decorators';
const CLOSED = 'closed', const CLOSED = 'closed',
SAVING = 'saving', SAVING = 'saving',
@ -35,11 +36,32 @@ const CLOSED = 'closed',
}; };
const Composer = RestModel.extend({ const Composer = RestModel.extend({
_categoryId: null,
archetypes: function() { archetypes: function() {
return this.site.get('archetypes'); return this.site.get('archetypes');
}.property(), }.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), creatingTopic: Em.computed.equal('action', CREATE_TOPIC),
creatingPrivateMessage: Em.computed.equal('action', PRIVATE_MESSAGE), creatingPrivateMessage: Em.computed.equal('action', PRIVATE_MESSAGE),
notCreatingPrivateMessage: Em.computed.not('creatingPrivateMessage'), notCreatingPrivateMessage: Em.computed.not('creatingPrivateMessage'),
@ -56,6 +78,7 @@ const Composer = RestModel.extend({
viewOpen: Em.computed.equal('composeState', OPEN), viewOpen: Em.computed.equal('composeState', OPEN),
viewDraft: Em.computed.equal('composeState', DRAFT), viewDraft: Em.computed.equal('composeState', DRAFT),
composeStateChanged: function() { composeStateChanged: function() {
var oldOpen = this.get('composerOpened'); var oldOpen = this.get('composerOpened');
@ -339,20 +362,24 @@ const Composer = RestModel.extend({
this.keyValueStore.set({ key: 'composer.showPreview', value: this.get('showPreview') }); this.keyValueStore.set({ key: 'composer.showPreview', value: this.get('showPreview') });
}, },
applyTopicTemplate: function() { applyTopicTemplate(oldCategoryId, categoryId) {
if (this.get('action') !== CREATE_TOPIC) { return; } if (this.get('action') !== CREATE_TOPIC) { return; }
if (!Ember.isEmpty(this.get('reply'))) { return; } let reply = this.get('reply');
const categoryId = this.get('categoryId'); // If the user didn't change the template, clear it
const category = this.site.categories.find((c) => c.get('id') === categoryId); if (oldCategoryId) {
const oldCat = this.site.categories.findProperty('id', oldCategoryId);
if (oldCat && (oldCat.get('topic_template') === reply)) {
reply = "";
}
}
if (!Ember.isEmpty(reply)) { return; }
const category = this.site.categories.findProperty('id', categoryId);
if (category) { if (category) {
const topicTemplate = category.get('topic_template'); this.set('reply', category.get('topic_template') || "");
if (!Ember.isEmpty(topicTemplate)) {
this.set('reply', topicTemplate);
} }
} },
}.observes('categoryId'),
/* /*
Open a composer Open a composer
@ -397,14 +424,15 @@ const Composer = RestModel.extend({
} }
} }
const categoryId = opts.categoryId || this.get('topic.category.id');
this.setProperties({ this.setProperties({
categoryId,
archetypeId: opts.archetypeId || this.site.get('default_archetype'), archetypeId: opts.archetypeId || this.site.get('default_archetype'),
metaData: opts.metaData ? Em.Object.create(opts.metaData) : null, metaData: opts.metaData ? Em.Object.create(opts.metaData) : null,
reply: opts.reply || this.get("reply") || "" 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) { if (opts.postId) {
this.set('loading', true); this.set('loading', true);
this.store.find('post', opts.postId).then(function(post) { this.store.find('post', opts.postId).then(function(post) {