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} pane
18 * When a pane is activated
19 * @param {Roo.bootstrap.dash.TabPane} pane
31 Roo.extend(Roo.ux.FlipCounter, Roo.bootstrap.Component, {
37 ;(function ( $, window, document, undefined ) {
39 // Create the defaults once
40 var pluginName = 'flipCounter',
43 onFlip: function() {},
44 onResize: function() {}
48 function FlipCounter( element, options ) {
50 this.options = $.extend({}, defaults, options);
51 this._defaults = defaults;
52 this._name = pluginName;
56 FlipCounter.prototype.init = function () {
58 var startNum = elem.html();
59 if (startNum === "") startNum = "0";
60 elem.html('<ul class="flipcounter"></ul>');
62 this.ul = elem.children('ul');
64 this.digits = new Array();
66 for (i=startNum.length-1; i>=0; i=i-1)
68 this.addDigit(startNum[i]);
72 FlipCounter.prototype.addDigit = function (num) {
73 // Add separator after every 3rd digit
74 if (this.digits.length % 3 == 0 && this.digits.length != 0)
79 this.ul.prepend('<li>\
80 <div class="numberwrap">\
81 <div class="flipper_top flipper_top1"></div>\
82 <div class="flipper_top flipper_top2 flipper_top_back">\
84 <div class="rings"></div>\
86 <div class="flipper_top flipper_top_front">\
88 <div class="rings"></div>\
90 <div class="flipper_bottom flipper_bottom4"></div>\
91 <div class="flipper_bottom flipper_bottom3"></div>\
92 <div class="flipper_bottom flipper_bottom2"></div>\
93 <div class="flipper_bottom flipper_bottom1 flipper_bottom_back">\
95 <div class="rings"></div>\
97 <div class="flipper_bottom flipper_bottom_front">\
99 <div class="rings"></div>\
104 var li = this.ul.find('li:first-child');
105 var digit = new Digit(li, num);
106 digit.manager = this;
107 this.digits.push(digit);
110 this.ulWidth = this.ulWidth + digit.li.outerWidth(true);
111 this.ul.css('min-width', this.ulWidth);
112 this.ul.css('min-height', digit.li.outerHeight(true));
115 FlipCounter.prototype.removeDigit = function () {
116 var digit = this.digits.splice(this.digits.length-1, 1)[0];
117 this.ulWidth = this.ulWidth - digit.li.outerWidth(true);
121 if (this.digits.length % 3 == 0)
123 var comma = this.ul.find('li.comma:first-child');
124 this.ulWidth = this.ulWidth - comma.outerWidth(true);
128 // Update width to current
129 this.ul.css('min-width', this.ulWidth);
132 FlipCounter.prototype.addSeparator = function (num) {
133 this.ul.prepend('<li class="comma">,</li>');
136 var comma = this.ul.find('li.comma:first-child');
137 this.ulWidth = this.ulWidth + comma.outerWidth(true);
138 this.ul.css('min-width', this.ulWidth);
141 FlipCounter.prototype.updateTo = function (num) {
142 var numStr = parseInt(num).toString();
144 // Change the number of digits displayed if needed
145 if (numStr.length != this.digits.length)
147 var diff = numStr.length - this.digits.length;
150 for (i=0; i<diff; i=i+1) {
156 for (i=diff; i<0; i=i+1) {
161 this.options['onResize']();
164 // Change all digit values
165 for (i=0; i<numStr.length; i=i+1)
167 this.digits[i].flipTo(numStr[numStr.length - 1 - i]);
171 function Digit( element, currentNumber ) {
172 currentNumber = parseInt(currentNumber);
174 this.li = $(element);
175 this.topFrontDiv = this.li.find('.flipper_top_front');
176 this.bottomFrontDiv = this.li.find('.flipper_bottom_front');
177 this.topNumBack = this.li.find('.flipper_top_back span');
178 this.topNumFront = this.li.find('.flipper_top_front span');
179 this.bottomNumBack = this.li.find('.flipper_bottom_back span');
180 this.bottomNumFront = this.li.find('.flipper_bottom_front span');
182 this.targetNum = currentNumber;
183 this.currentNum = currentNumber;
184 this.nextNum = currentNumber;
186 this.currentlyAnimating = false;
189 Digit.prototype.flipTo = function (num) {
190 if (this.currentNum === num)
193 this.targetNum = num;
194 if (this.currentlyAnimating)
200 Digit.prototype.animNext = function () {
201 if (this.currentNum == this.targetNum)
203 this.currentlyAnimating = false;
207 var doRandomDelay = !this.currentlyAnimating;
208 this.currentlyAnimating = true;
209 this.nextNum = this.currentNum + 1;
210 if (this.nextNum > 9)
213 var delay = Math.random()/5;
214 if (!doRandomDelay) delay = 0.01;
216 // Animate top flipper
218 digit.topNumBack.html(digit.nextNum);
219 digit.topFrontDiv.tween({
224 duration: this.manager.options.speed,
230 // Animate bottom flipper with delay
231 digit.bottomFrontDiv.tween({
235 time: delay + this.manager.options.speed,
236 duration: this.manager.options.speed * 0.5,
239 onStart: function() {
240 digit.bottomNumFront.html(digit.nextNum);
243 digit.currentNum = digit.nextNum;
244 digit.topNumFront.html(digit.currentNum);
245 digit.topFrontDiv.removeAttr('style', '');
246 digit.bottomNumBack.html(digit.currentNum);
248 digit.manager.options['onFlip']();
254 // A really lightweight plugin wrapper around the constructor,
255 // preventing against multiple instantiations
256 $.fn[pluginName+'Init'] = function ( options ) {
257 return this.each(function () {
258 if (!$.data(this, 'plugin_' + pluginName)) {
259 $.data(this, 'plugin_' + pluginName,
260 new FlipCounter( this, options ));
265 $.fn[pluginName+'Update'] = function ( num ) {
266 return this.each(function () {
267 var obj = $.data(this, 'plugin_' + pluginName);
274 })( jQuery, window, document );