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         this.update();
89         
90         if(this.isInline) {
91             this.show();
92         }
93     },
94     
95     setValue: function(v)
96     {   
97         Roo.bootstrap.MonthField.superclass.setValue.call(this, v);
98         
99         this.update();
100
101         this.fireEvent('select', this, this.date);
102         
103     },
104     
105     getValue: function()
106     {
107         return this.value;
108     },
109     
110     onClick: function(e) 
111     {
112         e.stopPropagation();
113         e.preventDefault();
114         
115         var target = e.getTarget();
116         
117         if(target.nodeName.toLowerCase() === 'i'){
118             target = Roo.get(target).dom.parentNode;
119         }
120         
121         var nodeName = target.nodeName;
122         var className = target.className;
123         var html = target.innerHTML;
124         
125         if(nodeName.toLowerCase() != 'span' || className.indexOf('disabled') > -1 || className.indexOf('month') == -1){
126             return;
127         }
128         
129         this.vIndex = Roo.bootstrap.MonthField.dates[this.language].monthsShort.indexOf(html);
130         
131         this.setValue(Roo.bootstrap.MonthField.dates[this.language].months[this.vIndex]);
132         this.hide();
133                         
134     },
135     
136     picker : function()
137     {
138         return this.pickerEl;
139     },
140     
141     fillMonths: function()
142     {    
143         var i = 0
144         var months = this.picker().select('>.datepicker-months td', true).first();
145         
146         months.dom.innerHTML = '';
147         
148         while (i < 12) {
149             var month = {
150                 tag: 'span',
151                 cls: 'month',
152                 html: Roo.bootstrap.MonthField.dates[this.language].monthsShort[i++]
153             }
154             
155             months.createChild(month);
156         }
157         
158     },
159     
160     update: function()
161     {
162         Roo.each(this.pickerEl.select('> .datepicker-months tbody > tr > td > span', true).elements, function(e){
163             Roo.log(e);
164         })
165     },
166     
167     place: function()
168     {
169         if(this.isInline) return;
170         
171         this.picker().removeClass(['bottom', 'top']);
172         
173         if((Roo.lib.Dom.getViewHeight() + Roo.get(document.body).getScroll().top) - (this.inputEl().getBottom() + this.picker().getHeight()) < 0){
174             /*
175              * place to the top of element!
176              *
177              */
178             
179             this.picker().addClass('top');
180             this.picker().setTop(this.inputEl().getTop() - this.picker().getHeight()).setLeft(this.inputEl().getLeft());
181             
182             return;
183         }
184         
185         this.picker().addClass('bottom');
186         
187         this.picker().setTop(this.inputEl().getBottom()).setLeft(this.inputEl().getLeft());
188     },
189     
190     onFocus : function()
191     {
192         Roo.bootstrap.MonthField.superclass.onFocus.call(this);
193         this.show();
194     },
195     
196     onBlur : function()
197     {
198         Roo.bootstrap.MonthField.superclass.onBlur.call(this);
199         
200         var d = this.inputEl().getValue();
201         
202         this.setValue(d);
203                 
204         this.hide();
205     },
206     
207     show : function()
208     {
209         this.picker().show();
210         this.picker().select('>.datepicker-months', true).first().show();
211         this.update();
212         this.place();
213         
214         this.fireEvent('show', this, this.date);
215     },
216     
217     hide : function()
218     {
219         if(this.isInline) return;
220         this.picker().hide();
221         this.fireEvent('hide', this, this.date);
222         
223     },
224     
225     onMousedown: function(e)
226     {
227         e.stopPropagation();
228         e.preventDefault();
229     },
230     
231     keyup: function(e)
232     {
233         Roo.bootstrap.MonthField.superclass.keyup.call(this);
234         this.update();
235     },
236
237     fireKey: function(e)
238     {
239         if (!this.picker().isVisible()){
240             if (e.keyCode == 27) // allow escape to hide and re-show picker
241                 this.show();
242             return;
243         }
244         
245         var dateChanged = false,
246         dir, day, month,
247         newDate, newViewDate;
248         
249 //        switch(e.keyCode){
250 //            case 27: // escape
251 //                this.hide();
252 //                e.preventDefault();
253 //                break;
254 //            case 37: // left
255 //            case 39: // right
256 //                if (!this.keyboardNavigation) break;
257 //                dir = e.keyCode == 37 ? -1 : 1;
258 //                
259 //                if (e.ctrlKey){
260 //                    newDate = this.moveYear(this.date, dir);
261 //                    newViewDate = this.moveYear(this.viewDate, dir);
262 //                } else if (e.shiftKey){
263 //                    newDate = this.moveMonth(this.date, dir);
264 //                    newViewDate = this.moveMonth(this.viewDate, dir);
265 //                } else {
266 //                    newDate = new Date(this.date);
267 //                    newDate.setUTCDate(this.date.getUTCDate() + dir);
268 //                    newViewDate = new Date(this.viewDate);
269 //                    newViewDate.setUTCDate(this.viewDate.getUTCDate() + dir);
270 //                }
271 //                if (this.dateWithinRange(newDate)){
272 //                    this.date = newDate;
273 //                    this.viewDate = newViewDate;
274 //                    this.setValue(this.formatDate(this.date));
275 ////                    this.update();
276 //                    e.preventDefault();
277 //                    dateChanged = true;
278 //                }
279 //                break;
280 //            case 38: // up
281 //            case 40: // down
282 //                if (!this.keyboardNavigation) break;
283 //                dir = e.keyCode == 38 ? -1 : 1;
284 //                if (e.ctrlKey){
285 //                    newDate = this.moveYear(this.date, dir);
286 //                    newViewDate = this.moveYear(this.viewDate, dir);
287 //                } else if (e.shiftKey){
288 //                    newDate = this.moveMonth(this.date, dir);
289 //                    newViewDate = this.moveMonth(this.viewDate, dir);
290 //                } else {
291 //                    newDate = new Date(this.date);
292 //                    newDate.setUTCDate(this.date.getUTCDate() + dir * 7);
293 //                    newViewDate = new Date(this.viewDate);
294 //                    newViewDate.setUTCDate(this.viewDate.getUTCDate() + dir * 7);
295 //                }
296 //                if (this.dateWithinRange(newDate)){
297 //                    this.date = newDate;
298 //                    this.viewDate = newViewDate;
299 //                    this.setValue(this.formatDate(this.date));
300 ////                    this.update();
301 //                    e.preventDefault();
302 //                    dateChanged = true;
303 //                }
304 //                break;
305 //            case 13: // enter
306 //                this.setValue(this.formatDate(this.date));
307 //                this.hide();
308 //                e.preventDefault();
309 //                break;
310 //            case 9: // tab
311 //                this.setValue(this.formatDate(this.date));
312 //                this.hide();
313 //                break;
314 //            case 16: // shift
315 //            case 17: // ctrl
316 //            case 18: // alt
317 //                break;
318 //            default :
319 //                this.hide();
320 //                
321 //        }
322     },
323     
324     remove: function() 
325     {
326         this.picker().remove();
327     }
328    
329 });
330
331 Roo.apply(Roo.bootstrap.MonthField,  {
332     
333     content : {
334         tag: 'tbody',
335         cn: [
336         {
337             tag: 'tr',
338             cn: [
339             {
340                 tag: 'td',
341                 colspan: '7'
342             }
343             ]
344         }
345         ]
346     },
347     
348     dates:{
349         en: {
350             months: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
351             monthsShort: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
352         }
353     }
354 });
355
356 Roo.apply(Roo.bootstrap.MonthField,  {
357   
358     template : {
359         tag: 'div',
360         cls: 'datepicker dropdown-menu roo-dynamic',
361         cn: [
362             {
363                 tag: 'div',
364                 cls: 'datepicker-months',
365                 cn: [
366                 {
367                     tag: 'table',
368                     cls: 'table-condensed',
369                     cn:[
370                         Roo.bootstrap.DateField.content
371                     ]
372                 }
373                 ]
374             }
375         ]
376     }
377 });
378
379  
380
381  
382