Skip to content

Commit 03c6feb

Browse files
Merge pull request #15121 from Snuffleupagus/loadingBar-cleanup
[api-minor] Further modernize the `ProgressBar` class (PR 14918 follow-up)
2 parents 845b7f0 + d9ce176 commit 03c6feb

File tree

5 files changed

+55
-57
lines changed

5 files changed

+55
-57
lines changed

examples/mobile-viewer/viewer.css

+2-2
Original file line numberDiff line numberDiff line change
@@ -221,13 +221,13 @@ canvas {
221221
}
222222
}
223223

224-
#loadingBar .progress.indeterminate {
224+
#loadingBar.indeterminate .progress {
225225
transform: none;
226226
background-color: rgba(153, 153, 153, 1);
227227
transition: none;
228228
}
229229

230-
#loadingBar .indeterminate .glimmer {
230+
#loadingBar.indeterminate .progress .glimmer {
231231
position: absolute;
232232
top: 0;
233233
left: 0;

examples/mobile-viewer/viewer.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ const PDFViewerApplication = {
161161
},
162162

163163
get loadingBar() {
164-
const bar = new pdfjsViewer.ProgressBar("#loadingBar");
164+
const bar = new pdfjsViewer.ProgressBar("loadingBar");
165165

166166
return pdfjsLib.shadow(this, "loadingBar", bar);
167167
},

web/app.js

+26-24
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,7 @@ const PDFViewerApplication = {
718718
},
719719

720720
get loadingBar() {
721-
const bar = new ProgressBar("#loadingBar");
721+
const bar = new ProgressBar("loadingBar");
722722
return shadow(this, "loadingBar", bar);
723723
},
724724

@@ -1160,31 +1160,33 @@ const PDFViewerApplication = {
11601160
// that we discard some of the loaded data. This can cause the loading
11611161
// bar to move backwards. So prevent this by only updating the bar if it
11621162
// increases.
1163-
if (percent > this.loadingBar.percent || isNaN(percent)) {
1164-
this.loadingBar.percent = percent;
1165-
1166-
// When disableAutoFetch is enabled, it's not uncommon for the entire file
1167-
// to never be fetched (depends on e.g. the file structure). In this case
1168-
// the loading bar will not be completely filled, nor will it be hidden.
1169-
// To prevent displaying a partially filled loading bar permanently, we
1170-
// hide it when no data has been loaded during a certain amount of time.
1171-
const disableAutoFetch = this.pdfDocument
1172-
? this.pdfDocument.loadingParams.disableAutoFetch
1173-
: AppOptions.get("disableAutoFetch");
1174-
1175-
if (disableAutoFetch && percent) {
1176-
if (this.disableAutoFetchLoadingBarTimeout) {
1177-
clearTimeout(this.disableAutoFetchLoadingBarTimeout);
1178-
this.disableAutoFetchLoadingBarTimeout = null;
1179-
}
1180-
this.loadingBar.show();
1163+
if (percent <= this.loadingBar.percent) {
1164+
return;
1165+
}
1166+
this.loadingBar.percent = percent;
11811167

1182-
this.disableAutoFetchLoadingBarTimeout = setTimeout(() => {
1183-
this.loadingBar.hide();
1184-
this.disableAutoFetchLoadingBarTimeout = null;
1185-
}, DISABLE_AUTO_FETCH_LOADING_BAR_TIMEOUT);
1186-
}
1168+
// When disableAutoFetch is enabled, it's not uncommon for the entire file
1169+
// to never be fetched (depends on e.g. the file structure). In this case
1170+
// the loading bar will not be completely filled, nor will it be hidden.
1171+
// To prevent displaying a partially filled loading bar permanently, we
1172+
// hide it when no data has been loaded during a certain amount of time.
1173+
const disableAutoFetch =
1174+
this.pdfDocument?.loadingParams.disableAutoFetch ??
1175+
AppOptions.get("disableAutoFetch");
1176+
1177+
if (!disableAutoFetch || isNaN(percent)) {
1178+
return;
1179+
}
1180+
if (this.disableAutoFetchLoadingBarTimeout) {
1181+
clearTimeout(this.disableAutoFetchLoadingBarTimeout);
1182+
this.disableAutoFetchLoadingBarTimeout = null;
11871183
}
1184+
this.loadingBar.show();
1185+
1186+
this.disableAutoFetchLoadingBarTimeout = setTimeout(() => {
1187+
this.loadingBar.hide();
1188+
this.disableAutoFetchLoadingBarTimeout = null;
1189+
}, DISABLE_AUTO_FETCH_LOADING_BAR_TIMEOUT);
11881190
},
11891191

11901192
load(pdfDocument) {

web/ui_utils.js

+24-28
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,12 @@ function clamp(v, min, max) {
689689
}
690690

691691
class ProgressBar {
692+
#classList = null;
693+
694+
#percent = 0;
695+
696+
#visible = true;
697+
692698
constructor(id) {
693699
if (
694700
(typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) &&
@@ -699,34 +705,24 @@ class ProgressBar {
699705
"please use CSS rules to modify its appearance instead."
700706
);
701707
}
702-
this.visible = true;
703-
704-
// Fetch the sub-elements for later.
705-
this.div = document.querySelector(id + " .progress");
706-
// Get the loading bar element, so it can be resized to fit the viewer.
707-
this.bar = this.div.parentNode;
708-
709-
this.percent = 0;
710-
}
711-
712-
#updateBar() {
713-
if (this._indeterminate) {
714-
this.div.classList.add("indeterminate");
715-
return;
716-
}
717-
this.div.classList.remove("indeterminate");
718-
719-
docStyle.setProperty("--progressBar-percent", `${this._percent}%`);
708+
const bar = document.getElementById(id);
709+
this.#classList = bar.classList;
720710
}
721711

722712
get percent() {
723-
return this._percent;
713+
return this.#percent;
724714
}
725715

726716
set percent(val) {
727-
this._indeterminate = isNaN(val);
728-
this._percent = clamp(val, 0, 100);
729-
this.#updateBar();
717+
this.#percent = clamp(val, 0, 100);
718+
719+
if (isNaN(val)) {
720+
this.#classList.add("indeterminate");
721+
return;
722+
}
723+
this.#classList.remove("indeterminate");
724+
725+
docStyle.setProperty("--progressBar-percent", `${this.#percent}%`);
730726
}
731727

732728
setWidth(viewer) {
@@ -741,19 +737,19 @@ class ProgressBar {
741737
}
742738

743739
hide() {
744-
if (!this.visible) {
740+
if (!this.#visible) {
745741
return;
746742
}
747-
this.visible = false;
748-
this.bar.classList.add("hidden");
743+
this.#visible = false;
744+
this.#classList.add("hidden");
749745
}
750746

751747
show() {
752-
if (this.visible) {
748+
if (this.#visible) {
753749
return;
754750
}
755-
this.visible = true;
756-
this.bar.classList.remove("hidden");
751+
this.#visible = true;
752+
this.#classList.remove("hidden");
757753
}
758754
}
759755

web/viewer.css

+2-2
Original file line numberDiff line numberDiff line change
@@ -384,13 +384,13 @@ select {
384384
}
385385
}
386386

387-
#loadingBar .progress.indeterminate {
387+
#loadingBar.indeterminate .progress {
388388
transform: none;
389389
background-color: var(--progressBar-indeterminate-bg-color);
390390
transition: none;
391391
}
392392

393-
#loadingBar .progress.indeterminate .glimmer {
393+
#loadingBar.indeterminate .progress .glimmer {
394394
position: absolute;
395395
top: 0;
396396
left: 0;

0 commit comments

Comments
 (0)