Roo/bootstrap/MonthField.js
[roojs1] / Roo / bootstrap / MonthField.js
1 /*
2  * - LGPL
3  *
4  * MonthField
5  * 
6  */
7
8 /**
9  * @class Roo.bootstrap.MonthField
10  * @extends Roo.bootstrap.Input
11  * Bootstrap MonthField class
12  * 
13  * @cfg {String} language default en
14  * 
15  * @constructor
16  * Create a new MonthField
17  * @param {Object} config The config object
18  */
19
20 Roo.bootstrap.MonthField = function(config){
21     Roo.bootstrap.MonthField.superclass.constructor.call(this, config);
22     
23     this.addEvents({
24         /**
25          * @event show
26          * Fires when this field show.
27          * @param {Roo.bootstrap.MonthField} this
28          * @param {Mixed} date The date value
29          */
30         show : true,
31         /**
32          * @event show
33          * Fires when this field hide.
34          * @param {Roo.bootstrap.MonthField} this
35          * @param {Mixed} date The date value
36          */
37         hide : true,
38         /**
39          * @event select
40          * Fires when select a date.
41          * @param {Roo.bootstrap.MonthField} this
42          * @param {Mixed} date The date value
43          */
44         select : true
45     });
46 };
47
48 Roo.extend(Roo.bootstrap.MonthField, Roo.bootstrap.Input,  {
49     
50     format : "F",
51     
52     onRender: function(ct, position)
53     {
54         
55         Roo.bootstrap.MonthField.superclass.onRender.call(this, ct, position);
56         
57         this.language = this.language || 'en';
58         this.language = this.language in Roo.bootstrap.MonthField.dates ? this.language : this.language.split('-')[0];
59         this.language = this.language in Roo.bootstrap.MonthField.dates ? this.language : "en";
60         
61         this.isRTL = Roo.bootstrap.MonthField.dates[this.language].rtl || false;
62         this.format = this.format || 'F';
63         this.isInline = false;
64         this.isInput = true;
65         this.component = this.el.select('.add-on', true).first() || false;
66         this.component = (this.component && this.component.length === 0) ? false : this.component;
67         this.hasInput = this.component && this.inputEL().length;
68         
69         this.minViewMode = 1;
70         this.viewMode = 1;
71                 
72         this.pickerEl = Roo.get(document.body).createChild(Roo.bootstrap.MonthField.template);
73         
74         this.picker().setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
75         
76         this.picker().on('mousedown', this.onMousedown, this);
77         this.picker().on('click', this.onClick, this);
78         
79         this.picker().addClass('datepicker-dropdown');
80         
81         this.startViewMode = this.viewMode;
82
83         Roo.each(this.picker().select('tbody > tr > td', true).elements, function(v){
84             v.setStyle('width', '189px');
85         });
86         
87         this.fillMonths();
88         
89         this.update();
90         
91         if(this.isInline) {
92             this.show();
93         }
94     },
95     
96     setValue: function(v)
97     {   
98         Roo.bootstrap.MonthField.superclass.setValue.call(this, v);
99         
100         this.update();
101
102         this.fireEvent('select', this, this.date);
103         
104     },
105     
106     getValue: function()
107     {
108         return this.value;
109     },
110     
111     onClick: function(e) 
112     {
113         e.stopPropagation();
114         e.preventDefault();
115         
116         var target = e.getTarget();
117         
118         if(target.nodeName.toLowerCase() === 'i'){
119             target = Roo.get(target).dom.parentNode;
120         }
121         
122         var nodeName = target.nodeName;
123         var className = target.className;
124         var html = target.innerHTML;
125         
126         if(nodeName.toLowerCase() != 'span' || className.indexOf('disabled') > -1 || className.indexOf('month') == -1){
127             return;
128         }
129         var v = Roo.bootstrap.MonthField.dates[this.language].months[Roo.bootstrap.MonthField.dates[this.language].monthsShort.indexOf(html)];
130         
131         Roo.log(v);
132                 
133         this.setValue(v);
134         this.hide();
135                         
136     },
137     
138     picker : function()
139     {
140         return this.pickerEl;
141     },
142     
143     fillMonths: function()
144     {    
145         var i = 0
146         var months = this.picker().select('>.datepicker-months td', true).first();
147         
148         months.dom.innerHTML = '';
149         
150         while (i < 12) {
151             var month = {
152                 tag: 'span',
153                 cls: 'month',
154                 html: Roo.bootstrap.MonthField.dates[this.language].monthsShort[i++]
155             }
156             
157             months.createChild(month);
158         }
159         
160     },
161     
162     update: function()
163     {
164         this.fill();
165     },
166     
167     fill: function() 
168     {
169         this.fillMonths();
170     },
171     
172     place: function()
173     {
174         if(this.isInline) return;
175         
176         this.picker().removeClass(['bottom', 'top']);
177         
178         if((Roo.lib.Dom.getViewHeight() + Roo.get(document.body).getScroll().top) - (this.inputEl().getBottom() + this.picker().getHeight()) < 0){
179             /*
180              * place to the top of element!
181              *
182              */
183             
184             this.picker().addClass('top');
185             this.picker().setTop(this.inputEl().getTop() - this.picker().getHeight()).setLeft(this.inputEl().getLeft());
186             
187             return;
188         }
189         
190         this.picker().addClass('bottom');
191         
192         this.picker().setTop(this.inputEl().getBottom()).setLeft(this.inputEl().getLeft());
193     },
194     
195     onFocus : function()
196     {
197         Roo.bootstrap.MonthField.superclass.onFocus.call(this);
198         this.show();
199     },
200     
201     onBlur : function()
202     {
203         Roo.bootstrap.MonthField.superclass.onBlur.call(this);
204         
205         var d = this.inputEl().getValue();
206         
207         this.setValue(d);
208                 
209         this.hide();
210     },
211     
212     show : function()
213     {
214         this.picker().show();
215         this.picker().select('>.datepicker-months', true).first().show();
216         this.update();
217         this.place();
218         
219         this.fireEvent('show', this, this.date);
220     },
221     
222     hide : function()
223     {
224         if(this.isInline) return;
225         this.picker().hide();
226         this.fireEvent('hide', this, this.date);
227         
228     },
229     
230     onMousedown: function(e)
231     {
232         e.stopPropagation();
233         e.preventDefault();
234     },
235     
236     keyup: function(e)
237     {
238         Roo.bootstrap.MonthField.superclass.keyup.call(this);
239         this.update();
240     },
241
242     fireKey: function(e)
243     {
244         if (!this.picker().isVisible()){
245             if (e.keyCode == 27) // allow escape to hide and re-show picker
246                 this.show();
247             return;
248         }
249         
250         var dateChanged = false,
251         dir, day, month,
252         newDate, newViewDate;
253         
254 //        switch(e.keyCode){
255 //            case 27: // escape
256 //                this.hide();
257 //                e.preventDefault();
258 //                break;
259 //            case 37: // left
260 //            case 39: // right
261 //                if (!this.keyboardNavigation) break;
262 //                dir = e.keyCode == 37 ? -1 : 1;
263 //                
264 //                if (e.ctrlKey){
265 //                    newDate = this.moveYear(this.date, dir);
266 //                    newViewDate = this.moveYear(this.viewDate, dir);
267 //                } else if (e.shiftKey){
268 //                    newDate = this.moveMonth(this.date, dir);
269 //                    newViewDate = this.moveMonth(this.viewDate, dir);
270 //                } else {
271 //                    newDate = new Date(this.date);
272 //                    newDate.setUTCDate(this.date.getUTCDate() + dir);
273 //                    newViewDate = new Date(this.viewDate);
274 //                    newViewDate.setUTCDate(this.viewDate.getUTCDate() + dir);
275 //                }
276 //                if (this.dateWithinRange(newDate)){
277 //                    this.date = newDate;
278 //                    this.viewDate = newViewDate;
279 //                    this.setValue(this.formatDate(this.date));
280 ////                    this.update();
281 //                    e.preventDefault();
282 //                    dateChanged = true;
283 //                }
284 //                break;
285 //            case 38: // up
286 //            case 40: // down
287 //                if (!this.keyboardNavigation) break;
288 //                dir = e.keyCode == 38 ? -1 : 1;
289 //                if (e.ctrlKey){
290 //                    newDate = this.moveYear(this.date, dir);
291 //                    newViewDate = this.moveYear(this.viewDate, dir);
292 //                } else if (e.shiftKey){
293 //                    newDate = this.moveMonth(this.date, dir);
294 //                    newViewDate = this.moveMonth(this.viewDate, dir);
295 //                } else {
296 //                    newDate = new Date(this.date);
297 //                    newDate.setUTCDate(this.date.getUTCDate() + dir * 7);
298 //                    newViewDate = new Date(this.viewDate);
299 //                    newViewDate.setUTCDate(this.viewDate.getUTCDate() + dir * 7);
300 //                }
301 //                if (this.dateWithinRange(newDate)){
302 //                    this.date = newDate;
303 //                    this.viewDate = newViewDate;
304 //                    this.setValue(this.formatDate(this.date));
305 ////                    this.update();
306 //                    e.preventDefault();
307 //                    dateChanged = true;
308 //                }
309 //                break;
310 //            case 13: // enter
311 //                this.setValue(this.formatDate(this.date));
312 //                this.hide();
313 //                e.preventDefault();
314 //                break;
315 //            case 9: // tab
316 //                this.setValue(this.formatDate(this.date));
317 //                this.hide();
318 //                break;
319 //            case 16: // shift
320 //            case 17: // ctrl
321 //            case 18: // alt
322 //                break;
323 //            default :
324 //                this.hide();
325 //                
326 //        }
327     },
328     
329     remove: function() 
330     {
331         this.picker().remove();
332     }
333    
334 });
335
336 Roo.apply(Roo.bootstrap.MonthField,  {
337     
338     content : {
339         tag: 'tbody',
340         cn: [
341         {
342             tag: 'tr',
343             cn: [
344             {
345                 tag: 'td',
346                 colspan: '7'
347             }
348             ]
349         }
350         ]
351     },
352     
353     dates:{
354         en: {
355             months: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
356             monthsShort: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
357         }
358     }
359 });
360
361 Roo.apply(Roo.bootstrap.MonthField,  {
362   
363     template : {
364         tag: 'div',
365         cls: 'datepicker dropdown-menu roo-dynamic',
366         cn: [
367             {
368                 tag: 'div',
369                 cls: 'datepicker-months',
370                 cn: [
371                 {
372                     tag: 'table',
373                     cls: 'table-condensed',
374                     cn:[
375                         Roo.bootstrap.DateField.content
376                     ]
377                 }
378                 ]
379             }
380         ]
381     }
382 });
383
384  
385
386  
387