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