From 7676c5dfe721bcf521a9619b41e05f48c0a45932 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Thu, 2 Jul 2015 16:18:59 -0400 Subject: [PATCH] Can add topic templates to categories, prepopulated on compose --- .../components/category-panel-base.js.es6 | 11 +++++++++++ .../discourse/components/edit-category-tab.js.es6 | 13 ++++++++++--- .../components/edit-category-topic-template.js.es6 | 11 +++++++++++ .../javascripts/discourse/models/category.js | 3 ++- .../javascripts/discourse/models/composer.js.es6 | 13 ++++++++++++- .../templates/components/edit-category-panel.hbs | 1 + .../components/edit-category-topic-template.hbs | 2 ++ .../discourse/templates/modal/edit-category.hbs | 1 + app/assets/stylesheets/common/base/modal.scss | 14 +++++++++++++- app/assets/stylesheets/desktop/modal.scss | 1 + app/controllers/categories_controller.rb | 1 + app/serializers/basic_category_serializer.rb | 3 ++- config/locales/client.en.yml | 1 + ...50702201926_add_topic_template_to_categories.rb | 5 +++++ .../acceptance/category-edit-test.js.es6 | 13 +++++++++++++ 15 files changed, 86 insertions(+), 7 deletions(-) create mode 100644 app/assets/javascripts/discourse/components/category-panel-base.js.es6 create mode 100644 app/assets/javascripts/discourse/components/edit-category-topic-template.js.es6 create mode 100644 app/assets/javascripts/discourse/templates/components/edit-category-panel.hbs create mode 100644 app/assets/javascripts/discourse/templates/components/edit-category-topic-template.hbs create mode 100644 db/migrate/20150702201926_add_topic_template_to_categories.rb diff --git a/app/assets/javascripts/discourse/components/category-panel-base.js.es6 b/app/assets/javascripts/discourse/components/category-panel-base.js.es6 new file mode 100644 index 00000000000..f4f9ec421ba --- /dev/null +++ b/app/assets/javascripts/discourse/components/category-panel-base.js.es6 @@ -0,0 +1,11 @@ +const CategoryPanelBase = Ember.Component.extend({ + classNameBindings: [':modal-tab', 'activeTab::invisible'], +}); + +export default CategoryPanelBase; + +export function buildCategoryPanel(tab, extras) { + return CategoryPanelBase.extend({ + activeTab: Ember.computed.equal('selectedTab', tab) + }, extras || {}); +} diff --git a/app/assets/javascripts/discourse/components/edit-category-tab.js.es6 b/app/assets/javascripts/discourse/components/edit-category-tab.js.es6 index 27bda38cfa9..72c95192ac3 100644 --- a/app/assets/javascripts/discourse/components/edit-category-tab.js.es6 +++ b/app/assets/javascripts/discourse/components/edit-category-tab.js.es6 @@ -1,12 +1,19 @@ export default Em.Component.extend({ tagName: 'li', - classNameBindings: ['active'], + classNameBindings: ['active', 'tabClassName'], + + tabClassName: function() { + return 'edit-category-' + this.get('tab'); + }.property('tab'), active: Discourse.computed.propertyEqual('selectedTab', 'tab'), - title: Discourse.computed.i18n('tab', 'category.%@'), + + title: function() { + return I18n.t('category.' + this.get('tab').replace('-', '_')); + }.property('tab'), _addToCollection: function() { - this.get('panels').addObject('edit-category-' + this.get('tab')); + this.get('panels').addObject(this.get('tabClassName')); }.on('didInsertElement'), actions: { diff --git a/app/assets/javascripts/discourse/components/edit-category-topic-template.js.es6 b/app/assets/javascripts/discourse/components/edit-category-topic-template.js.es6 new file mode 100644 index 00000000000..b5386da9310 --- /dev/null +++ b/app/assets/javascripts/discourse/components/edit-category-topic-template.js.es6 @@ -0,0 +1,11 @@ +import { buildCategoryPanel } from 'discourse/components/edit-category-panel'; + +export default buildCategoryPanel('topic-template', { + _activeTabChanged: function() { + if (this.get('activeTab')) { + Ember.run.schedule('afterRender', function() { + $('#wmd-input').focus(); + }); + } + }.observes('activeTab') +}); diff --git a/app/assets/javascripts/discourse/models/category.js b/app/assets/javascripts/discourse/models/category.js index 8deb5cb0c37..7c3cefc1e55 100644 --- a/app/assets/javascripts/discourse/models/category.js +++ b/app/assets/javascripts/discourse/models/category.js @@ -76,7 +76,8 @@ Discourse.Category = Discourse.Model.extend({ logo_url: this.get('logo_url'), background_url: this.get('background_url'), allow_badges: this.get('allow_badges'), - custom_fields: this.get('custom_fields') + custom_fields: this.get('custom_fields'), + topic_template: this.get('topic_template') }, type: this.get('id') ? 'PUT' : 'POST' }); diff --git a/app/assets/javascripts/discourse/models/composer.js.es6 b/app/assets/javascripts/discourse/models/composer.js.es6 index 4170748632e..839190d6f54 100644 --- a/app/assets/javascripts/discourse/models/composer.js.es6 +++ b/app/assets/javascripts/discourse/models/composer.js.es6 @@ -346,13 +346,24 @@ const Composer = RestModel.extend({ } } + const categoryId = opts.categoryId || this.get('topic.category.id'); this.setProperties({ - categoryId: opts.categoryId || this.get('topic.category.id'), + categoryId, archetypeId: opts.archetypeId || this.site.get('default_archetype'), metaData: opts.metaData ? Em.Object.create(opts.metaData) : null, reply: opts.reply || this.get("reply") || "" }); + if (opts.action === CREATE_TOPIC && 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 (opts.postId) { this.set('loading', true); this.store.find('post', opts.postId).then(function(post) { diff --git a/app/assets/javascripts/discourse/templates/components/edit-category-panel.hbs b/app/assets/javascripts/discourse/templates/components/edit-category-panel.hbs new file mode 100644 index 00000000000..2fe3ab6dd32 --- /dev/null +++ b/app/assets/javascripts/discourse/templates/components/edit-category-panel.hbs @@ -0,0 +1 @@ +{{component customComponent tab=tab selectedTab=selectedTab category=category}} diff --git a/app/assets/javascripts/discourse/templates/components/edit-category-topic-template.hbs b/app/assets/javascripts/discourse/templates/components/edit-category-topic-template.hbs new file mode 100644 index 00000000000..7a410abbd84 --- /dev/null +++ b/app/assets/javascripts/discourse/templates/components/edit-category-topic-template.hbs @@ -0,0 +1,2 @@ + +{{pagedown-editor value=category.topic_template}} diff --git a/app/assets/javascripts/discourse/templates/modal/edit-category.hbs b/app/assets/javascripts/discourse/templates/modal/edit-category.hbs index 3075cadc383..6cde3451daf 100644 --- a/app/assets/javascripts/discourse/templates/modal/edit-category.hbs +++ b/app/assets/javascripts/discourse/templates/modal/edit-category.hbs @@ -6,6 +6,7 @@ {{/unless}} {{edit-category-tab panels=panels selectedTab=selectedTab tab="settings"}} {{edit-category-tab panels=panels selectedTab=selectedTab tab="images"}} + {{edit-category-tab panels=panels selectedTab=selectedTab tab="topic-template"}}