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 = elem.dom.innerHTML();
48 if (startNum === "") {
53 this.ul = elem.children('ul');
55 this.digits = new Array();
57 for (i=startNum.length-1; i>=0; i=i-1)
59 this.addDigit(startNum[i]);
63 FlipCounter.prototype.addDigit = function (num) {
64 // Add separator after every 3rd digit
65 if (this.digits.length % 3 == 0 && this.digits.length != 0)
70 this.ul.prepend('<li>\
71 <div class="numberwrap">\
72 <div class="flipper_top flipper_top1"></div>\
73 <div class="flipper_top flipper_top2 flipper_top_back">\
75 <div class="rings"></div>\
77 <div class="flipper_top flipper_top_front">\
79 <div class="rings"></div>\
81 <div class="flipper_bottom flipper_bottom4"></div>\
82 <div class="flipper_bottom flipper_bottom3"></div>\
83 <div class="flipper_bottom flipper_bottom2"></div>\
84 <div class="flipper_bottom flipper_bottom1 flipper_bottom_back">\
86 <div class="rings"></div>\
88 <div class="flipper_bottom flipper_bottom_front">\
90 <div class="rings"></div>\
95 var li = this.ul.find('li:first-child');
96 var digit = new Digit(li, num);
98 this.digits.push(digit);
101 this.ulWidth = this.ulWidth + digit.li.outerWidth(true);
102 this.ul.css('min-width', this.ulWidth);
103 this.ul.css('min-height', digit.li.outerHeight(true));
106 FlipCounter.prototype.removeDigit = function () {
107 var digit = this.digits.splice(this.digits.length-1, 1)[0];
108 this.ulWidth = this.ulWidth - digit.li.outerWidth(true);
112 if (this.digits.length % 3 == 0)
114 var comma = this.ul.find('li.comma:first-child');
115 this.ulWidth = this.ulWidth - comma.outerWidth(true);
119 // Update width to current
120 this.ul.css('min-width', this.ulWidth);
123 FlipCounter.prototype.addSeparator = function (num) {
124 this.ul.prepend('<li class="comma">,</li>');
127 var comma = this.ul.find('li.comma:first-child');
128 this.ulWidth = this.ulWidth + comma.outerWidth(true);
129 this.ul.css('min-width', this.ulWidth);
132 FlipCounter.prototype.updateTo = function (num) {
133 var numStr = parseInt(num).toString();
135 // Change the number of digits displayed if needed
136 if (numStr.length != this.digits.length)
138 var diff = numStr.length - this.digits.length;
141 for (i=0; i<diff; i=i+1) {
147 for (i=diff; i<0; i=i+1) {
152 this.options['onResize']();
155 // Change all digit values
156 for (i=0; i<numStr.length; i=i+1)
158 this.digits[i].flipTo(numStr[numStr.length - 1 - i]);
162 function Digit( element, currentNumber ) {
163 currentNumber = parseInt(currentNumber);
165 this.li = $(element);
166 this.topFrontDiv = this.li.find('.flipper_top_front');
167 this.bottomFrontDiv = this.li.find('.flipper_bottom_front');
168 this.topNumBack = this.li.find('.flipper_top_back span');
169 this.topNumFront = this.li.find('.flipper_top_front span');
170 this.bottomNumBack = this.li.find('.flipper_bottom_back span');
171 this.bottomNumFront = this.li.find('.flipper_bottom_front span');
173 this.targetNum = currentNumber;
174 this.currentNum = currentNumber;
175 this.nextNum = currentNumber;
177 this.currentlyAnimating = false;
180 Digit.prototype.flipTo = function (num) {
181 if (this.currentNum === num)
184 this.targetNum = num;
185 if (this.currentlyAnimating)
191 Digit.prototype.animNext = function () {
192 if (this.currentNum == this.targetNum)
194 this.currentlyAnimating = false;
198 var doRandomDelay = !this.currentlyAnimating;
199 this.currentlyAnimating = true;
200 this.nextNum = this.currentNum + 1;
201 if (this.nextNum > 9)
204 var delay = Math.random()/5;
205 if (!doRandomDelay) delay = 0.01;
207 // Animate top flipper
209 digit.topNumBack.html(digit.nextNum);
210 digit.topFrontDiv.tween({
215 duration: this.manager.options.speed,
221 // Animate bottom flipper with delay
222 digit.bottomFrontDiv.tween({
226 time: delay + this.manager.options.speed,
227 duration: this.manager.options.speed * 0.5,
230 onStart: function() {
231 digit.bottomNumFront.html(digit.nextNum);
234 digit.currentNum = digit.nextNum;
235 digit.topNumFront.html(digit.currentNum);
236 digit.topFrontDiv.removeAttr('style', '');
237 digit.bottomNumBack.html(digit.currentNum);
239 digit.manager.options['onFlip']();
245 // A really lightweight plugin wrapper around the constructor,
246 // preventing against multiple instantiations
247 $.fn[pluginName+'Init'] = function ( options ) {
248 return this.each(function () {
249 if (!$.data(this, 'plugin_' + pluginName)) {
250 $.data(this, 'plugin_' + pluginName,
251 new FlipCounter( this, options ));
256 $.fn[pluginName+'Update'] = function ( num ) {
257 return this.each(function () {
258 var obj = $.data(this, 'plugin_' + pluginName);
265 })( jQuery, window, document );