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