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;
|
transform: none;
|
||||||
background-color: rgba(153, 153, 153, 1);
|
background-color: rgba(153, 153, 153, 1);
|
||||||
transition: none;
|
transition: none;
|
||||||
}
|
}
|
||||||
|
|
||||||
#loadingBar .indeterminate .glimmer {
|
#loadingBar.indeterminate .progress .glimmer {
|
||||||
position: absolute;
|
position: absolute;
|
||||||
top: 0;
|
top: 0;
|
||||||
left: 0;
|
left: 0;
|
||||||
|
@ -161,7 +161,7 @@ const PDFViewerApplication = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
get loadingBar() {
|
get loadingBar() {
|
||||||
const bar = new pdfjsViewer.ProgressBar("#loadingBar");
|
const bar = new pdfjsViewer.ProgressBar("loadingBar");
|
||||||
|
|
||||||
return pdfjsLib.shadow(this, "loadingBar", bar);
|
return pdfjsLib.shadow(this, "loadingBar", bar);
|
||||||
},
|
},
|
||||||
|
52
web/app.js
52
web/app.js
@ -718,7 +718,7 @@ const PDFViewerApplication = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
get loadingBar() {
|
get loadingBar() {
|
||||||
const bar = new ProgressBar("#loadingBar");
|
const bar = new ProgressBar("loadingBar");
|
||||||
return shadow(this, "loadingBar", bar);
|
return shadow(this, "loadingBar", bar);
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -1160,31 +1160,33 @@ const PDFViewerApplication = {
|
|||||||
// that we discard some of the loaded data. This can cause the loading
|
// 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
|
// bar to move backwards. So prevent this by only updating the bar if it
|
||||||
// increases.
|
// increases.
|
||||||
if (percent > this.loadingBar.percent || isNaN(percent)) {
|
if (percent <= this.loadingBar.percent) {
|
||||||
this.loadingBar.percent = percent;
|
return;
|
||||||
|
|
||||||
// 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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
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) {
|
load(pdfDocument) {
|
||||||
|
@ -689,6 +689,12 @@ function clamp(v, min, max) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class ProgressBar {
|
class ProgressBar {
|
||||||
|
#classList = null;
|
||||||
|
|
||||||
|
#percent = 0;
|
||||||
|
|
||||||
|
#visible = true;
|
||||||
|
|
||||||
constructor(id) {
|
constructor(id) {
|
||||||
if (
|
if (
|
||||||
(typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) &&
|
(typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) &&
|
||||||
@ -699,34 +705,24 @@ class ProgressBar {
|
|||||||
"please use CSS rules to modify its appearance instead."
|
"please use CSS rules to modify its appearance instead."
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
this.visible = true;
|
const bar = document.getElementById(id);
|
||||||
|
this.#classList = bar.classList;
|
||||||
// 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}%`);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
get percent() {
|
get percent() {
|
||||||
return this._percent;
|
return this.#percent;
|
||||||
}
|
}
|
||||||
|
|
||||||
set percent(val) {
|
set percent(val) {
|
||||||
this._indeterminate = isNaN(val);
|
this.#percent = clamp(val, 0, 100);
|
||||||
this._percent = clamp(val, 0, 100);
|
|
||||||
this.#updateBar();
|
if (isNaN(val)) {
|
||||||
|
this.#classList.add("indeterminate");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.#classList.remove("indeterminate");
|
||||||
|
|
||||||
|
docStyle.setProperty("--progressBar-percent", `${this.#percent}%`);
|
||||||
}
|
}
|
||||||
|
|
||||||
setWidth(viewer) {
|
setWidth(viewer) {
|
||||||
@ -741,19 +737,19 @@ class ProgressBar {
|
|||||||
}
|
}
|
||||||
|
|
||||||
hide() {
|
hide() {
|
||||||
if (!this.visible) {
|
if (!this.#visible) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.visible = false;
|
this.#visible = false;
|
||||||
this.bar.classList.add("hidden");
|
this.#classList.add("hidden");
|
||||||
}
|
}
|
||||||
|
|
||||||
show() {
|
show() {
|
||||||
if (this.visible) {
|
if (this.#visible) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.visible = true;
|
this.#visible = true;
|
||||||
this.bar.classList.remove("hidden");
|
this.#classList.remove("hidden");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -384,13 +384,13 @@ select {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#loadingBar .progress.indeterminate {
|
#loadingBar.indeterminate .progress {
|
||||||
transform: none;
|
transform: none;
|
||||||
background-color: var(--progressBar-indeterminate-bg-color);
|
background-color: var(--progressBar-indeterminate-bg-color);
|
||||||
transition: none;
|
transition: none;
|
||||||
}
|
}
|
||||||
|
|
||||||
#loadingBar .progress.indeterminate .glimmer {
|
#loadingBar.indeterminate .progress .glimmer {
|
||||||
position: absolute;
|
position: absolute;
|
||||||
top: 0;
|
top: 0;
|
||||||
left: 0;
|
left: 0;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user