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         Roo.log(this.format);
482         Roo.log(v);
483         if (!v && (this.useIso || value.match(/^(\d{4})-0?(\d+)-0?(\d+)/))) {
484             v = Date.parseDate(value, 'Y-m-d');
485         }
486         if(!v && this.altFormats){
487             if(!this.altFormatsArray){
488                 this.altFormatsArray = this.altFormats.split("|");
489             }
490             for(var i = 0, len = this.altFormatsArray.length; i < len && !v; i++){
491                 v = Date.parseDate(value, this.altFormatsArray[i]);
492             }
493         }
494         return v;
495     },
496     
497     formatDate : function(date, fmt)
498     {   
499         return (!date || !(date instanceof Date)) ?
500         date : date.dateFormat(fmt || this.format);
501     },
502     
503     onFocus : function()
504     {
505         Roo.bootstrap.DateField.superclass.onFocus.call(this);
506         this.show();
507     },
508     
509     onBlur : function()
510     {
511         Roo.bootstrap.DateField.superclass.onBlur.call(this);
512         
513         var d = this.inputEl().getValue();
514         
515         this.setValue(d);
516                 
517         this.hide();
518     },
519     
520     show : function()
521     {
522         this.picker().show();
523         this.update();
524         this.place();
525         
526         this.fireEvent('show', this, this.date);
527     },
528     
529     hide : function()
530     {
531         if(this.isInline) return;
532         this.picker().hide();
533         this.viewMode = this.startViewMode;
534         this.showMode();
535         
536         this.fireEvent('hide', this, this.date);
537         
538     },
539     
540     onMousedown: function(e)
541     {
542         e.stopPropagation();
543         e.preventDefault();
544     },
545     
546     keyup: function(e)
547     {
548         Roo.bootstrap.DateField.superclass.keyup.call(this);
549         this.update();
550     },
551
552     setValue: function(v)
553     {
554         
555         // v can be a string or a date..
556         Roo.log('set Value');
557         
558         Roo.log(v);
559         
560         Roo.log(this.parseDate(v));
561         
562         var d = new Date(this.parseDate(v) ).clearTime();
563         
564         if(isNaN(d.getTime())){
565             this.date = this.viewDate = '';
566             Roo.bootstrap.DateField.superclass.setValue.call(this, '');
567             return;
568         }
569         
570         v = this.formatDate(d);
571         
572         Roo.bootstrap.DateField.superclass.setValue.call(this, v);
573         
574         this.date = new Date(d.getTime() - d.getTimezoneOffset()*60000);
575      
576         this.update();
577
578         this.fireEvent('select', this, this.date);
579         
580     },
581     
582     getValue: function()
583     {
584         return this.formatDate(this.date);
585     },
586     
587     fireKey: function(e)
588     {
589         if (!this.picker().isVisible()){
590             if (e.keyCode == 27) // allow escape to hide and re-show picker
591                 this.show();
592             return;
593         }
594         
595         var dateChanged = false,
596         dir, day, month,
597         newDate, newViewDate;
598         
599         switch(e.keyCode){
600             case 27: // escape
601                 this.hide();
602                 e.preventDefault();
603                 break;
604             case 37: // left
605             case 39: // right
606                 if (!this.keyboardNavigation) break;
607                 dir = e.keyCode == 37 ? -1 : 1;
608                 
609                 if (e.ctrlKey){
610                     newDate = this.moveYear(this.date, dir);
611                     newViewDate = this.moveYear(this.viewDate, dir);
612                 } else if (e.shiftKey){
613                     newDate = this.moveMonth(this.date, dir);
614                     newViewDate = this.moveMonth(this.viewDate, dir);
615                 } else {
616                     newDate = new Date(this.date);
617                     newDate.setUTCDate(this.date.getUTCDate() + dir);
618                     newViewDate = new Date(this.viewDate);
619                     newViewDate.setUTCDate(this.viewDate.getUTCDate() + dir);
620                 }
621                 if (this.dateWithinRange(newDate)){
622                     this.date = newDate;
623                     this.viewDate = newViewDate;
624                     this.setValue(this.formatDate(this.date));
625 //                    this.update();
626                     e.preventDefault();
627                     dateChanged = true;
628                 }
629                 break;
630             case 38: // up
631             case 40: // down
632                 if (!this.keyboardNavigation) break;
633                 dir = e.keyCode == 38 ? -1 : 1;
634                 if (e.ctrlKey){
635                     newDate = this.moveYear(this.date, dir);
636                     newViewDate = this.moveYear(this.viewDate, dir);
637                 } else if (e.shiftKey){
638                     newDate = this.moveMonth(this.date, dir);
639                     newViewDate = this.moveMonth(this.viewDate, dir);
640                 } else {
641                     newDate = new Date(this.date);
642                     newDate.setUTCDate(this.date.getUTCDate() + dir * 7);
643                     newViewDate = new Date(this.viewDate);
644                     newViewDate.setUTCDate(this.viewDate.getUTCDate() + dir * 7);
645                 }
646                 if (this.dateWithinRange(newDate)){
647                     this.date = newDate;
648                     this.viewDate = newViewDate;
649                     this.setValue(this.formatDate(this.date));
650 //                    this.update();
651                     e.preventDefault();
652                     dateChanged = true;
653                 }
654                 break;
655             case 13: // enter
656                 this.setValue(this.formatDate(this.date));
657                 this.hide();
658                 e.preventDefault();
659                 break;
660             case 9: // tab
661                 this.setValue(this.formatDate(this.date));
662                 this.hide();
663                 break;
664             case 16: // shift
665             case 17: // ctrl
666             case 18: // alt
667                 break;
668             default :
669                 this.hide();
670                 
671         }
672     },
673     
674     
675     onClick: function(e) 
676     {
677         e.stopPropagation();
678         e.preventDefault();
679         
680         var target = e.getTarget();
681         
682         if(target.nodeName.toLowerCase() === 'i'){
683             target = Roo.get(target).dom.parentNode;
684         }
685         
686         var nodeName = target.nodeName;
687         var className = target.className;
688         var html = target.innerHTML;
689         //Roo.log(nodeName);
690         
691         switch(nodeName.toLowerCase()) {
692             case 'th':
693                 switch(className) {
694                     case 'switch':
695                         this.showMode(1);
696                         break;
697                     case 'prev':
698                     case 'next':
699                         var dir = Roo.bootstrap.DateField.modes[this.viewMode].navStep * (className == 'prev' ? -1 : 1);
700                         switch(this.viewMode){
701                                 case 0:
702                                         this.viewDate = this.moveMonth(this.viewDate, dir);
703                                         break;
704                                 case 1:
705                                 case 2:
706                                         this.viewDate = this.moveYear(this.viewDate, dir);
707                                         break;
708                         }
709                         this.fill();
710                         break;
711                     case 'today':
712                         var date = new Date();
713                         this.date = this.UTCDate(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0);
714 //                        this.fill()
715                         this.setValue(this.formatDate(this.date));
716                         
717                         this.hide();
718                         break;
719                 }
720                 break;
721             case 'span':
722                 if (className.indexOf('disabled') < 0) {
723                     this.viewDate.setUTCDate(1);
724                     if (className.indexOf('month') > -1) {
725                         this.viewDate.setUTCMonth(Roo.bootstrap.DateField.dates[this.language].monthsShort.indexOf(html));
726                     } else {
727                         var year = parseInt(html, 10) || 0;
728                         this.viewDate.setUTCFullYear(year);
729                         
730                     }
731                     
732                     if(this.singleMode){
733                         Roo.log('format!!!!');
734                         Roo.log(this.viewDate);
735                         Roo.log(this.formatDate(this.viewDate));
736                         this.setValue(this.formatDate(this.viewDate));
737                         this.hide();
738                         return;
739                     }
740                     
741                     this.showMode(-1);
742                     this.fill();
743                 }
744                 break;
745                 
746             case 'td':
747                 //Roo.log(className);
748                 if (className.indexOf('day') > -1 && className.indexOf('disabled') < 0 ){
749                     var day = parseInt(html, 10) || 1;
750                     var year = this.viewDate.getUTCFullYear(),
751                         month = this.viewDate.getUTCMonth();
752
753                     if (className.indexOf('old') > -1) {
754                         if(month === 0 ){
755                             month = 11;
756                             year -= 1;
757                         }else{
758                             month -= 1;
759                         }
760                     } else if (className.indexOf('new') > -1) {
761                         if (month == 11) {
762                             month = 0;
763                             year += 1;
764                         } else {
765                             month += 1;
766                         }
767                     }
768                     //Roo.log([year,month,day]);
769                     this.date = this.UTCDate(year, month, day,0,0,0,0);
770                     this.viewDate = this.UTCDate(year, month, Math.min(28, day),0,0,0,0);
771 //                    this.fill();
772                     //Roo.log(this.formatDate(this.date));
773                     this.setValue(this.formatDate(this.date));
774                     this.hide();
775                 }
776                 break;
777         }
778     },
779     
780     setStartDate: function(startDate)
781     {
782         this.startDate = startDate || -Infinity;
783         if (this.startDate !== -Infinity) {
784             this.startDate = this.parseDate(this.startDate);
785         }
786         this.update();
787         this.updateNavArrows();
788     },
789
790     setEndDate: function(endDate)
791     {
792         this.endDate = endDate || Infinity;
793         if (this.endDate !== Infinity) {
794             this.endDate = this.parseDate(this.endDate);
795         }
796         this.update();
797         this.updateNavArrows();
798     },
799     
800     setDaysOfWeekDisabled: function(daysOfWeekDisabled)
801     {
802         this.daysOfWeekDisabled = daysOfWeekDisabled || [];
803         if (typeof(this.daysOfWeekDisabled) !== 'object') {
804             this.daysOfWeekDisabled = this.daysOfWeekDisabled.split(/,\s*/);
805         }
806         this.daysOfWeekDisabled = this.daysOfWeekDisabled.map(function (d) {
807             return parseInt(d, 10);
808         });
809         this.update();
810         this.updateNavArrows();
811     },
812     
813     updateNavArrows: function() 
814     {
815         if(this.singleMode){
816             return;
817         }
818         
819         var d = new Date(this.viewDate),
820         year = d.getUTCFullYear(),
821         month = d.getUTCMonth();
822         
823         Roo.each(this.picker().select('.prev', true).elements, function(v){
824             v.show();
825             switch (this.viewMode) {
826                 case 0:
827
828                     if (this.startDate !== -Infinity && year <= this.startDate.getUTCFullYear() && month <= this.startDate.getUTCMonth()) {
829                         v.hide();
830                     }
831                     break;
832                 case 1:
833                 case 2:
834                     if (this.startDate !== -Infinity && year <= this.startDate.getUTCFullYear()) {
835                         v.hide();
836                     }
837                     break;
838             }
839         });
840         
841         Roo.each(this.picker().select('.next', true).elements, function(v){
842             v.show();
843             switch (this.viewMode) {
844                 case 0:
845
846                     if (this.endDate !== Infinity && year >= this.endDate.getUTCFullYear() && month >= this.endDate.getUTCMonth()) {
847                         v.hide();
848                     }
849                     break;
850                 case 1:
851                 case 2:
852                     if (this.endDate !== Infinity && year >= this.endDate.getUTCFullYear()) {
853                         v.hide();
854                     }
855                     break;
856             }
857         })
858     },
859     
860     moveMonth: function(date, dir)
861     {
862         if (!dir) return date;
863         var new_date = new Date(date.valueOf()),
864         day = new_date.getUTCDate(),
865         month = new_date.getUTCMonth(),
866         mag = Math.abs(dir),
867         new_month, test;
868         dir = dir > 0 ? 1 : -1;
869         if (mag == 1){
870             test = dir == -1
871             // If going back one month, make sure month is not current month
872             // (eg, Mar 31 -> Feb 31 == Feb 28, not Mar 02)
873             ? function(){
874                 return new_date.getUTCMonth() == month;
875             }
876             // If going forward one month, make sure month is as expected
877             // (eg, Jan 31 -> Feb 31 == Feb 28, not Mar 02)
878             : function(){
879                 return new_date.getUTCMonth() != new_month;
880             };
881             new_month = month + dir;
882             new_date.setUTCMonth(new_month);
883             // Dec -> Jan (12) or Jan -> Dec (-1) -- limit expected date to 0-11
884             if (new_month < 0 || new_month > 11)
885                 new_month = (new_month + 12) % 12;
886         } else {
887             // For magnitudes >1, move one month at a time...
888             for (var i=0; i<mag; i++)
889                 // ...which might decrease the day (eg, Jan 31 to Feb 28, etc)...
890                 new_date = this.moveMonth(new_date, dir);
891             // ...then reset the day, keeping it in the new month
892             new_month = new_date.getUTCMonth();
893             new_date.setUTCDate(day);
894             test = function(){
895                 return new_month != new_date.getUTCMonth();
896             };
897         }
898         // Common date-resetting loop -- if date is beyond end of month, make it
899         // end of month
900         while (test()){
901             new_date.setUTCDate(--day);
902             new_date.setUTCMonth(new_month);
903         }
904         return new_date;
905     },
906
907     moveYear: function(date, dir)
908     {
909         return this.moveMonth(date, dir*12);
910     },
911
912     dateWithinRange: function(date)
913     {
914         return date >= this.startDate && date <= this.endDate;
915     },
916
917     
918     remove: function() 
919     {
920         this.picker().remove();
921     }
922    
923 });
924
925 Roo.apply(Roo.bootstrap.DateField,  {
926     
927     head : {
928         tag: 'thead',
929         cn: [
930         {
931             tag: 'tr',
932             cn: [
933             {
934                 tag: 'th',
935                 cls: 'prev',
936                 html: '<i class="fa fa-arrow-left"/>'
937             },
938             {
939                 tag: 'th',
940                 cls: 'switch',
941                 colspan: '5'
942             },
943             {
944                 tag: 'th',
945                 cls: 'next',
946                 html: '<i class="fa fa-arrow-right"/>'
947             }
948
949             ]
950         }
951         ]
952     },
953     
954     content : {
955         tag: 'tbody',
956         cn: [
957         {
958             tag: 'tr',
959             cn: [
960             {
961                 tag: 'td',
962                 colspan: '7'
963             }
964             ]
965         }
966         ]
967     },
968     
969     footer : {
970         tag: 'tfoot',
971         cn: [
972         {
973             tag: 'tr',
974             cn: [
975             {
976                 tag: 'th',
977                 colspan: '7',
978                 cls: 'today'
979             }
980                     
981             ]
982         }
983         ]
984     },
985     
986     dates:{
987         en: {
988             days: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"],
989             daysShort: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],
990             daysMin: ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa", "Su"],
991             months: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
992             monthsShort: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
993             today: "Today"
994         }
995     },
996     
997     modes: [
998     {
999         clsName: 'days',
1000         navFnc: 'Month',
1001         navStep: 1
1002     },
1003     {
1004         clsName: 'months',
1005         navFnc: 'FullYear',
1006         navStep: 1
1007     },
1008     {
1009         clsName: 'years',
1010         navFnc: 'FullYear',
1011         navStep: 10
1012     }]
1013 });
1014
1015 Roo.apply(Roo.bootstrap.DateField,  {
1016   
1017     template : {
1018         tag: 'div',
1019         cls: 'datepicker dropdown-menu roo-dynamic',
1020         cn: [
1021         {
1022             tag: 'div',
1023             cls: 'datepicker-days',
1024             cn: [
1025             {
1026                 tag: 'table',
1027                 cls: 'table-condensed',
1028                 cn:[
1029                 Roo.bootstrap.DateField.head,
1030                 {
1031                     tag: 'tbody'
1032                 },
1033                 Roo.bootstrap.DateField.footer
1034                 ]
1035             }
1036             ]
1037         },
1038         {
1039             tag: 'div',
1040             cls: 'datepicker-months',
1041             cn: [
1042             {
1043                 tag: 'table',
1044                 cls: 'table-condensed',
1045                 cn:[
1046                 Roo.bootstrap.DateField.head,
1047                 Roo.bootstrap.DateField.content,
1048                 Roo.bootstrap.DateField.footer
1049                 ]
1050             }
1051             ]
1052         },
1053         {
1054             tag: 'div',
1055             cls: 'datepicker-years',
1056             cn: [
1057             {
1058                 tag: 'table',
1059                 cls: 'table-condensed',
1060                 cn:[
1061                 Roo.bootstrap.DateField.head,
1062                 Roo.bootstrap.DateField.content,
1063                 Roo.bootstrap.DateField.footer
1064                 ]
1065             }
1066             ]
1067         }
1068         ]
1069     }
1070 });
1071
1072  
1073
1074