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