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