ux/TimePicker.js
[roojs1] / ux / TimePicker.js
1
2
3  
4            
5 Roo.ux.TimePicker = function(config){
6     
7     
8     // these are required... error out if not avail..
9     var deps = true;
10     if (typeof(Raphael) == 'undefined') {
11         deps = false;
12     }
13     if (deps && typeof(Raphael.prototype) == 'undefined') {
14         deps = false;
15     }
16     if (!deps) {
17         alert("Error - required components not found  - Raphael and piechart");
18         return;
19     }
20     
21     Roo.ux.TimePicker.superclass.constructor.call(this, config);
22 };
23
24 Roo.extend(Roo.ux.TimePicker , Roo.form.DisplayField ,  {
25     
26     paper : false,
27     width: 350,
28     height: 350,
29     
30     onRender : function(ct, position){
31         
32         Roo.form.DisplayField.superclass.onRender.call(this, ct, position);
33         //if(this.inputValue !== undefined){
34         this.wrap = this.el.wrap();
35         
36         this.viewEl = this.wrap.createChild({ tag: 'div', cls: 'x-form-displayfield'});
37         
38         
39         //?? needed??
40         if (this.bodyStyle) {
41             this.viewEl.applyStyles(this.bodyStyle);
42         }
43         //this.viewEl.setStyle('padding', '2px');
44         
45         this.viewEl.setHeight(this.height);
46         this.viewEl.setWidth(this.width);
47     
48     // let's create a pie chart...
49         
50         var paper = this.paper = Raphael(this.viewEl.dom);
51         
52         paper.setSize(this.width,this.height);
53         
54         var data = [];
55         var colors = [];
56         
57         // say we open for 7 - 19
58         var open = 7;
59         var close = 19;
60         
61         data.push(open * 4);
62         
63         // colours should be configurable...
64         
65         colors.push('#003');
66         var clr = [ '#003' , '#FCC' , '#Fee', '#FCC'];
67                    
68         var times = [ '' ];
69         
70         for (var i = (open*4); i < (close*4);i++) {
71             data.push(1);
72             
73             if (!i) {
74                 times.push( 'midnight' )
75             } else if (i == 48) {
76                 times.push( 'noon' );
77             } else {
78                 var min = ( i % 4) * 15;
79                 min = !min ? ':00' : (':' + min);
80                 var hr = Math.floor(i/4);
81                 var tail = hr < 12 ? 'am' : 'pm';
82                 hr = hr % 12;
83                 times.push(hr  + min + tail);
84             }
85             
86             
87             
88             colors.push(clr[i % clr.length]);
89         }
90         data.push((24-close) * 4);
91         colors.push('#003');
92         Roo.log(data.length);
93         Roo.log(JSON.stringify(data));
94         Roo.log(JSON.stringify(colors));
95         Roo.log(colors.length);
96
97         var pie = paper.piechart(
98             paper.height  / 2,
99             paper.height  / 2 ,
100             (paper.height / 2) - 70,
101             data ,
102             { 
103                 cut: data.length   ,
104                 colors :  colors,
105                 no_sort : true,
106                 start_angle :  270
107             }
108         );
109         paper.circle(pie.cx,pie.cy, (paper.height / 2) - 100).attr({fill: "#fff"})
110         
111         var atime = "Pick\nTime";
112         var asector = false;
113         
114         var tdisplay  = paper.text(pie.cx,pie.cy,atime).attr({ font: "32px  'Lucida Sans Unicode', 'Lucida Grande', sans-serif", weight: 'bold', fill: '#000' });
115         pie.hover(
116              // in?
117             function() {
118                 Roo.log(this.j);
119                 if (!this.j || this.j == data.length -1) {
120                     return;
121                 }
122                  this.sector.stop();
123                  if (this.sector != asector) {
124                     this.sector.scale(1.1, 1.1, this.cx, this.cy);
125                  }
126                  tdisplay.attr( { text : times[this.j]} );
127                  if (this.label) {
128                      this.label[0].stop();
129                      this.label[0].attr({ r: 7.5 });
130                      this.label[1].attr({ "font-weight": 800 });
131                  }
132              },
133              // out..?
134              function () {
135                 if (!this.j || this.j == data.length) {
136                     return;
137                 }
138                 if (this.sector != asector) {
139                     this.sector.animate({ transform: 's1 1 ' + this.cx + ' ' + this.cy }, 500, "bounce");
140                 }
141  
142                 if (this.label) {
143                      this.label[0].animate({ r: 5 }, 500, "bounce");
144                      this.label[1].attr({ "font-weight": 400 });
145                 }
146                 tdisplay.attr( { text : atime } );
147              
148         });
149         pie.click(function() {
150             if (!this.j || this.j == data.length -1) {
151                 return;
152             }
153             atime = times[this.j];
154             tdisplay.attr( { text : atime } );
155             if (asector) {
156                 asector.animate({ transform: 's1 1 ' + this.cx + ' ' + this.cy }, 500, "bounce");
157             }
158             
159             this.sector.scale(1.1, 1.1, this.cx, this.cy);
160             asector = this.sector;
161
162             
163             
164         })
165         
166         
167         
168         for (var i = 0; i < (24/3); i++) {
169             var angle = ( (360/24) * i * -3 )  -90;
170             var p = pie.sector(pie.cx, pie.cy, 120, angle, angle, false);
171             Roo.log([p[4],p[5], (i*3)+':00']);
172             
173             var hr = (i*3);
174             
175             var txt = (hr < 12 ? hr : (hr-12)) +  (hr < 12 ? 'am' : 'pm');
176             if (!hr) { txt  = ""; }
177             if (hr == 12) { txt= '12 noon'}
178             paper.text(p[4],p[5], txt);
179         }
180             
181         //pie.each(function() { Roo.log(this)});
182         this.setValue(this.value);
183         
184     });
185     
186     
187 });