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         
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             this.vIndex = Roo.bootstrap.MonthField.dates[this.language].months.indexOf(this.value);
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 dir;
256         
257         switch(e.keyCode){
258             case 27: // escape
259                 this.hide();
260                 e.preventDefault();
261                 break;
262             case 37: // left
263             case 39: // right
264                 dir = e.keyCode == 37 ? -1 : 1;
265                 
266                 this.vIndex = this.vIndex + dir;
267                 
268                 if(this.vIndex < 0){
269                     this.vIndex = 0;
270                 }
271                 
272                 if(this.vIndex > 11){
273                     this.vIndex = 11;
274                 }
275                 
276                 Roo.log(this.vIndex);
277                 
278                 this.setValue(Roo.bootstrap.MonthField.dates[this.language].months[this.vIndex]);
279                 
280                 break;
281             case 38: // up
282             case 40: // down
283                 
284                 dir = e.keyCode == 38 ? -1 : 1;
285                 
286                 this.vIndex = this.vIndex + dir * 4;
287                 
288                 if(this.vIndex < 0){
289                     this.vIndex = 0;
290                 }
291                 
292                 if(this.vIndex > 11){
293                     this.vIndex = 11;
294                 }
295                 Roo.log(this.vIndex);
296                 this.setValue(Roo.bootstrap.MonthField.dates[this.language].months[this.vIndex]);
297                 break;
298                 
299             case 13: // enter
300                 this.setValue(Roo.bootstrap.MonthField.dates[this.language].months[this.vIndex]);
301                 this.hide();
302                 e.preventDefault();
303                 break;
304             case 9: // tab
305                 this.setValue(Roo.bootstrap.MonthField.dates[this.language].months[this.vIndex]);
306                 this.hide();
307                 break;
308             case 16: // shift
309             case 17: // ctrl
310             case 18: // alt
311                 break;
312             default :
313                 this.hide();
314                 
315         }
316     },
317     
318     remove: function() 
319     {
320         this.picker().remove();
321     }
322    
323 });
324
325 Roo.apply(Roo.bootstrap.MonthField,  {
326     
327     content : {
328         tag: 'tbody',
329         cn: [
330         {
331             tag: 'tr',
332             cn: [
333             {
334                 tag: 'td',
335                 colspan: '7'
336             }
337             ]
338         }
339         ]
340     },
341     
342     dates:{
343         en: {
344             months: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
345             monthsShort: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
346         }
347     }
348 });
349
350 Roo.apply(Roo.bootstrap.MonthField,  {
351   
352     template : {
353         tag: 'div',
354         cls: 'datepicker dropdown-menu roo-dynamic',
355         cn: [
356             {
357                 tag: 'div',
358                 cls: 'datepicker-months',
359                 cn: [
360                 {
361                     tag: 'table',
362                     cls: 'table-condensed',
363                     cn:[
364                         Roo.bootstrap.DateField.content
365                     ]
366                 }
367                 ]
368             }
369         ]
370     }
371 });
372
373  
374
375  
376