ux/FlipCounter.js
[roojs1] / ux / FlipCounter.js
1
2
3 Roo.ux.FlipCounter = function(options)
4 {
5     Roo.ux.FlipCounter.superclass.constructor.call(this, options);
6     
7     //Roo.apply(this, options);
8     //this.el = $(element);
9     //this.options = $.extend({}, defaults, options);
10     this.addEvents({
11         // raw events
12         /**
13          * @event flip
14          * When a box is flipped
15          * @param {Roo.ux.FlipCounter} counter
16          */
17         "flip" : true,
18         /**
19          * @event resize
20          * When a box is resized
21          * @param {Roo.ux.FlipCounter} counter
22          */
23         "resize" : true
24         
25          
26     });
27     this.digits = new Array();
28        
29     //this.init();
30
31
32     
33 }
34 Roo.extend(Roo.ux.FlipCounter, Roo.bootstrap.Component, {
35     
36     speed : 0.2,
37     startnumber : 0,
38     currentNumber : 0,
39     decimal : 0,
40     
41     digits : false, // array...
42     ulWidth : 0,
43     cls : '',
44     
45     getAutoCreate : function(){
46         
47         return {
48             tag: 'ul',
49             cls: 'flipcounter ' + this.cls,
50             
51         };
52     },
53         
54     initEvents : function ()
55     { 
56         var startNum = (1.0*this.startnumber).toFixed(this.decimal);
57         
58         for (i=startNum.length-1; i>=0; i=i-1)
59         {
60             if (startNum[i] == '.') {
61                 this.addSeparator('.');
62                 continue;
63             }
64             this.addDigit(startNum[i]);
65         }
66         this.currentNumber = this.startnumber;
67     },
68     
69     addDigit : function (num)
70     {
71         // Add separator after every 3rd digit
72         /*if (this.digits.length % 3 == 0 && this.digits.length != 0)
73         {
74             this.addSeparator(',');
75         }
76         */
77           
78         
79         var digit = new Roo.ux.FlipCounter.Digit({ manager : this, currentNumber : num });
80         digit.manager = this;
81         this.digits.push(digit);
82         digit.render(this.el, 0);
83          
84         // Update width
85         this.ulWidth = this.ulWidth + digit.el.getWidth(true);
86         this.el.set({
87             'min-width' : this.ulWidth,
88             'min-height' :digit.el.getHeight(true)
89         });
90         
91     },
92     
93     removeDigit : function ()
94     {
95         var digit = this.digits.splice(this.digits.length-1, 1)[0];
96         
97         this.ulWidth = this.ulWidth - digit.el.getWidth(true);
98         digit.el.remove();
99         
100         // Remove separators
101        // if (this.digits.length % 3 == 0)
102       //  {
103           //  var comma = this.el.select('li.comma:first-child');
104           //  this.ulWidth = this.ulWidth - comma.getWidth(true);
105           //  comma.remove();
106         //}
107         
108         // Update width to current
109         this.el.set( { 'min-width' : this.ulWidth});
110     },
111     
112     addSeparator : function (str)
113     {
114         var comma = this.el.insertHtml('afterBegin','<li class="comma">'+str+'</li>',true);
115         
116         // Update width
117         
118         this.ulWidth = this.ulWidth + comma.getWidth(true);
119         this.el.set({'min-width' : this.ulWidth});
120     },
121     
122     updateTo : function (num)
123     {
124         var numStr = (1.0*num).toFixed(this.decimal);
125         
126         this.currentNumber = num;
127         // Change the number of digits displayed if needed
128         if (numStr.length != this.digits.length)
129         {
130             var diff = numStr.length - this.digits.length;
131             if (diff > 0)
132             {
133                 for (i=0; i<diff; i=i+1) {
134                     this.addDigit(0);
135                 }
136             }
137             else
138             {
139                 for (i=diff; i<0; i=i+1) {
140                     this.removeDigit();
141                 }
142             }
143             
144             this.fireEvent('onResize',this);
145         }
146         
147         // Change all digit values
148         for (i=0; i<numStr.length; i=i+1)
149         {
150             this.digits[i].flipTo(numStr[numStr.length - 1 - i]);
151         }
152     }
153     
154 });
155
156 Roo.ux.FlipCounter.Digit = function(options)
157 {
158     //Roo.apply(this, options);
159     Roo.ux.FlipCounter.Digit.superclass.constructor.call(this, options);
160
161     
162     this.currentNumber = parseInt(this.currentNumber);
163     
164     
165 }
166
167 Roo.extend(Roo.ux.FlipCounter.Digit, Roo.bootstrap.Component, {
168
169     manager : null, // the flipcounter... 
170     currentNumber : 0,
171     
172     
173     currentNum : 0,
174     nextNum : 0,
175     targetNum : 0,
176     
177     topFrontDiv  : null,
178     bottomFrontDiv : null,
179     topNumBack : null,
180     topNumFront : null,
181     bottomNumBack : null,
182     bottomNumFront : null,
183     
184     
185     
186     
187     getAutoCreate : function(){
188         
189         return {
190                 tag: 'li',
191                 cn : [
192                     {
193                         cls: 'numberwrap',
194                         cn : [
195                             { cls : 'flipper_top flipper_top1' },
196                             {
197                                 cls : 'flipper_top flipper_top2 flipper_top_back',
198                                 cn : [
199                                     { tag: 'span', html: this.currentNumber },
200                                     { cls : 'rings' }
201                                 ]
202                         
203                             },
204                             {
205                                 cls : 'flipper_top flipper_top_front',
206                                 cn : [
207                                     { tag: 'span', html: this.currentNumber },
208                                     { cls : 'rings' }
209                                 ]
210                         
211                             },
212                             { cls : 'flipper_bottom flipper_bottom4' },
213                             { cls : 'flipper_bottom flipper_bottom3' },
214                             { cls : 'flipper_bottom flipper_bottom2' },
215                             {
216                                 cls : 'flipper_bottom flipper_bottom1 flipper_bottom_back',
217                                 cn : [
218                                     { tag: 'span', html: this.currentNumber },
219                                     { cls : 'rings' }
220                                 ]
221                             },
222                             {
223                                 cls : 'flipper_bottom flipper_bottom_front',
224                                 cn : [
225                                     { tag: 'span', html: this.currentNumber },
226                                     { cls : 'rings' }
227                                 ]
228                             },
229                         ]
230                     }
231                 ]
232         };
233     },
234     
235     
236     initEvents : function()
237     {
238         
239          
240         
241         this.topFrontDiv = this.el.select('.flipper_top_front',true).first();
242         this.bottomFrontDiv = this.el.select('.flipper_bottom_front',true).first();
243         this.topNumBack = this.el.select('.flipper_top_back span',true).first();
244         this.topNumFront = this.el.select('.flipper_top_front span',true).first();
245         this.bottomNumBack = this.el.select('.flipper_bottom_back span',true).first();
246         this.bottomNumFront = this.el.select('.flipper_bottom_front span',true).first();
247         
248         this.targetNum = this.currentNumber;
249         this.currentNum = this.currentNumber;
250         this.nextNum = this.currentNumber;
251         
252         this.currentlyAnimating = false;
253     },
254     
255     flipTo : function (num)
256     {
257         if (this.currentNum === num)
258             return;
259         
260         this.targetNum = num;
261         if (this.currentlyAnimating) {
262             return;
263         }
264         
265         this.animNext();
266     },
267     
268     animNext : function ()
269     {
270         if (this.currentNum == this.targetNum)
271         {
272             this.currentlyAnimating = false;
273             return;
274         }
275         
276         var doRandomDelay = !this.currentlyAnimating;
277         this.currentlyAnimating = true;
278         this.nextNum = this.currentNum + 1;
279         if (this.nextNum > 9) {
280             this.nextNum = 0;
281         }
282         
283         var delay = Math.random()/5;
284         if (!doRandomDelay) {
285             delay = 0.01;
286         }
287         
288         // Animate top flipper
289         var digit = this;
290         digit.topNumBack.dom.innerHTML = digit.nextNum;
291         (function() {
292             digit.topFrontDiv.animate(
293                 {
294                     scaleY: {from :1, to : 0}
295                 },
296                 this.manager.speed, //duration
297                 function() {}, // oncomplate
298                 'easeIn', //easing,
299                 'motion' // desplay type.
300             );
301         }).defer(delay, this);
302         
303         (function() {
304             
305             digit.bottomNumFront.dom.innerHTML  = digit.nextNum;
306             
307             digit.bottomFrontDiv.animate(
308                 {
309                     scaleY: {from: 0, to : 1},
310                     
311                 },
312                 this.manager.speed * 0.5, //duration
313                 function() {
314                     digit.currentNum = digit.nextNum;
315                     digit.topNumFront.dom.innerHTML = digit.currentNum;
316                     digit.topFrontDiv.attr('style', '');
317                     digit.bottomNumBack.dom.innerHTML = digit.currentNum;
318                     
319                     digit.animNext();
320                     digit.manager.fireEvent('onFlip', digit.manager);
321                     
322                  }, // oncomplate
323                 'easeOut', //easing,
324                 'motion' // desplay type.
325             )
326             
327         }).defer(delay + this.manager.speed, this);
328                 
329                 
330  
331        //??? digit.bottomFrontDiv. digit.bottomNumFront.html(digit.nextNum);
332         
333        
334     }
335 });