From 4cde844ffe6e2e129fab778a24ad5273dabcb59c Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Fri, 12 Oct 2018 15:25:38 +0200 Subject: [PATCH 1/2] Add a `DOMTokenList.toggle` polyfill for the second, optional, "force" parameter This is based on the polyfill available at https://developer.mozilla.org/en-US/docs/Web/API/Element/classList#Polyfill --- src/shared/compatibility.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/shared/compatibility.js b/src/shared/compatibility.js index d1417421b38e4..66eff05915037 100644 --- a/src/shared/compatibility.js +++ b/src/shared/compatibility.js @@ -90,6 +90,28 @@ const hasDOM = typeof window === 'object' && typeof document === 'object'; }; })(); +// Provides support for DOMTokenList.prototype.toggle, with the optional +// "force" parameter, in legacy browsers. +// Support: IE +(function checkDOMTokenListToggle() { + if (!hasDOM || isNodeJS()) { + return; + } + const div = document.createElement('div'); + if (div.classList.toggle('test', 0) === false) { + return; + } + const originalDOMTokenListToggle = DOMTokenList.prototype.toggle; + + DOMTokenList.prototype.toggle = function(token) { + if (arguments.length > 1) { + const force = !!arguments[1]; + return (this[force ? 'add' : 'remove'](token), force); + } + return originalDOMTokenListToggle(token); + }; +})(); + // Provides support for String.prototype.includes in legacy browsers. // Support: IE, Chrome<41 (function checkStringIncludes() { From ea4db64f417dde6a2d4ec576d5bbbe2160684a04 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Fri, 12 Oct 2018 15:29:45 +0200 Subject: [PATCH 2/2] Convert some occurrences, in the `/web` folder, of `classList.{add, remove}` to `classList.toggle` with the "force" parameter --- web/base_viewer.js | 14 ++++---------- web/pdf_find_bar.js | 8 ++------ web/secondary_toolbar.js | 8 ++++---- web/toolbar.js | 6 +----- 4 files changed, 11 insertions(+), 25 deletions(-) diff --git a/web/base_viewer.js b/web/base_viewer.js index bf8f091a85fe4..d97ff24a25a40 100644 --- a/web/base_viewer.js +++ b/web/base_viewer.js @@ -1038,16 +1038,10 @@ class BaseViewer { _updateScrollMode(pageNumber = null) { const scrollMode = this._scrollMode, viewer = this.viewer; - if (scrollMode === ScrollMode.HORIZONTAL) { - viewer.classList.add('scrollHorizontal'); - } else { - viewer.classList.remove('scrollHorizontal'); - } - if (scrollMode === ScrollMode.WRAPPED) { - viewer.classList.add('scrollWrapped'); - } else { - viewer.classList.remove('scrollWrapped'); - } + viewer.classList.toggle('scrollHorizontal', + scrollMode === ScrollMode.HORIZONTAL); + viewer.classList.toggle('scrollWrapped', + scrollMode === ScrollMode.WRAPPED); if (!this.pdfDocument || !pageNumber) { return; diff --git a/web/pdf_find_bar.js b/web/pdf_find_bar.js index e3becbd67cd99..665352797a607 100644 --- a/web/pdf_find_bar.js +++ b/web/pdf_find_bar.js @@ -132,13 +132,9 @@ class PDFFindBar { break; } - if (notFound) { - this.findField.classList.add('notFound'); - } else { - this.findField.classList.remove('notFound'); - } - + this.findField.classList.toggle('notFound', notFound); this.findField.setAttribute('data-status', status); + Promise.resolve(findMsg).then((msg) => { this.findMsg.textContent = msg; this._adjustWidth(); diff --git a/web/secondary_toolbar.js b/web/secondary_toolbar.js index bb41f2068cb2f..b9f6f6701d9fd 100644 --- a/web/secondary_toolbar.js +++ b/web/secondary_toolbar.js @@ -123,11 +123,11 @@ class SecondaryToolbar { // current `BaseViewer` instance (in particular `PDFSinglePageViewer`). this.eventBus.on('baseviewerinit', (evt) => { if (evt.source instanceof PDFSinglePageViewer) { - this.toolbarButtonContainer.classList.add('hiddenScrollModeButtons'); - this.toolbarButtonContainer.classList.add('hiddenSpreadModeButtons'); + this.toolbarButtonContainer.classList.add('hiddenScrollModeButtons', + 'hiddenSpreadModeButtons'); } else { - this.toolbarButtonContainer.classList.remove('hiddenScrollModeButtons'); - this.toolbarButtonContainer.classList.remove('hiddenSpreadModeButtons'); + this.toolbarButtonContainer.classList.remove('hiddenScrollModeButtons', + 'hiddenSpreadModeButtons'); } }); } diff --git a/web/toolbar.js b/web/toolbar.js index 622b5dd20d17e..997284fc25051 100644 --- a/web/toolbar.js +++ b/web/toolbar.js @@ -224,11 +224,7 @@ class Toolbar { updateLoadingIndicatorState(loading = false) { let pageNumberInput = this.items.pageNumber; - if (loading) { - pageNumberInput.classList.add(PAGE_NUMBER_LOADING_INDICATOR); - } else { - pageNumberInput.classList.remove(PAGE_NUMBER_LOADING_INDICATOR); - } + pageNumberInput.classList.toggle(PAGE_NUMBER_LOADING_INDICATOR, loading); } _adjustScaleWidth() {