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