BootstrapProgressbarManager

Easily create and manipulate Bootstrap progress bar.

Features

  1. No need to write html for the progress bar
  2. Easily update progress value
  3. Easily style bootstrap progress bar
  4. Easily create and manage stacked progress bar

Requirement

*Bootsrap 3+

Definition Of Terms

  1. progress element: refers to the container element generated by the plugin with progress class
  2. progress bar element: refers to the main progress element generated by the plugin with progress-bar class
  3. Progress object: this is the handler returned by the plugin var ProgressHandler = $('#progress').progressbarManager()

Usage

Add the plugin

        <script src="bootstrap-progressbar-manager.min.js"></script>

Call the plugin on the container element you want the progress html to be appended to. This will not override existing elements or content of the container element

$(selector).progressbarManager( options );

Where

HTML

Simple Progress Bar Example

JAVASCRIPT

 myProgress = $('#progress').progressbarManager({      
     totalValue : 50,
     initValue : 10 ,
  });

The above generates:

<div id="pbm-bootsrap-progress-1" class="progress">
    <div id="pbm-progress-bar-1" role="progress-bar" aria-valuenow="20" aria-valuemin="0" aria-valuemax="100"     class="progress-bar progress-bar-primary" style="width: 20%; min-width: 2em;">20%</div>
</div>

and append it to the element with id progress.

Example Progressbar

The call to progressbarManager returns progress object which you can be used to modify the progress element on the fly.

To update the value of the progress for instance :

    myProgress.setValue(30 );

The plugin automatically converts values to percentage representation.

Note: *If you attempt to set a value higher than the value of the totalValue option, the progress bar percentage value will be set to 100% *.

The totalValue option can be a string that can be converted to number representation for instance if you are using it to monitor download or upload progress, you can use the bytes representation

 myDownloadProgress = $('#progress').progressbarManager({      
     totalValue : '40000kb',
     animate : true ,
     stripe : true
  });
  myDownloadProgress.setValue( '4000kb');

Create animated progress bar with stripe

 myProgress = $('#progress').progressbarManager({      
     totalValue : 50,
     initValue : 10 ,
     animate : true ,
     stripe : true
  });

Animated Progressbar

Animated Progress Bar With Different Color

 myProgress = $('#progress').progressbarManager({      
     totalValue : 50,
     initValue : 10 ,
     animate : true ,
     stripe : true ,
     style : 'danger'
  });

Animated Progressbar With Different style

You can use bootsrap contectual style name: success , primary , danger , info and primary

Simulate download with setInterval()

 myDownloadProgress = $('#progress').progressbarManager({      
     totalValue : '20000kb',
     initValue : '0kb' ,
     animate : true ,
     stripe : true ,
     style : 'primary' ,
  });
  // lets simulate download
  var chunk = 0 ;
  var downloading = setInterval(function(){

       myDownloadProgress.setValue(chunk);

      if(myDownloadProgress.isComplete()){
          //change the style
          myDownloadProgress.style('success');
          // nothing to download again
          clearInterval(downloading);
      }
      chunk += 2000;
  }, 2000);

Simulate Download Progress

Custom Progressbar Text

 myDownloadProgress = $('#progress').progressbarManager({      
     totalValue : '20000kb',
     initValue : '0kb' ,
     animate : true ,
     stripe : true ,
     style : 'primary' ,
     showValueHandler : function(progressData){
        progressData.elem.text('Downloaded '+ progressData.currentValue+'%');
     }
  });

Custom Progressbar Text

Some Options can be set after the plugin is initialized. See available option

myDownloadProgress.animate().stripe().setValue( '4000kb');

or remove the stripe

myDownloadProgress.removeStripe();

Stacked Progress Bar

Bootstrap progress bar supports stacked progress bar and you can use the plugin to create stacked progress bar. This can be archived by calling addBar() of the progress object.

Stacked Progress Bar Example

 myProgress = $('#progress').progressbarManager({      
     totalValue : 50,
     initValue : 50 ,
     // this is important
     total : 200
  });

You can add new progress bar to the progress element

 newProgressBarId = myProgress.addBar({      
     totalValue : 50,
     initValue : 40 ,
     style : 'danger'
  });
   newProgressBarId2 = myProgress.addBar({      
     totalValue : 50,
     initValue : 40 ,
     style : 'info'
  });

Stacked Progress Bar

Updating Item Within The Stack

We will save name for each item in the custom data object, then use it to show the text for the progress

// add the initial
myStackedProgress = $('#example-6').progressbarManager({      
     totalValue : 50,
     initValue : 50 ,
     // this is important
     total : 200,
     debug : true ,
     data : {
         name : 'Stack One'
     },
     showValueHandler : function (progressbarData){
         progressbarData.elem.text(progressbarData.data.name)
     }
  });

Add new items

  newProgressBarId2 = myStackedProgress.addBar({      
     totalValue : 30,
     initValue : 20 ,
     style : 'danger',
     data : {
         name : 'Stack Two'
     }
  });
  newProgressBarId3 = myStackedProgress.addBar({      
     totalValue : 30,
     initValue : 20 ,
     style : 'info' ,
     data : {
         name : 'Stack Three'
     }
  });

Then update the second bar

    myStackedProgress.style('success' , newProgressBarId2).animate(newProgressBarId2).setValue( 50 ,newProgressBarId2)

Updating Stacked Progress Bar

Note the custom data

Note:

Check the plugin options to learn more.

Plugin Options

Name Type Description Default
totalValue int The total of the progress we are tracking. If we are tracking upload progress for instance the size of the file should be set to this value.Note: The plugin permits you to use value that can be parsed as int, meaning you can use the size of the file directly e.g 50000kb( this means subsequent values you add will be in kilobytes. 100
initValue int The initial value of the progress bar element 0
style string Bootstrap contextual styling class. You can use info or success or danger or warning or primary. This will add progress-bar-{style} class to the progress element. You can set this option on the fly by calling 'style(styleName)' method of the progress object primary
stripe boolean Whether to add progress-bar-striped class to the progress bar element. true
animate boolean Whether to add active class to the progress bar element true
addDefaultBar boolean Whether to automatically add progress bar element to the progress element. If this option is false, only the progress element will be created initially. Progress bar element can be added by calling 'addBar()' method of the progress object. true
id string The id attribute value to set for the progress element. auto generated
barIdPrefix string The prefix to add to auto generated progress bar element id 'pbm-progress-bar-'
total int The grand total of the progress. This option is for stacked progress bar. This option checked when addBar() method of the progress object is called.For every progress bar element you add, the totalValue of the progress bar is summed and checked by subsequent calls to addBar() if sum , the progress bar will not be aaded to the progress element if (totalValue - sum ) < initValue meaning no required space for new progress bar ( a message will be logged to the console if debug option is set to true ) totalValue option
data object Alloww you save custom data for the progress bar. If using stacked progress bar, each item will have its own data object { }
debug boolean Whether to log messages to the console false
showValueHandler function This function is called to display the current progress percent. An object of progress bar metadata is passed See available option the section for the progress bar metadata The default shows the current percentage value as text attribute of the progress bar element
hideValueHandler function This function is called to hide the progress bar element current progress as feed back for user. The default hides the text attribute the progress bar element
onComplete function callback to be fired when the currentValue of the progress reaches the totalValue. If you are using the stack progress bar, this will be fired when the sum of the currentValue of the progresss bar in the stack reaches the total option empty function
onBarComplete function This callback is fired when progress bar currentValue reaches its total value. empty function

Progress Object Public Methods

Name Description Arguments return
setValue Used to update the current value of the progress bar First (int) newValue : The new value to set Second (string) barId : Option set provide the progress bar id. If you are using stacked progress bar, you can target specific progress bar within the stack by specify its id. Default used is the default progress bar added by default the progress object (for method chaining)
animate Add active class to the progress bar element First (string) barId: Optionally provide id of the progress bar to target the progress object
animateRemove Remove active class from progress bar element First (string) barId: Optionally provide id of the progress bar to target The progress object
stripe Add progress-bar-stripe class to the progress bar element First (string) barId: Optionally provide id of the progress bar to target The progress object
style Change the style of the progress bar element (string) type: info or danger or success or warning or primary Second (string) barId: Optionally provide id of the progress bar to target The progress object
showValue Display the cuccent progress percentage. This methods calls the 1showValueHandler` option the plugin First: (string) barId : Optionally provide id of the progress bar to target The progress object
hideValue Hide the current progress percentage. This methods calls the hideValueHandler option the plugin First:(string) barId : Optionally provide id of the progress bar to target The progress object
isComplete Check if the progress bar is complete (currentValue is up to totalValue) First (string) barId : Optionally provide id of the progress bar to target Boolean
complete Make the progress bar element complete First: (string) barId : Optionally provide id of the progress bar to target The progress object
completeAll Make all the progress bar element in the stack complete First: (string) barId : Optionally provide id of the progress bar to target The progress object
addBar Add new progress bar element to the prigress element. This method is called automatically when the plugin is initialized and addDefaultBar option is true . This means the desciption of parameters for this method can be found in the section describing the plugin options (object) barOptns : The available options are initValue totalValue style animate stripe data showValueHandler: ** : This overides the showValueHandler in the main option for the new progress bar **hideValueHandler : This overides the hideValueHandler in the main option for the new progress bar The id of the new progress bar added
getBar Returns the progress bar metadata First: (string) barId : Optionally provide id of the progress bar to return or the id of the progress bar added by default is used (object) Object Literal See available properties
removeBar Remove a progress bar element (string) barId : The id of the progress bar to target The progress object
destroy Destroy the progress elementitself none void
Progress Bar meta