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, {
39 var startNum = elem.dom.innerHTML();
40 if (startNum === "") {
43 elem.html('<ul class="flipcounter"></ul>');
45 this.ul = elem.children('ul');
47 this.digits = new Array();
49 for (i=startNum.length-1; i>=0; i=i-1)
51 this.addDigit(startNum[i]);
55 FlipCounter.prototype.addDigit = function (num) {
56 // Add separator after every 3rd digit
57 if (this.digits.length % 3 == 0 && this.digits.length != 0)
62 this.ul.prepend('<li>\
63 <div class="numberwrap">\
64 <div class="flipper_top flipper_top1"></div>\
65 <div class="flipper_top flipper_top2 flipper_top_back">\
67 <div class="rings"></div>\
69 <div class="flipper_top flipper_top_front">\
71 <div class="rings"></div>\
73 <div class="flipper_bottom flipper_bottom4"></div>\
74 <div class="flipper_bottom flipper_bottom3"></div>\
75 <div class="flipper_bottom flipper_bottom2"></div>\
76 <div class="flipper_bottom flipper_bottom1 flipper_bottom_back">\
78 <div class="rings"></div>\
80 <div class="flipper_bottom flipper_bottom_front">\
82 <div class="rings"></div>\
87 var li = this.ul.find('li:first-child');
88 var digit = new Digit(li, num);
90 this.digits.push(digit);
93 this.ulWidth = this.ulWidth + digit.li.outerWidth(true);
94 this.ul.css('min-width', this.ulWidth);
95 this.ul.css('min-height', digit.li.outerHeight(true));
98 FlipCounter.prototype.removeDigit = function () {
99 var digit = this.digits.splice(this.digits.length-1, 1)[0];
100 this.ulWidth = this.ulWidth - digit.li.outerWidth(true);
104 if (this.digits.length % 3 == 0)
106 var comma = this.ul.find('li.comma:first-child');
107 this.ulWidth = this.ulWidth - comma.outerWidth(true);
111 // Update width to current
112 this.ul.css('min-width', this.ulWidth);
115 FlipCounter.prototype.addSeparator = function (num) {
116 this.ul.prepend('<li class="comma">,</li>');
119 var comma = this.ul.find('li.comma:first-child');
120 this.ulWidth = this.ulWidth + comma.outerWidth(true);
121 this.ul.css('min-width', this.ulWidth);
124 FlipCounter.prototype.updateTo = function (num) {
125 var numStr = parseInt(num).toString();
127 // Change the number of digits displayed if needed
128 if (numStr.length != this.digits.length)
130 var diff = numStr.length - this.digits.length;
133 for (i=0; i<diff; i=i+1) {
139 for (i=diff; i<0; i=i+1) {
144 this.options['onResize']();
147 // Change all digit values
148 for (i=0; i<numStr.length; i=i+1)
150 this.digits[i].flipTo(numStr[numStr.length - 1 - i]);
154 function Digit( element, currentNumber ) {
155 currentNumber = parseInt(currentNumber);
157 this.li = $(element);
158 this.topFrontDiv = this.li.find('.flipper_top_front');
159 this.bottomFrontDiv = this.li.find('.flipper_bottom_front');
160 this.topNumBack = this.li.find('.flipper_top_back span');
161 this.topNumFront = this.li.find('.flipper_top_front span');
162 this.bottomNumBack = this.li.find('.flipper_bottom_back span');
163 this.bottomNumFront = this.li.find('.flipper_bottom_front span');
165 this.targetNum = currentNumber;
166 this.currentNum = currentNumber;
167 this.nextNum = currentNumber;
169 this.currentlyAnimating = false;
172 Digit.prototype.flipTo = function (num) {
173 if (this.currentNum === num)
176 this.targetNum = num;
177 if (this.currentlyAnimating)
183 Digit.prototype.animNext = function () {
184 if (this.currentNum == this.targetNum)
186 this.currentlyAnimating = false;
190 var doRandomDelay = !this.currentlyAnimating;
191 this.currentlyAnimating = true;
192 this.nextNum = this.currentNum + 1;
193 if (this.nextNum > 9)
196 var delay = Math.random()/5;
197 if (!doRandomDelay) delay = 0.01;
199 // Animate top flipper
201 digit.topNumBack.html(digit.nextNum);
202 digit.topFrontDiv.tween({
207 duration: this.manager.options.speed,
213 // Animate bottom flipper with delay
214 digit.bottomFrontDiv.tween({
218 time: delay + this.manager.options.speed,
219 duration: this.manager.options.speed * 0.5,
222 onStart: function() {
223 digit.bottomNumFront.html(digit.nextNum);
226 digit.currentNum = digit.nextNum;
227 digit.topNumFront.html(digit.currentNum);
228 digit.topFrontDiv.removeAttr('style', '');
229 digit.bottomNumBack.html(digit.currentNum);
231 digit.manager.options['onFlip']();
237 // A really lightweight plugin wrapper around the constructor,
238 // preventing against multiple instantiations
239 $.fn[pluginName+'Init'] = function ( options ) {
240 return this.each(function () {
241 if (!$.data(this, 'plugin_' + pluginName)) {
242 $.data(this, 'plugin_' + pluginName,
243 new FlipCounter( this, options ));
248 $.fn[pluginName+'Update'] = function ( num ) {
249 return this.each(function () {
250 var obj = $.data(this, 'plugin_' + pluginName);
257 })( jQuery, window, document );