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)
23 * @cfg {Boolean} keyboardNavigation default true
24 * @cfg {String} language default en
27 * Create a new DateField
28 * @param {Object} config The config object
31 Roo.bootstrap.DateField = function(config){
32 Roo.bootstrap.DateField.superclass.constructor.call(this, config);
36 * Fires when this field show.
37 * @param {Roo.bootstrap.DateField} this
38 * @param {Mixed} date The date value
43 * Fires when this field hide.
44 * @param {Roo.bootstrap.DateField} this
45 * @param {Mixed} date The date value
50 * Fires when select a date.
51 * @param {Roo.bootstrap.DateField} this
52 * @param {Mixed} date The date value
58 Roo.extend(Roo.bootstrap.DateField, Roo.bootstrap.Input, {
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').
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').
71 altFormats : "m/d/Y|m-d-y|m-d-Y|m/d|m-d|md|mdy|mdY|d",
79 todayHighlight : false,
85 keyboardNavigation: true,
93 daysOfWeekDisabled: [],
101 return new Date(Date.UTC.apply(Date, arguments));
106 var today = new Date();
107 return this.UTCDate(today.getUTCFullYear(), today.getUTCMonth(), today.getUTCDate());
110 getDate: function() {
111 var d = this.getUTCDate();
112 return new Date(d.getTime() + (d.getTimezoneOffset()*60000));
115 getUTCDate: function() {
119 setDate: function(d) {
120 this.setUTCDate(new Date(d.getTime() - (d.getTimezoneOffset()*60000)));
123 setUTCDate: function(d) {
125 this.setValue(this.formatDate(this.date));
128 onRender: function(ct, position)
131 Roo.bootstrap.DateField.superclass.onRender.call(this, ct, position);
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";
137 this.isRTL = Roo.bootstrap.DateField.dates[this.language].rtl || false;
138 this.format = this.format || 'm/d/y';
139 this.isInline = false;
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;
145 if (typeof(this.minViewMode === 'string')) {
146 switch (this.minViewMode) {
148 this.minViewMode = 1;
151 this.minViewMode = 2;
154 this.minViewMode = 0;
159 if (typeof(this.viewMode === 'string')) {
160 switch (this.viewMode) {
173 this.pickerEl = Roo.get(document.body).createChild(Roo.bootstrap.DateField.template);
175 // this.el.select('>.input-group', true).first().createChild(Roo.bootstrap.DateField.template);
177 this.picker().setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
179 this.picker().on('mousedown', this.onMousedown, this);
180 this.picker().on('click', this.onClick, this);
182 this.picker().addClass('datepicker-dropdown');
184 this.startViewMode = this.viewMode;
187 Roo.each(this.picker().select('thead > tr > th', true).elements, function(v){
188 v.setVisibilityMode(Roo.Element.DISPLAY)
192 Roo.each(this.picker().select('tbody > tr > td', true).elements, function(v){
193 v.setStyle('width', '189px');
197 Roo.each(this.picker().select('tfoot th.today', true).elements, function(v){
198 if(!this.calendarWeeks){
203 v.dom.innerHTML = Roo.bootstrap.DateField.dates[this.language].today
204 v.attr('colspan', function(i, val){
205 return parseInt(val) + 1;
210 this.weekEnd = this.weekStart === 0 ? 6 : this.weekStart - 1;
212 this.setStartDate(this.startDate);
213 this.setEndDate(this.endDate);
215 this.setDaysOfWeekDisabled(this.daysOfWeekDisabled);
229 return this.pickerEl;
230 // return this.el.select('.datepicker', true).first();
235 var dowCnt = this.weekStart;
244 if(this.calendarWeeks){
252 while (dowCnt < this.weekStart + 7) {
256 html: Roo.bootstrap.DateField.dates[this.language].daysMin[(dowCnt++)%7]
260 this.picker().select('>.datepicker-days thead', true).first().createChild(dow);
263 fillMonths: function()
266 var months = this.picker().select('>.datepicker-months td', true).first();
268 months.dom.innerHTML = '';
274 html: Roo.bootstrap.DateField.dates[this.language].monthsShort[i++]
277 months.createChild(month);
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;
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);
291 this.viewDate = new Date(this.date);
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();
309 this.picker().select('>.datepicker-days thead th.switch', true).first().dom.innerHTML = Roo.bootstrap.DateField.dates[this.language].months[month]+' '+year;
311 // this.picker().select('>tfoot th.today', true).first().dom.innerHTML = Roo.bootstrap.DateField.dates[this.language].today;
313 // this.picker.select('>tfoot th.today').
314 // .text(dates[this.language].today)
315 // .toggle(this.todayBtn !== false);
317 this.updateNavArrows();
320 var prevMonth = this.UTCDate(year, month-1, 28,0,0,0,0),
322 day = prevMonth.getDaysInMonth(prevMonth.getUTCFullYear(), prevMonth.getUTCMonth());
324 prevMonth.setUTCDate(day);
326 prevMonth.setUTCDate(day - (prevMonth.getUTCDay() - this.weekStart + 7)%7);
328 var nextMonth = new Date(prevMonth);
330 nextMonth.setUTCDate(nextMonth.getUTCDate() + 42);
332 nextMonth = nextMonth.valueOf();
334 var fillMonths = false;
336 this.picker().select('>.datepicker-days tbody',true).first().dom.innerHTML = '';
338 while(prevMonth.valueOf() < nextMonth) {
341 if (prevMonth.getUTCDay() === this.weekStart) {
343 this.picker().select('>.datepicker-days tbody',true).first().createChild(fillMonths);
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.
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;
372 if (prevMonth.getUTCFullYear() < year || (prevMonth.getUTCFullYear() == year && prevMonth.getUTCMonth() < month)) {
374 } else if (prevMonth.getUTCFullYear() > year || (prevMonth.getUTCFullYear() == year && prevMonth.getUTCMonth() > month)) {
377 if (this.todayHighlight &&
378 prevMonth.getUTCFullYear() == today.getFullYear() &&
379 prevMonth.getUTCMonth() == today.getMonth() &&
380 prevMonth.getUTCDate() == today.getDate()) {
384 if (currentDate && prevMonth.valueOf() === currentDate) {
385 clsName += ' active';
388 if (prevMonth.valueOf() < this.startDate || prevMonth.valueOf() > this.endDate ||
389 this.daysOfWeekDisabled.indexOf(prevMonth.getUTCDay()) !== -1) {
390 clsName += ' disabled';
395 cls: 'day ' + clsName,
396 html: prevMonth.getDate()
399 prevMonth.setDate(prevMonth.getDate()+1);
402 var currentYear = this.date && this.date.getUTCFullYear();
403 var currentMonth = this.date && this.date.getUTCMonth();
405 this.picker().select('>.datepicker-months th.switch',true).first().dom.innerHTML = year;
407 Roo.each(this.picker().select('>.datepicker-months tbody span',true).elements, function(v,k){
408 v.removeClass('active');
410 if(currentYear === year && k === currentMonth){
411 v.addClass('active');
414 if (year < startYear || year > endYear || (year == startYear && k < startMonth) || (year == endYear && k > endMonth)) {
415 v.addClass('disabled');
421 year = parseInt(year/10, 10) * 10;
423 this.picker().select('>.datepicker-years th.switch', true).first().dom.innerHTML = year + '-' + (year + 9);
425 this.picker().select('>.datepicker-years tbody td',true).first().dom.innerHTML = '';
428 for (var i = -1; i < 11; i++) {
429 this.picker().select('>.datepicker-years tbody td',true).first().createChild({
431 cls: 'year' + (i === -1 || i === 10 ? ' old' : '') + (currentYear === year ? ' active' : '') + (year < startYear || year > endYear ? ' disabled' : ''),
439 showMode: function(dir)
442 this.viewMode = Math.max(this.minViewMode, Math.min(2, this.viewMode + dir));
445 Roo.each(this.picker().select('>div',true).elements, function(v){
446 v.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
449 this.picker().select('>.datepicker-'+Roo.bootstrap.DateField.modes[this.viewMode].clsName, true).first().show();
454 if(this.isInline) return;
456 this.picker().removeClass(['bottom', 'top']);
458 if((Roo.lib.Dom.getViewHeight() + Roo.get(document.body).getScroll().top) - (this.inputEl().getBottom() + this.picker().getHeight()) < 0){
460 * place to the top of element!
464 this.picker().addClass('top');
465 this.picker().setTop(this.inputEl().getTop() - this.picker().getHeight()).setLeft(this.inputEl().getLeft());
470 this.picker().addClass('bottom');
472 this.picker().setTop(this.inputEl().getBottom()).setLeft(this.inputEl().getLeft());
475 parseDate : function(value)
477 if(!value || value instanceof Date){
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');
484 if(!v && this.altFormats){
485 if(!this.altFormatsArray){
486 this.altFormatsArray = this.altFormats.split("|");
488 for(var i = 0, len = this.altFormatsArray.length; i < len && !v; i++){
489 v = Date.parseDate(value, this.altFormatsArray[i]);
495 formatDate : function(date, fmt)
497 return (!date || !(date instanceof Date)) ?
498 date : date.dateFormat(fmt || this.format);
503 Roo.bootstrap.DateField.superclass.onFocus.call(this);
509 Roo.bootstrap.DateField.superclass.onBlur.call(this);
511 var d = this.inputEl().getValue();
520 this.picker().show();
524 this.fireEvent('show', this, this.date);
529 if(this.isInline) return;
530 this.picker().hide();
531 this.viewMode = this.startViewMode;
534 this.fireEvent('hide', this, this.date);
538 onMousedown: function(e)
546 Roo.bootstrap.DateField.superclass.keyup.call(this);
550 setValue: function(v)
553 // v can be a string or a date..
556 var d = new Date(this.parseDate(v) ).clearTime();
558 if(isNaN(d.getTime())){
559 this.date = this.viewDate = '';
560 Roo.bootstrap.DateField.superclass.setValue.call(this, '');
564 v = this.formatDate(d);
566 Roo.bootstrap.DateField.superclass.setValue.call(this, v);
568 this.date = new Date(d.getTime() - d.getTimezoneOffset()*60000);
572 this.fireEvent('select', this, this.date);
578 return this.formatDate(this.date);
583 if (!this.picker().isVisible()){
584 if (e.keyCode == 27) // allow escape to hide and re-show picker
589 var dateChanged = false,
591 newDate, newViewDate;
600 if (!this.keyboardNavigation) break;
601 dir = e.keyCode == 37 ? -1 : 1;
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);
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);
615 if (this.dateWithinRange(newDate)){
617 this.viewDate = newViewDate;
618 this.setValue(this.formatDate(this.date));
626 if (!this.keyboardNavigation) break;
627 dir = e.keyCode == 38 ? -1 : 1;
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);
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);
640 if (this.dateWithinRange(newDate)){
642 this.viewDate = newViewDate;
643 this.setValue(this.formatDate(this.date));
650 this.setValue(this.formatDate(this.date));
655 this.setValue(this.formatDate(this.date));
674 var target = e.getTarget();
676 if(target.nodeName.toLowerCase() === 'i'){
677 target = Roo.get(target).dom.parentNode;
680 var nodeName = target.nodeName;
681 var className = target.className;
682 var html = target.innerHTML;
685 switch(nodeName.toLowerCase()) {
693 var dir = Roo.bootstrap.DateField.modes[this.viewMode].navStep * (className == 'prev' ? -1 : 1);
694 switch(this.viewMode){
696 this.viewDate = this.moveMonth(this.viewDate, dir);
700 this.viewDate = this.moveYear(this.viewDate, dir);
706 var date = new Date();
707 this.date = this.UTCDate(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0);
709 this.setValue(this.formatDate(this.date));
716 if (className.indexOf('disabled') < 0) {
717 this.viewDate.setUTCDate(1);
718 if (className.indexOf('month') > -1) {
719 this.viewDate.setUTCMonth(Roo.bootstrap.DateField.dates[this.language].monthsShort.indexOf(html));
721 var year = parseInt(html, 10) || 0;
722 this.viewDate.setUTCFullYear(year);
727 this.setValue(this.formatDate(this.viewDate));
738 //Roo.log(className);
739 if (className.indexOf('day') > -1 && className.indexOf('disabled') < 0 ){
740 var day = parseInt(html, 10) || 1;
741 var year = this.viewDate.getUTCFullYear(),
742 month = this.viewDate.getUTCMonth();
744 if (className.indexOf('old') > -1) {
751 } else if (className.indexOf('new') > -1) {
759 //Roo.log([year,month,day]);
760 this.date = this.UTCDate(year, month, day,0,0,0,0);
761 this.viewDate = this.UTCDate(year, month, Math.min(28, day),0,0,0,0);
763 //Roo.log(this.formatDate(this.date));
764 this.setValue(this.formatDate(this.date));
771 setStartDate: function(startDate)
773 this.startDate = startDate || -Infinity;
774 if (this.startDate !== -Infinity) {
775 this.startDate = this.parseDate(this.startDate);
778 this.updateNavArrows();
781 setEndDate: function(endDate)
783 this.endDate = endDate || Infinity;
784 if (this.endDate !== Infinity) {
785 this.endDate = this.parseDate(this.endDate);
788 this.updateNavArrows();
791 setDaysOfWeekDisabled: function(daysOfWeekDisabled)
793 this.daysOfWeekDisabled = daysOfWeekDisabled || [];
794 if (typeof(this.daysOfWeekDisabled) !== 'object') {
795 this.daysOfWeekDisabled = this.daysOfWeekDisabled.split(/,\s*/);
797 this.daysOfWeekDisabled = this.daysOfWeekDisabled.map(function (d) {
798 return parseInt(d, 10);
801 this.updateNavArrows();
804 updateNavArrows: function()
810 var d = new Date(this.viewDate),
811 year = d.getUTCFullYear(),
812 month = d.getUTCMonth();
814 Roo.each(this.picker().select('.prev', true).elements, function(v){
816 switch (this.viewMode) {
819 if (this.startDate !== -Infinity && year <= this.startDate.getUTCFullYear() && month <= this.startDate.getUTCMonth()) {
825 if (this.startDate !== -Infinity && year <= this.startDate.getUTCFullYear()) {
832 Roo.each(this.picker().select('.next', true).elements, function(v){
834 switch (this.viewMode) {
837 if (this.endDate !== Infinity && year >= this.endDate.getUTCFullYear() && month >= this.endDate.getUTCMonth()) {
843 if (this.endDate !== Infinity && year >= this.endDate.getUTCFullYear()) {
851 moveMonth: function(date, dir)
853 if (!dir) return date;
854 var new_date = new Date(date.valueOf()),
855 day = new_date.getUTCDate(),
856 month = new_date.getUTCMonth(),
859 dir = dir > 0 ? 1 : -1;
862 // If going back one month, make sure month is not current month
863 // (eg, Mar 31 -> Feb 31 == Feb 28, not Mar 02)
865 return new_date.getUTCMonth() == month;
867 // If going forward one month, make sure month is as expected
868 // (eg, Jan 31 -> Feb 31 == Feb 28, not Mar 02)
870 return new_date.getUTCMonth() != new_month;
872 new_month = month + dir;
873 new_date.setUTCMonth(new_month);
874 // Dec -> Jan (12) or Jan -> Dec (-1) -- limit expected date to 0-11
875 if (new_month < 0 || new_month > 11)
876 new_month = (new_month + 12) % 12;
878 // For magnitudes >1, move one month at a time...
879 for (var i=0; i<mag; i++)
880 // ...which might decrease the day (eg, Jan 31 to Feb 28, etc)...
881 new_date = this.moveMonth(new_date, dir);
882 // ...then reset the day, keeping it in the new month
883 new_month = new_date.getUTCMonth();
884 new_date.setUTCDate(day);
886 return new_month != new_date.getUTCMonth();
889 // Common date-resetting loop -- if date is beyond end of month, make it
892 new_date.setUTCDate(--day);
893 new_date.setUTCMonth(new_month);
898 moveYear: function(date, dir)
900 return this.moveMonth(date, dir*12);
903 dateWithinRange: function(date)
905 return date >= this.startDate && date <= this.endDate;
911 this.picker().remove();
916 Roo.apply(Roo.bootstrap.DateField, {
927 html: '<i class="fa fa-arrow-left"/>'
937 html: '<i class="fa fa-arrow-right"/>'
979 days: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"],
980 daysShort: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],
981 daysMin: ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa", "Su"],
982 months: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
983 monthsShort: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
1006 Roo.apply(Roo.bootstrap.DateField, {
1010 cls: 'datepicker dropdown-menu roo-dynamic',
1014 cls: 'datepicker-days',
1018 cls: 'table-condensed',
1020 Roo.bootstrap.DateField.head,
1024 Roo.bootstrap.DateField.footer
1031 cls: 'datepicker-months',
1035 cls: 'table-condensed',
1037 Roo.bootstrap.DateField.head,
1038 Roo.bootstrap.DateField.content,
1039 Roo.bootstrap.DateField.footer
1046 cls: 'datepicker-years',
1050 cls: 'table-condensed',
1052 Roo.bootstrap.DateField.head,
1053 Roo.bootstrap.DateField.content,
1054 Roo.bootstrap.DateField.footer