ux/FlipCounter.js
[roojs1] / ux / FlipCounter.js
1
2
3 Roo.ux.FlipCounter = function(options)
4 {
5     Roo.bootstrap.FlipCounter.superclass.constructor.call(this, config);
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     
152     this.currentNumber = parseInt(this.currentNumber);
153     
154     
155 }
156
157 Roo.extend(Roo.ux.FlipCounter.Digit, Roo.bootstrap.Component, {
158
159     manager : null, // the flipcounter... 
160     currentNumber : 0,
161     targetNum : 0,
162     
163     topFrontDiv  : null,
164     bottomFrontDiv : null,
165     topNumBack : null,
166     topNumFront : null,
167     bottomNumBack : null,
168     bottomNumFront : null,
169     
170     
171     getAutoCreate : function(){
172         
173         return {
174                 tag: 'li',
175                 cn : [
176                     {
177                         cls: 'numberwrap',
178                         cn : [
179                             { cls : 'flipper_top flipper_top1' },
180                             {
181                                 cls : 'flipper_top flipper_top2 flipper_top_back',
182                                 cn : [
183                                     { tag: 'span', html: num },
184                                     { cls : 'rings' }
185                                 ]
186                         
187                             },
188                             {
189                                 cls : 'flipper_top flipper_top_front',
190                                 cn : [
191                                     { tag: 'span', html: num },
192                                     { cls : 'rings' }
193                                 ]
194                         
195                             },
196                             { cls : 'flipper_bottom flipper_bottom4' },
197                             { cls : 'flipper_bottom flipper_bottom3' },
198                             { cls : 'flipper_bottom flipper_bottom2' },
199                             {
200                                 cls : 'flipper_bottom flipper_bottom1 flipper_bottom_back',
201                                 cn : [
202                                     { tag: 'span', html: num },
203                                     { cls : 'rings' }
204                                 ]
205                             },
206                             {
207                                 cls : 'flipper_bottom flipper_bottom_front',
208                                 cn : [
209                                     { tag: 'span', html: num },
210                                     { cls : 'rings' }
211                                 ]
212                             },
213                         ]
214                     }
215                 ]
216         };
217     },
218     
219     
220     initEvents : function()
221     {
222         
223          
224         
225         this.topFrontDiv = this.el.select('.flipper_top_front',true);
226         this.bottomFrontDiv = this.el.select('.flipper_bottom_front',true);
227         this.topNumBack = this.el.select('.flipper_top_back span',true);
228         this.topNumFront = this.el.select('.flipper_top_front span',true);
229         this.bottomNumBack = this.el.select('.flipper_bottom_back span',true);
230         this.bottomNumFront = this.el.select('.flipper_bottom_front span',true);
231         
232         this.targetNum = currentNumber;
233         this.currentNum = currentNumber;
234         this.nextNum = currentNumber;
235         
236         this.currentlyAnimating = false;
237     },
238     
239     flipTo : function (num)
240     {
241         if (this.currentNum === num)
242             return;
243         
244         this.targetNum = num;
245         if (this.currentlyAnimating) {
246             return;
247         }
248         
249         this.animNext();
250     },
251     
252     animNext : function ()
253     {
254         if (this.currentNum == this.targetNum)
255         {
256             this.currentlyAnimating = false;
257             return;
258         }
259         
260         var doRandomDelay = !this.currentlyAnimating;
261         this.currentlyAnimating = true;
262         this.nextNum = this.currentNum + 1;
263         if (this.nextNum > 9) {
264             this.nextNum = 0;
265         }
266         
267         var delay = Math.random()/5;
268         if (!doRandomDelay) {
269             delay = 0.01;
270         }
271         
272         // Animate top flipper
273         var digit = this;
274         digit.topNumBack.html(digit.nextNum);
275         (function() {
276             digit.topFrontDiv.animate(
277                 {
278                     scaleY: {from :1, to : 0}
279                 },
280                 this.manager.speed, //duration
281                 function() {}, // oncomplate
282                 'easeIn', //easing,
283                 'motion' // desplay type.
284             );
285         }).defer(delay, this);
286         
287         (function() {
288             
289             digit.bottomNumFront.dom.innerHTML  = digit.nextNum;
290             
291             digit.bottomFrontDiv.animate(
292                 {
293                     scaleY: {from: 0, to : 1},
294                     
295                 },
296                 this.manager.options.speed * 0.5, //duration
297                 function() {
298                     digit.currentNum = digit.nextNum;
299                     digit.topNumFront.dom.innerHTML = digit.currentNum;
300                     digit.topFrontDiv.attr('style', '');
301                     digit.bottomNumBack.dom.innerHTML = digit.currentNum;
302                     
303                     digit.animNext();
304                     digit.manager.fireEvent('onFlip', digit.manager);
305                     
306                  }, // oncomplate
307                 'easeOut', //easing,
308                 'motion' // desplay type.
309             )
310             
311         }).defer(delay + this.manager.speed, this);
312                 
313                 
314  
315        //??? digit.bottomFrontDiv. digit.bottomNumFront.html(digit.nextNum);
316         
317        
318     }
319 });