diff --git a/app/assets/javascripts/discourse/components/add-category-class.js.es6 b/app/assets/javascripts/discourse/components/add-category-class.js.es6 new file mode 100644 index 00000000000..7d8ad2dc376 --- /dev/null +++ b/app/assets/javascripts/discourse/components/add-category-class.js.es6 @@ -0,0 +1,34 @@ +import { observes } from 'ember-addons/ember-computed-decorators'; + +export default Ember.Component.extend({ + _slug: null, + + didInsertElement() { + this._super(); + this.refreshClass(); + }, + + _updateClass() { + if (this.isDestroying || this.isDestroyed) { return; } + const slug = this.get('category.fullSlug'); + this._removeClass(); + if (slug) { + $("body").addClass(`category-${slug}`); + } + }, + + @observes('category.fullSlug') + refreshClass() { + Ember.run.scheduleOnce('afterRender', this, this._updateClass); + }, + + _removeClass() { + $("body").removeClass((_, css) => (css.match(/\bcategory-\S+/g) || []).join(" ")); + }, + + willDestroyElement() { + this._super(); + this._removeClass(); + } + +}); diff --git a/app/assets/javascripts/discourse/mixins/add-category-class.js.es6 b/app/assets/javascripts/discourse/mixins/add-category-class.js.es6 deleted file mode 100644 index b3496ee8cbf..00000000000 --- a/app/assets/javascripts/discourse/mixins/add-category-class.js.es6 +++ /dev/null @@ -1,27 +0,0 @@ -// Mix this in to a view that has a `categoryFullSlug` property to automatically -// add it to the body as the view is entered / left / model is changed. -// This is used for keeping the `body` style in sync for the background image. - -import Ember from "ember"; -const { on, observer } = Ember; - -export default { - _categoryChanged: on("didInsertElement", observer("categoryFullSlug", function() { - const categoryFullSlug = this.get("categoryFullSlug"); - - this._removeClass(); - - if (categoryFullSlug) { - $("body").addClass("category-" + categoryFullSlug); - } - })), - - _leave: on("willDestroyElement", function() { - this.removeObserver("categoryFullSlug"); - this._removeClass(); - }), - - _removeClass() { - $("body").removeClass((_, css) => (css.match(/\bcategory-\S+/g) || []).join(" ")); - }, -}; diff --git a/app/assets/javascripts/discourse/templates/navigation/category.hbs b/app/assets/javascripts/discourse/templates/navigation/category.hbs index a18531d5bbd..e7f01c1cd1b 100644 --- a/app/assets/javascripts/discourse/templates/navigation/category.hbs +++ b/app/assets/javascripts/discourse/templates/navigation/category.hbs @@ -1,3 +1,5 @@ +{{add-category-class category=category}} +
{{#if category.logo_url}} {{cdn-img src=category.logo_url class="category-logo"}} diff --git a/app/assets/javascripts/discourse/templates/topic.hbs b/app/assets/javascripts/discourse/templates/topic.hbs index 625155eae71..af3c187fdeb 100644 --- a/app/assets/javascripts/discourse/templates/topic.hbs +++ b/app/assets/javascripts/discourse/templates/topic.hbs @@ -1,4 +1,6 @@ + {{#if model}} + {{add-category-class category=model.category}}
{{discourse-banner user=currentUser banner=site.banner overlay=hasScrolled hide=model.errorLoading}}
diff --git a/app/assets/javascripts/discourse/views/navigation-category.js.es6 b/app/assets/javascripts/discourse/views/navigation-category.js.es6 deleted file mode 100644 index 6a871ecae3a..00000000000 --- a/app/assets/javascripts/discourse/views/navigation-category.js.es6 +++ /dev/null @@ -1,5 +0,0 @@ -import AddCategoryClass from 'discourse/mixins/add-category-class'; - -export default Ember.View.extend(AddCategoryClass, { - categoryFullSlug: Ember.computed.alias('controller.category.fullSlug') -}); diff --git a/app/assets/javascripts/discourse/views/topic.js.es6 b/app/assets/javascripts/discourse/views/topic.js.es6 index e41bd71130e..4e91f706500 100644 --- a/app/assets/javascripts/discourse/views/topic.js.es6 +++ b/app/assets/javascripts/discourse/views/topic.js.es6 @@ -1,10 +1,9 @@ -import AddCategoryClass from 'discourse/mixins/add-category-class'; import AddArchetypeClass from 'discourse/mixins/add-archetype-class'; import ClickTrack from 'discourse/lib/click-track'; import Scrolling from 'discourse/mixins/scrolling'; import { selectedText } from 'discourse/lib/utilities'; -const TopicView = Ember.View.extend(AddCategoryClass, AddArchetypeClass, Scrolling, { +const TopicView = Ember.View.extend(AddArchetypeClass, Scrolling, { templateName: 'topic', topic: Ember.computed.alias('controller.model'), @@ -18,7 +17,6 @@ const TopicView = Ember.View.extend(AddCategoryClass, AddArchetypeClass, Scrolli menuVisible: true, SHORT_POST: 1200, - categoryFullSlug: Em.computed.alias('topic.category.fullSlug'), postStream: Em.computed.alias('topic.postStream'), archetype: Em.computed.alias('topic.archetype'),