Re-factor the OverlayManager class to use a WeakMap internally

This way we're able to store the `<dialog>` elements directly, which removes the need to use manually specified name-strings thus simplifying both the `OverlayManager` itself and its calling code.
This commit is contained in:
Jonas Jenwald 2022-03-25 14:10:22 +01:00
parent f0aa08b464
commit 923bd52cdb
6 changed files with 48 additions and 66 deletions

View file

@ -18,6 +18,7 @@ import { PDFPrintServiceFactory, PDFViewerApplication } from "./app.js";
import { getXfaHtmlForPrinting } from "./print_utils.js";
let activeService = null;
let dialog = null;
let overlayManager = null;
// Renders the page to the canvas of the given print service, and returns
@ -131,8 +132,8 @@ PDFPrintService.prototype = {
this.scratchCanvas = null;
activeService = null;
ensureOverlay().then(function () {
if (overlayManager.active === "printServiceDialog") {
overlayManager.close("printServiceDialog");
if (overlayManager.active === dialog) {
overlayManager.close(dialog);
}
});
},
@ -229,7 +230,7 @@ window.print = function () {
}
ensureOverlay().then(function () {
if (activeService) {
overlayManager.open("printServiceDialog");
overlayManager.open(dialog);
}
});
@ -239,8 +240,8 @@ window.print = function () {
if (!activeService) {
console.error("Expected print service to be initialized.");
ensureOverlay().then(function () {
if (overlayManager.active === "printServiceDialog") {
overlayManager.close("printServiceDialog");
if (overlayManager.active === dialog) {
overlayManager.close(dialog);
}
});
return; // eslint-disable-line no-unsafe-finally
@ -281,10 +282,10 @@ function abort() {
}
function renderProgress(index, total, l10n) {
const progressDialog = document.getElementById("printServiceDialog");
dialog ||= document.getElementById("printServiceDialog");
const progress = Math.round((100 * index) / total);
const progressBar = progressDialog.querySelector("progress");
const progressPerc = progressDialog.querySelector(".relative-progress");
const progressBar = dialog.querySelector("progress");
const progressPerc = dialog.querySelector(".relative-progress");
progressBar.value = progress;
l10n.get("print_progress_percent", { progress }).then(msg => {
progressPerc.textContent = msg;
@ -336,10 +337,9 @@ function ensureOverlay() {
if (!overlayManager) {
throw new Error("The overlay manager has not yet been initialized.");
}
const dialog = document.getElementById("printServiceDialog");
dialog ||= document.getElementById("printServiceDialog");
overlayPromise = overlayManager.register(
"printServiceDialog",
dialog,
/* canForceClose = */ true
);