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
57 * Fires when before select a date.
58 * @param {Roo.bootstrap.DateField} this
59 * @param {Mixed} date The date value
65 Roo.extend(Roo.bootstrap.DateField, Roo.bootstrap.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').
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').
78 altFormats : "m/d/Y|m-d-y|m-d-Y|m/d|m-d|md|mdy|mdY|d",
86 todayHighlight : false,
92 keyboardNavigation: true,
100 daysOfWeekDisabled: [],
108 return new Date(Date.UTC.apply(Date, arguments));
113 var today = new Date();
114 return this.UTCDate(today.getUTCFullYear(), today.getUTCMonth(), today.getUTCDate());
117 getDate: function() {
118 var d = this.getUTCDate();
119 return new Date(d.getTime() + (d.getTimezoneOffset()*60000));
122 getUTCDate: function() {
126 setDate: function(d) {
127 this.setUTCDate(new Date(d.getTime() - (d.getTimezoneOffset()*60000)));
130 setUTCDate: function(d) {
132 this.setValue(this.formatDate(this.date));
135 onRender: function(ct, position)
138 Roo.bootstrap.DateField.superclass.onRender.call(this, ct, position);
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";
144 this.isRTL = Roo.bootstrap.DateField.dates[this.language].rtl || false;
145 this.format = this.format || 'm/d/y';
146 this.isInline = false;
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;
152 if (typeof(this.minViewMode === 'string')) {
153 switch (this.minViewMode) {
155 this.minViewMode = 1;
158 this.minViewMode = 2;
161 this.minViewMode = 0;
166 if (typeof(this.viewMode === 'string')) {
167 switch (this.viewMode) {
180 this.pickerEl = Roo.get(document.body).createChild(Roo.bootstrap.DateField.template);
182 // this.el.select('>.input-group', true).first().createChild(Roo.bootstrap.DateField.template);
184 this.picker().setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
186 this.picker().on('mousedown', this.onMousedown, this);
187 this.picker().on('click', this.onClick, this);
189 this.picker().addClass('datepicker-dropdown');
191 this.startViewMode = this.viewMode;
194 Roo.each(this.picker().select('thead > tr > th', true).elements, function(v){
195 v.setVisibilityMode(Roo.Element.DISPLAY);
199 Roo.each(this.picker().select('tbody > tr > td', true).elements, function(v){
200 v.setStyle('width', '189px');
204 Roo.each(this.picker().select('tfoot th.today', true).elements, function(v){
205 if(!this.calendarWeeks){
210 v.dom.innerHTML = Roo.bootstrap.DateField.dates[this.language].today;
211 v.attr('colspan', function(i, val){
212 return parseInt(val) + 1;
217 this.weekEnd = this.weekStart === 0 ? 6 : this.weekStart - 1;
219 this.setStartDate(this.startDate);
220 this.setEndDate(this.endDate);
222 this.setDaysOfWeekDisabled(this.daysOfWeekDisabled);
236 return this.pickerEl;
237 // return this.el.select('.datepicker', true).first();
242 var dowCnt = this.weekStart;
251 if(this.calendarWeeks){
259 while (dowCnt < this.weekStart + 7) {
263 html: Roo.bootstrap.DateField.dates[this.language].daysMin[(dowCnt++)%7]
267 this.picker().select('>.datepicker-days thead', true).first().createChild(dow);
270 fillMonths: function()
273 var months = this.picker().select('>.datepicker-months td', true).first();
275 months.dom.innerHTML = '';
281 html: Roo.bootstrap.DateField.dates[this.language].monthsShort[i++]
284 months.createChild(month);
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;
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);
298 this.viewDate = new Date(this.date);
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();
316 this.picker().select('>.datepicker-days thead th.switch', true).first().dom.innerHTML = Roo.bootstrap.DateField.dates[this.language].months[month]+' '+year;
318 // this.picker().select('>tfoot th.today', true).first().dom.innerHTML = Roo.bootstrap.DateField.dates[this.language].today;
320 // this.picker.select('>tfoot th.today').
321 // .text(dates[this.language].today)
322 // .toggle(this.todayBtn !== false);
324 this.updateNavArrows();
327 var prevMonth = this.UTCDate(year, month-1, 28,0,0,0,0),
329 day = prevMonth.getDaysInMonth(prevMonth.getUTCFullYear(), prevMonth.getUTCMonth());
331 prevMonth.setUTCDate(day);
333 prevMonth.setUTCDate(day - (prevMonth.getUTCDay() - this.weekStart + 7)%7);
335 var nextMonth = new Date(prevMonth);
337 nextMonth.setUTCDate(nextMonth.getUTCDate() + 42);
339 nextMonth = nextMonth.valueOf();
341 var fillMonths = false;
343 this.picker().select('>.datepicker-days tbody',true).first().dom.innerHTML = '';
345 while(prevMonth.valueOf() <= nextMonth) {
348 if (prevMonth.getUTCDay() === this.weekStart) {
350 this.picker().select('>.datepicker-days tbody',true).first().createChild(fillMonths);
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.
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;
379 if (prevMonth.getUTCFullYear() < year || (prevMonth.getUTCFullYear() == year && prevMonth.getUTCMonth() < month)) {
381 } else if (prevMonth.getUTCFullYear() > year || (prevMonth.getUTCFullYear() == year && prevMonth.getUTCMonth() > month)) {
384 if (this.todayHighlight &&
385 prevMonth.getUTCFullYear() == today.getFullYear() &&
386 prevMonth.getUTCMonth() == today.getMonth() &&
387 prevMonth.getUTCDate() == today.getDate()) {
391 if (currentDate && prevMonth.valueOf() === currentDate) {
392 clsName += ' active';
395 if (prevMonth.valueOf() < this.startDate || prevMonth.valueOf() > this.endDate ||
396 this.daysOfWeekDisabled.indexOf(prevMonth.getUTCDay()) !== -1) {
397 clsName += ' disabled';
402 cls: 'day ' + clsName,
403 html: prevMonth.getDate()
406 prevMonth.setDate(prevMonth.getDate()+1);
409 var currentYear = this.date && this.date.getUTCFullYear();
410 var currentMonth = this.date && this.date.getUTCMonth();
412 this.picker().select('>.datepicker-months th.switch',true).first().dom.innerHTML = year;
414 Roo.each(this.picker().select('>.datepicker-months tbody span',true).elements, function(v,k){
415 v.removeClass('active');
417 if(currentYear === year && k === currentMonth){
418 v.addClass('active');
421 if (year < startYear || year > endYear || (year == startYear && k < startMonth) || (year == endYear && k > endMonth)) {
422 v.addClass('disabled');
428 year = parseInt(year/10, 10) * 10;
430 this.picker().select('>.datepicker-years th.switch', true).first().dom.innerHTML = year + '-' + (year + 9);
432 this.picker().select('>.datepicker-years tbody td',true).first().dom.innerHTML = '';
435 for (var i = -1; i < 11; i++) {
436 this.picker().select('>.datepicker-years tbody td',true).first().createChild({
438 cls: 'year' + (i === -1 || i === 10 ? ' old' : '') + (currentYear === year ? ' active' : '') + (year < startYear || year > endYear ? ' disabled' : ''),
446 showMode: function(dir)
449 this.viewMode = Math.max(this.minViewMode, Math.min(2, this.viewMode + dir));
452 Roo.each(this.picker().select('>div',true).elements, function(v){
453 v.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
456 this.picker().select('>.datepicker-'+Roo.bootstrap.DateField.modes[this.viewMode].clsName, true).first().show();
465 this.picker().removeClass(['bottom', 'top']);
467 if((Roo.lib.Dom.getViewHeight() + Roo.get(document.body).getScroll().top) - (this.inputEl().getBottom() + this.picker().getHeight()) < 0){
469 * place to the top of element!
473 this.picker().addClass('top');
474 this.picker().setTop(this.inputEl().getTop() - this.picker().getHeight()).setLeft(this.inputEl().getLeft());
479 this.picker().addClass('bottom');
481 this.picker().setTop(this.inputEl().getBottom()).setLeft(this.inputEl().getLeft());
484 parseDate : function(value)
486 if(!value || value instanceof Date){
489 var v = Date.parseDate(value, this.format);
490 if (!v && (this.useIso || value.match(/^(\d{4})-0?(\d+)-0?(\d+)/))) {
491 v = Date.parseDate(value, 'Y-m-d');
493 if(!v && this.altFormats){
494 if(!this.altFormatsArray){
495 this.altFormatsArray = this.altFormats.split("|");
497 for(var i = 0, len = this.altFormatsArray.length; i < len && !v; i++){
498 v = Date.parseDate(value, this.altFormatsArray[i]);
504 formatDate : function(date, fmt)
506 return (!date || !(date instanceof Date)) ?
507 date : date.dateFormat(fmt || this.format);
512 Roo.bootstrap.DateField.superclass.onFocus.call(this);
518 Roo.bootstrap.DateField.superclass.onBlur.call(this);
520 var d = this.inputEl().getValue();
527 showPopup : function()
529 this.picker().show();
533 this.fireEvent('showpopup', this, this.date);
536 hidePopup : function()
541 this.picker().hide();
542 this.viewMode = this.startViewMode;
545 this.fireEvent('hidepopup', this, this.date);
549 onMousedown: function(e)
557 Roo.bootstrap.DateField.superclass.keyup.call(this);
561 setValue: function(v)
563 if(this.fireEvent('beforeselect', this, v) !== false){
564 var d = new Date(this.parseDate(v) ).clearTime();
566 if(isNaN(d.getTime())){
567 this.date = this.viewDate = '';
568 Roo.bootstrap.DateField.superclass.setValue.call(this, '');
572 v = this.formatDate(d);
574 Roo.bootstrap.DateField.superclass.setValue.call(this, v);
576 this.date = new Date(d.getTime() - d.getTimezoneOffset()*60000);
580 this.fireEvent('select', this, this.date);
586 return this.formatDate(this.date);
591 if (!this.picker().isVisible()){
592 if (e.keyCode == 27) { // allow escape to hide and re-show picker
598 var dateChanged = false,
600 newDate, newViewDate;
609 if (!this.keyboardNavigation) {
612 dir = e.keyCode == 37 ? -1 : 1;
615 newDate = this.moveYear(this.date, dir);
616 newViewDate = this.moveYear(this.viewDate, dir);
617 } else if (e.shiftKey){
618 newDate = this.moveMonth(this.date, dir);
619 newViewDate = this.moveMonth(this.viewDate, dir);
621 newDate = new Date(this.date);
622 newDate.setUTCDate(this.date.getUTCDate() + dir);
623 newViewDate = new Date(this.viewDate);
624 newViewDate.setUTCDate(this.viewDate.getUTCDate() + dir);
626 if (this.dateWithinRange(newDate)){
628 this.viewDate = newViewDate;
629 this.setValue(this.formatDate(this.date));
637 if (!this.keyboardNavigation) {
640 dir = e.keyCode == 38 ? -1 : 1;
642 newDate = this.moveYear(this.date, dir);
643 newViewDate = this.moveYear(this.viewDate, dir);
644 } else if (e.shiftKey){
645 newDate = this.moveMonth(this.date, dir);
646 newViewDate = this.moveMonth(this.viewDate, dir);
648 newDate = new Date(this.date);
649 newDate.setUTCDate(this.date.getUTCDate() + dir * 7);
650 newViewDate = new Date(this.viewDate);
651 newViewDate.setUTCDate(this.viewDate.getUTCDate() + dir * 7);
653 if (this.dateWithinRange(newDate)){
655 this.viewDate = newViewDate;
656 this.setValue(this.formatDate(this.date));
663 this.setValue(this.formatDate(this.date));
668 this.setValue(this.formatDate(this.date));
687 var target = e.getTarget();
689 if(target.nodeName.toLowerCase() === 'i'){
690 target = Roo.get(target).dom.parentNode;
693 var nodeName = target.nodeName;
694 var className = target.className;
695 var html = target.innerHTML;
698 switch(nodeName.toLowerCase()) {
706 var dir = Roo.bootstrap.DateField.modes[this.viewMode].navStep * (className == 'prev' ? -1 : 1);
707 switch(this.viewMode){
709 this.viewDate = this.moveMonth(this.viewDate, dir);
713 this.viewDate = this.moveYear(this.viewDate, dir);
719 var date = new Date();
720 this.date = this.UTCDate(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0);
722 this.setValue(this.formatDate(this.date));
729 if (className.indexOf('disabled') < 0) {
730 if (!this.viewDate) {
731 this.viewDate = new Date();
733 this.viewDate.setUTCDate(1);
734 if (className.indexOf('month') > -1) {
735 this.viewDate.setUTCMonth(Roo.bootstrap.DateField.dates[this.language].monthsShort.indexOf(html));
737 var year = parseInt(html, 10) || 0;
738 this.viewDate.setUTCFullYear(year);
743 this.setValue(this.formatDate(this.viewDate));
754 //Roo.log(className);
755 if (className.indexOf('day') > -1 && className.indexOf('disabled') < 0 ){
756 var day = parseInt(html, 10) || 1;
757 var year = (this.viewDate || new Date()).getUTCFullYear(),
758 month = (this.viewDate || new Date()).getUTCMonth();
760 if (className.indexOf('old') > -1) {
767 } else if (className.indexOf('new') > -1) {
775 //Roo.log([year,month,day]);
776 this.date = this.UTCDate(year, month, day,0,0,0,0);
777 this.viewDate = this.UTCDate(year, month, Math.min(28, day),0,0,0,0);
779 //Roo.log(this.formatDate(this.date));
780 this.setValue(this.formatDate(this.date));
787 setStartDate: function(startDate)
789 this.startDate = startDate || -Infinity;
790 if (this.startDate !== -Infinity) {
791 this.startDate = this.parseDate(this.startDate);
794 this.updateNavArrows();
797 setEndDate: function(endDate)
799 this.endDate = endDate || Infinity;
800 if (this.endDate !== Infinity) {
801 this.endDate = this.parseDate(this.endDate);
804 this.updateNavArrows();
807 setDaysOfWeekDisabled: function(daysOfWeekDisabled)
809 this.daysOfWeekDisabled = daysOfWeekDisabled || [];
810 if (typeof(this.daysOfWeekDisabled) !== 'object') {
811 this.daysOfWeekDisabled = this.daysOfWeekDisabled.split(/,\s*/);
813 this.daysOfWeekDisabled = this.daysOfWeekDisabled.map(function (d) {
814 return parseInt(d, 10);
817 this.updateNavArrows();
820 updateNavArrows: function()
826 var d = new Date(this.viewDate),
827 year = d.getUTCFullYear(),
828 month = d.getUTCMonth();
830 Roo.each(this.picker().select('.prev', true).elements, function(v){
832 switch (this.viewMode) {
835 if (this.startDate !== -Infinity && year <= this.startDate.getUTCFullYear() && month <= this.startDate.getUTCMonth()) {
841 if (this.startDate !== -Infinity && year <= this.startDate.getUTCFullYear()) {
848 Roo.each(this.picker().select('.next', true).elements, function(v){
850 switch (this.viewMode) {
853 if (this.endDate !== Infinity && year >= this.endDate.getUTCFullYear() && month >= this.endDate.getUTCMonth()) {
859 if (this.endDate !== Infinity && year >= this.endDate.getUTCFullYear()) {
867 moveMonth: function(date, dir)
872 var new_date = new Date(date.valueOf()),
873 day = new_date.getUTCDate(),
874 month = new_date.getUTCMonth(),
877 dir = dir > 0 ? 1 : -1;
880 // If going back one month, make sure month is not current month
881 // (eg, Mar 31 -> Feb 31 == Feb 28, not Mar 02)
883 return new_date.getUTCMonth() == month;
885 // If going forward one month, make sure month is as expected
886 // (eg, Jan 31 -> Feb 31 == Feb 28, not Mar 02)
888 return new_date.getUTCMonth() != new_month;
890 new_month = month + dir;
891 new_date.setUTCMonth(new_month);
892 // Dec -> Jan (12) or Jan -> Dec (-1) -- limit expected date to 0-11
893 if (new_month < 0 || new_month > 11) {
894 new_month = (new_month + 12) % 12;
897 // For magnitudes >1, move one month at a time...
898 for (var i=0; i<mag; i++) {
899 // ...which might decrease the day (eg, Jan 31 to Feb 28, etc)...
900 new_date = this.moveMonth(new_date, dir);
902 // ...then reset the day, keeping it in the new month
903 new_month = new_date.getUTCMonth();
904 new_date.setUTCDate(day);
906 return new_month != new_date.getUTCMonth();
909 // Common date-resetting loop -- if date is beyond end of month, make it
912 new_date.setUTCDate(--day);
913 new_date.setUTCMonth(new_month);
918 moveYear: function(date, dir)
920 return this.moveMonth(date, dir*12);
923 dateWithinRange: function(date)
925 return date >= this.startDate && date <= this.endDate;
931 this.picker().remove();
934 validateValue : function(value)
936 if(this.getVisibilityEl().hasClass('hidden')){
940 if(value.length < 1) {
947 if(value.length < this.minLength){
950 if(value.length > this.maxLength){
954 var vt = Roo.form.VTypes;
955 if(!vt[this.vtype](value, this)){
959 if(typeof this.validator == "function"){
960 var msg = this.validator(value);
966 if(this.regex && !this.regex.test(value)){
970 if(typeof(this.parseDate(value)) == 'undefined'){
974 if (this.endDate !== Infinity && this.parseDate(value).getTime() > this.endDate.getTime()) {
978 if (this.startDate !== -Infinity && this.parseDate(value).getTime() < this.startDate.getTime()) {
988 this.date = this.viewDate = '';
990 Roo.bootstrap.DateField.superclass.setValue.call(this, '');
995 Roo.apply(Roo.bootstrap.DateField, {
1006 html: '<i class="fa fa-arrow-left"/>'
1016 html: '<i class="fa fa-arrow-right"/>'
1058 days: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"],
1059 daysShort: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],
1060 daysMin: ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa", "Su"],
1061 months: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
1062 monthsShort: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
1085 Roo.apply(Roo.bootstrap.DateField, {
1089 cls: 'datepicker dropdown-menu roo-dynamic shadow',
1093 cls: 'datepicker-days',
1097 cls: 'table-condensed',
1099 Roo.bootstrap.DateField.head,
1103 Roo.bootstrap.DateField.footer
1110 cls: 'datepicker-months',
1114 cls: 'table-condensed',
1116 Roo.bootstrap.DateField.head,
1117 Roo.bootstrap.DateField.content,
1118 Roo.bootstrap.DateField.footer
1125 cls: 'datepicker-years',
1129 cls: 'table-condensed',
1131 Roo.bootstrap.DateField.head,
1132 Roo.bootstrap.DateField.content,
1133 Roo.bootstrap.DateField.footer