|
| 1 | +<script lang="ts"> |
| 2 | + import { get_current_component } from 'svelte/internal' |
| 3 | + import { tweened } from 'svelte/motion' |
| 4 | +
|
| 5 | + import { forwardEventsBuilder } from '$lib/directives' |
| 6 | + import type { Colors } from '$lib/types' |
| 7 | + import { classname } from '$lib/utils' |
| 8 | +
|
| 9 | + import { El } from '../Base' |
| 10 | + import type { AppLoadingBarProps } from './AppLoadingBar.types' |
| 11 | +
|
| 12 | + type $$Props = AppLoadingBarProps |
| 13 | +
|
| 14 | + export let cssPrefix: $$Props['cssPrefix'] = 'app-loading-bar' |
| 15 | + export let tag: $$Props['tag'] = 'div' |
| 16 | +
|
| 17 | + /** |
| 18 | + * The Color of loading bar |
| 19 | + */ |
| 20 | + export let color: $$Props['color'] = 'primary' |
| 21 | + /** |
| 22 | + * The amount of time that loading will take |
| 23 | + */ |
| 24 | + export let duration: $$Props['duration'] = 1000 |
| 25 | + /** |
| 26 | + * Always show loading bar in top of page |
| 27 | + */ |
| 28 | + export let fixedPosition: $$Props['fixedPosition'] = false |
| 29 | + /** |
| 30 | + * Use Indeterminate prop when you don't know how long the loading will take |
| 31 | + */ |
| 32 | + export let indeterminate: $$Props['indeterminate'] = false |
| 33 | + /** |
| 34 | + * Show and start the loading when component mounted |
| 35 | + */ |
| 36 | + export let show: $$Props['show'] = false |
| 37 | +
|
| 38 | + let MAX: number = 100 |
| 39 | + let interval: NodeJS.Timer |
| 40 | + let total: number = 0 |
| 41 | + let value = tweened<number>(0, { duration }) |
| 42 | +
|
| 43 | + $: if ($value > (max * 95) / 100) clearInterval(interval) |
| 44 | + $: if (show) start() |
| 45 | + $: max = MAX + total |
| 46 | + $: width = ($value * 100) / max |
| 47 | +
|
| 48 | + $: cssProps = { |
| 49 | + color, |
| 50 | + fixedPosition, |
| 51 | + indeterminate, |
| 52 | + show, |
| 53 | + } |
| 54 | +
|
| 55 | + /** |
| 56 | + * Show component and start the loading animation |
| 57 | + */ |
| 58 | + export function start() { |
| 59 | + show = true |
| 60 | + value.set(0, { duration: 0 }) |
| 61 | + if (interval) clearInterval(interval) |
| 62 | + interval = setInterval(increment, duration) |
| 63 | + } |
| 64 | + /** |
| 65 | + * Add a task that Loading Bar should wait until it's done |
| 66 | + * @param number |
| 67 | + */ |
| 68 | + export function push(number = MAX / 4) { |
| 69 | + show = true |
| 70 | + total += number |
| 71 | + } |
| 72 | + /** |
| 73 | + * finish previously added task. |
| 74 | + * @param number |
| 75 | + */ |
| 76 | + export function pop(number = MAX / 4) { |
| 77 | + total -= number |
| 78 | + if (total > 0) return |
| 79 | + total = 0 |
| 80 | + done() |
| 81 | + } |
| 82 | + /** |
| 83 | + * Finish loading animation and remove LoadingBar from page. |
| 84 | + */ |
| 85 | + export function done() { |
| 86 | + clearInterval(interval) |
| 87 | + value.set(max, { duration: duration! / 2 }) |
| 88 | + setTimeout(() => (show = false), duration! / 2) |
| 89 | + } |
| 90 | + function increment() { |
| 91 | + $value += (MAX - $value) / 4 |
| 92 | + } |
| 93 | +</script> |
| 94 | + |
| 95 | +<El {...$$restProps} {cssProps} {tag} {cssPrefix}> |
| 96 | + <El tag="div" cssPrefix="{cssPrefix}-body" style="width: {width}%;" cssProps={{ color }} /> |
| 97 | +</El> |
0 commit comments