Roo/bootstrap/DateField.js
[roojs1] / Roo / bootstrap / DateField.js
1 /*
2  * - LGPL
3  *
4  * DateField
5  * 
6  */
7
8 /**
9  * @class Roo.bootstrap.DateField
10  * @extends Roo.bootstrap.Input
11  * Bootstrap DateField class
12  * @cfg {Number} weekStart default 0
13  * @cfg {String} viewMode default empty, (months|years)
14  * @cfg {String} minViewMode default empty, (months|years)
15  * @cfg {Number} startDate default -Infinity
16  * @cfg {Number} endDate default Infinity
17  * @cfg {Boolean} todayHighlight default false
18  * @cfg {Boolean} todayBtn default false
19  * @cfg {Boolean} calendarWeeks default false
20  * @cfg {Object} daysOfWeekDisabled default empty
21  * @cfg {Boolean} singleMode default false (true | false)
22  * 
23  * @cfg {Boolean} keyboardNavigation default true
24  * @cfg {String} language default en
25  * 
26  * @constructor
27  * Create a new DateField
28  * @param {Object} config The config object
29  */
30
31 Roo.bootstrap.DateField = function(config){
32     Roo.bootstrap.DateField.superclass.constructor.call(this, config);
33      this.addEvents({
34             /**
35              * @event show
36              * Fires when this field show.
37              * @param {Roo.bootstrap.DateField} this
38              * @param {Mixed} date The date value
39              */
40             show : true,
41             /**
42              * @event show
43              * Fires when this field hide.
44              * @param {Roo.bootstrap.DateField} this
45              * @param {Mixed} date The date value
46              */
47             hide : true,
48             /**
49              * @event select
50              * Fires when select a date.
51              * @param {Roo.bootstrap.DateField} this
52              * @param {Mixed} date The date value
53              */
54             select : true
55         });
56 };
57
58 Roo.extend(Roo.bootstrap.DateField, Roo.bootstrap.Input,  {
59     
60     /**
61      * @cfg {String} format
62      * The default date format string which can be overriden for localization support.  The format must be
63      * valid according to {@link Date#parseDate} (defaults to 'm/d/y').
64      */
65     format : "m/d/y",
66     /**
67      * @cfg {String} altFormats
68      * Multiple date formats separated by "|" to try when parsing a user input value and it doesn't match the defined
69      * format (defaults to 'm/d/Y|m-d-y|m-d-Y|m/d|m-d|d').
70      */
71     altFormats : "m/d/Y|m-d-y|m-d-Y|m/d|m-d|md|mdy|mdY|d",
72     
73     weekStart : 0,
74     
75     viewMode : '',
76     
77     minViewMode : '',
78     
79     todayHighlight : false,
80     
81     todayBtn: false,
82     
83     language: 'en',
84     
85     keyboardNavigation: true,
86     
87     calendarWeeks: false,
88     
89     startDate: -Infinity,
90     
91     endDate: Infinity,
92     
93     daysOfWeekDisabled: [],
94     
95     _events: [],
96     
97     singleMode : false,
98     
99     UTCDate: function()
100     {
101         return new Date(Date.UTC.apply(Date, arguments));
102     },
103     
104     UTCToday: function()
105     {
106         var today = new Date();
107         return this.UTCDate(today.getUTCFullYear(), today.getUTCMonth(), today.getUTCDate());
108     },
109     
110     getDate: function() {
111             var d = this.getUTCDate();
112             return new Date(d.getTime() + (d.getTimezoneOffset()*60000));
113     },
114     
115     getUTCDate: function() {
116             return this.date;
117     },
118     
119     setDate: function(d) {
120             this.setUTCDate(new Date(d.getTime() - (d.getTimezoneOffset()*60000)));
121     },
122     
123     setUTCDate: function(d) {
124             this.date = d;
125             this.setValue(this.formatDate(this.date));
126     },
127         
128     onRender: function(ct, position)
129     {
130         
131         Roo.bootstrap.DateField.superclass.onRender.call(this, ct, position);
132         
133         this.language = this.language || 'en';
134         this.language = this.language in Roo.bootstrap.DateField.dates ? this.language : this.language.split('-')[0];
135         this.language = this.language in Roo.bootstrap.DateField.dates ? this.language : "en";
136         
137         this.isRTL = Roo.bootstrap.DateField.dates[this.language].rtl || false;
138         this.format = this.format || 'm/d/y';
139         this.isInline = false;
140         this.isInput = true;
141         this.component = this.el.select('.add-on', true).first() || false;
142         this.component = (this.component && this.component.length === 0) ? false : this.component;
143         this.hasInput = this.component && this.inputEL().length;
144         
145         if (typeof(this.minViewMode === 'string')) {
146             switch (this.minViewMode) {
147                 case 'months':
148                     this.minViewMode = 1;
149                     break;
150                 case 'years':
151                     this.minViewMode = 2;
152                     break;
153                 default:
154                     this.minViewMode = 0;
155                     break;
156             }
157         }
158         
159         if (typeof(this.viewMode === 'string')) {
160             switch (this.viewMode) {
161                 case 'months':
162                     this.viewMode = 1;
163                     break;
164                 case 'years':
165                     this.viewMode = 2;
166                     break;
167                 default:
168                     this.viewMode = 0;
169                     break;
170             }
171         }
172                 
173         this.pickerEl = Roo.get(document.body).createChild(Roo.bootstrap.DateField.template);
174         
175 //        this.el.select('>.input-group', true).first().createChild(Roo.bootstrap.DateField.template);
176         
177         this.picker().setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
178         
179         this.picker().on('mousedown', this.onMousedown, this);
180         this.picker().on('click', this.onClick, this);
181         
182         this.picker().addClass('datepicker-dropdown');
183         
184         this.startViewMode = this.viewMode;
185         
186         if(this.singleMode){
187             Roo.each(this.picker().select('thead > tr > th', true).elements, function(v){
188                 v.setVisibilityMode(Roo.Element.DISPLAY)
189                 v.hide();
190             })
191             Roo.log(this.picker().select('tbody > tr > td', true).first());
192             this.picker().select('tbody > tr > td', true).first().setStyle('width', '189px');
193         }
194         
195         Roo.each(this.picker().select('tfoot th.today', true).elements, function(v){
196             if(!this.calendarWeeks){
197                 v.remove();
198                 return;
199             };
200             
201             v.dom.innerHTML = Roo.bootstrap.DateField.dates[this.language].today
202             v.attr('colspan', function(i, val){
203                 return parseInt(val) + 1;
204             });
205         })
206                         
207         
208         this.weekEnd = this.weekStart === 0 ? 6 : this.weekStart - 1;
209         
210         this.setStartDate(this.startDate);
211         this.setEndDate(this.endDate);
212         
213         this.setDaysOfWeekDisabled(this.daysOfWeekDisabled);
214         
215         this.fillDow();
216         this.fillMonths();
217         this.update();
218         this.showMode();
219         
220         if(this.isInline) {
221             this.show();
222         }
223     },
224     
225     picker : function()
226     {
227         return this.pickerEl;
228 //        return this.el.select('.datepicker', true).first();
229     },
230     
231     fillDow: function()
232     {
233         var dowCnt = this.weekStart;
234         
235         var dow = {
236             tag: 'tr',
237             cn: [
238                 
239             ]
240         };
241         
242         if(this.calendarWeeks){
243             dow.cn.push({
244                 tag: 'th',
245                 cls: 'cw',
246                 html: ' '
247             })
248         }
249         
250         while (dowCnt < this.weekStart + 7) {
251             dow.cn.push({
252                 tag: 'th',
253                 cls: 'dow',
254                 html: Roo.bootstrap.DateField.dates[this.language].daysMin[(dowCnt++)%7]
255             });
256         }
257         
258         this.picker().select('>.datepicker-days thead', true).first().createChild(dow);
259     },
260     
261     fillMonths: function()
262     {    
263         var i = 0
264         var months = this.picker().select('>.datepicker-months td', true).first();
265         
266         months.dom.innerHTML = '';
267         
268         while (i < 12) {
269             var month = {
270                 tag: 'span',
271                 cls: 'month',
272                 html: Roo.bootstrap.DateField.dates[this.language].monthsShort[i++]
273             }
274             
275             months.createChild(month);
276         }
277         
278     },
279     
280     update: function()
281     {
282         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;
283         
284         if (this.date < this.startDate) {
285             this.viewDate = new Date(this.startDate);
286         } else if (this.date > this.endDate) {
287             this.viewDate = new Date(this.endDate);
288         } else {
289             this.viewDate = new Date(this.date);
290         }
291         
292         this.fill();
293     },
294     
295     fill: function() 
296     {
297         var d = new Date(this.viewDate),
298                 year = d.getUTCFullYear(),
299                 month = d.getUTCMonth(),
300                 startYear = this.startDate !== -Infinity ? this.startDate.getUTCFullYear() : -Infinity,
301                 startMonth = this.startDate !== -Infinity ? this.startDate.getUTCMonth() : -Infinity,
302                 endYear = this.endDate !== Infinity ? this.endDate.getUTCFullYear() : Infinity,
303                 endMonth = this.endDate !== Infinity ? this.endDate.getUTCMonth() : Infinity,
304                 currentDate = this.date && this.date.valueOf(),
305                 today = this.UTCToday();
306         
307         this.picker().select('>.datepicker-days thead th.switch', true).first().dom.innerHTML = Roo.bootstrap.DateField.dates[this.language].months[month]+' '+year;
308         
309 //        this.picker().select('>tfoot th.today', true).first().dom.innerHTML = Roo.bootstrap.DateField.dates[this.language].today;
310         
311 //        this.picker.select('>tfoot th.today').
312 //                                              .text(dates[this.language].today)
313 //                                              .toggle(this.todayBtn !== false);
314     
315         this.updateNavArrows();
316         this.fillMonths();
317                                                 
318         var prevMonth = this.UTCDate(year, month-1, 28,0,0,0,0),
319         
320         day = prevMonth.getDaysInMonth(prevMonth.getUTCFullYear(), prevMonth.getUTCMonth());
321          
322         prevMonth.setUTCDate(day);
323         
324         prevMonth.setUTCDate(day - (prevMonth.getUTCDay() - this.weekStart + 7)%7);
325         
326         var nextMonth = new Date(prevMonth);
327         
328         nextMonth.setUTCDate(nextMonth.getUTCDate() + 42);
329         
330         nextMonth = nextMonth.valueOf();
331         
332         var fillMonths = false;
333         
334         this.picker().select('>.datepicker-days tbody',true).first().dom.innerHTML = '';
335         
336         while(prevMonth.valueOf() < nextMonth) {
337             var clsName = '';
338             
339             if (prevMonth.getUTCDay() === this.weekStart) {
340                 if(fillMonths){
341                     this.picker().select('>.datepicker-days tbody',true).first().createChild(fillMonths);
342                 }
343                     
344                 fillMonths = {
345                     tag: 'tr',
346                     cn: []
347                 };
348                 
349                 if(this.calendarWeeks){
350                     // ISO 8601: First week contains first thursday.
351                     // ISO also states week starts on Monday, but we can be more abstract here.
352                     var
353                     // Start of current week: based on weekstart/current date
354                     ws = new Date(+prevMonth + (this.weekStart - prevMonth.getUTCDay() - 7) % 7 * 864e5),
355                     // Thursday of this week
356                     th = new Date(+ws + (7 + 4 - ws.getUTCDay()) % 7 * 864e5),
357                     // First Thursday of year, year from thursday
358                     yth = new Date(+(yth = this.UTCDate(th.getUTCFullYear(), 0, 1)) + (7 + 4 - yth.getUTCDay())%7*864e5),
359                     // Calendar week: ms between thursdays, div ms per day, div 7 days
360                     calWeek =  (th - yth) / 864e5 / 7 + 1;
361                     
362                     fillMonths.cn.push({
363                         tag: 'td',
364                         cls: 'cw',
365                         html: calWeek
366                     });
367                 }
368             }
369             
370             if (prevMonth.getUTCFullYear() < year || (prevMonth.getUTCFullYear() == year && prevMonth.getUTCMonth() < month)) {
371                 clsName += ' old';
372             } else if (prevMonth.getUTCFullYear() > year || (prevMonth.getUTCFullYear() == year && prevMonth.getUTCMonth() > month)) {
373                 clsName += ' new';
374             }
375             if (this.todayHighlight &&
376                 prevMonth.getUTCFullYear() == today.getFullYear() &&
377                 prevMonth.getUTCMonth() == today.getMonth() &&
378                 prevMonth.getUTCDate() == today.getDate()) {
379                 clsName += ' today';
380             }
381             
382             if (currentDate && prevMonth.valueOf() === currentDate) {
383                 clsName += ' active';
384             }
385             
386             if (prevMonth.valueOf() < this.startDate || prevMonth.valueOf() > this.endDate ||
387                     this.daysOfWeekDisabled.indexOf(prevMonth.getUTCDay()) !== -1) {
388                     clsName += ' disabled';
389             }
390             
391             fillMonths.cn.push({
392                 tag: 'td',
393                 cls: 'day ' + clsName,
394                 html: prevMonth.getDate()
395             })
396             
397             prevMonth.setDate(prevMonth.getDate()+1);
398         }
399           
400         var currentYear = this.date && this.date.getUTCFullYear();
401         var currentMonth = this.date && this.date.getUTCMonth();
402         
403         this.picker().select('>.datepicker-months th.switch',true).first().dom.innerHTML = year;
404         
405         Roo.each(this.picker().select('>.datepicker-months tbody span',true).elements, function(v,k){
406             v.removeClass('active');
407             
408             if(currentYear === year && k === currentMonth){
409                 v.addClass('active');
410             }
411             
412             if (year < startYear || year > endYear || (year == startYear && k < startMonth) || (year == endYear && k > endMonth)) {
413                 v.addClass('disabled');
414             }
415             
416         });
417         
418         
419         year = parseInt(year/10, 10) * 10;
420         
421         this.picker().select('>.datepicker-years th.switch', true).first().dom.innerHTML = year + '-' + (year + 9);
422         
423         this.picker().select('>.datepicker-years tbody td',true).first().dom.innerHTML = '';
424         
425         year -= 1;
426         for (var i = -1; i < 11; i++) {
427             this.picker().select('>.datepicker-years tbody td',true).first().createChild({
428                 tag: 'span',
429                 cls: 'year' + (i === -1 || i === 10 ? ' old' : '') + (currentYear === year ? ' active' : '') + (year < startYear || year > endYear ? ' disabled' : ''),
430                 html: year
431             })
432             
433             year += 1;
434         }
435     },
436     
437     showMode: function(dir) 
438     {
439         if (dir) {
440             this.viewMode = Math.max(this.minViewMode, Math.min(2, this.viewMode + dir));
441         }
442         
443         Roo.each(this.picker().select('>div',true).elements, function(v){
444             v.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
445             v.hide();
446         });
447         this.picker().select('>.datepicker-'+Roo.bootstrap.DateField.modes[this.viewMode].clsName, true).first().show();
448     },
449     
450     place: function()
451     {
452         if(this.isInline) return;
453         
454         this.picker().removeClass(['bottom', 'top']);
455         
456         if((Roo.lib.Dom.getViewHeight() + Roo.get(document.body).getScroll().top) - (this.inputEl().getBottom() + this.picker().getHeight()) < 0){
457             /*
458              * place to the top of element!
459              *
460              */
461             
462             this.picker().addClass('top');
463             this.picker().setTop(this.inputEl().getTop() - this.picker().getHeight()).setLeft(this.inputEl().getLeft());
464             
465             return;
466         }
467         
468         this.picker().addClass('bottom');
469         
470         this.picker().setTop(this.inputEl().getBottom()).setLeft(this.inputEl().getLeft());
471     },
472     
473     parseDate : function(value)
474     {
475         if(!value || value instanceof Date){
476             return value;
477         }
478         var v = Date.parseDate(value, this.format);
479         if (!v && (this.useIso || value.match(/^(\d{4})-0?(\d+)-0?(\d+)/))) {
480             v = Date.parseDate(value, 'Y-m-d');
481         }
482         if(!v && this.altFormats){
483             if(!this.altFormatsArray){
484                 this.altFormatsArray = this.altFormats.split("|");
485             }
486             for(var i = 0, len = this.altFormatsArray.length; i < len && !v; i++){
487                 v = Date.parseDate(value, this.altFormatsArray[i]);
488             }
489         }
490         return v;
491     },
492     
493     formatDate : function(date, fmt)
494     {
495         return (!date || !(date instanceof Date)) ?
496         date : date.dateFormat(fmt || this.format);
497     },
498     
499     onFocus : function()
500     {
501         Roo.bootstrap.DateField.superclass.onFocus.call(this);
502         this.show();
503     },
504     
505     onBlur : function()
506     {
507         Roo.bootstrap.DateField.superclass.onBlur.call(this);
508         
509         var d = this.inputEl().getValue();
510         
511         this.setValue(d);
512                 
513         this.hide();
514     },
515     
516     show : function()
517     {
518         this.picker().show();
519         this.update();
520         this.place();
521         
522         this.fireEvent('show', this, this.date);
523     },
524     
525     hide : function()
526     {
527         if(this.isInline) return;
528         this.picker().hide();
529         this.viewMode = this.startViewMode;
530         this.showMode();
531         
532         this.fireEvent('hide', this, this.date);
533         
534     },
535     
536     onMousedown: function(e)
537     {
538         e.stopPropagation();
539         e.preventDefault();
540     },
541     
542     keyup: function(e)
543     {
544         Roo.bootstrap.DateField.superclass.keyup.call(this);
545         this.update();
546     },
547
548     setValue: function(v)
549     {
550         
551         // v can be a string or a date..
552         
553         
554         var d = new Date(this.parseDate(v) ).clearTime();
555         
556         if(isNaN(d.getTime())){
557             this.date = this.viewDate = '';
558             Roo.bootstrap.DateField.superclass.setValue.call(this, '');
559             return;
560         }
561         
562         v = this.formatDate(d);
563         
564         Roo.bootstrap.DateField.superclass.setValue.call(this, v);
565         
566         this.date = new Date(d.getTime() - d.getTimezoneOffset()*60000);
567      
568         this.update();
569
570         this.fireEvent('select', this, this.date);
571         
572     },
573     
574     getValue: function()
575     {
576         return this.formatDate(this.date);
577     },
578     
579     fireKey: function(e)
580     {
581         if (!this.picker().isVisible()){
582             if (e.keyCode == 27) // allow escape to hide and re-show picker
583                 this.show();
584             return;
585         }
586         
587         var dateChanged = false,
588         dir, day, month,
589         newDate, newViewDate;
590         
591         switch(e.keyCode){
592             case 27: // escape
593                 this.hide();
594                 e.preventDefault();
595                 break;
596             case 37: // left
597             case 39: // right
598                 if (!this.keyboardNavigation) break;
599                 dir = e.keyCode == 37 ? -1 : 1;
600                 
601                 if (e.ctrlKey){
602                     newDate = this.moveYear(this.date, dir);
603                     newViewDate = this.moveYear(this.viewDate, dir);
604                 } else if (e.shiftKey){
605                     newDate = this.moveMonth(this.date, dir);
606                     newViewDate = this.moveMonth(this.viewDate, dir);
607                 } else {
608                     newDate = new Date(this.date);
609                     newDate.setUTCDate(this.date.getUTCDate() + dir);
610                     newViewDate = new Date(this.viewDate);
611                     newViewDate.setUTCDate(this.viewDate.getUTCDate() + dir);
612                 }
613                 if (this.dateWithinRange(newDate)){
614                     this.date = newDate;
615                     this.viewDate = newViewDate;
616                     this.setValue(this.formatDate(this.date));
617 //                    this.update();
618                     e.preventDefault();
619                     dateChanged = true;
620                 }
621                 break;
622             case 38: // up
623             case 40: // down
624                 if (!this.keyboardNavigation) break;
625                 dir = e.keyCode == 38 ? -1 : 1;
626                 if (e.ctrlKey){
627                     newDate = this.moveYear(this.date, dir);
628                     newViewDate = this.moveYear(this.viewDate, dir);
629                 } else if (e.shiftKey){
630                     newDate = this.moveMonth(this.date, dir);
631                     newViewDate = this.moveMonth(this.viewDate, dir);
632                 } else {
633                     newDate = new Date(this.date);
634                     newDate.setUTCDate(this.date.getUTCDate() + dir * 7);
635                     newViewDate = new Date(this.viewDate);
636                     newViewDate.setUTCDate(this.viewDate.getUTCDate() + dir * 7);
637                 }
638                 if (this.dateWithinRange(newDate)){
639                     this.date = newDate;
640                     this.viewDate = newViewDate;
641                     this.setValue(this.formatDate(this.date));
642 //                    this.update();
643                     e.preventDefault();
644                     dateChanged = true;
645                 }
646                 break;
647             case 13: // enter
648                 this.setValue(this.formatDate(this.date));
649                 this.hide();
650                 e.preventDefault();
651                 break;
652             case 9: // tab
653                 this.setValue(this.formatDate(this.date));
654                 this.hide();
655                 break;
656             case 16: // shift
657             case 17: // ctrl
658             case 18: // alt
659                 break;
660             default :
661                 this.hide();
662                 
663         }
664     },
665     
666     
667     onClick: function(e) 
668     {
669         e.stopPropagation();
670         e.preventDefault();
671         
672         var target = e.getTarget();
673         
674         if(target.nodeName.toLowerCase() === 'i'){
675             target = Roo.get(target).dom.parentNode;
676         }
677         
678         var nodeName = target.nodeName;
679         var className = target.className;
680         var html = target.innerHTML;
681         //Roo.log(nodeName);
682         
683         switch(nodeName.toLowerCase()) {
684             case 'th':
685                 switch(className) {
686                     case 'switch':
687                         this.showMode(1);
688                         break;
689                     case 'prev':
690                     case 'next':
691                         var dir = Roo.bootstrap.DateField.modes[this.viewMode].navStep * (className == 'prev' ? -1 : 1);
692                         switch(this.viewMode){
693                                 case 0:
694                                         this.viewDate = this.moveMonth(this.viewDate, dir);
695                                         break;
696                                 case 1:
697                                 case 2:
698                                         this.viewDate = this.moveYear(this.viewDate, dir);
699                                         break;
700                         }
701                         this.fill();
702                         break;
703                     case 'today':
704                         var date = new Date();
705                         this.date = this.UTCDate(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0);
706 //                        this.fill()
707                         this.setValue(this.formatDate(this.date));
708                         
709                         this.hide();
710                         break;
711                 }
712                 break;
713             case 'span':
714                 if (className.indexOf('disabled') < 0) {
715                     this.viewDate.setUTCDate(1);
716                     if (className.indexOf('month') > -1) {
717                         this.viewDate.setUTCMonth(Roo.bootstrap.DateField.dates[this.language].monthsShort.indexOf(html));
718                     } else {
719                         var year = parseInt(html, 10) || 0;
720                         this.viewDate.setUTCFullYear(year);
721                         
722                     }
723                     
724                     if(this.singleMode){
725                         this.setValue(this.formatDate(this.viewDate));
726                         this.hide();
727                         return;
728                     }
729                     
730                     this.showMode(-1);
731                     this.fill();
732                 }
733                 break;
734                 
735             case 'td':
736                 //Roo.log(className);
737                 if (className.indexOf('day') > -1 && className.indexOf('disabled') < 0 ){
738                     var day = parseInt(html, 10) || 1;
739                     var year = this.viewDate.getUTCFullYear(),
740                         month = this.viewDate.getUTCMonth();
741
742                     if (className.indexOf('old') > -1) {
743                         if(month === 0 ){
744                             month = 11;
745                             year -= 1;
746                         }else{
747                             month -= 1;
748                         }
749                     } else if (className.indexOf('new') > -1) {
750                         if (month == 11) {
751                             month = 0;
752                             year += 1;
753                         } else {
754                             month += 1;
755                         }
756                     }
757                     //Roo.log([year,month,day]);
758                     this.date = this.UTCDate(year, month, day,0,0,0,0);
759                     this.viewDate = this.UTCDate(year, month, Math.min(28, day),0,0,0,0);
760 //                    this.fill();
761                     //Roo.log(this.formatDate(this.date));
762                     this.setValue(this.formatDate(this.date));
763                     this.hide();
764                 }
765                 break;
766         }
767     },
768     
769     setStartDate: function(startDate)
770     {
771         this.startDate = startDate || -Infinity;
772         if (this.startDate !== -Infinity) {
773             this.startDate = this.parseDate(this.startDate);
774         }
775         this.update();
776         this.updateNavArrows();
777     },
778
779     setEndDate: function(endDate)
780     {
781         this.endDate = endDate || Infinity;
782         if (this.endDate !== Infinity) {
783             this.endDate = this.parseDate(this.endDate);
784         }
785         this.update();
786         this.updateNavArrows();
787     },
788     
789     setDaysOfWeekDisabled: function(daysOfWeekDisabled)
790     {
791         this.daysOfWeekDisabled = daysOfWeekDisabled || [];
792         if (typeof(this.daysOfWeekDisabled) !== 'object') {
793             this.daysOfWeekDisabled = this.daysOfWeekDisabled.split(/,\s*/);
794         }
795         this.daysOfWeekDisabled = this.daysOfWeekDisabled.map(function (d) {
796             return parseInt(d, 10);
797         });
798         this.update();
799         this.updateNavArrows();
800     },
801     
802     updateNavArrows: function() 
803     {
804         if(this.singleMode){
805             return;
806         }
807         
808         var d = new Date(this.viewDate),
809         year = d.getUTCFullYear(),
810         month = d.getUTCMonth();
811         
812         Roo.each(this.picker().select('.prev', true).elements, function(v){
813             v.show();
814             switch (this.viewMode) {
815                 case 0:
816
817                     if (this.startDate !== -Infinity && year <= this.startDate.getUTCFullYear() && month <= this.startDate.getUTCMonth()) {
818                         v.hide();
819                     }
820                     break;
821                 case 1:
822                 case 2:
823                     if (this.startDate !== -Infinity && year <= this.startDate.getUTCFullYear()) {
824                         v.hide();
825                     }
826                     break;
827             }
828         });
829         
830         Roo.each(this.picker().select('.next', true).elements, function(v){
831             v.show();
832             switch (this.viewMode) {
833                 case 0:
834
835                     if (this.endDate !== Infinity && year >= this.endDate.getUTCFullYear() && month >= this.endDate.getUTCMonth()) {
836                         v.hide();
837                     }
838                     break;
839                 case 1:
840                 case 2:
841                     if (this.endDate !== Infinity && year >= this.endDate.getUTCFullYear()) {
842                         v.hide();
843                     }
844                     break;
845             }
846         })
847     },
848     
849     moveMonth: function(date, dir)
850     {
851         if (!dir) return date;
852         var new_date = new Date(date.valueOf()),
853         day = new_date.getUTCDate(),
854         month = new_date.getUTCMonth(),
855         mag = Math.abs(dir),
856         new_month, test;
857         dir = dir > 0 ? 1 : -1;
858         if (mag == 1){
859             test = dir == -1
860             // If going back one month, make sure month is not current month
861             // (eg, Mar 31 -> Feb 31 == Feb 28, not Mar 02)
862             ? function(){
863                 return new_date.getUTCMonth() == month;
864             }
865             // If going forward one month, make sure month is as expected
866             // (eg, Jan 31 -> Feb 31 == Feb 28, not Mar 02)
867             : function(){
868                 return new_date.getUTCMonth() != new_month;
869             };
870             new_month = month + dir;
871             new_date.setUTCMonth(new_month);
872             // Dec -> Jan (12) or Jan -> Dec (-1) -- limit expected date to 0-11
873             if (new_month < 0 || new_month > 11)
874                 new_month = (new_month + 12) % 12;
875         } else {
876             // For magnitudes >1, move one month at a time...
877             for (var i=0; i<mag; i++)
878                 // ...which might decrease the day (eg, Jan 31 to Feb 28, etc)...
879                 new_date = this.moveMonth(new_date, dir);
880             // ...then reset the day, keeping it in the new month
881             new_month = new_date.getUTCMonth();
882             new_date.setUTCDate(day);
883             test = function(){
884                 return new_month != new_date.getUTCMonth();
885             };
886         }
887         // Common date-resetting loop -- if date is beyond end of month, make it
888         // end of month
889         while (test()){
890             new_date.setUTCDate(--day);
891             new_date.setUTCMonth(new_month);
892         }
893         return new_date;
894     },
895
896     moveYear: function(date, dir)
897     {
898         return this.moveMonth(date, dir*12);
899     },
900
901     dateWithinRange: function(date)
902     {
903         return date >= this.startDate && date <= this.endDate;
904     },
905
906     
907     remove: function() 
908     {
909         this.picker().remove();
910     }
911    
912 });
913
914 Roo.apply(Roo.bootstrap.DateField,  {
915     
916     head : {
917         tag: 'thead',
918         cn: [
919         {
920             tag: 'tr',
921             cn: [
922             {
923                 tag: 'th',
924                 cls: 'prev',
925                 html: '<i class="fa fa-arrow-left"/>'
926             },
927             {
928                 tag: 'th',
929                 cls: 'switch',
930                 colspan: '5'
931             },
932             {
933                 tag: 'th',
934                 cls: 'next',
935                 html: '<i class="fa fa-arrow-right"/>'
936             }
937
938             ]
939         }
940         ]
941     },
942     
943     content : {
944         tag: 'tbody',
945         cn: [
946         {
947             tag: 'tr',
948             cn: [
949             {
950                 tag: 'td',
951                 colspan: '7'
952             }
953             ]
954         }
955         ]
956     },
957     
958     footer : {
959         tag: 'tfoot',
960         cn: [
961         {
962             tag: 'tr',
963             cn: [
964             {
965                 tag: 'th',
966                 colspan: '7',
967                 cls: 'today'
968             }
969                     
970             ]
971         }
972         ]
973     },
974     
975     dates:{
976         en: {
977             days: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"],
978             daysShort: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],
979             daysMin: ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa", "Su"],
980             months: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
981             monthsShort: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
982             today: "Today"
983         }
984     },
985     
986     modes: [
987     {
988         clsName: 'days',
989         navFnc: 'Month',
990         navStep: 1
991     },
992     {
993         clsName: 'months',
994         navFnc: 'FullYear',
995         navStep: 1
996     },
997     {
998         clsName: 'years',
999         navFnc: 'FullYear',
1000         navStep: 10
1001     }]
1002 });
1003
1004 Roo.apply(Roo.bootstrap.DateField,  {
1005   
1006     template : {
1007         tag: 'div',
1008         cls: 'datepicker dropdown-menu',
1009         cn: [
1010         {
1011             tag: 'div',
1012             cls: 'datepicker-days',
1013             cn: [
1014             {
1015                 tag: 'table',
1016                 cls: 'table-condensed',
1017                 cn:[
1018                 Roo.bootstrap.DateField.head,
1019                 {
1020                     tag: 'tbody'
1021                 },
1022                 Roo.bootstrap.DateField.footer
1023                 ]
1024             }
1025             ]
1026         },
1027         {
1028             tag: 'div',
1029             cls: 'datepicker-months',
1030             cn: [
1031             {
1032                 tag: 'table',
1033                 cls: 'table-condensed',
1034                 cn:[
1035                 Roo.bootstrap.DateField.head,
1036                 Roo.bootstrap.DateField.content,
1037                 Roo.bootstrap.DateField.footer
1038                 ]
1039             }
1040             ]
1041         },
1042         {
1043             tag: 'div',
1044             cls: 'datepicker-years',
1045             cn: [
1046             {
1047                 tag: 'table',
1048                 cls: 'table-condensed',
1049                 cn:[
1050                 Roo.bootstrap.DateField.head,
1051                 Roo.bootstrap.DateField.content,
1052                 Roo.bootstrap.DateField.footer
1053                 ]
1054             }
1055             ]
1056         }
1057         ]
1058     }
1059 });
1060
1061  
1062
1063