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