DEV: Refactor clean-dom (#15609)

* Move it out of page-tracking initializer
* Don't use jQuery anymore
* Remove `activeElement` IE10 bug workaround
* Remove `$("html").data("hide-dropdown")` support; it dates back to the "initial" commit (68ad545f0f/app/assets/javascripts/discourse/routes/discourse_route.js.coffee (L26-L27)) and I don't think it was being used for years now
This commit is contained in:
Jarek Radosz 2022-02-01 19:29:53 +01:00 committed by GitHub
parent 2e2fee9da3
commit 2e74f1dd28
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 46 additions and 39 deletions

View File

@ -0,0 +1,46 @@
import { scheduleOnce } from "@ember/runloop";
function _clean(transition) {
if (window.MiniProfiler && transition.from) {
window.MiniProfiler.pageTransition();
}
// Close some elements that may be open
document.querySelectorAll("header ul.icons li").forEach((element) => {
element.classList.remove("active");
});
document.querySelectorAll(`[data-toggle="dropdown"]`).forEach((element) => {
element.parentElement.classList.remove("open");
});
// Close the lightbox
if ($.magnificPopup?.instance) {
$.magnificPopup.instance.close();
document.body.classList.remove("mfp-zoom-out-cur");
}
// Remove any link focus
const { activeElement } = document;
if (activeElement && !activeElement.classList.contains("no-blur")) {
activeElement.blur();
}
this.lookup("route:application").send("closeModal");
this.lookup("service:app-events").trigger("dom:clean");
this.lookup("service:document-title").updateContextCount(0);
}
export default {
name: "clean-dom-on-route-change",
after: "inject-objects",
initialize(container) {
const router = container.lookup("router:main");
router.on("routeDidChange", (transition) => {
scheduleOnce("afterRender", container, _clean, transition);
});
},
};

View File

@ -3,7 +3,6 @@ import {
resetPageTracking,
startPageTracking,
} from "discourse/lib/page-tracker";
import { cleanDOM } from "discourse/lib/clean-dom";
import { viewTrackingRequired } from "discourse/lib/ajax";
export default {
@ -13,11 +12,7 @@ export default {
initialize(container) {
// Tell our AJAX system to track a page transition
const router = container.lookup("router:main");
router.on("routeWillChange", viewTrackingRequired);
router.on("routeDidChange", (transition) => {
cleanDOM(container, { skipMiniProfilerPageTransition: !transition.from });
});
let appEvents = container.lookup("service:app-events");
let documentTitle = container.lookup("service:document-title");

View File

@ -1,34 +0,0 @@
import { scheduleOnce } from "@ember/runloop";
function _clean(opts = {}) {
if (window.MiniProfiler && !opts.skipMiniProfilerPageTransition) {
window.MiniProfiler.pageTransition();
}
// Close some elements that may be open
$("header ul.icons li").removeClass("active");
$('[data-toggle="dropdown"]').parent().removeClass("open");
// close the lightbox
if ($.magnificPopup && $.magnificPopup.instance) {
$.magnificPopup.instance.close();
$("body").removeClass("mfp-zoom-out-cur");
}
// Remove any link focus
// NOTE: the '.not("body")' is here to prevent a bug in IE10 on Win7
// cf. https://stackoverflow.com/questions/5657371
$(document.activeElement).not("body").not(".no-blur").blur();
this.lookup("route:application").send("closeModal");
const hideDropDownFunction = $("html").data("hide-dropdown");
if (hideDropDownFunction) {
hideDropDownFunction();
}
this.lookup("service:app-events").trigger("dom:clean");
this.lookup("service:document-title").updateContextCount(0);
}
export function cleanDOM(container, opts) {
scheduleOnce("afterRender", container, _clean, opts);
}