This repository has been archived on 2023-03-18. You can view files and clone it, but cannot push or open issues or pull requests.
osr-discourse-src/app/assets/javascripts/discourse/components/add-category-class.js.es6
2018-06-15 17:03:24 +02:00

38 lines
758 B
JavaScript

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();
}
});