3 Roo.ux.FlipCounter = function(options)
5 Roo.apply(this, options);
6 //this.el = $(element);
7 //this.options = $.extend({}, defaults, options);
12 * When a box is flipped
13 * @param {Roo.ux.FlipCounter} counter
18 * When a box is resized
19 * @param {Roo.ux.FlipCounter} counter
31 Roo.extend(Roo.ux.FlipCounter, Roo.bootstrap.Component, {
36 getAutoCreate : function(){
47 var startNum = ""+ this.startnumber;
49 this.digits = new Array();
51 for (i=startNum.length-1; i>=0; i=i-1)
53 this.addDigit(startNum[i]);
57 addDigit : function (num) {
58 // Add separator after every 3rd digit
59 if (this.digits.length % 3 == 0 && this.digits.length != 0)
64 this.ul.prepend('<li>\
65 <div class="numberwrap">\
66 <div class="flipper_top flipper_top1"></div>\
67 <div class="flipper_top flipper_top2 flipper_top_back">\
69 <div class="rings"></div>\
71 <div class="flipper_top flipper_top_front">\
73 <div class="rings"></div>\
75 <div class="flipper_bottom flipper_bottom4"></div>\
76 <div class="flipper_bottom flipper_bottom3"></div>\
77 <div class="flipper_bottom flipper_bottom2"></div>\
78 <div class="flipper_bottom flipper_bottom1 flipper_bottom_back">\
80 <div class="rings"></div>\
82 <div class="flipper_bottom flipper_bottom_front">\
84 <div class="rings"></div>\
89 var li = this.ul.find('li:first-child');
90 var digit = new Digit(li, num);
92 this.digits.push(digit);
95 this.ulWidth = this.ulWidth + digit.li.outerWidth(true);
96 this.ul.css('min-width', this.ulWidth);
97 this.ul.css('min-height', digit.li.outerHeight(true));
100 FlipCounter.prototype.removeDigit = function () {
101 var digit = this.digits.splice(this.digits.length-1, 1)[0];
102 this.ulWidth = this.ulWidth - digit.li.outerWidth(true);
106 if (this.digits.length % 3 == 0)
108 var comma = this.ul.find('li.comma:first-child');
109 this.ulWidth = this.ulWidth - comma.outerWidth(true);
113 // Update width to current
114 this.ul.css('min-width', this.ulWidth);
117 FlipCounter.prototype.addSeparator = function (num) {
118 this.ul.prepend('<li class="comma">,</li>');
121 var comma = this.ul.find('li.comma:first-child');
122 this.ulWidth = this.ulWidth + comma.outerWidth(true);
123 this.ul.css('min-width', this.ulWidth);
126 FlipCounter.prototype.updateTo = function (num) {
127 var numStr = parseInt(num).toString();
129 // Change the number of digits displayed if needed
130 if (numStr.length != this.digits.length)
132 var diff = numStr.length - this.digits.length;
135 for (i=0; i<diff; i=i+1) {
141 for (i=diff; i<0; i=i+1) {
146 this.options['onResize']();
149 // Change all digit values
150 for (i=0; i<numStr.length; i=i+1)
152 this.digits[i].flipTo(numStr[numStr.length - 1 - i]);
156 function Digit( element, currentNumber ) {
157 currentNumber = parseInt(currentNumber);
159 this.li = $(element);
160 this.topFrontDiv = this.li.find('.flipper_top_front');
161 this.bottomFrontDiv = this.li.find('.flipper_bottom_front');
162 this.topNumBack = this.li.find('.flipper_top_back span');
163 this.topNumFront = this.li.find('.flipper_top_front span');
164 this.bottomNumBack = this.li.find('.flipper_bottom_back span');
165 this.bottomNumFront = this.li.find('.flipper_bottom_front span');
167 this.targetNum = currentNumber;
168 this.currentNum = currentNumber;
169 this.nextNum = currentNumber;
171 this.currentlyAnimating = false;
174 Digit.prototype.flipTo = function (num) {
175 if (this.currentNum === num)
178 this.targetNum = num;
179 if (this.currentlyAnimating)
185 Digit.prototype.animNext = function () {
186 if (this.currentNum == this.targetNum)
188 this.currentlyAnimating = false;
192 var doRandomDelay = !this.currentlyAnimating;
193 this.currentlyAnimating = true;
194 this.nextNum = this.currentNum + 1;
195 if (this.nextNum > 9)
198 var delay = Math.random()/5;
199 if (!doRandomDelay) delay = 0.01;
201 // Animate top flipper
203 digit.topNumBack.html(digit.nextNum);
204 digit.topFrontDiv.tween({
209 duration: this.manager.options.speed,
215 // Animate bottom flipper with delay
216 digit.bottomFrontDiv.tween({
220 time: delay + this.manager.options.speed,
221 duration: this.manager.options.speed * 0.5,
224 onStart: function() {
225 digit.bottomNumFront.html(digit.nextNum);
228 digit.currentNum = digit.nextNum;
229 digit.topNumFront.html(digit.currentNum);
230 digit.topFrontDiv.removeAttr('style', '');
231 digit.bottomNumBack.html(digit.currentNum);
233 digit.manager.options['onFlip']();
239 // A really lightweight plugin wrapper around the constructor,
240 // preventing against multiple instantiations
241 $.fn[pluginName+'Init'] = function ( options ) {
242 return this.each(function () {
243 if (!$.data(this, 'plugin_' + pluginName)) {
244 $.data(this, 'plugin_' + pluginName,
245 new FlipCounter( this, options ));
250 $.fn[pluginName+'Update'] = function ( num ) {
251 return this.each(function () {
252 var obj = $.data(this, 'plugin_' + pluginName);
259 })( jQuery, window, document );