ux/TimePicker.js
[roojs1] / ux / TimePicker.js
1
2
3  
4            
5 Roo.form.TimePicker = function(config){
6     
7     Roo.log("CTOR CALLED");
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         Roo.log("Error - required components not found  - Raphael and piechart"); 
18         return;
19     }
20     
21     Roo.form.TimePicker.superclass.constructor.call(this, config);
22 };
23
24 Roo.extend(Roo.form.TimePicker , Roo.form.DisplayField ,  {
25     
26     paper : false,
27     width: 280,
28     height: 250,
29     
30     startHour : '7:30',
31     endHour : '19:15',
32     
33     tdisplay  : false,
34     
35     onRender : function(ct, position){
36         
37         Roo.form.DisplayField.superclass.onRender.call(this, ct, position);
38         //if(this.inputValue !== undefined){
39         this.wrap = this.el.wrap();
40         
41         this.viewEl = this.wrap.createChild({ tag: 'div', cls: 'x-form-displayfield'});
42         
43         
44         //?? needed??
45         if (this.bodyStyle) {
46             this.viewEl.applyStyles(this.bodyStyle);
47         }
48         //this.viewEl.setStyle('padding', '2px');
49         
50         this.viewEl.setHeight(this.height);
51         this.viewEl.setWidth(this.width);
52         
53          
54         var paper = this.paper = Raphael(this.viewEl.dom);
55         
56         paper.setSize(this.width,this.height);
57         
58         this.renderPaper(this.startHour,this.endHour);
59     },
60     
61     
62     
63     renderPaper : function( startHour , endHour )
64     { 
65         // let's create a pie chart...
66         var open =  startHour.split(':').shift() * 1;
67         var close =  endHour.split(':').shift() * 1;
68         
69         var paper = this.paper;
70         this.paper.clear();
71         
72         var data = [];
73         var colors = [];
74         
75         // say we open for 7 - 19
76         
77         
78         Roo.log([open, close]);
79         data.push(open);
80         
81         // colours should be configurable...
82         
83         colors.push('#003'); // off hour..
84         //var clr = [ '#003' , '#FCC' , '#FEE', '#FCC'];
85         var clr = [ '#FEE', '#FCC' ];
86                    
87         var times = [ '' ];
88         
89         for (var i = open; i < close ;i++) {
90             data.push(1);
91             if (i < 10) {
92                 times.push( '0'  + i);
93             } else { 
94                 times.push( '' + i);
95             }
96             colors.push(clr[i % clr.length]);
97         }
98         data.push( 24-close );
99         colors.push('#003');
100         
101         //Roo.log(data.length);
102         //Roo.log(JSON.stringify(data));
103         //Roo.log(JSON.stringify(colors));
104         //Roo.log(colors.length);
105
106         var outerpie = this.outerpie = paper.piechart(
107             paper.width  / 2,
108             paper.height  / 2 ,
109             110,
110             data ,
111             { 
112                 cut: data.length   ,
113                 colors :  colors,
114                 no_sort : true,
115                 start_angle :  270
116             }
117         );
118         
119         
120         // fill in th emiddle...
121         
122         paper.circle(outerpie.cx,outerpie.cy, 90).attr({fill: "#fff"})
123         // fixme .. add the quater hour segments..
124         
125         var innerpie = this.innerpie = paper.piechart(
126             paper.width  / 2,
127             paper.height  / 2 ,
128             80,
129             [ 1 , 1 ,1 , 1 ],
130             { 
131                 cut: 4   ,
132                 colors :   [ '#FEE', '#FCC' ,'#FEE', '#FCC' ] ,
133                 no_sort : true,
134                 start_angle :  135
135             }
136         );
137
138         paper.circle(outerpie.cx,outerpie.cy, 60).attr({fill: "#fff"})
139  
140         
141       
142         var tdisplay  = this.tdisplay = paper.text(outerpie.cx,outerpie.cy, "Pick\nTime").attr({
143                 font: "32px  'Lucida Sans Unicode', 'Lucida Grande', sans-serif",
144                 weight: 'bold', fill: '#000' });
145         
146         
147         
148         
149         
150         for (var i = 0; i < (24/3); i++) {
151             var angle = ( (360/24) * i * -3 )  -90;
152             var p = outerpie.sector(outerpie.cx, outerpie.cy, 105, angle, angle, false);
153             var innerp = outerpie.sector(outerpie.cx, outerpie.cy, 70, angle, angle, false);
154
155             Roo.log([p[4],p[5], (i*3)+':00']);
156             
157             var hr = (i*3);
158             
159             var txt = (hr < 12 ? hr : (hr-12)) +  (hr < 12 ? 'am' : 'pm');
160             if (!hr) { txt  = ""; }
161             if (hr == 12) {
162                 txt= '12 noon'
163             }
164             
165             paper.text(p[4],p[5], txt);
166             // inner...
167             if (i%2 ==1) {
168                 continue;
169             }
170             paper.text(innerp[4],innerp[5], ((Math.floor(i/2) * 15) + 30) % 60);
171             
172             
173         }
174         var asector = false;
175
176         var _t = this;
177         outerpie.hover(
178              // in?
179             function() {
180                 Roo.log(this.j);
181                 if (!this.j || this.j == data.length -1) {
182                     return;
183                 }
184                 var ov = _t.getValue();
185                
186                 this.sector.stop();
187                 
188                  
189                 if (this.sector != asector) {
190                     this.sector.scale(1.1, 1.1, this.cx, this.cy);
191                 }
192             
193                 var min = ov ? _t.getValue().split(':').pop() : '00';
194                 var ntime = times[this.j] + ':' + min;
195                 tdisplay.attr( { text : _t.formatTime(ntime) });
196                  
197                 
198             },
199              // out..?
200             function () {
201                 if (!this.j || this.j == data.length) {
202                     return;
203                 }
204                 if (this.sector != asector) {
205                     this.sector.animate({ transform: 's1 1 ' + this.cx + ' ' + this.cy }, 500, "bounce");
206                 }
207                     
208                 tdisplay.attr( { text : _t.formatTime(_t.getValue()) } );
209              
210         });
211         outerpie.click(function() {
212             if (!this.j || this.j == data.length -1) {
213                 return;
214             }
215            
216             var ov = _t.getValue();
217             var min = ov ? _t.getValue().split(':').pop() : '00';
218             var ntime = times[this.j] + ':' + min;
219             _t.setValue(ntime);
220             
221             if (asector) {
222                 // reset 
223                 asector.transform( 's1 1');
224             }
225             
226             //this.sector.scale(1.1, 1.1, this.cx, this.cy);
227             asector = this.sector;
228  
229             
230         });
231         
232          var bsector = false;
233
234          innerpie.hover(
235              // in?
236             function() {
237                 Roo.log(this.j);
238                 /*
239                 if (!this.j || this.j == data.length -1) {
240                     return;
241                 }
242                 */
243                 this.sector.stop();
244                 if (this.sector != bsector) {
245                     this.sector.scale(1.1, 1.1, this.cx, this.cy);
246                 }
247                 var min = this.j*15;
248                  min = min ? min : '00';
249                 
250                 var ov = _t.getValue();
251                 var hr = ov ? _t.getValue().split(':').shift() : '12';
252                 var ntime = hr + ':' + min;
253                 tdisplay.attr( { text : _t.formatTime(ntime) });
254                  
255                 
256             },
257              // out..?
258             function () {
259                 /*
260                 if (!this.j || this.j == data.length) {
261                     return;
262                 }
263                 */
264                 if (this.sector != bsector) {
265                     this.sector.animate({ transform: 's1 1 ' + this.cx + ' ' + this.cy }, 500, "bounce");
266                 }
267  
268                 tdisplay.attr( { text : _t.formatTime(_t.getValue()) } );
269              
270         });
271         innerpie.click(function() {
272              
273             Roo.log(this.j);
274             var min = this.j*15;
275             min = min ? min : '00';
276                 
277              
278             var ov = _t.getValue();
279             var hr = ov ? _t.getValue().split(':').shift() : '12';
280             var ntime = hr + ':' + min;
281             _t.setValue(ntime);
282             //tdisplay.attr( { text : _t.formatTime(ntime) });
283             
284             //var ov = _t.getValue();
285             //var min = ov ? _t.getValue().split(':').pop() : '00';
286             //var ntime = times[this.j] + ':' + min;
287             //_t.setValue(ntime);
288             
289             if (bsector) {
290                 bsector.transform( 's1 1');
291             }
292             
293             //this.sector.scale(1.1, 1.1, this.cx, this.cy);
294             bsector = this.sector;
295  
296             
297         });
298         this.setValue(this.value);
299         
300         
301         
302         
303         
304         //pie.each(function() { Roo.log(this)});
305         //this.setValue(this.value);
306     },
307     setValue : function(v){
308         this.value = v;
309         if (!this.viewEl) {
310             return;
311         }
312         //this.viewEl.dom.innerHTML = html;
313         Roo.form.DisplayField.superclass.setValue.call(this, v);
314         if (this.tdisplay) {
315             this.tdisplay.attr( { text : this.formatTime(v) } );
316         }
317
318     },
319     formatTime : function(ntime)
320     {
321         if (!ntime) {
322             return "Pick\nTime";
323         }
324         if (ntime == '00:00') {
325             return "Midnight";
326         }
327         if (ntime == '12:00') {
328             return "12\nnoon";
329         }
330         Roo.log(ntime);
331         var d = Date.parseDate(ntime, "H:i");
332         return d.format('h:i') + "\n" + d.format('a')  ; 
333         
334         
335         
336     }
337     
338 });