Skip to content
Browse files

added update/success/fail callbacks

  • Loading branch information...
1 parent 29af550 commit 4701fa72debcbb5c501b18f9f5db0ee97f81f364 Stephan Groß committed Apr 12, 2012
Showing with 35 additions and 21 deletions.
  1. +35 −21 bootstrap-progressbar.js
View
56 bootstrap-progressbar.js
@@ -1,5 +1,5 @@
/* ========================================================
- * bootstrap-progressbar v0.4.0
+ * bootstrap-progressbar v0.4.1
* ========================================================
* Copyright 2012 minddust.com
*
@@ -33,20 +33,27 @@
, percentage = $this.attr('data-percentage')
, amount_part = $this.attr('data-amount-part')
, amount_total = $this.attr('data-amount-total')
- , callback
+ , update
+ , done
+ , fail
- if ( options.use_percentage && !percentage ) return
+ update = ( options.update && typeof(options.update) == 'function' ) ? options.update : $.fn.progressbar.defaults.update
+ done = ( options.done && typeof(options.done) == 'function' ) ? options.done : $.fn.progressbar.defaults.done
+ fail = ( options.fail && typeof(options.fail) == 'function' ) ? options.fail : $.fn.progressbar.defaults.fail
+
+ if ( options.use_percentage && !percentage ) {
+ fail("bootstrap-progressbar: you can't use percentage without data-percentage being set")
+ return
+ }
else if ( !options.use_percentage ) {
- if ( !amount_part && !amount_total )
+ if ( !amount_part && !amount_total ) {
+ fail("bootstrap-progressbar: you can't use values without data-amount-part and data-amount-total being set")
return
- else {
- percentage = Math.round(100 * amount_part / amount_total)
}
+ else
+ percentage = Math.round(100 * amount_part / amount_total)
}
- if ( options.callback && typeof(options.callback) == 'function' ) callback = options.callback
- else callback = $.fn.progressbar.defaults.callback
-
if ( options.display_text === $.fn.progressbar.display_text.center ) {
$parent.css('position', 'relative')
$this.css({
@@ -62,38 +69,43 @@
var $back = $parent.find('.progressbar-back-text')
, $front = $parent.find('.progressbar-front-text')
$front.css('width', $parent.css('width'))
- $(window).resize(function() { $front.css('width', $parent.css('width')) })
+ $(window).resize(function() { $front.css('width', $parent.css('width')) }) // normal resizing would brick the structure because width is in px
}
setTimeout(function() {
$this.css('width', percentage+'%')
var current_percentage
, current_value
+ , this_width
, parent_width
, text
var progress = setInterval(function() {
- parent_width = $this.parent().width()
- current_percentage = Math.round(100 * $this.width() / parent_width)
- current_value = Math.round($this.width() / parent_width * amount_total)
+ this_width = $this.width()
+ parent_width = $parent.width()
+ current_percentage = Math.round(100 * this_width / parent_width)
+ current_value = Math.round(this_width / parent_width * amount_total)
if ( current_percentage >= percentage ) {
current_percentage = percentage
current_value = amount_part
+ done()
clearInterval(progress)
}
- if ( options.use_percentage ) text = current_percentage +'%'
- else text = current_value + ' / ' + amount_total
+ if ( options.display_text !== $.fn.progressbar.display_text.none ) {
+ text = ( options.use_percentage ) ? (current_percentage +'%') : (current_value + ' / ' + amount_total)
- if ( options.display_text === $.fn.progressbar.display_text.filled ) $this.text(text)
- else if ( options.display_text === $.fn.progressbar.display_text.center ) {
- $front.text(text)
- $back.text(text)
+ if ( options.display_text === $.fn.progressbar.display_text.filled )
+ $this.text(text)
+ else if ( options.display_text === $.fn.progressbar.display_text.center ) {
+ $front.text(text)
+ $back.text(text)
+ }
}
- callback(current_percentage)
+ update(current_percentage)
}, options.refresh_speed)
}, options.transition_delay)
@@ -125,7 +137,9 @@
, display_text: $.fn.progressbar.display_text.none
, use_percentage: true
, border_radius: '4px'
- , callback: $.noop
+ , update: $.noop
+ , done: $.noop
+ , fail: $.noop
}
$.fn.progressbar.Constructor = Progressbar

0 comments on commit 4701fa7

Please sign in to comment.
Something went wrong with that request. Please try again.