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