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