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