UProgress
Creates and control a µProgress.
Kind: global class
new UProgress([parent], [opts], [rtl])
- Create a new UProgress instance.
- Create a
divfor the µProgress and add it to theparent. - Register to the
resizeevent.
| Param | Type | Default | Description |
|---|---|---|---|
| [parent] | HTMLElement | document.body | The HTMLElement on which the µProgress will stand on top of. |
| [opts] | Object | The µProgress configutation. | |
| [rtl] | boolean | false | true to move the µProgress from right to left, false for left to right. |
| [opts.start] | number | 0.01 | The position in percentage (.35 is 35%, 1 is 100%) at which the µProgress starts. |
| [opts.end] | number | 0.99 | The position in percentage (.35 is 35%, 1 is 100%) the µProgress is moving toward when started. Once reached, it will stop until set or done are called. |
| [opts.duration] | number | 25000 | The durarion in ms it takes for the µProgress to go from opts.start to opts.end. |
| [opts.doneDuration] | number | 100 | The duration in ms that the µProgress take to complete when done is called. |
| [opts.fadeDuration] | number | 200 | The duration in ms the µProgress takes to fade out after it has completed. |
| [opts.class] | number | 'uprogress' | The CSS class to set on the µProgress element. |
| [opts.barClass] | number | 'bar' | The CSS class to set on the µProgress bar element. |
| [opts.blurClass] | number | 'blur' | The CSS class to set on the µProgress blur element. |
uProgress.start() ⇒ boolean
Display the µProgress and start its progress from opts.start to opts.end at a speed corresponding to opts.duration. Has no effect if the µProgress is already started.
Kind: instance method of UProgress
Returns: boolean - true if the µProgress has started, false otherwise.
Example
Start the µProgress.
const uProgress = new UProgress();
uProgress.start();
// true
uProgress.set(duration, [target], [force]) ⇒ boolean
Change the µProgress target and the speed at which it reaches it. Useful to give a more accurate progress of multiple sequentials tasks. If the value of duration parameter is equal or greater than 1, then the µProgress will automatically fade out once it reaches 100% progress (equivalent to calling done with opts.doneDuration = duration).
Kind: instance method of UProgress
Returns: boolean - true if the µProgress duration or target has been changed, false otherwise.
| Param | Type | Default | Description |
|---|---|---|---|
| duration | number | The duration in ms the µProgress will take to reach its target. | |
| [target] | number | The position in percentage (.35 is 35%, 1 is 100%) the µProgress will moving toward. Once reached, it will stop until set or done are called. If undefined or null, the µProgress target will stay the same. | |
| [force] | boolean | false | true to set the requested target even if ti makes the µProgress moves backward. |
Example
Update µProgress speed based on tasks progress.
// doHeavyTask calls a callback when done and is expected to takes up to 7s
// doLightTask calls a callback when done and is expected to takes up to 3s
const uProgress = new UProgress({
duration: 7000
end: 0.7
});
uProgress.start();
doHeavyTask(() => {
// when heavy task is done
uProgress.set(3000, .99);
doLightTask(() => {
// When light task is done
uProgress.done();
});
});
uProgress.done(destroy) ⇒ boolean
Gracefully complete the µProgress by moving quickly to 100% progress and then fading out. It will moves to 100% at the speed corresponding to opts.doneDuration and then fade out with the duration of opts.fadeDuration. Has no effect if the µProgress is not started or if done).
Kind: instance method of UProgress
Returns: boolean - true if the µProgress is going to be completed, false otherwise.
| Param | Type | Description |
|---|---|---|
| destroy | boolean | true to automatically call destroy once the µProgress has completed. |
Example
Complete the µProgress once the monitored task is done.
// `doTask` calls a callback when done
const uProgress = new UProgress();
uProgress.start();
doTask(() => {
// when task is done,
uProgress.done();
});
uProgress.status() ⇒ Status | boolean
Get the Status of the µProgress if it’s not destroyed, false otherwise.
Kind: instance method of UProgress
Returns: Status | boolean - The current Status or false.
Example
Get the status.
const uProgress = new UProgress({duration: 20000});
uProgress.start();
setTimeout(() => {
uProgress.status();
// {target: 0.99, duration: 15000, progress: 0.25}
}, 5000);
uProgress.options([opts], [rtl]) ⇒ UProgress
Update the µProgress instance configuration.
Kind: instance method of UProgress
Returns: UProgress - this, chainable
| Param | Type | Default | Description |
|---|---|---|---|
| [opts] | Object | The µProgress configutation. | |
| [rtl] | boolean | false | true to move the µProgress from right to left, false for left to right.Will be used on next call to start. |
| [opts.start] | number | 0.01 | The position in percentage (.35 is 35%, 1 is 100%) at which the µProgress starts. Will be used on next call to start. |
| [opts.end] | number | 0.99 | The position in percentage (.35 is 35%, 1 is 100%) the µProgress is moving toward when started. Once reached, it will stop until set or done are called. Will be used on next call to start. |
| [opts.duration] | number | 25000 | The durarion in ms it takes for the µProgress to go from opts.start to opts.end. Will be used on next call to start. |
| [opts.doneDuration] | number | 100 | The duration in ms that the µProgress take to complete when done is called. Will be used on next call to done. |
| [opts.fadeDuration] | number | 200 | The duration in ms the µProgress takes to fade out after it has completed. Will be used on next call to done. |
| [opts.class] | number | 'uprogress' | The CSS class to set on the the µProgress element. Will be applied right away. |
| [opts.barClass] | number | 'bar' | The CSS class to set on the the µProgress bar element. Will be applied right away. |
| [opts.blurClass] | number | 'blur' | The CSS class to set on the the µProgress blur element. Will be applied right away. |
Example
Change duration.
const uProgress = new UProgress();
uProgress.options({duration: 20000}).start();
setTimeout(() => {
uProgress.status();
// {target: 0.99, duration: 5000, progress: 0.75}
}, 15000);
uProgress.refresh()
Update the µProgress with and position based on its parent width. This methods is automatically called on a window resize event. However it has to be called manually if the parent container width changes for a reason other than a window resize.
Kind: instance method of UProgress
Example
Create a new µProgress on top of a modal and change it’s width.
const uProgress = new UProgress(document.getElementById('my-modal'));
// Modify the modal content (i.e. refresh content with Ajax)
changeModalContent();
uProgress.refresh();
uProgress.destroy()
- Remove the µProgress from the DOM.
- Remove the
resizeevent listener if this is the only non-destroyed instance.
Kind: instance method of UProgress
UProgress.Default : Object
The default options for all new µProgress instances.
Kind: static constant of UProgress
Read only: true
Properties
| Name | Type | Default | Description |
|---|---|---|---|
| [rtl] | boolean | false | true to move the µProgress from right to left, false for left to right. |
| [start] | number | 0.01 | The position in percentage (.35 is 35%, 1 is 100%) at which the µProgress starts. |
| [end] | number | 0.99 | The position in percentage (.35 is 35%, 1 is 100%) the µProgress is moving toward when started. Once reached, it will stop until set or done are called. |
| [duration] | number | 2500 | The durarion in ms it takes for the µProgress to go from start to end. |
| [doneDuration] | number | 100 | The duration in ms that the µProgress take to complete when done is called. |
| [fadeDuration] | number | 200 | The duration in ms the µProgress takes to fade out after it has completed. |
| [class] | number | 'uprogress' | The CSS class to set on the the µProgress element. |
| [blurClass] | number | 'blur' | The CSS class to set on the the µProgress blur element. |
| [barClass] | number | 'bar' | The CSS class to set on the the µProgress bar element. |
| [resizeDebounce] | number | 300 | the debounce threshold after which a window resize event trigger a call to refresh. |
Example
Change default options
UProgress.Default.duration = 3000;
UProgress.Default.class = 'custom-uprogress';
UProgress~Status : Object
Status of the µProgress instance.
Kind: inner typedef of UProgress
Properties
| Name | Type | Description |
|---|---|---|
| target | number | The position in percentage (.35 is 35%, 1 is 100%) the µProgress is moving toward. Once reached, it will stop until set or done are called. |
| duration | number | The duration in ms left to reach the position Status.target. Only returned if the µProgress is started. |
| progress | number | The current progress in percentage (.35 is 35%, 1 is 100%). Only returned if the µProgress is started. |