A tiny progressbar like on YouTube, Github, ...
Include tinybar.js in your html file
<script src="path/to/tinybar.js"></script>with typescript (the .d.ts file is used)
import {TinyBarExport} from "path/to/tinyBar";
declare var TinyBar: TinyBarExportor require in js via
var TinyBar = require('/path/to/tinybar')with typescript (the .d.ts file is required)
import TinyBar = require('path/to/tinybar)Variables with [] are optional.
var tinyBar = new TinyBar.TinyBar([settings], [parent], [callback])settings: Settingsthe settings for the barparent: string | HTMLElementhtml id where the bar should be inserted (via append) OR the html parent objectcallback: () => voidcalled when the dom elements were created and the bar is ready to be used
settings and parentId can be exchanged (the order shouldn't matter ...)
to use the callback you need to provide settings and parent (can be both null)
so you could also do this
var tinyBar = new TinyBar.TinyBar([parentId], [settings], [callback])The transition properties cannot be set through the css property because you are able to change the html structure of the bar and we need to change the transition (backwards is faster) on the fly.
css: anyan object with key-value pairs (key css style name [htmlObj.style.key], value style value) to style the bar default:
{
backgroundColor: '#29d',
boxShadow: '0 0 10px rgba(119,182,255,0.7)',
position: 'absolute',
left: '0',
right: '100%',
top: '0',
bottom: '0'
}cssWrapper: anyan object with key-value pairs (key css style name [htmlObj.style.key], value style value) to style the bar wrapper default:
{
height: '2px',
position: 'relative',
backgroundColor: 'transparent',
zIndex: '1050'
}wrapperCssClasses: string[]the css classes to add the the bar wrapper
default:[]cssClasses: string[]the css classes to add to the bar
default:[]incrementTimeoutInMs: numberthe delay in ms, every tick the .inc method is called
default:300changeValueTransition: stringthe transition for the bar when the value changes (must not be empty! and cannot be set through the css property)
default:'all 0.3s ease'changeValueBackTransition: stringthe transition for the bar when animating from bigger to smaller value (backwards) (must not be empty! and cannot be set through the css property)
default:'all 0.2s ease'changeVisibilityTransition: stringthe transition for the bar wrapper when the visibility (must not be empty! and cannot be set through the css property)
default:'all 0.05s linear'changeValueProperty: stringthe property name of the value property (property that changes when the value should change)
default:nullchangeVisibilityProperty: stringthe property name of the visibility property (property that changes when the visibility should change)
default:nullchangeVisibilityElement: BarElementthe element that will used to change the visibility
default:BarElement.barchangeValueElement: BarElementthe element that will used to change the value
default:BarElement.barWrapperapplyInitialBarWrapperStyle: (barWrapperDiv:HTMLDivElement, shouldPositionTopMost:boolean) => voidfunction that applies the initial style to the bar wrapper (the initial styles)
default:
{
if (this.cssWrapper)
for (let key in this.cssWrapper) {
if (this.cssWrapper.hasOwnProperty(key) && barWrapperDiv.style[key] !== undefined) {
barWrapperDiv.style[key] = this.cssWrapper[key]
}
}
if (barWrapperDiv.style.transition) {
console.error('tinybar: the changeVisibilityTransition property must be set (not though css property) ')
}
barWrapperDiv.style.transition = this.changeVisibilityTransition
//override style...
if (shouldPositionTopMost) {
barWrapperDiv.style.position = 'fixed'
barWrapperDiv.style.left = '0'
barWrapperDiv.style.right = '0'
barWrapperDiv.style.top = '0'
}
this.extendInitialBarWrapperStyle(barWrapperDiv, shouldPositionTopMost)
}extendInitialBarWrapperStyle: (barWrapperDiv:HTMLDivElement, shouldPositionTopMost:boolean) => voida function that is called after applyInitialBarWrapperStyle to do some minor changes on the bar wrapper style
default:
{}applyInitialBarStyle: (barDiv:HTMLDivElement, shouldPositionTopMost:boolean) => voidfunction that applies the initial style to the bar (the initial styles)
default:
{
if (this.css)
for (let key in this.css) {
if (this.css.hasOwnProperty(key) && barDiv.style[key] !== undefined) {
barDiv.style[key] = this.css[key]
}
}
if (barDiv.style.transition) {
console.error('tinybar: the changeValueTransition (& the changeValueBackTransition) property must be set (not though css property) ')
}
barDiv.style.transition = this.changeValueTransition
//let the user modify the style
this.extendInitialBarStyle(barDiv, shouldPositionTopMost)
}extendInitialBarStyle: (barDiv:HTMLDivElement, shouldPositionTopMost:boolean) => voida function that is called after applyInitialBarStyle to do some minor changes on the bar style
default:
{}changeBarVisibility: (barWrapperDiv:HTMLDivElement, barDiv:HTMLDivElement, newVisibility:boolean) => voidfunction that changes the visibility of the bar default:
{
if (newVisibility) {
barWrapperDiv.style.opacity = '1';
} else {
barWrapperDiv.style.opacity = '0';
}
}changeValueFunc: (barWrapperDiv:HTMLDivElement, barDiv:HTMLDivElement, value:number) => voidfunction to change to value of the bar default:
{
barDiv.style.right = (100 - value) + '%'
}BarElement.barWrapperrepresents the bar wrapperBarElement.barrepresents the bar
access:
var _ = TinyBar.BarElement.barProgressbarStatus.initialonly when the bar is createdProgressbarStatus.startedthe bar is "running"ProgressbarStatus.finishedthe bar has finished
access:
var _ = TinyBar.ProgressbarStatus.barMaking the bar red
var TinyBar = require('/path/to/tinybar')
var tinyBar = new TinyBar.TinyBar({
backgroundColor: 'red'
})Let the bar go from right to left
var tinyBar = new TinyBar.TinyBar({
extendInitialBarStyle: function (barDiv, shouldPositionTopMost) {
barDiv.style.right = '0'
barDiv.style.left = '100%'
},
changeValueFunc: function (barWrapperDiv, barDiv, value) {
barDiv.style.left = (100 - value) + '%'
}
})Use parent
var tinyBar = new TinyBar.TinyBar('myHtmlId')version: string... the version ;D use as readonlyprojectName: stringthe project name, used for outputsettings: Settingsthe settings of the current progressbar
TODO which options should not be changed??value: numberthe current value (percentage 0 <= value <= 100) use as readonlystatus: ProgressbarStatusthe status of the bar use as readonlyshouldPositionTopMost: booleantrue: no parent provided so position at the top, false: parent id present use as readonlybarWrapper:HTMLDivElementthe html div that represents the bar wrapper use as readonlybar:HTMLDivElementthe html div element represents the bar use as readonly
-
constructor(settings?:Settings | string, htmlParentDivId?:string | Settings | HTMLElement, domElementsCreatedCallback?:() => void) => TinyBar
settingsthe settings for the bar
htmlParentDivIdhtmlParentDivId the parent div id OR the parent html div object OR null (to position the progressbar at the top)
domElementsCreatedCallbackcalled when the dom elements were created and the bar is ready to be used -
start(startingValue:number = 0.5) => TinyBardisplays the bar and sets the given value
startingValuethe value to start with -
go(value:number, callback?:() => void, hideBarWhenFinished:boolean = true) => voidgoes to the given percentage (0 <= percentage <= 100)
valuethe percentage to set (you can decrease the value with this function)
callbackcalled when the value has changed and the transition has finished
hideBarWhenFinishedtrue: hides the bar when the value is >= 100, false: not -
inc(callback?:() => void) => voidincrements the value by a random value but never reaches 100%
taken from https://github.com/rstacruz/nprogress/blob/master/nprogress.js (check out the project!)
callbackcalled when the value has changed and the transition has finished -
done(hideBar:boolean = true, callback?:() => void) => void -
autoIncrement() => voidstarts automatically incrementing the value of the bar (calls .inc in a setInterval loop) -
clearAutoIncrement() => voidclears the auto increment interval
Every time an instance of TinyBar is created the default settings are applied...
to change the default settings (e.g. you want all bars to be black) try
TinyBar.defaultSettings.css.backgroundColor = 'black'where TinyBar.defaultSettings has the type Settings
//crate the bar
var tinyBar = new TinyBar.TinyBar()
//start and increment
tinybar.start().autoIncrement()
//do something...
//finally hide the bar
tinybar.done()For more examples see the examples dir
To run the require examples with webpack you will need webpack and the webpack-dev-server
to install dependencies open a shell and cd to the example project folder (examples) and run:
npm install
or
npm install webpack
npm install webpack-dev-server
then you can execute npm run dev to start the webpack-dev-server (wich serves the bundle.js on port 8080)
- All callbacks are called with the corresponding TinyBar as the
thisvalue
