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