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