DEV: Modernize `<AddTopicStatusClasses>` to use bodyClass helper (#23704)

This commit is contained in:
David Taylor 2023-09-28 17:44:04 +01:00 committed by GitHub
parent 0ec3cc4eb2
commit 43791ea2b5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 50 deletions

View File

@ -0,0 +1,15 @@
import bodyClass from "discourse/helpers/body-class";
const AddTopicStatusClasses = <template>
{{#if @topic.invisible}}
{{bodyClass "topic-status-unlisted"}}
{{/if}}
{{#if @topic.pinned}}
{{bodyClass "topic-status-pinned"}}
{{/if}}
{{#if @topic.unpinned}}
{{bodyClass "topic-status-unpinned"}}
{{/if}}
</template>;
export default AddTopicStatusClasses;

View File

@ -1,50 +0,0 @@
import Component from "@ember/component";
import { scheduleOnce } from "@ember/runloop";
export default Component.extend({
tagName: "",
didInsertElement() {
this._super(...arguments);
this.refreshClass();
},
_updateClass() {
if (this.isDestroying || this.isDestroyed) {
return;
}
const body = document.getElementsByTagName("body")[0];
this._removeClass();
if (this.topic.invisible) {
body.classList.add("topic-status-unlisted");
}
if (this.topic.pinned) {
body.classList.add("topic-status-pinned");
}
if (this.topic.unpinned) {
body.classList.add("topic-status-unpinned");
}
},
didReceiveAttrs() {
this._super(...arguments);
this.refreshClass();
},
refreshClass() {
scheduleOnce("afterRender", this, this._updateClass);
},
_removeClass() {
const regx = new RegExp(/\btopic-status-\S+/, "g");
const body = document.getElementsByTagName("body")[0];
body.className = body.className.replace(regx, "");
},
willDestroyElement() {
this._super(...arguments);
this._removeClass();
},
});