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