From 2029777b8696850e08454c0518228bc51f505f32 Mon Sep 17 00:00:00 2001 From: Jarek Radosz Date: Mon, 4 Dec 2023 12:53:07 +0100 Subject: [PATCH] DEV: Replace AdminWrapper with body-class/html-class (#24683) AdminWrapper isn't used (directly) in all-the* Co-authored-by: David Taylor --- .../admin/addon/components/admin-wrapper.js | 15 ---- .../admin/addon/templates/admin-revamp.hbs | 17 ++-- .../admin/addon/templates/admin.hbs | 79 ++++++++++--------- .../discourse/app/helpers/body-class.js | 5 +- .../discourse/app/helpers/html-class.js | 14 ++++ .../{body-classes.js => element-classes.js} | 21 ++--- 6 files changed, 77 insertions(+), 74 deletions(-) delete mode 100644 app/assets/javascripts/admin/addon/components/admin-wrapper.js create mode 100644 app/assets/javascripts/discourse/app/helpers/html-class.js rename app/assets/javascripts/discourse/app/services/{body-classes.js => element-classes.js} (56%) diff --git a/app/assets/javascripts/admin/addon/components/admin-wrapper.js b/app/assets/javascripts/admin/addon/components/admin-wrapper.js deleted file mode 100644 index 98d18997d4f..00000000000 --- a/app/assets/javascripts/admin/addon/components/admin-wrapper.js +++ /dev/null @@ -1,15 +0,0 @@ -import Component from "@ember/component"; - -export default class AdminWrapper extends Component { - didInsertElement() { - super.didInsertElement(...arguments); - document.querySelector("html").classList.add("admin-area"); - document.querySelector("body").classList.add("admin-interface"); - } - - willDestroyElement() { - super.willDestroyElement(...arguments); - document.querySelector("html").classList.remove("admin-area"); - document.querySelector("body").classList.remove("admin-interface"); - } -} diff --git a/app/assets/javascripts/admin/addon/templates/admin-revamp.hbs b/app/assets/javascripts/admin/addon/templates/admin-revamp.hbs index 9e53b938259..0c2000916c7 100644 --- a/app/assets/javascripts/admin/addon/templates/admin-revamp.hbs +++ b/app/assets/javascripts/admin/addon/templates/admin-revamp.hbs @@ -1,12 +1,13 @@ {{hide-application-footer}} - -
-
-
-
- {{outlet}} -
+{{html-class "admin-area"}} +{{body-class "admin-interface"}} + +
+
+
+
+ {{outlet}}
- \ No newline at end of file +
\ No newline at end of file diff --git a/app/assets/javascripts/admin/addon/templates/admin.hbs b/app/assets/javascripts/admin/addon/templates/admin.hbs index ca0fbe7a1ba..d03ada8566a 100644 --- a/app/assets/javascripts/admin/addon/templates/admin.hbs +++ b/app/assets/javascripts/admin/addon/templates/admin.hbs @@ -1,44 +1,45 @@ {{hide-application-footer}} - -
-
-
- -
+{{html-class "admin-area"}} +{{body-class "admin-interface"}} -
-
- {{outlet}} -
+
+
+
+ +
+ +
+
+ {{outlet}}
- \ No newline at end of file +
\ No newline at end of file diff --git a/app/assets/javascripts/discourse/app/helpers/body-class.js b/app/assets/javascripts/discourse/app/helpers/body-class.js index c7322b725ff..b541b312d52 100644 --- a/app/assets/javascripts/discourse/app/helpers/body-class.js +++ b/app/assets/javascripts/discourse/app/helpers/body-class.js @@ -2,11 +2,12 @@ import Helper from "@ember/component/helper"; import { inject as service } from "@ember/service"; export default class BodyClass extends Helper { - @service bodyClasses; + @service elementClasses; compute([...classes]) { - this.bodyClasses.registerClasses( + this.elementClasses.registerClasses( this, + document.body, classes.flatMap((c) => c?.split(" ")).filter(Boolean) ); } diff --git a/app/assets/javascripts/discourse/app/helpers/html-class.js b/app/assets/javascripts/discourse/app/helpers/html-class.js new file mode 100644 index 00000000000..6c0cf124be7 --- /dev/null +++ b/app/assets/javascripts/discourse/app/helpers/html-class.js @@ -0,0 +1,14 @@ +import Helper from "@ember/component/helper"; +import { inject as service } from "@ember/service"; + +export default class HtmlClass extends Helper { + @service elementClasses; + + compute([...classes]) { + this.elementClasses.registerClasses( + this, + document.documentElement, + classes.flatMap((c) => c?.split(" ")).filter(Boolean) + ); + } +} diff --git a/app/assets/javascripts/discourse/app/services/body-classes.js b/app/assets/javascripts/discourse/app/services/element-classes.js similarity index 56% rename from app/assets/javascripts/discourse/app/services/body-classes.js rename to app/assets/javascripts/discourse/app/services/element-classes.js index b9b9663aa55..2d015bd0dd5 100644 --- a/app/assets/javascripts/discourse/app/services/body-classes.js +++ b/app/assets/javascripts/discourse/app/services/element-classes.js @@ -3,36 +3,37 @@ import Service from "@ember/service"; import { disableImplicitInjections } from "discourse/lib/implicit-injections"; @disableImplicitInjections -export default class BodyClassesService extends Service { +export default class ElementClassesService extends Service { + /** @type Map */ #helpers = new Map(); - registerClasses(helper, classes) { + registerClasses(helper, element, classes) { if (this.#helpers.has(helper)) { const previousClasses = this.#helpers.get(helper); this.#helpers.set(helper, classes); - this.removeUnusedClasses(previousClasses); + this.removeUnusedClasses(element, previousClasses); } else { this.#helpers.set(helper, classes); registerDestructor(helper, () => { const previousClasses = this.#helpers.get(helper); this.#helpers.delete(helper); - this.removeUnusedClasses(previousClasses); + this.removeUnusedClasses(element, previousClasses); }); } - for (const bodyClass of classes) { - document.body.classList.add(bodyClass); + for (const elementClass of classes) { + element.classList.add(elementClass); } } - removeUnusedClasses(classes) { + removeUnusedClasses(element, classes) { const remainingClasses = new Set([...this.#helpers.values()].flat()); - for (const bodyClass of classes) { - if (!remainingClasses.has(bodyClass)) { - document.body.classList.remove(bodyClass); + for (const elementClass of classes) { + if (!remainingClasses.has(elementClass)) { + element.classList.remove(elementClass); } } }