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