Skip to content

Commit

Permalink
Merge pull request #15121 from Snuffleupagus/loadingBar-cleanup
Browse files Browse the repository at this point in the history
[api-minor] Further modernize the `ProgressBar` class (PR 14918 follow-up)
  • Loading branch information
Snuffleupagus authored Jul 2, 2022
2 parents 845b7f0 + d9ce176 commit 03c6feb
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 57 deletions.
4 changes: 2 additions & 2 deletions examples/mobile-viewer/viewer.css
Original file line number Diff line number Diff line change
Expand Up @@ -221,13 +221,13 @@ canvas {
}
}

#loadingBar .progress.indeterminate {
#loadingBar.indeterminate .progress {
transform: none;
background-color: rgba(153, 153, 153, 1);
transition: none;
}

#loadingBar .indeterminate .glimmer {
#loadingBar.indeterminate .progress .glimmer {
position: absolute;
top: 0;
left: 0;
Expand Down
2 changes: 1 addition & 1 deletion examples/mobile-viewer/viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ const PDFViewerApplication = {
},

get loadingBar() {
const bar = new pdfjsViewer.ProgressBar("#loadingBar");
const bar = new pdfjsViewer.ProgressBar("loadingBar");

return pdfjsLib.shadow(this, "loadingBar", bar);
},
Expand Down
50 changes: 26 additions & 24 deletions web/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,7 @@ const PDFViewerApplication = {
},

get loadingBar() {
const bar = new ProgressBar("#loadingBar");
const bar = new ProgressBar("loadingBar");
return shadow(this, "loadingBar", bar);
},

Expand Down Expand Up @@ -1160,31 +1160,33 @@ const PDFViewerApplication = {
// that we discard some of the loaded data. This can cause the loading
// bar to move backwards. So prevent this by only updating the bar if it
// increases.
if (percent > this.loadingBar.percent || isNaN(percent)) {
this.loadingBar.percent = percent;

// When disableAutoFetch is enabled, it's not uncommon for the entire file
// to never be fetched (depends on e.g. the file structure). In this case
// the loading bar will not be completely filled, nor will it be hidden.
// To prevent displaying a partially filled loading bar permanently, we
// hide it when no data has been loaded during a certain amount of time.
const disableAutoFetch = this.pdfDocument
? this.pdfDocument.loadingParams.disableAutoFetch
: AppOptions.get("disableAutoFetch");

if (disableAutoFetch && percent) {
if (this.disableAutoFetchLoadingBarTimeout) {
clearTimeout(this.disableAutoFetchLoadingBarTimeout);
this.disableAutoFetchLoadingBarTimeout = null;
}
this.loadingBar.show();
if (percent <= this.loadingBar.percent) {
return;
}
this.loadingBar.percent = percent;

this.disableAutoFetchLoadingBarTimeout = setTimeout(() => {
this.loadingBar.hide();
this.disableAutoFetchLoadingBarTimeout = null;
}, DISABLE_AUTO_FETCH_LOADING_BAR_TIMEOUT);
}
// When disableAutoFetch is enabled, it's not uncommon for the entire file
// to never be fetched (depends on e.g. the file structure). In this case
// the loading bar will not be completely filled, nor will it be hidden.
// To prevent displaying a partially filled loading bar permanently, we
// hide it when no data has been loaded during a certain amount of time.
const disableAutoFetch =
this.pdfDocument?.loadingParams.disableAutoFetch ??
AppOptions.get("disableAutoFetch");

if (!disableAutoFetch || isNaN(percent)) {
return;
}
if (this.disableAutoFetchLoadingBarTimeout) {
clearTimeout(this.disableAutoFetchLoadingBarTimeout);
this.disableAutoFetchLoadingBarTimeout = null;
}
this.loadingBar.show();

this.disableAutoFetchLoadingBarTimeout = setTimeout(() => {
this.loadingBar.hide();
this.disableAutoFetchLoadingBarTimeout = null;
}, DISABLE_AUTO_FETCH_LOADING_BAR_TIMEOUT);
},

load(pdfDocument) {
Expand Down
52 changes: 24 additions & 28 deletions web/ui_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,12 @@ function clamp(v, min, max) {
}

class ProgressBar {
#classList = null;

#percent = 0;

#visible = true;

constructor(id) {
if (
(typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) &&
Expand All @@ -699,34 +705,24 @@ class ProgressBar {
"please use CSS rules to modify its appearance instead."
);
}
this.visible = true;

// Fetch the sub-elements for later.
this.div = document.querySelector(id + " .progress");
// Get the loading bar element, so it can be resized to fit the viewer.
this.bar = this.div.parentNode;

this.percent = 0;
}

#updateBar() {
if (this._indeterminate) {
this.div.classList.add("indeterminate");
return;
}
this.div.classList.remove("indeterminate");

docStyle.setProperty("--progressBar-percent", `${this._percent}%`);
const bar = document.getElementById(id);
this.#classList = bar.classList;
}

get percent() {
return this._percent;
return this.#percent;
}

set percent(val) {
this._indeterminate = isNaN(val);
this._percent = clamp(val, 0, 100);
this.#updateBar();
this.#percent = clamp(val, 0, 100);

if (isNaN(val)) {
this.#classList.add("indeterminate");
return;
}
this.#classList.remove("indeterminate");

docStyle.setProperty("--progressBar-percent", `${this.#percent}%`);
}

setWidth(viewer) {
Expand All @@ -741,19 +737,19 @@ class ProgressBar {
}

hide() {
if (!this.visible) {
if (!this.#visible) {
return;
}
this.visible = false;
this.bar.classList.add("hidden");
this.#visible = false;
this.#classList.add("hidden");
}

show() {
if (this.visible) {
if (this.#visible) {
return;
}
this.visible = true;
this.bar.classList.remove("hidden");
this.#visible = true;
this.#classList.remove("hidden");
}
}

Expand Down
4 changes: 2 additions & 2 deletions web/viewer.css
Original file line number Diff line number Diff line change
Expand Up @@ -384,13 +384,13 @@ select {
}
}

#loadingBar .progress.indeterminate {
#loadingBar.indeterminate .progress {
transform: none;
background-color: var(--progressBar-indeterminate-bg-color);
transition: none;
}

#loadingBar .progress.indeterminate .glimmer {
#loadingBar.indeterminate .progress .glimmer {
position: absolute;
top: 0;
left: 0;
Expand Down

0 comments on commit 03c6feb

Please sign in to comment.