Merge pull request #15121 from Snuffleupagus/loadingBar-cleanup
[api-minor] Further modernize the `ProgressBar` class (PR 14918 follow-up)
This commit is contained in:
commit
03c6febc44
@ -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;
|
||||
|
@ -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);
|
||||
},
|
||||
|
52
web/app.js
52
web/app.js
@ -718,7 +718,7 @@ const PDFViewerApplication = {
|
||||
},
|
||||
|
||||
get loadingBar() {
|
||||
const bar = new ProgressBar("#loadingBar");
|
||||
const bar = new ProgressBar("loadingBar");
|
||||
return shadow(this, "loadingBar", bar);
|
||||
},
|
||||
|
||||
@ -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();
|
||||
|
||||
this.disableAutoFetchLoadingBarTimeout = setTimeout(() => {
|
||||
this.loadingBar.hide();
|
||||
this.disableAutoFetchLoadingBarTimeout = null;
|
||||
}, DISABLE_AUTO_FETCH_LOADING_BAR_TIMEOUT);
|
||||
}
|
||||
if (percent <= this.loadingBar.percent) {
|
||||
return;
|
||||
}
|
||||
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?.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) {
|
||||
|
@ -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")) &&
|
||||
@ -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) {
|
||||
@ -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");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user