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
22 * @cfg {Boolean} keyboardNavigation default true
23 * @cfg {String} language default en
26 * Create a new DateField
27 * @param {Object} config The config object
30 Roo.bootstrap.DateField = function(config){
31 Roo.bootstrap.DateField.superclass.constructor.call(this, config);
35 * Fires when this field show.
36 * @param {Roo.bootstrap.DateField} this
37 * @param {Mixed} date The date value
42 * Fires when this field hide.
43 * @param {Roo.bootstrap.DateField} this
44 * @param {Mixed} date The date value
49 * Fires when select a date.
50 * @param {Roo.bootstrap.DateField} this
51 * @param {Mixed} date The date value
57 Roo.extend(Roo.bootstrap.DateField, Roo.bootstrap.Input, {
60 * @cfg {String} format
61 * The default date format string which can be overriden for localization support. The format must be
62 * valid according to {@link Date#parseDate} (defaults to 'm/d/y').
66 * @cfg {String} altFormats
67 * Multiple date formats separated by "|" to try when parsing a user input value and it doesn't match the defined
68 * format (defaults to 'm/d/Y|m-d-y|m-d-Y|m/d|m-d|d').
70 altFormats : "m/d/Y|m-d-y|m-d-Y|m/d|m-d|md|mdy|mdY|d",
78 todayHighlight : false,
84 keyboardNavigation: true,
92 daysOfWeekDisabled: [],
98 return new Date(Date.UTC.apply(Date, arguments));
103 var today = new Date();
104 return this.UTCDate(today.getUTCFullYear(), today.getUTCMonth(), today.getUTCDate());
107 getDate: function() {
108 var d = this.getUTCDate();
109 return new Date(d.getTime() + (d.getTimezoneOffset()*60000));
112 getUTCDate: function() {
116 setDate: function(d) {
117 this.setUTCDate(new Date(d.getTime() - (d.getTimezoneOffset()*60000)));
120 setUTCDate: function(d) {
122 this.setValue(this.formatDate(this.date));
125 onRender: function(ct, position)
128 Roo.bootstrap.DateField.superclass.onRender.call(this, ct, position);
130 this.language = this.language || 'en';
131 this.language = this.language in Roo.bootstrap.DateField.dates ? this.language : this.language.split('-')[0];
132 this.language = this.language in Roo.bootstrap.DateField.dates ? this.language : "en";
134 this.isRTL = Roo.bootstrap.DateField.dates[this.language].rtl || false;
135 this.format = this.format || 'm/d/y';
136 this.isInline = false;
138 this.component = this.el.select('.add-on', true).first() || false;
139 this.component = (this.component && this.component.length === 0) ? false : this.component;
140 this.hasInput = this.component && this.inputEL().length;
142 if (typeof(this.minViewMode === 'string')) {
143 switch (this.minViewMode) {
145 this.minViewMode = 1;
148 this.minViewMode = 2;
151 this.minViewMode = 0;
156 if (typeof(this.viewMode === 'string')) {
157 switch (this.viewMode) {
170 this.pickerEl = Roo.get(document.body).createChild(Roo.bootstrap.DateField.template);
172 // this.el.select('>.input-group', true).first().createChild(Roo.bootstrap.DateField.template);
174 this.picker().setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
176 this.picker().on('mousedown', this.onMousedown, this);
177 this.picker().on('click', this.onClick, this);
179 this.picker().addClass('datepicker-dropdown');
181 this.startViewMode = this.viewMode;
184 Roo.each(this.picker().select('tfoot th.today', true).elements, function(v){
185 if(!this.calendarWeeks){
190 v.dom.innerHTML = Roo.bootstrap.DateField.dates[this.language].today
191 v.attr('colspan', function(i, val){
192 return parseInt(val) + 1;
197 this.weekEnd = this.weekStart === 0 ? 6 : this.weekStart - 1;
199 this.setStartDate(this.startDate);
200 this.setEndDate(this.endDate);
202 this.setDaysOfWeekDisabled(this.daysOfWeekDisabled);
216 return this.pickerEl;
217 // return this.el.select('.datepicker', true).first();
222 var dowCnt = this.weekStart;
231 if(this.calendarWeeks){
239 while (dowCnt < this.weekStart + 7) {
243 html: Roo.bootstrap.DateField.dates[this.language].daysMin[(dowCnt++)%7]
247 this.picker().select('>.datepicker-days thead', true).first().createChild(dow);
250 fillMonths: function()
253 var months = this.picker().select('>.datepicker-months td', true).first();
255 months.dom.innerHTML = '';
261 html: Roo.bootstrap.DateField.dates[this.language].monthsShort[i++]
264 months.createChild(month);
271 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;
273 if (this.date < this.startDate) {
274 this.viewDate = new Date(this.startDate);
275 } else if (this.date > this.endDate) {
276 this.viewDate = new Date(this.endDate);
278 this.viewDate = new Date(this.date);
286 var d = new Date(this.viewDate),
287 year = d.getUTCFullYear(),
288 month = d.getUTCMonth(),
289 startYear = this.startDate !== -Infinity ? this.startDate.getUTCFullYear() : -Infinity,
290 startMonth = this.startDate !== -Infinity ? this.startDate.getUTCMonth() : -Infinity,
291 endYear = this.endDate !== Infinity ? this.endDate.getUTCFullYear() : Infinity,
292 endMonth = this.endDate !== Infinity ? this.endDate.getUTCMonth() : Infinity,
293 currentDate = this.date && this.date.valueOf(),
294 today = this.UTCToday();
296 this.picker().select('>.datepicker-days thead th.switch', true).first().dom.innerHTML = Roo.bootstrap.DateField.dates[this.language].months[month]+' '+year;
298 // this.picker().select('>tfoot th.today', true).first().dom.innerHTML = Roo.bootstrap.DateField.dates[this.language].today;
300 // this.picker.select('>tfoot th.today').
301 // .text(dates[this.language].today)
302 // .toggle(this.todayBtn !== false);
304 this.updateNavArrows();
307 var prevMonth = this.UTCDate(year, month-1, 28,0,0,0,0),
309 day = prevMonth.getDaysInMonth(prevMonth.getUTCFullYear(), prevMonth.getUTCMonth());
311 prevMonth.setUTCDate(day);
313 prevMonth.setUTCDate(day - (prevMonth.getUTCDay() - this.weekStart + 7)%7);
315 var nextMonth = new Date(prevMonth);
317 nextMonth.setUTCDate(nextMonth.getUTCDate() + 42);
319 nextMonth = nextMonth.valueOf();
321 var fillMonths = false;
323 this.picker().select('>.datepicker-days tbody',true).first().dom.innerHTML = '';
325 while(prevMonth.valueOf() < nextMonth) {
328 if (prevMonth.getUTCDay() === this.weekStart) {
330 this.picker().select('>.datepicker-days tbody',true).first().createChild(fillMonths);
338 if(this.calendarWeeks){
339 // ISO 8601: First week contains first thursday.
340 // ISO also states week starts on Monday, but we can be more abstract here.
342 // Start of current week: based on weekstart/current date
343 ws = new Date(+prevMonth + (this.weekStart - prevMonth.getUTCDay() - 7) % 7 * 864e5),
344 // Thursday of this week
345 th = new Date(+ws + (7 + 4 - ws.getUTCDay()) % 7 * 864e5),
346 // First Thursday of year, year from thursday
347 yth = new Date(+(yth = this.UTCDate(th.getUTCFullYear(), 0, 1)) + (7 + 4 - yth.getUTCDay())%7*864e5),
348 // Calendar week: ms between thursdays, div ms per day, div 7 days
349 calWeek = (th - yth) / 864e5 / 7 + 1;
359 if (prevMonth.getUTCFullYear() < year || (prevMonth.getUTCFullYear() == year && prevMonth.getUTCMonth() < month)) {
361 } else if (prevMonth.getUTCFullYear() > year || (prevMonth.getUTCFullYear() == year && prevMonth.getUTCMonth() > month)) {
364 if (this.todayHighlight &&
365 prevMonth.getUTCFullYear() == today.getFullYear() &&
366 prevMonth.getUTCMonth() == today.getMonth() &&
367 prevMonth.getUTCDate() == today.getDate()) {
371 if (currentDate && prevMonth.valueOf() === currentDate) {
372 clsName += ' active';
375 if (prevMonth.valueOf() < this.startDate || prevMonth.valueOf() > this.endDate ||
376 this.daysOfWeekDisabled.indexOf(prevMonth.getUTCDay()) !== -1) {
377 clsName += ' disabled';
382 cls: 'day ' + clsName,
383 html: prevMonth.getDate()
386 prevMonth.setDate(prevMonth.getDate()+1);
389 var currentYear = this.date && this.date.getUTCFullYear();
390 var currentMonth = this.date && this.date.getUTCMonth();
392 this.picker().select('>.datepicker-months th.switch',true).first().dom.innerHTML = year;
394 Roo.each(this.picker().select('>.datepicker-months tbody span',true).elements, function(v,k){
395 v.removeClass('active');
397 if(currentYear === year && k === currentMonth){
398 v.addClass('active');
401 if (year < startYear || year > endYear || (year == startYear && k < startMonth) || (year == endYear && k > endMonth)) {
402 v.addClass('disabled');
408 year = parseInt(year/10, 10) * 10;
410 this.picker().select('>.datepicker-years th.switch', true).first().dom.innerHTML = year + '-' + (year + 9);
412 this.picker().select('>.datepicker-years tbody td',true).first().dom.innerHTML = '';
415 for (var i = -1; i < 11; i++) {
416 this.picker().select('>.datepicker-years tbody td',true).first().createChild({
418 cls: 'year' + (i === -1 || i === 10 ? ' old' : '') + (currentYear === year ? ' active' : '') + (year < startYear || year > endYear ? ' disabled' : ''),
426 showMode: function(dir)
428 Roo.log('show mode!!!');
430 Roo.log(this.minViewMode);
432 this.viewMode = Math.max(this.minViewMode, Math.min(2, this.viewMode + dir));
434 Roo.log(this.viewMode);
435 // if(this.viewMode < 1){
439 Roo.each(this.picker().select('>div',true).elements, function(v){
440 v.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
443 this.picker().select('>.datepicker-'+Roo.bootstrap.DateField.modes[this.viewMode].clsName, true).first().show();
448 if(this.isInline) return;
450 this.picker().removeClass(['bottom', 'top']);
452 if((Roo.lib.Dom.getViewHeight() + Roo.get(document.body).getScroll().top) - (this.inputEl().getBottom() + this.picker().getHeight()) < 0){
454 * place to the top of element!
458 this.picker().addClass('top');
459 this.picker().setTop(this.inputEl().getTop() - this.picker().getHeight()).setLeft(this.inputEl().getLeft());
464 this.picker().addClass('bottom');
466 this.picker().setTop(this.inputEl().getBottom()).setLeft(this.inputEl().getLeft());
469 parseDate : function(value)
471 if(!value || value instanceof Date){
474 var v = Date.parseDate(value, this.format);
475 if (!v && (this.useIso || value.match(/^(\d{4})-0?(\d+)-0?(\d+)/))) {
476 v = Date.parseDate(value, 'Y-m-d');
478 if(!v && this.altFormats){
479 if(!this.altFormatsArray){
480 this.altFormatsArray = this.altFormats.split("|");
482 for(var i = 0, len = this.altFormatsArray.length; i < len && !v; i++){
483 v = Date.parseDate(value, this.altFormatsArray[i]);
489 formatDate : function(date, fmt)
491 return (!date || !(date instanceof Date)) ?
492 date : date.dateFormat(fmt || this.format);
497 Roo.bootstrap.DateField.superclass.onFocus.call(this);
503 Roo.bootstrap.DateField.superclass.onBlur.call(this);
505 var d = this.inputEl().getValue();
514 this.picker().show();
518 this.fireEvent('show', this, this.date);
523 if(this.isInline) return;
524 this.picker().hide();
525 this.viewMode = this.startViewMode;
528 this.fireEvent('hide', this, this.date);
532 onMousedown: function(e)
540 Roo.bootstrap.DateField.superclass.keyup.call(this);
544 setValue: function(v)
547 // v can be a string or a date..
550 var d = new Date(this.parseDate(v) ).clearTime();
552 if(isNaN(d.getTime())){
553 this.date = this.viewDate = '';
554 Roo.bootstrap.DateField.superclass.setValue.call(this, '');
558 v = this.formatDate(d);
560 Roo.bootstrap.DateField.superclass.setValue.call(this, v);
562 this.date = new Date(d.getTime() - d.getTimezoneOffset()*60000);
566 this.fireEvent('select', this, this.date);
572 return this.formatDate(this.date);
577 if (!this.picker().isVisible()){
578 if (e.keyCode == 27) // allow escape to hide and re-show picker
583 var dateChanged = false,
585 newDate, newViewDate;
594 if (!this.keyboardNavigation) break;
595 dir = e.keyCode == 37 ? -1 : 1;
598 newDate = this.moveYear(this.date, dir);
599 newViewDate = this.moveYear(this.viewDate, dir);
600 } else if (e.shiftKey){
601 newDate = this.moveMonth(this.date, dir);
602 newViewDate = this.moveMonth(this.viewDate, dir);
604 newDate = new Date(this.date);
605 newDate.setUTCDate(this.date.getUTCDate() + dir);
606 newViewDate = new Date(this.viewDate);
607 newViewDate.setUTCDate(this.viewDate.getUTCDate() + dir);
609 if (this.dateWithinRange(newDate)){
611 this.viewDate = newViewDate;
612 this.setValue(this.formatDate(this.date));
620 if (!this.keyboardNavigation) break;
621 dir = e.keyCode == 38 ? -1 : 1;
623 newDate = this.moveYear(this.date, dir);
624 newViewDate = this.moveYear(this.viewDate, dir);
625 } else if (e.shiftKey){
626 newDate = this.moveMonth(this.date, dir);
627 newViewDate = this.moveMonth(this.viewDate, dir);
629 newDate = new Date(this.date);
630 newDate.setUTCDate(this.date.getUTCDate() + dir * 7);
631 newViewDate = new Date(this.viewDate);
632 newViewDate.setUTCDate(this.viewDate.getUTCDate() + dir * 7);
634 if (this.dateWithinRange(newDate)){
636 this.viewDate = newViewDate;
637 this.setValue(this.formatDate(this.date));
644 this.setValue(this.formatDate(this.date));
649 this.setValue(this.formatDate(this.date));
668 var target = e.getTarget();
670 if(target.nodeName.toLowerCase() === 'i'){
671 target = Roo.get(target).dom.parentNode;
674 var nodeName = target.nodeName;
675 var className = target.className;
676 var html = target.innerHTML;
679 switch(nodeName.toLowerCase()) {
687 var dir = Roo.bootstrap.DateField.modes[this.viewMode].navStep * (className == 'prev' ? -1 : 1);
688 switch(this.viewMode){
690 this.viewDate = this.moveMonth(this.viewDate, dir);
694 this.viewDate = this.moveYear(this.viewDate, dir);
700 var date = new Date();
701 this.date = this.UTCDate(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0);
703 this.setValue(this.formatDate(this.date));
710 if (className.indexOf('disabled') < 0) {
711 this.viewDate.setUTCDate(1);
712 if (className.indexOf('month') > -1) {
713 this.viewDate.setUTCMonth(Roo.bootstrap.DateField.dates[this.language].monthsShort.indexOf(html));
715 var year = parseInt(html, 10) || 0;
716 this.viewDate.setUTCFullYear(year);
725 //Roo.log(className);
726 if (className.indexOf('day') > -1 && className.indexOf('disabled') < 0 ){
727 var day = parseInt(html, 10) || 1;
728 var year = this.viewDate.getUTCFullYear(),
729 month = this.viewDate.getUTCMonth();
731 if (className.indexOf('old') > -1) {
738 } else if (className.indexOf('new') > -1) {
746 //Roo.log([year,month,day]);
747 this.date = this.UTCDate(year, month, day,0,0,0,0);
748 this.viewDate = this.UTCDate(year, month, Math.min(28, day),0,0,0,0);
750 //Roo.log(this.formatDate(this.date));
751 this.setValue(this.formatDate(this.date));
758 setStartDate: function(startDate)
760 this.startDate = startDate || -Infinity;
761 if (this.startDate !== -Infinity) {
762 this.startDate = this.parseDate(this.startDate);
765 this.updateNavArrows();
768 setEndDate: function(endDate)
770 this.endDate = endDate || Infinity;
771 if (this.endDate !== Infinity) {
772 this.endDate = this.parseDate(this.endDate);
775 this.updateNavArrows();
778 setDaysOfWeekDisabled: function(daysOfWeekDisabled)
780 this.daysOfWeekDisabled = daysOfWeekDisabled || [];
781 if (typeof(this.daysOfWeekDisabled) !== 'object') {
782 this.daysOfWeekDisabled = this.daysOfWeekDisabled.split(/,\s*/);
784 this.daysOfWeekDisabled = this.daysOfWeekDisabled.map(function (d) {
785 return parseInt(d, 10);
788 this.updateNavArrows();
791 updateNavArrows: function()
793 var d = new Date(this.viewDate),
794 year = d.getUTCFullYear(),
795 month = d.getUTCMonth();
797 Roo.each(this.picker().select('.prev', true).elements, function(v){
799 switch (this.viewMode) {
802 if (this.startDate !== -Infinity && year <= this.startDate.getUTCFullYear() && month <= this.startDate.getUTCMonth()) {
808 if (this.startDate !== -Infinity && year <= this.startDate.getUTCFullYear()) {
815 Roo.each(this.picker().select('.next', true).elements, function(v){
817 switch (this.viewMode) {
820 if (this.endDate !== Infinity && year >= this.endDate.getUTCFullYear() && month >= this.endDate.getUTCMonth()) {
826 if (this.endDate !== Infinity && year >= this.endDate.getUTCFullYear()) {
834 moveMonth: function(date, dir)
836 if (!dir) return date;
837 var new_date = new Date(date.valueOf()),
838 day = new_date.getUTCDate(),
839 month = new_date.getUTCMonth(),
842 dir = dir > 0 ? 1 : -1;
845 // If going back one month, make sure month is not current month
846 // (eg, Mar 31 -> Feb 31 == Feb 28, not Mar 02)
848 return new_date.getUTCMonth() == month;
850 // If going forward one month, make sure month is as expected
851 // (eg, Jan 31 -> Feb 31 == Feb 28, not Mar 02)
853 return new_date.getUTCMonth() != new_month;
855 new_month = month + dir;
856 new_date.setUTCMonth(new_month);
857 // Dec -> Jan (12) or Jan -> Dec (-1) -- limit expected date to 0-11
858 if (new_month < 0 || new_month > 11)
859 new_month = (new_month + 12) % 12;
861 // For magnitudes >1, move one month at a time...
862 for (var i=0; i<mag; i++)
863 // ...which might decrease the day (eg, Jan 31 to Feb 28, etc)...
864 new_date = this.moveMonth(new_date, dir);
865 // ...then reset the day, keeping it in the new month
866 new_month = new_date.getUTCMonth();
867 new_date.setUTCDate(day);
869 return new_month != new_date.getUTCMonth();
872 // Common date-resetting loop -- if date is beyond end of month, make it
875 new_date.setUTCDate(--day);
876 new_date.setUTCMonth(new_month);
881 moveYear: function(date, dir)
883 return this.moveMonth(date, dir*12);
886 dateWithinRange: function(date)
888 return date >= this.startDate && date <= this.endDate;
894 this.picker().remove();
899 Roo.apply(Roo.bootstrap.DateField, {
910 html: '<i class="fa fa-arrow-left"/>'
920 html: '<i class="fa fa-arrow-right"/>'
962 days: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"],
963 daysShort: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],
964 daysMin: ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa", "Su"],
965 months: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
966 monthsShort: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
989 Roo.apply(Roo.bootstrap.DateField, {
993 cls: 'datepicker dropdown-menu',
997 cls: 'datepicker-days',
1001 cls: 'table-condensed',
1003 Roo.bootstrap.DateField.head,
1007 Roo.bootstrap.DateField.footer
1014 cls: 'datepicker-months',
1018 cls: 'table-condensed',
1020 Roo.bootstrap.DateField.head,
1021 Roo.bootstrap.DateField.content,
1022 Roo.bootstrap.DateField.footer
1029 cls: 'datepicker-years',
1033 cls: 'table-condensed',
1035 Roo.bootstrap.DateField.head,
1036 Roo.bootstrap.DateField.content,
1037 Roo.bootstrap.DateField.footer