9 * @class Roo.bootstrap.form.DateField
10 * @extends Roo.bootstrap.form.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.form.DateField = function(config){
32 Roo.bootstrap.form.DateField.superclass.constructor.call(this, config);
36 * Fires when this field show.
37 * @param {Roo.bootstrap.form.DateField} this
38 * @param {Mixed} date The date value
43 * Fires when this field hide.
44 * @param {Roo.bootstrap.form.DateField} this
45 * @param {Mixed} date The date value
50 * Fires when select a date.
51 * @param {Roo.bootstrap.form.DateField} this
52 * @param {Mixed} date The date value
57 * Fires when before select a date.
58 * @param {Roo.bootstrap.form.DateField} this
59 * @param {Mixed} date The date value
65 Roo.extend(Roo.bootstrap.form.DateField, Roo.bootstrap.form.Input, {
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').
80 todayHighlight : false,
86 keyboardNavigation: true,
94 daysOfWeekDisabled: [],
103 * @cfg {Boolean} editable False to prevent the user from typing text directly into the field (defaults to true)
109 return new Date(Date.UTC.apply(Date, arguments));
114 var today = new Date();
115 return this.UTCDate(today.getUTCFullYear(), today.getUTCMonth(), today.getUTCDate());
118 getDate: function() {
119 var d = this.getUTCDate();
120 return new Date(d.getTime() + (d.getTimezoneOffset()*60000));
123 getUTCDate: function() {
127 setDate: function(d) {
128 this.setUTCDate(new Date(d.getTime() - (d.getTimezoneOffset()*60000)));
131 setUTCDate: function(d) {
133 this.setValue(this.date);
136 translateDates: function(lang)
138 var translation = Roo.bootstrap.form.DateField.dates[lang] = {
146 var locale = lang.replace('_', '-');
148 var is_latin = [ 'zh-hk', 'zh-cn', 'jp', 'ko' ].indexOf(locale.toLowerCase()) < 0;
152 for(var i = 0; i < 7; i++) {
153 var date = new Date(2020, 0, 5 + i);
155 var day = new Intl.DateTimeFormat(locale, {
159 var dayShort = new Intl.DateTimeFormat(locale, {
163 var dayMin = new Intl.DateTimeFormat(locale, {
168 dayShort = day.substring(0, 3);
169 dayMin = day.substring(0, 2);
172 translation.days.push(day);
173 translation.daysShort.push(dayShort);
174 translation.daysMin.push(dayMin);
178 for(var i = 0; i < 12; i++) {
179 var date = new Date(2020, i);
181 var month = new Intl.DateTimeFormat(locale, {
185 var monthShort = new Intl.DateTimeFormat(locale, {
190 monthShort = month.substring(0, 3);
193 translation.months.push(month);
194 translation.monthsShort.push(monthShort);
198 onRender: function(ct, position)
201 Roo.bootstrap.form.DateField.superclass.onRender.call(this, ct, position);
203 this.translateDates(this.language);
205 this.isRTL = Roo.bootstrap.form.DateField.dates[this.language].rtl || false;
206 this.format = this.format || 'm/d/y';
207 this.isInline = false;
209 this.component = this.el.select('.add-on', true).first() || false;
210 this.component = (this.component && this.component.length === 0) ? false : this.component;
211 this.hasInput = this.component && this.inputEl().length;
213 if (typeof(this.minViewMode === 'string')) {
214 switch (this.minViewMode) {
216 this.minViewMode = 1;
219 this.minViewMode = 2;
222 this.minViewMode = 0;
227 if (typeof(this.viewMode === 'string')) {
228 switch (this.viewMode) {
241 this.pickerEl = Roo.get(document.body).createChild(Roo.bootstrap.form.DateField.template);
243 // this.el.select('>.input-group', true).first().createChild(Roo.bootstrap.form.DateField.template);
245 this.picker().setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
247 this.picker().on('mousedown', this.onMousedown, this);
248 this.picker().on('click', this.onClick, this);
250 this.picker().addClass('datepicker-dropdown');
252 this.startViewMode = this.viewMode;
255 Roo.each(this.picker().select('thead > tr > th', true).elements, function(v){
256 v.setVisibilityMode(Roo.Element.DISPLAY);
260 Roo.each(this.picker().select('tbody > tr > td', true).elements, function(v){
261 v.setStyle('width', '189px');
265 Roo.each(this.picker().select('tfoot th.today', true).elements, function(v){
266 v.dom.innerHTML = Roo.bootstrap.form.DateField.todayText;
270 this.weekEnd = this.weekStart === 0 ? 6 : this.weekStart - 1;
272 this.setStartDate(this.startDate);
273 this.setEndDate(this.endDate);
275 this.setDaysOfWeekDisabled(this.daysOfWeekDisabled);
286 this.hiddenField = this.inputEl().insertSibling(
287 {tag : 'input', type : 'hidden', name : this.name},
291 this.inputEl().dom.setAttribute('name', this.name + '____hidden___');
294 this.inputEl().dom.setAttribute('readOnly', true);
301 return this.pickerEl;
302 // return this.el.select('.datepicker', true).first();
307 var dowCnt = this.weekStart;
316 while (dowCnt < this.weekStart + 7) {
320 html: Roo.bootstrap.form.DateField.dates[this.language].daysMin[(dowCnt++)%7]
324 this.picker().select('>.datepicker-days thead', true).first().createChild(dow);
327 fillMonths: function()
330 var months = this.picker().select('>.datepicker-months td', true).first();
332 months.dom.innerHTML = '';
338 html: Roo.bootstrap.form.DateField.dates[this.language].monthsShort[i++]
341 months.createChild(month);
348 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;
350 if (this.date < this.startDate) {
351 this.viewDate = new Date(this.startDate);
352 } else if (this.date > this.endDate) {
353 this.viewDate = new Date(this.endDate);
355 this.viewDate = new Date(this.date);
363 var d = new Date(this.viewDate),
364 year = d.getUTCFullYear(),
365 month = d.getUTCMonth(),
366 startYear = this.startDate !== -Infinity ? this.startDate.getUTCFullYear() : -Infinity,
367 startMonth = this.startDate !== -Infinity ? this.startDate.getUTCMonth() : -Infinity,
368 endYear = this.endDate !== Infinity ? this.endDate.getUTCFullYear() : Infinity,
369 endMonth = this.endDate !== Infinity ? this.endDate.getUTCMonth() : Infinity,
370 currentDate = this.date && this.date.valueOf(),
371 today = this.UTCToday();
373 this.picker().select('>.datepicker-days thead th.switch', true).first().dom.innerHTML = Roo.bootstrap.form.DateField.dates[this.language].months[month]+' '+year;
375 this.updateNavArrows();
378 var prevMonth = this.UTCDate(year, month-1, 28,0,0,0,0),
380 day = prevMonth.getDaysInMonth(prevMonth.getUTCFullYear(), prevMonth.getUTCMonth());
382 prevMonth.setUTCDate(day);
384 prevMonth.setUTCDate(day - (prevMonth.getUTCDay() - this.weekStart + 7)%7);
386 var nextMonth = new Date(prevMonth);
388 nextMonth.setUTCDate(nextMonth.getUTCDate() + 42);
390 nextMonth = nextMonth.valueOf();
392 var fillMonths = false;
394 this.picker().select('>.datepicker-days tbody',true).first().dom.innerHTML = '';
396 while(prevMonth.valueOf() <= nextMonth) {
399 if (prevMonth.getUTCDay() === this.weekStart) {
401 this.picker().select('>.datepicker-days tbody',true).first().createChild(fillMonths);
410 if (prevMonth.getUTCFullYear() < year || (prevMonth.getUTCFullYear() == year && prevMonth.getUTCMonth() < month)) {
412 } else if (prevMonth.getUTCFullYear() > year || (prevMonth.getUTCFullYear() == year && prevMonth.getUTCMonth() > month)) {
415 if (this.todayHighlight &&
416 prevMonth.getUTCFullYear() == today.getFullYear() &&
417 prevMonth.getUTCMonth() == today.getMonth() &&
418 prevMonth.getUTCDate() == today.getDate()) {
422 if (currentDate && prevMonth.valueOf() === currentDate) {
423 clsName += ' active';
426 if (prevMonth.valueOf() < this.startDate || prevMonth.valueOf() > this.endDate ||
427 this.daysOfWeekDisabled.indexOf(prevMonth.getUTCDay()) !== -1) {
428 clsName += ' disabled';
433 cls: 'day ' + clsName,
434 html: prevMonth.getDate()
437 prevMonth.setDate(prevMonth.getDate()+1);
440 var currentYear = this.date && this.date.getUTCFullYear();
441 var currentMonth = this.date && this.date.getUTCMonth();
443 this.picker().select('>.datepicker-months th.switch',true).first().dom.innerHTML = year;
445 Roo.each(this.picker().select('>.datepicker-months tbody span',true).elements, function(v,k){
446 v.removeClass('active');
448 if(currentYear === year && k === currentMonth){
449 v.addClass('active');
452 if (year < startYear || year > endYear || (year == startYear && k < startMonth) || (year == endYear && k > endMonth)) {
453 v.addClass('disabled');
459 year = parseInt(year/10, 10) * 10;
461 this.picker().select('>.datepicker-years th.switch', true).first().dom.innerHTML = year + '-' + (year + 9);
463 this.picker().select('>.datepicker-years tbody td',true).first().dom.innerHTML = '';
466 for (var i = -1; i < 11; i++) {
467 this.picker().select('>.datepicker-years tbody td',true).first().createChild({
469 cls: 'year' + (i === -1 || i === 10 ? ' old' : '') + (currentYear === year ? ' active' : '') + (year < startYear || year > endYear ? ' disabled' : ''),
477 showMode: function(dir)
480 this.viewMode = Math.max(this.minViewMode, Math.min(2, this.viewMode + dir));
483 Roo.each(this.picker().select('>div',true).elements, function(v){
484 v.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
487 this.picker().select('>.datepicker-'+Roo.bootstrap.form.DateField.modes[this.viewMode].clsName, true).first().show();
496 this.picker().removeClass(['bottom', 'top']);
498 if((Roo.lib.Dom.getViewHeight() + Roo.get(document.body).getScroll().top) - (this.inputEl().getBottom() + this.picker().getHeight()) < 0){
500 * place to the top of element!
504 this.picker().addClass('top');
505 this.picker().setTop(this.inputEl().getTop() - this.picker().getHeight()).setLeft(this.inputEl().getLeft());
510 this.picker().addClass('bottom');
512 this.picker().setTop(this.inputEl().getBottom()).setLeft(this.inputEl().getLeft());
515 // return false when it fails
516 parseDate : function(value)
521 if(value instanceof Date){
524 var v = Date.parseDate(value, 'Y-m-d');
526 return (typeof(v) == 'undefined') ? false : v;
529 formatDate : function(date, fmt)
531 return (!date || !(date instanceof Date)) ?
532 date : date.dateFormat(fmt || this.format);
535 translateDate : function(date)
537 switch(this.language) {
539 return new Intl.DateTimeFormat('zh-CN', {
545 return this.formatDate(date);
551 Roo.bootstrap.form.DateField.superclass.onFocus.call(this);
557 Roo.bootstrap.form.DateField.superclass.onBlur.call(this);
560 var d = this.inputEl().getValue();
561 var date = this.parseDate(d);
566 this.setValue(this.getValue());
573 showPopup : function()
578 this.picker().show();
582 this.fireEvent('showpopup', this, this.date);
585 hidePopup : function()
590 this.picker().hide();
591 this.viewMode = this.startViewMode;
594 this.inputEl().blur();
596 this.fireEvent('hidepopup', this, this.date);
600 onMousedown: function(e)
608 Roo.bootstrap.form.DateField.superclass.keyup.call(this);
612 setValue: function(v)
614 if(this.fireEvent('beforeselect', this, v) !== false){
615 var d = this.parseDate(v);
618 this.date = this.viewDate = this.value = this.hiddenField.value = '';
620 this.inputEl().dom.value = '';
626 d = new Date(d).clearTime();
628 this.value = this.hiddenField.value = d.dateFormat('Y-m-d');
630 v = this.translateDate(d);
632 this.inputEl().dom.value = (v === null || v === undefined ? '' : v);
636 this.date = new Date(d.getTime() - d.getTimezoneOffset()*60000);
640 this.fireEvent('select', this, this.date);
645 setRawValue : function(v){
646 if(this.fireEvent('beforeselect', this, v) !== false){
647 var d = this.parseDate(v);
650 this.date = this.viewDate = this.value = this.hiddenField.value = '';
652 this.inputEl().dom.value = (v === null || v === undefined ? '' : v);
657 d = new Date(d).clearTime();
659 this.value = this.hiddenField.value = d.dateFormat('Y-m-d');
661 v = this.translateDate(d);
663 this.inputEl().dom.value = (v === null || v === undefined ? '' : v);
666 this.date = new Date(d.getTime() - d.getTimezoneOffset()*60000);
670 this.fireEvent('select', this, this.date);
679 getRawValue : function(){
680 return this.getValue();
685 if (!this.picker().isVisible()){
686 if (e.keyCode == 27) { // allow escape to hide and re-show picker
692 var dateChanged = false,
694 newDate, newViewDate;
703 if (!this.keyboardNavigation) {
706 dir = e.keyCode == 37 ? -1 : 1;
709 newDate = this.moveYear(this.date, dir);
710 newViewDate = this.moveYear(this.viewDate, dir);
711 } else if (e.shiftKey){
712 newDate = this.moveMonth(this.date, dir);
713 newViewDate = this.moveMonth(this.viewDate, dir);
715 newDate = new Date(this.date);
716 newDate.setUTCDate(this.date.getUTCDate() + dir);
717 newViewDate = new Date(this.viewDate);
718 newViewDate.setUTCDate(this.viewDate.getUTCDate() + dir);
720 if (this.dateWithinRange(newDate)){
722 this.viewDate = newViewDate;
723 this.setValue(this.date);
731 if (!this.keyboardNavigation) {
734 dir = e.keyCode == 38 ? -1 : 1;
736 newDate = this.moveYear(this.date, dir);
737 newViewDate = this.moveYear(this.viewDate, dir);
738 } else if (e.shiftKey){
739 newDate = this.moveMonth(this.date, dir);
740 newViewDate = this.moveMonth(this.viewDate, dir);
742 newDate = new Date(this.date);
743 newDate.setUTCDate(this.date.getUTCDate() + dir * 7);
744 newViewDate = new Date(this.viewDate);
745 newViewDate.setUTCDate(this.viewDate.getUTCDate() + dir * 7);
747 if (this.dateWithinRange(newDate)){
749 this.viewDate = newViewDate;
750 this.setValue(this.date);
757 this.setValue(this.date);
762 this.setValue(this.date);
781 var target = e.getTarget();
783 if(target.nodeName.toLowerCase() === 'i'){
784 target = Roo.get(target).dom.parentNode;
787 var nodeName = target.nodeName;
788 var className = target.className;
789 var html = target.innerHTML;
792 switch(nodeName.toLowerCase()) {
800 var dir = Roo.bootstrap.form.DateField.modes[this.viewMode].navStep * (className == 'prev' ? -1 : 1);
801 switch(this.viewMode){
803 this.viewDate = this.moveMonth(this.viewDate, dir);
807 this.viewDate = this.moveYear(this.viewDate, dir);
813 var date = new Date();
814 this.date = this.UTCDate(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0);
816 this.setValue(this.date);
823 if (className.indexOf('disabled') < 0) {
824 if (!this.viewDate) {
825 this.viewDate = new Date();
827 this.viewDate.setUTCDate(1);
828 if (className.indexOf('month') > -1) {
829 this.viewDate.setUTCMonth(Roo.bootstrap.form.DateField.dates[this.language].monthsShort.indexOf(html));
831 var year = parseInt(html, 10) || 0;
832 this.viewDate.setUTCFullYear(year);
837 this.setValue(this.viewDate);
848 //Roo.log(className);
849 if (className.indexOf('day') > -1 && className.indexOf('disabled') < 0 ){
850 var day = parseInt(html, 10) || 1;
851 var year = (this.viewDate || new Date()).getUTCFullYear(),
852 month = (this.viewDate || new Date()).getUTCMonth();
854 if (className.indexOf('old') > -1) {
861 } else if (className.indexOf('new') > -1) {
869 //Roo.log([year,month,day]);
870 this.date = this.UTCDate(year, month, day,0,0,0,0);
871 this.viewDate = this.UTCDate(year, month, Math.min(28, day),0,0,0,0);
873 this.setValue(this.date);
880 setStartDate: function(startDate)
882 this.startDate = startDate || -Infinity;
883 if (this.startDate !== -Infinity) {
884 var date = this.parseDate(this.startDate);
885 this.startDate = date ? date : -Infinity;
888 this.updateNavArrows();
891 setEndDate: function(endDate)
893 this.endDate = endDate || Infinity;
894 if (this.endDate !== Infinity) {
895 var date = this.parseDate(this.endDate);
896 this.endDate = date ? date : Infinity;
899 this.updateNavArrows();
902 setDaysOfWeekDisabled: function(daysOfWeekDisabled)
904 this.daysOfWeekDisabled = daysOfWeekDisabled || [];
905 if (typeof(this.daysOfWeekDisabled) !== 'object') {
906 this.daysOfWeekDisabled = this.daysOfWeekDisabled.split(/,\s*/);
908 this.daysOfWeekDisabled = this.daysOfWeekDisabled.map(function (d) {
909 return parseInt(d, 10);
912 this.updateNavArrows();
915 updateNavArrows: function()
921 var d = new Date(this.viewDate),
922 year = d.getUTCFullYear(),
923 month = d.getUTCMonth();
925 Roo.each(this.picker().select('.prev', true).elements, function(v){
927 switch (this.viewMode) {
930 if (this.startDate !== -Infinity && year <= this.startDate.getUTCFullYear() && month <= this.startDate.getUTCMonth()) {
936 if (this.startDate !== -Infinity && year <= this.startDate.getUTCFullYear()) {
943 Roo.each(this.picker().select('.next', true).elements, function(v){
945 switch (this.viewMode) {
948 if (this.endDate !== Infinity && year >= this.endDate.getUTCFullYear() && month >= this.endDate.getUTCMonth()) {
954 if (this.endDate !== Infinity && year >= this.endDate.getUTCFullYear()) {
962 moveMonth: function(date, dir)
967 var new_date = new Date(date.valueOf()),
968 day = new_date.getUTCDate(),
969 month = new_date.getUTCMonth(),
972 dir = dir > 0 ? 1 : -1;
975 // If going back one month, make sure month is not current month
976 // (eg, Mar 31 -> Feb 31 == Feb 28, not Mar 02)
978 return new_date.getUTCMonth() == month;
980 // If going forward one month, make sure month is as expected
981 // (eg, Jan 31 -> Feb 31 == Feb 28, not Mar 02)
983 return new_date.getUTCMonth() != new_month;
985 new_month = month + dir;
986 new_date.setUTCMonth(new_month);
987 // Dec -> Jan (12) or Jan -> Dec (-1) -- limit expected date to 0-11
988 if (new_month < 0 || new_month > 11) {
989 new_month = (new_month + 12) % 12;
992 // For magnitudes >1, move one month at a time...
993 for (var i=0; i<mag; i++) {
994 // ...which might decrease the day (eg, Jan 31 to Feb 28, etc)...
995 new_date = this.moveMonth(new_date, dir);
997 // ...then reset the day, keeping it in the new month
998 new_month = new_date.getUTCMonth();
999 new_date.setUTCDate(day);
1001 return new_month != new_date.getUTCMonth();
1004 // Common date-resetting loop -- if date is beyond end of month, make it
1007 new_date.setUTCDate(--day);
1008 new_date.setUTCMonth(new_month);
1013 moveYear: function(date, dir)
1015 return this.moveMonth(date, dir*12);
1018 dateWithinRange: function(date)
1020 return date >= this.startDate && date <= this.endDate;
1026 this.picker().remove();
1029 validateValue : function(value)
1031 if(this.getVisibilityEl().hasClass('hidden')){
1035 if(value.length < 1) {
1036 if(this.allowBlank){
1042 if(value.length < this.minLength){
1045 if(value.length > this.maxLength){
1049 var vt = Roo.form.VTypes;
1050 if(!vt[this.vtype](value, this)){
1054 if(typeof this.validator == "function"){
1055 var msg = this.validator(value);
1061 if(this.regex && !this.regex.test(value)){
1065 if(!this.parseDate(value)){
1069 if (this.endDate !== Infinity && this.parseDate(value).getTime() > this.endDate.getTime()) {
1073 if (this.startDate !== -Infinity && this.parseDate(value).getTime() < this.startDate.getTime()) {
1083 this.date = this.viewDate = '';
1085 Roo.bootstrap.form.DateField.superclass.setValue.call(this, '');
1090 Roo.apply(Roo.bootstrap.form.DateField, {
1101 html: '<i class="fa fa-arrow-left"/>'
1111 html: '<i class="fa fa-arrow-right"/>'
1153 todayText : "Today",
1173 Roo.apply(Roo.bootstrap.form.DateField, {
1177 cls: 'datepicker dropdown-menu roo-dynamic shadow',
1181 cls: 'datepicker-days',
1185 cls: 'table-condensed',
1187 Roo.bootstrap.form.DateField.head,
1191 Roo.bootstrap.form.DateField.footer
1198 cls: 'datepicker-months',
1202 cls: 'table-condensed',
1204 Roo.bootstrap.form.DateField.head,
1205 Roo.bootstrap.form.DateField.content,
1206 Roo.bootstrap.form.DateField.footer
1213 cls: 'datepicker-years',
1217 cls: 'table-condensed',
1219 Roo.bootstrap.form.DateField.head,
1220 Roo.bootstrap.form.DateField.content,
1221 Roo.bootstrap.form.DateField.footer