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     closeHour : '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 = 7;
62         var close = 19;
63         
64         data.push(open);
65         
66         // colours should be configurable...
67         
68         colors.push('#003');
69         var clr = [ '#003' , '#FCC' , '#Fee', '#FCC'];
70                    
71         var times = [ '' ];
72         
73         for (var i = (open*4); i < (close*4);i++) {
74             data.push(1);
75             
76             if (!i) {
77                 times.push( 'midnight' )
78             } else if (i == 48) {
79                 times.push( 'noon' );
80             } else {
81                 var min = ( i % 4) * 15;
82                 min = !min ? ':00' : (':' + min);
83                 var hr = Math.floor(i/4);
84                 var tail = hr < 12 ? 'am' : 'pm';
85                 hr = hr % 12;
86                 times.push(hr  + min + tail);
87             }
88              
89             colors.push(clr[i % clr.length]);
90         }
91         data.push((24-close) * 4);
92         colors.push('#003');
93         
94         //Roo.log(data.length);
95         //Roo.log(JSON.stringify(data));
96         //Roo.log(JSON.stringify(colors));
97         //Roo.log(colors.length);
98
99         var pie = paper.piechart(
100             paper.height  / 2,
101             paper.height  / 2 ,
102             (paper.height / 2) - 70,
103             data ,
104             { 
105                 cut: data.length   ,
106                 colors :  colors,
107                 no_sort : true,
108                 start_angle :  270
109             }
110         );
111         // fill in th emiddle...
112         
113         
114         
115         paper.circle(pie.cx,pie.cy, (paper.height / 2) - 100).attr({fill: "#fff"})
116         
117         var atime = "Pick\nTime";
118         var asector = false;
119         
120         var tdisplay  = paper.text(pie.cx,pie.cy,atime).attr({ font: "32px  'Lucida Sans Unicode', 'Lucida Grande', sans-serif", weight: 'bold', fill: '#000' });
121         pie.hover(
122              // in?
123             function() {
124                 Roo.log(this.j);
125                 if (!this.j || this.j == data.length -1) {
126                     return;
127                 }
128                  this.sector.stop();
129                  if (this.sector != asector) {
130                     this.sector.scale(1.1, 1.1, this.cx, this.cy);
131                  }
132                  tdisplay.attr( { text : times[this.j]} );
133                  if (this.label) {
134                      this.label[0].stop();
135                      this.label[0].attr({ r: 7.5 });
136                      this.label[1].attr({ "font-weight": 800 });
137                  }
138              },
139              // out..?
140              function () {
141                 if (!this.j || this.j == data.length) {
142                     return;
143                 }
144                 if (this.sector != asector) {
145                     this.sector.animate({ transform: 's1 1 ' + this.cx + ' ' + this.cy }, 500, "bounce");
146                 }
147  
148                 if (this.label) {
149                      this.label[0].animate({ r: 5 }, 500, "bounce");
150                      this.label[1].attr({ "font-weight": 400 });
151                 }
152                 tdisplay.attr( { text : atime } );
153              
154         });
155         pie.click(function() {
156             if (!this.j || this.j == data.length -1) {
157                 return;
158             }
159             atime = times[this.j];
160             tdisplay.attr( { text : atime } );
161             if (asector) {
162                 asector.animate({ transform: 's1 1 ' + this.cx + ' ' + this.cy }, 500, "bounce");
163             }
164             
165             this.sector.scale(1.1, 1.1, this.cx, this.cy);
166             asector = this.sector;
167
168             
169             
170         })
171         
172         
173         
174         for (var i = 0; i < (24/3); i++) {
175             var angle = ( (360/24) * i * -3 )  -90;
176             var p = pie.sector(pie.cx, pie.cy, 120, angle, angle, false);
177             Roo.log([p[4],p[5], (i*3)+':00']);
178             
179             var hr = (i*3);
180             
181             var txt = (hr < 12 ? hr : (hr-12)) +  (hr < 12 ? 'am' : 'pm');
182             if (!hr) { txt  = ""; }
183             if (hr == 12) { txt= '12 noon'}
184             paper.text(p[4],p[5], txt);
185         }
186             
187         //pie.each(function() { Roo.log(this)});
188         this.setValue(this.value);
189         
190     });
191     
192     
193 });