diff --git a/assets/coffee/tick.coffee b/assets/coffee/tick.coffee index d49ee60..2f167f6 100755 --- a/assets/coffee/tick.coffee +++ b/assets/coffee/tick.coffee @@ -46,6 +46,7 @@ $.fn.ticker = (options) -> separators boolean if true, all arbitrary characters inbetween digits are wrapped in seperated elements if false, these characters are stripped out autostart boolean whether or not to start the ticker when instantiated + minlength int minimum length of the element, leading zeros will be put in place if not long enough Events @@ -64,6 +65,7 @@ class Tick delay : options.delay or 1000 separators: if options.separators? then options.separators else false autostart : if options.autostart? then options.autostart else true + minlength : parseInt(options.minlength, 10) or 1 @increment = @build_increment_callback( options.incremental ) @@ -96,9 +98,14 @@ class Tick render: () -> - digits = String( @value ).split( '' ) + digits = String( @value ) containers = @element.children( ':not(.tick-separator)' ) + # add leading zeros if length is not long enough + if digits.length < @options.minlength + while (digits.length < @options.minlength) + digits = "0" + digits + # add new containers for each digit that doesnt exist (if they do, just update them) if digits.length > containers.length for i in [0...(digits.length - containers.length)] diff --git a/assets/js/tick.js b/assets/js/tick.js index 78a9564..64d64d5 100755 --- a/assets/js/tick.js +++ b/assets/js/tick.js @@ -49,6 +49,7 @@ separators boolean if true, all arbitrary characters inbetween digits are wrapped in seperated elements if false, these characters are stripped out autostart boolean whether or not to start the ticker when instantiated + minlength int minimum length of the element, leading zeros will be put in place if not long enough Events @@ -68,7 +69,8 @@ this.options = { delay: options.delay || 1000, separators: options.separators != null ? options.separators : false, - autostart: options.autostart != null ? options.autostart : true + autostart: options.autostart != null ? options.autostart : true, + minlength: parseInt(options.minlength, 10) || 1 }; this.increment = this.build_increment_callback(options.incremental); this.value = Number(this.element.html().replace(/[^\d.]/g, '')); @@ -95,8 +97,13 @@ Tick.prototype.render = function() { var container, containers, digits, i, _i, _j, _k, _len, _ref, _ref1, _ref2, _results; - digits = String(this.value).split(''); + digits = String(this.value); containers = this.element.children(':not(.tick-separator)'); + if (digits.length < this.options.minlength) { + while (digits.length < this.options.minlength) { + digits = "0" + digits; + } + } if (digits.length > containers.length) { for (i = _i = 0, _ref = digits.length - containers.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) { if (this.options.separators && this.separators[i]) {