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){
454 v.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
457 this.picker().select('>.datepicker-'+Roo.bootstrap.DateField.modes[this.viewMode].clsName, true).first().show();
466 this.picker().removeClass(['bottom', 'top']);
468 if((Roo.lib.Dom.getViewHeight() + Roo.get(document.body).getScroll().top) - (this.inputEl().getBottom() + this.picker().getHeight()) < 0){
470 * place to the top of element!
474 this.picker().addClass('top');
475 this.picker().setTop(this.inputEl().getTop() - this.picker().getHeight()).setLeft(this.inputEl().getLeft());
480 this.picker().addClass('bottom');
482 this.picker().setTop(this.inputEl().getBottom()).setLeft(this.inputEl().getLeft());
485 parseDate : function(value)
487 if(!value || value instanceof Date){
490 var v = Date.parseDate(value, this.format);
491 if (!v && (this.useIso || value.match(/^(\d{4})-0?(\d+)-0?(\d+)/))) {
492 v = Date.parseDate(value, 'Y-m-d');
494 if(!v && this.altFormats){
495 if(!this.altFormatsArray){
496 this.altFormatsArray = this.altFormats.split("|");
498 for(var i = 0, len = this.altFormatsArray.length; i < len && !v; i++){
499 v = Date.parseDate(value, this.altFormatsArray[i]);
505 formatDate : function(date, fmt)
507 return (!date || !(date instanceof Date)) ?
508 date : date.dateFormat(fmt || this.format);
513 Roo.bootstrap.DateField.superclass.onFocus.call(this);
519 Roo.bootstrap.DateField.superclass.onBlur.call(this);
521 var d = this.inputEl().getValue();
528 showPopup : function()
530 this.picker().show();
534 this.fireEvent('showpopup', this, this.date);
537 hidePopup : function()
542 this.picker().hide();
543 this.viewMode = this.startViewMode;
546 this.fireEvent('hidepopup', this, this.date);
550 onMousedown: function(e)
558 Roo.bootstrap.DateField.superclass.keyup.call(this);
562 setValue: function(v)
564 if(this.fireEvent('beforeselect', this, v) !== false){
565 var d = new Date(this.parseDate(v) ).clearTime();
567 if(isNaN(d.getTime())){
568 this.date = this.viewDate = '';
569 Roo.bootstrap.DateField.superclass.setValue.call(this, '');
573 v = this.formatDate(d);
575 Roo.bootstrap.DateField.superclass.setValue.call(this, v);
577 this.date = new Date(d.getTime() - d.getTimezoneOffset()*60000);
581 this.fireEvent('select', this, this.date);
587 return this.formatDate(this.date);
592 if (!this.picker().isVisible()){
593 if (e.keyCode == 27) { // allow escape to hide and re-show picker
599 var dateChanged = false,
601 newDate, newViewDate;
610 if (!this.keyboardNavigation) {
613 dir = e.keyCode == 37 ? -1 : 1;
616 newDate = this.moveYear(this.date, dir);
617 newViewDate = this.moveYear(this.viewDate, dir);
618 } else if (e.shiftKey){
619 newDate = this.moveMonth(this.date, dir);
620 newViewDate = this.moveMonth(this.viewDate, dir);
622 newDate = new Date(this.date);
623 newDate.setUTCDate(this.date.getUTCDate() + dir);
624 newViewDate = new Date(this.viewDate);
625 newViewDate.setUTCDate(this.viewDate.getUTCDate() + dir);
627 if (this.dateWithinRange(newDate)){
629 this.viewDate = newViewDate;
630 this.setValue(this.formatDate(this.date));
638 if (!this.keyboardNavigation) {
641 dir = e.keyCode == 38 ? -1 : 1;
643 newDate = this.moveYear(this.date, dir);
644 newViewDate = this.moveYear(this.viewDate, dir);
645 } else if (e.shiftKey){
646 newDate = this.moveMonth(this.date, dir);
647 newViewDate = this.moveMonth(this.viewDate, dir);
649 newDate = new Date(this.date);
650 newDate.setUTCDate(this.date.getUTCDate() + dir * 7);
651 newViewDate = new Date(this.viewDate);
652 newViewDate.setUTCDate(this.viewDate.getUTCDate() + dir * 7);
654 if (this.dateWithinRange(newDate)){
656 this.viewDate = newViewDate;
657 this.setValue(this.formatDate(this.date));
664 this.setValue(this.formatDate(this.date));
669 this.setValue(this.formatDate(this.date));
688 var target = e.getTarget();
690 if(target.nodeName.toLowerCase() === 'i'){
691 target = Roo.get(target).dom.parentNode;
694 var nodeName = target.nodeName;
695 var className = target.className;
696 var html = target.innerHTML;
699 switch(nodeName.toLowerCase()) {
707 var dir = Roo.bootstrap.DateField.modes[this.viewMode].navStep * (className == 'prev' ? -1 : 1);
708 switch(this.viewMode){
710 this.viewDate = this.moveMonth(this.viewDate, dir);
714 this.viewDate = this.moveYear(this.viewDate, dir);
720 var date = new Date();
721 this.date = this.UTCDate(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0);
723 this.setValue(this.formatDate(this.date));
730 if (className.indexOf('disabled') < 0) {
731 this.viewDate.setUTCDate(1);
732 if (className.indexOf('month') > -1) {
733 this.viewDate.setUTCMonth(Roo.bootstrap.DateField.dates[this.language].monthsShort.indexOf(html));
735 var year = parseInt(html, 10) || 0;
736 this.viewDate.setUTCFullYear(year);
741 this.setValue(this.formatDate(this.viewDate));
752 //Roo.log(className);
753 if (className.indexOf('day') > -1 && className.indexOf('disabled') < 0 ){
754 var day = parseInt(html, 10) || 1;
755 var year = this.viewDate.getUTCFullYear(),
756 month = this.viewDate.getUTCMonth();
758 if (className.indexOf('old') > -1) {
765 } else if (className.indexOf('new') > -1) {
773 //Roo.log([year,month,day]);
774 this.date = this.UTCDate(year, month, day,0,0,0,0);
775 this.viewDate = this.UTCDate(year, month, Math.min(28, day),0,0,0,0);
777 //Roo.log(this.formatDate(this.date));
778 this.setValue(this.formatDate(this.date));
785 setStartDate: function(startDate)
787 this.startDate = startDate || -Infinity;
788 if (this.startDate !== -Infinity) {
789 this.startDate = this.parseDate(this.startDate);
792 this.updateNavArrows();
795 setEndDate: function(endDate)
797 this.endDate = endDate || Infinity;
798 if (this.endDate !== Infinity) {
799 this.endDate = this.parseDate(this.endDate);
802 this.updateNavArrows();
805 setDaysOfWeekDisabled: function(daysOfWeekDisabled)
807 this.daysOfWeekDisabled = daysOfWeekDisabled || [];
808 if (typeof(this.daysOfWeekDisabled) !== 'object') {
809 this.daysOfWeekDisabled = this.daysOfWeekDisabled.split(/,\s*/);
811 this.daysOfWeekDisabled = this.daysOfWeekDisabled.map(function (d) {
812 return parseInt(d, 10);
815 this.updateNavArrows();
818 updateNavArrows: function()
824 var d = new Date(this.viewDate),
825 year = d.getUTCFullYear(),
826 month = d.getUTCMonth();
828 Roo.each(this.picker().select('.prev', true).elements, function(v){
830 switch (this.viewMode) {
833 if (this.startDate !== -Infinity && year <= this.startDate.getUTCFullYear() && month <= this.startDate.getUTCMonth()) {
839 if (this.startDate !== -Infinity && year <= this.startDate.getUTCFullYear()) {
846 Roo.each(this.picker().select('.next', true).elements, function(v){
848 switch (this.viewMode) {
851 if (this.endDate !== Infinity && year >= this.endDate.getUTCFullYear() && month >= this.endDate.getUTCMonth()) {
857 if (this.endDate !== Infinity && year >= this.endDate.getUTCFullYear()) {
865 moveMonth: function(date, dir)
870 var new_date = new Date(date.valueOf()),
871 day = new_date.getUTCDate(),
872 month = new_date.getUTCMonth(),
875 dir = dir > 0 ? 1 : -1;
878 // If going back one month, make sure month is not current month
879 // (eg, Mar 31 -> Feb 31 == Feb 28, not Mar 02)
881 return new_date.getUTCMonth() == month;
883 // If going forward one month, make sure month is as expected
884 // (eg, Jan 31 -> Feb 31 == Feb 28, not Mar 02)
886 return new_date.getUTCMonth() != new_month;
888 new_month = month + dir;
889 new_date.setUTCMonth(new_month);
890 // Dec -> Jan (12) or Jan -> Dec (-1) -- limit expected date to 0-11
891 if (new_month < 0 || new_month > 11) {
892 new_month = (new_month + 12) % 12;
895 // For magnitudes >1, move one month at a time...
896 for (var i=0; i<mag; i++) {
897 // ...which might decrease the day (eg, Jan 31 to Feb 28, etc)...
898 new_date = this.moveMonth(new_date, dir);
900 // ...then reset the day, keeping it in the new month
901 new_month = new_date.getUTCMonth();
902 new_date.setUTCDate(day);
904 return new_month != new_date.getUTCMonth();
907 // Common date-resetting loop -- if date is beyond end of month, make it
910 new_date.setUTCDate(--day);
911 new_date.setUTCMonth(new_month);
916 moveYear: function(date, dir)
918 return this.moveMonth(date, dir*12);
921 dateWithinRange: function(date)
923 return date >= this.startDate && date <= this.endDate;
929 this.picker().remove();
932 validateValue : function(value)
934 if(this.getVisibilityEl().hasClass('hidden')){
938 if(value.length < 1) {
945 if(value.length < this.minLength){
948 if(value.length > this.maxLength){
952 var vt = Roo.form.VTypes;
953 if(!vt[this.vtype](value, this)){
957 if(typeof this.validator == "function"){
958 var msg = this.validator(value);
964 if(this.regex && !this.regex.test(value)){
968 if(typeof(this.parseDate(value)) == 'undefined'){
972 if (this.endDate !== Infinity && this.parseDate(value).getTime() > this.endDate.getTime()) {
976 if (this.startDate !== -Infinity && this.parseDate(value).getTime() < this.startDate.getTime()) {
986 this.date = this.viewDate = '';
988 Roo.bootstrap.DateField.superclass.setValue.call(this, '');
993 Roo.apply(Roo.bootstrap.DateField, {
1004 html: '<i class="fa fa-arrow-left"/>'
1014 html: '<i class="fa fa-arrow-right"/>'
1056 days: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"],
1057 daysShort: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],
1058 daysMin: ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa", "Su"],
1059 months: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
1060 monthsShort: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
1083 Roo.apply(Roo.bootstrap.DateField, {
1087 cls: 'datepicker dropdown-menu roo-dynamic',
1091 cls: 'datepicker-days',
1095 cls: 'table-condensed',
1097 Roo.bootstrap.DateField.head,
1101 Roo.bootstrap.DateField.footer
1108 cls: 'datepicker-months',
1112 cls: 'table-condensed',
1114 Roo.bootstrap.DateField.head,
1115 Roo.bootstrap.DateField.content,
1116 Roo.bootstrap.DateField.footer
1123 cls: 'datepicker-years',
1127 cls: 'table-condensed',
1129 Roo.bootstrap.DateField.head,
1130 Roo.bootstrap.DateField.content,
1131 Roo.bootstrap.DateField.footer