From a5d0e410c7bcadbd4decbf4eea9da232c1ec01a4 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Tue, 27 Aug 2024 14:48:18 +0200 Subject: [PATCH] Simplify the `PDFDocumentProperties.#updateUI` method We can remove the `reset`-parameter, since it's redundant, given that it's only used after `PDFDocumentProperties.#reset` has been invoked which means that `this.#fieldData === null` which is equivalent to resetting. Also, we don't need to have two separate loops in order to update the UI in this method. Finally, inline the `DEFAULT_FIELD_CONTENT` constant now that it's only used once. --- web/pdf_document_properties.js | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/web/pdf_document_properties.js b/web/pdf_document_properties.js index 70c36cab857f8..4e1a4148b7133 100644 --- a/web/pdf_document_properties.js +++ b/web/pdf_document_properties.js @@ -22,8 +22,6 @@ import { getPageSizeInches, isPortraitOrientation } from "./ui_utils.js"; import { PDFDateString } from "pdfjs-lib"; -const DEFAULT_FIELD_CONTENT = "-"; - // See https://en.wikibooks.org/wiki/Lentis/Conversion_to_the_Metric_Standard_in_the_United_States const NON_METRIC_LOCALES = ["en-us", "en-lr", "my"]; @@ -192,7 +190,7 @@ class PDFDocumentProperties { setDocument(pdfDocument) { if (this.pdfDocument) { this.#reset(); - this.#updateUI(true); + this.#updateUI(); } if (!pdfDocument) { return; @@ -214,24 +212,18 @@ class PDFDocumentProperties { /** * Always updates all of the dialog fields, to prevent inconsistent UI state. * NOTE: If the contents of a particular field is neither a non-empty string, - * nor a number, it will fall back to `DEFAULT_FIELD_CONTENT`. + * nor a number, it will fall back to "-". */ - #updateUI(reset = false) { - if (reset || !this.#fieldData) { - for (const id in this.fields) { - this.fields[id].textContent = DEFAULT_FIELD_CONTENT; - } - return; - } - if (this.overlayManager.active !== this.dialog) { - // Don't bother updating the dialog if has already been closed, + #updateUI() { + if (this.#fieldData && this.overlayManager.active !== this.dialog) { + // Don't bother updating the dialog if it's already been closed, + // unless it's being reset (i.e. `this.#fieldData === null`), // since it will be updated the next time `this.open` is called. return; } for (const id in this.fields) { - const content = this.#fieldData[id]; - this.fields[id].textContent = - content || content === 0 ? content : DEFAULT_FIELD_CONTENT; + const content = this.#fieldData?.[id]; + this.fields[id].textContent = content || content === 0 ? content : "-"; } }