|
| 1 | +// Forked and modified from nprogress v0.2.0 |
| 2 | + |
| 3 | +interface NProgressOptions { |
| 4 | + minimum: number |
| 5 | + template: string |
| 6 | + easing: string |
| 7 | + speed: number |
| 8 | + trickle: boolean |
| 9 | + trickleRate: number |
| 10 | + trickleSpeed: number |
| 11 | + parent: string |
| 12 | + barSelector: string |
| 13 | +} |
| 14 | + |
| 15 | +interface NProgress { |
| 16 | + settings: NProgressOptions |
| 17 | + status: number | null |
| 18 | + |
| 19 | + set(number: number): NProgress |
| 20 | + isStarted(): boolean |
| 21 | + start(): NProgress |
| 22 | + done(force?: boolean): NProgress |
| 23 | + inc(amount?: number): NProgress |
| 24 | + trickle(): NProgress |
| 25 | + |
| 26 | + /* Internal */ |
| 27 | + |
| 28 | + render(fromStart?: boolean): HTMLDivElement |
| 29 | + remove(): void |
| 30 | + isRendered(): boolean |
| 31 | +} |
| 32 | + |
| 33 | +export const nprogress: NProgress = { |
| 34 | + settings: { |
| 35 | + minimum: 0.08, |
| 36 | + easing: 'ease', |
| 37 | + speed: 200, |
| 38 | + trickle: true, |
| 39 | + trickleRate: 0.02, |
| 40 | + trickleSpeed: 800, |
| 41 | + barSelector: '[role="bar"]', |
| 42 | + parent: 'body', |
| 43 | + template: '<div class="bar" role="bar"></div>', |
| 44 | + }, |
| 45 | + status: null, |
| 46 | + set: (n) => { |
| 47 | + const started = nprogress.isStarted() |
| 48 | + n = clamp(n, nprogress.settings.minimum, 1) |
| 49 | + nprogress.status = n === 1 ? null : n |
| 50 | + const progress = nprogress.render(!started) |
| 51 | + const bar = progress.querySelector<HTMLElement>( |
| 52 | + nprogress.settings.barSelector |
| 53 | + )! |
| 54 | + const speed = nprogress.settings.speed |
| 55 | + const ease = nprogress.settings.easing |
| 56 | + |
| 57 | + // eslint-disable-next-line @typescript-eslint/no-unused-expressions |
| 58 | + progress.offsetWidth /* Repaint */ |
| 59 | + |
| 60 | + queue((next) => { |
| 61 | + // Add transition |
| 62 | + css(bar, { |
| 63 | + transform: 'translate3d(' + toBarPerc(n) + '%,0,0)', |
| 64 | + transition: 'all ' + speed + 'ms ' + ease, |
| 65 | + }) |
| 66 | + |
| 67 | + if (n === 1) { |
| 68 | + // Fade out |
| 69 | + css(progress, { |
| 70 | + transition: 'none', |
| 71 | + opacity: '1', |
| 72 | + }) |
| 73 | + // eslint-disable-next-line @typescript-eslint/no-unused-expressions |
| 74 | + progress.offsetWidth /* Repaint */ |
| 75 | + |
| 76 | + setTimeout(function () { |
| 77 | + css(progress, { |
| 78 | + transition: 'all ' + speed + 'ms linear', |
| 79 | + opacity: '0', |
| 80 | + }) |
| 81 | + setTimeout(function () { |
| 82 | + nprogress.remove() |
| 83 | + next() |
| 84 | + }, speed) |
| 85 | + }, speed) |
| 86 | + } else { |
| 87 | + setTimeout(() => next(), speed) |
| 88 | + } |
| 89 | + }) |
| 90 | + return nprogress |
| 91 | + }, |
| 92 | + |
| 93 | + isStarted: () => typeof nprogress.status === 'number', |
| 94 | + |
| 95 | + start: () => { |
| 96 | + if (!nprogress.status) nprogress.set(0) |
| 97 | + |
| 98 | + const work = (): void => { |
| 99 | + setTimeout(() => { |
| 100 | + if (!nprogress.status) return |
| 101 | + nprogress.trickle() |
| 102 | + work() |
| 103 | + }, nprogress.settings.trickleSpeed) |
| 104 | + } |
| 105 | + |
| 106 | + if (nprogress.settings.trickle) work() |
| 107 | + |
| 108 | + return nprogress |
| 109 | + }, |
| 110 | + |
| 111 | + done: (force) => { |
| 112 | + if (!force && !nprogress.status) return nprogress |
| 113 | + return nprogress.inc(0.3 + 0.5 * Math.random()).set(1) |
| 114 | + }, |
| 115 | + |
| 116 | + inc: (amount) => { |
| 117 | + let n = nprogress.status |
| 118 | + if (!n) { |
| 119 | + return nprogress.start() |
| 120 | + } |
| 121 | + |
| 122 | + if (typeof amount !== 'number') { |
| 123 | + amount = (1 - n) * clamp(Math.random() * n, 0.1, 0.95) |
| 124 | + } |
| 125 | + n = clamp(n + amount, 0, 0.994) |
| 126 | + return nprogress.set(n) |
| 127 | + }, |
| 128 | + |
| 129 | + trickle: () => nprogress.inc(Math.random() * nprogress.settings.trickleRate), |
| 130 | + |
| 131 | + render: (fromStart) => { |
| 132 | + if (nprogress.isRendered()) { |
| 133 | + return document.getElementById('nprogress') as HTMLDivElement |
| 134 | + } |
| 135 | + |
| 136 | + addClass(document.documentElement, 'nprogress-busy') |
| 137 | + |
| 138 | + const progress = document.createElement('div') |
| 139 | + progress.id = 'nprogress' |
| 140 | + progress.innerHTML = nprogress.settings.template |
| 141 | + |
| 142 | + const bar = progress.querySelector<HTMLElement>( |
| 143 | + nprogress.settings.barSelector |
| 144 | + )! |
| 145 | + const perc = fromStart ? '-100' : toBarPerc(nprogress.status || 0) |
| 146 | + const parent = document.querySelector<HTMLElement>( |
| 147 | + nprogress.settings.parent |
| 148 | + )! |
| 149 | + |
| 150 | + css(bar, { |
| 151 | + transition: 'all 0 linear', |
| 152 | + transform: 'translate3d(' + perc + '%,0,0)', |
| 153 | + }) |
| 154 | + |
| 155 | + if (parent !== document.body) { |
| 156 | + addClass(parent, 'nprogress-custom-parent') |
| 157 | + } |
| 158 | + |
| 159 | + parent?.appendChild(progress) |
| 160 | + return progress |
| 161 | + }, |
| 162 | + |
| 163 | + remove: () => { |
| 164 | + removeClass(document.documentElement, 'nprogress-busy') |
| 165 | + removeClass( |
| 166 | + document.querySelector(nprogress.settings.parent)!, |
| 167 | + 'nprogress-custom-parent' |
| 168 | + ) |
| 169 | + const progress = document.getElementById('nprogress') |
| 170 | + progress && removeElement(progress) |
| 171 | + }, |
| 172 | + |
| 173 | + isRendered: () => !!document.getElementById('nprogress'), |
| 174 | +} |
| 175 | + |
| 176 | +const clamp = (n: number, min: number, max: number): number => { |
| 177 | + if (n < min) return min |
| 178 | + if (n > max) return max |
| 179 | + return n |
| 180 | +} |
| 181 | +const toBarPerc = (n: number): number => (-1 + n) * 100 |
| 182 | +const queue = (function () { |
| 183 | + const pending: ((next: () => void) => void)[] = [] |
| 184 | + function next(): void { |
| 185 | + const fn = pending.shift() |
| 186 | + if (fn) { |
| 187 | + fn(next) |
| 188 | + } |
| 189 | + } |
| 190 | + return function (fn) { |
| 191 | + pending.push(fn) |
| 192 | + if (pending.length === 1) next() |
| 193 | + } |
| 194 | +})() |
| 195 | +const css = (function () { |
| 196 | + const cssPrefixes = ['Webkit', 'O', 'Moz', 'ms'] |
| 197 | + const cssProps = {} |
| 198 | + |
| 199 | + function camelCase(string: string): string { |
| 200 | + return string |
| 201 | + .replace(/^-ms-/, 'ms-') |
| 202 | + .replace(/-([\da-z])/gi, function (match, letter) { |
| 203 | + return letter.toUpperCase() |
| 204 | + }) |
| 205 | + } |
| 206 | + |
| 207 | + function getVendorProp(name: string): string { |
| 208 | + const style = document.body.style |
| 209 | + if (name in style) return name |
| 210 | + let i = cssPrefixes.length |
| 211 | + const capName = name.charAt(0).toUpperCase() + name.slice(1) |
| 212 | + let vendorName |
| 213 | + while (i--) { |
| 214 | + vendorName = cssPrefixes[i] + capName |
| 215 | + if (vendorName in style) return vendorName |
| 216 | + } |
| 217 | + return name |
| 218 | + } |
| 219 | + |
| 220 | + function getStyleProp(name: string): string { |
| 221 | + name = camelCase(name) |
| 222 | + return cssProps[name] || (cssProps[name] = getVendorProp(name)) |
| 223 | + } |
| 224 | + |
| 225 | + function applyCss(element: HTMLElement, prop: string, value: string): void { |
| 226 | + prop = getStyleProp(prop) |
| 227 | + element.style[prop] = value |
| 228 | + } |
| 229 | + |
| 230 | + return function (element: HTMLElement, properties: Record<string, string>) { |
| 231 | + for (const prop in properties) { |
| 232 | + const value = properties[prop] |
| 233 | + if ( |
| 234 | + value !== undefined && |
| 235 | + Object.prototype.hasOwnProperty.call(properties, prop) |
| 236 | + ) |
| 237 | + applyCss(element, prop, value) |
| 238 | + } |
| 239 | + } |
| 240 | +})() |
| 241 | + |
| 242 | +const hasClass = (element: Element | string, name: string): boolean => { |
| 243 | + const list = typeof element === 'string' ? element : classList(element) |
| 244 | + return list.indexOf(' ' + name + ' ') >= 0 |
| 245 | +} |
| 246 | +const addClass = (element: Element, name: string): void => { |
| 247 | + const oldList = classList(element) |
| 248 | + const newList = oldList + name |
| 249 | + if (hasClass(oldList, name)) return |
| 250 | + element.className = newList.substring(1) |
| 251 | +} |
| 252 | + |
| 253 | +const removeClass = (element: Element, name: string): void => { |
| 254 | + const oldList = classList(element) |
| 255 | + if (!hasClass(element, name)) return |
| 256 | + const newList = oldList.replace(' ' + name + ' ', ' ') |
| 257 | + element.className = newList.substring(1, newList.length - 1) |
| 258 | +} |
| 259 | + |
| 260 | +const classList = (element: Element): string => { |
| 261 | + return (' ' + (element.className || '') + ' ').replace(/\s+/gi, ' ') |
| 262 | +} |
| 263 | + |
| 264 | +const removeElement = (element: Element): void => { |
| 265 | + element && element.parentNode && element.parentNode.removeChild(element) |
| 266 | +} |
0 commit comments