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 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) {