| 1 |
export default class ProgressBar { |
| 2 |
value: number = 0; |
| 3 |
visible: boolean = false; |
| 4 |
hiding: boolean = false; |
| 5 |
|
| 6 |
className: string = 'progress-bar'; |
| 7 |
styleAttr: string = 'data-progressbar-styles data-swup-theme'; |
| 8 |
animationDuration: number = 300; |
| 9 |
minValue: number = 0.1; |
| 10 |
initialValue: number = 0.25; |
| 11 |
trickleValue: number = 0.03; |
| 12 |
trickleInterval?: number; |
| 13 |
|
| 14 |
styleElement: HTMLStyleElement; |
| 15 |
progressElement: HTMLDivElement; |
| 16 |
|
| 17 |
constructor({ |
| 18 |
className, |
| 19 |
styleAttr, |
| 20 |
animationDuration, |
| 21 |
minValue, |
| 22 |
initialValue, |
| 23 |
trickleValue |
| 24 |
}: { |
| 25 |
className?: string; |
| 26 |
styleAttr?: string; |
| 27 |
animationDuration?: number; |
| 28 |
minValue?: number; |
| 29 |
initialValue?: number; |
| 30 |
trickleValue?: number; |
| 31 |
} = {}) { |
| 32 |
if (className !== undefined) { |
| 33 |
this.className = String(className); |
| 34 |
} |
| 35 |
if (styleAttr !== undefined) { |
| 36 |
this.styleAttr = String(styleAttr); |
| 37 |
} |
| 38 |
if (animationDuration !== undefined) { |
| 39 |
this.animationDuration = Number(animationDuration); |
| 40 |
} |
| 41 |
if (minValue !== undefined) { |
| 42 |
this.minValue = Number(minValue); |
| 43 |
} |
| 44 |
if (initialValue !== undefined) { |
| 45 |
this.initialValue = Number(initialValue); |
| 46 |
} |
| 47 |
if (trickleValue !== undefined) { |
| 48 |
this.trickleValue = Number(trickleValue); |
| 49 |
} |
| 50 |
|
| 51 |
this.styleElement = this.createStyleElement(); |
| 52 |
this.progressElement = this.createProgressElement(); |
| 53 |
} |
| 54 |
|
| 55 |
get defaultStyles(): string { |
| 56 |
return ` |
| 57 |
.${this.className} { |
| 58 |
position: fixed; |
| 59 |
display: block; |
| 60 |
top: 0; |
| 61 |
left: 0; |
| 62 |
width: 100%; |
| 63 |
height: 3px; |
| 64 |
background-color: black; |
| 65 |
z-index: 9999; |
| 66 |
transition: |
| 67 |
transform ${this.animationDuration}ms ease-out, |
| 68 |
opacity ${this.animationDuration / 2}ms ${this.animationDuration / 2}ms ease-in; |
| 69 |
transform: translate3d(0, 0, 0) scaleX(var(--progress, 0)); |
| 70 |
transform-origin: 0; |
| 71 |
} |
| 72 |
`; |
| 73 |
} |
| 74 |
|
| 75 |
show(): void { |
| 76 |
if (!this.visible) { |
| 77 |
this.visible = true; |
| 78 |
this.installStyleElement(); |
| 79 |
this.installProgressElement(); |
| 80 |
this.startTrickling(); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
hide(): void { |
| 85 |
if (this.visible && !this.hiding) { |
| 86 |
this.hiding = true; |
| 87 |
this.fadeProgressElement(() => { |
| 88 |
this.uninstallProgressElement(); |
| 89 |
this.stopTrickling(); |
| 90 |
this.visible = false; |
| 91 |
this.hiding = false; |
| 92 |
}); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
setValue(value: number): void { |
| 97 |
this.value = Math.min(1, Math.max(this.minValue, value)); |
| 98 |
this.refresh(); |
| 99 |
} |
| 100 |
|
| 101 |
private installStyleElement(): void { |
| 102 |
document.head.prepend(this.styleElement); |
| 103 |
} |
| 104 |
|
| 105 |
private installProgressElement(): void { |
| 106 |
this.progressElement.style.setProperty('--progress', String(0)); |
| 107 |
this.progressElement.style.opacity = '1'; |
| 108 |
document.body.prepend(this.progressElement); |
| 109 |
this.progressElement.scrollTop = 0; // Force reflow to ensure the initial style takes effect |
| 110 |
this.setValue(Math.random() * this.initialValue); |
| 111 |
} |
| 112 |
|
| 113 |
private fadeProgressElement(callback: () => void): void { |
| 114 |
this.progressElement.style.opacity = '0'; |
| 115 |
setTimeout(callback, this.animationDuration * 1.5); |
| 116 |
} |
| 117 |
|
| 118 |
private uninstallProgressElement(): void { |
| 119 |
this.progressElement.remove(); |
| 120 |
} |
| 121 |
|
| 122 |
private startTrickling(): void { |
| 123 |
if (!this.trickleInterval) { |
| 124 |
this.trickleInterval = window.setInterval(this.trickle, this.animationDuration); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
private stopTrickling(): void { |
| 129 |
window.clearInterval(this.trickleInterval); |
| 130 |
delete this.trickleInterval; |
| 131 |
} |
| 132 |
|
| 133 |
private trickle = (): void => { |
| 134 |
const advance = Math.random() * this.trickleValue; |
| 135 |
this.setValue(this.value + advance); |
| 136 |
}; |
| 137 |
|
| 138 |
private refresh(): void { |
| 139 |
requestAnimationFrame(() => { |
| 140 |
this.progressElement.style.setProperty('--progress', String(this.value)); |
| 141 |
}); |
| 142 |
} |
| 143 |
|
| 144 |
private createStyleElement(): HTMLStyleElement { |
| 145 |
const element = document.createElement('style'); |
| 146 |
this.styleAttr.split(' ').forEach((attr) => element.setAttribute(attr, '')); |
| 147 |
element.textContent = this.defaultStyles; |
| 148 |
return element; |
| 149 |
} |
| 150 |
|
| 151 |
private createProgressElement(): HTMLDivElement { |
| 152 |
const element = document.createElement('div'); |
| 153 |
element.className = this.className; |
| 154 |
element.setAttribute('aria-hidden', 'true'); |
| 155 |
return element; |
| 156 |
} |
| 157 |
} |