| 1 |
/* |
| 2 |
* jQuery Numerator Plugin 0.2.1 |
| 3 |
* https://github.com/garethdn/jquery-numerator |
| 4 |
* |
| 5 |
* Copyright 2015, Gareth Nolan |
| 6 |
* http://ie.linkedin.com/in/garethnolan/ |
| 7 |
|
| 8 |
* Based on jQuery Boilerplate by Zeno Rocha with the help of Addy Osmani |
| 9 |
* http://jqueryboilerplate.com |
| 10 |
* |
| 11 |
* Licensed under the MIT license: |
| 12 |
* http://www.opensource.org/licenses/MIT |
| 13 |
*/ |
| 14 |
|
| 15 |
;(function (factory) { |
| 16 |
'use strict'; |
| 17 |
if (typeof define === 'function' && define.amd) { |
| 18 |
// AMD is used - Register as an anonymous module. |
| 19 |
define(['jquery'], factory); |
| 20 |
} else if (typeof exports === 'object') { |
| 21 |
factory(require('jquery')); |
| 22 |
} else { |
| 23 |
// Neither AMD nor CommonJS used. Use global variables. |
| 24 |
if (typeof jQuery === 'undefined') { |
| 25 |
throw 'jquery-numerator requires jQuery to be loaded first'; |
| 26 |
} |
| 27 |
factory(jQuery); |
| 28 |
} |
| 29 |
}(function ($) { |
| 30 |
|
| 31 |
var pluginName = "numerator", |
| 32 |
defaults = { |
| 33 |
easing: 'swing', |
| 34 |
duration: 500, |
| 35 |
delimiter: undefined, |
| 36 |
rounding: 0, |
| 37 |
toValue: undefined, |
| 38 |
fromValue: undefined, |
| 39 |
queue: false, |
| 40 |
onStart: function(){}, |
| 41 |
onStep: function(){}, |
| 42 |
onProgress: function(){}, |
| 43 |
onComplete: function(){} |
| 44 |
}; |
| 45 |
|
| 46 |
function Plugin ( element, options ) { |
| 47 |
this.element = element; |
| 48 |
this.settings = $.extend( {}, defaults, options ); |
| 49 |
this._defaults = defaults; |
| 50 |
this._name = pluginName; |
| 51 |
this.init(); |
| 52 |
} |
| 53 |
|
| 54 |
Plugin.prototype = { |
| 55 |
|
| 56 |
init: function () { |
| 57 |
this.parseElement(); |
| 58 |
this.setValue(); |
| 59 |
}, |
| 60 |
|
| 61 |
parseElement: function () { |
| 62 |
var elText = $.trim($(this.element).text()); |
| 63 |
|
| 64 |
this.settings.fromValue = this.settings.fromValue || this.format(elText); |
| 65 |
}, |
| 66 |
|
| 67 |
setValue: function() { |
| 68 |
var self = this; |
| 69 |
|
| 70 |
$({value: self.settings.fromValue}).animate({value: self.settings.toValue}, { |
| 71 |
|
| 72 |
duration: parseInt(self.settings.duration, 10), |
| 73 |
|
| 74 |
easing: self.settings.easing, |
| 75 |
|
| 76 |
start: self.settings.onStart, |
| 77 |
|
| 78 |
step: function(now, fx) { |
| 79 |
$(self.element).text(self.format(now)); |
| 80 |
// accepts two params - (now, fx) |
| 81 |
self.settings.onStep(now, fx); |
| 82 |
}, |
| 83 |
|
| 84 |
// accepts three params - (animation object, progress ratio, time remaining(ms)) |
| 85 |
progress: self.settings.onProgress, |
| 86 |
|
| 87 |
complete: self.settings.onComplete |
| 88 |
}); |
| 89 |
}, |
| 90 |
|
| 91 |
format: function(value){ |
| 92 |
var self = this; |
| 93 |
|
| 94 |
if ( parseInt(this.settings.rounding ) < 1) { |
| 95 |
value = parseInt(value, 10); |
| 96 |
} else { |
| 97 |
value = parseFloat(value).toFixed( parseInt(this.settings.rounding) ); |
| 98 |
} |
| 99 |
|
| 100 |
if (self.settings.delimiter) { |
| 101 |
return this.delimit(value) |
| 102 |
} else { |
| 103 |
return value; |
| 104 |
} |
| 105 |
}, |
| 106 |
|
| 107 |
// TODO: Add comments to this function |
| 108 |
delimit: function(value){ |
| 109 |
var self = this; |
| 110 |
|
| 111 |
value = value.toString(); |
| 112 |
|
| 113 |
if (self.settings.rounding && parseInt(self.settings.rounding, 10) > 0) { |
| 114 |
var decimals = value.substring( (value.length - (self.settings.rounding + 1)), value.length ), |
| 115 |
wholeValue = value.substring( 0, (value.length - (self.settings.rounding + 1))); |
| 116 |
|
| 117 |
return self.addDelimiter(wholeValue) + decimals; |
| 118 |
} else { |
| 119 |
return self.addDelimiter(value); |
| 120 |
} |
| 121 |
}, |
| 122 |
|
| 123 |
addDelimiter: function(value){ |
| 124 |
return value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, this.settings.delimiter); |
| 125 |
} |
| 126 |
}; |
| 127 |
|
| 128 |
$.fn[ pluginName ] = function ( options ) { |
| 129 |
return this.each(function() { |
| 130 |
if ( $.data( this, "plugin_" + pluginName ) ) { |
| 131 |
$.data(this, 'plugin_' + pluginName, null); |
| 132 |
} |
| 133 |
$.data( this, "plugin_" + pluginName, new Plugin( this, options ) ); |
| 134 |
}); |
| 135 |
}; |
| 136 |
|
| 137 |
})); |