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