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)
191 Roo.log(this.picker().select('tbody > tr > td', true).first());
192 this.picker().select('tbody > tr > td', true).first().setStyle('width', '189px');
195 Roo.each(this.picker().select('tfoot th.today', true).elements, function(v){
196 if(!this.calendarWeeks){
201 v.dom.innerHTML = Roo.bootstrap.DateField.dates[this.language].today
202 v.attr('colspan', function(i, val){
203 return parseInt(val) + 1;
208 this.weekEnd = this.weekStart === 0 ? 6 : this.weekStart - 1;
210 this.setStartDate(this.startDate);
211 this.setEndDate(this.endDate);
213 this.setDaysOfWeekDisabled(this.daysOfWeekDisabled);
227 return this.pickerEl;
228 // return this.el.select('.datepicker', true).first();
233 var dowCnt = this.weekStart;
242 if(this.calendarWeeks){
250 while (dowCnt < this.weekStart + 7) {
254 html: Roo.bootstrap.DateField.dates[this.language].daysMin[(dowCnt++)%7]
258 this.picker().select('>.datepicker-days thead', true).first().createChild(dow);
261 fillMonths: function()
264 var months = this.picker().select('>.datepicker-months td', true).first();
266 months.dom.innerHTML = '';
272 html: Roo.bootstrap.DateField.dates[this.language].monthsShort[i++]
275 months.createChild(month);
282 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;
284 if (this.date < this.startDate) {
285 this.viewDate = new Date(this.startDate);
286 } else if (this.date > this.endDate) {
287 this.viewDate = new Date(this.endDate);
289 this.viewDate = new Date(this.date);
297 var d = new Date(this.viewDate),
298 year = d.getUTCFullYear(),
299 month = d.getUTCMonth(),
300 startYear = this.startDate !== -Infinity ? this.startDate.getUTCFullYear() : -Infinity,
301 startMonth = this.startDate !== -Infinity ? this.startDate.getUTCMonth() : -Infinity,
302 endYear = this.endDate !== Infinity ? this.endDate.getUTCFullYear() : Infinity,
303 endMonth = this.endDate !== Infinity ? this.endDate.getUTCMonth() : Infinity,
304 currentDate = this.date && this.date.valueOf(),
305 today = this.UTCToday();
307 this.picker().select('>.datepicker-days thead th.switch', true).first().dom.innerHTML = Roo.bootstrap.DateField.dates[this.language].months[month]+' '+year;
309 // this.picker().select('>tfoot th.today', true).first().dom.innerHTML = Roo.bootstrap.DateField.dates[this.language].today;
311 // this.picker.select('>tfoot th.today').
312 // .text(dates[this.language].today)
313 // .toggle(this.todayBtn !== false);
315 this.updateNavArrows();
318 var prevMonth = this.UTCDate(year, month-1, 28,0,0,0,0),
320 day = prevMonth.getDaysInMonth(prevMonth.getUTCFullYear(), prevMonth.getUTCMonth());
322 prevMonth.setUTCDate(day);
324 prevMonth.setUTCDate(day - (prevMonth.getUTCDay() - this.weekStart + 7)%7);
326 var nextMonth = new Date(prevMonth);
328 nextMonth.setUTCDate(nextMonth.getUTCDate() + 42);
330 nextMonth = nextMonth.valueOf();
332 var fillMonths = false;
334 this.picker().select('>.datepicker-days tbody',true).first().dom.innerHTML = '';
336 while(prevMonth.valueOf() < nextMonth) {
339 if (prevMonth.getUTCDay() === this.weekStart) {
341 this.picker().select('>.datepicker-days tbody',true).first().createChild(fillMonths);
349 if(this.calendarWeeks){
350 // ISO 8601: First week contains first thursday.
351 // ISO also states week starts on Monday, but we can be more abstract here.
353 // Start of current week: based on weekstart/current date
354 ws = new Date(+prevMonth + (this.weekStart - prevMonth.getUTCDay() - 7) % 7 * 864e5),
355 // Thursday of this week
356 th = new Date(+ws + (7 + 4 - ws.getUTCDay()) % 7 * 864e5),
357 // First Thursday of year, year from thursday
358 yth = new Date(+(yth = this.UTCDate(th.getUTCFullYear(), 0, 1)) + (7 + 4 - yth.getUTCDay())%7*864e5),
359 // Calendar week: ms between thursdays, div ms per day, div 7 days
360 calWeek = (th - yth) / 864e5 / 7 + 1;
370 if (prevMonth.getUTCFullYear() < year || (prevMonth.getUTCFullYear() == year && prevMonth.getUTCMonth() < month)) {
372 } else if (prevMonth.getUTCFullYear() > year || (prevMonth.getUTCFullYear() == year && prevMonth.getUTCMonth() > month)) {
375 if (this.todayHighlight &&
376 prevMonth.getUTCFullYear() == today.getFullYear() &&
377 prevMonth.getUTCMonth() == today.getMonth() &&
378 prevMonth.getUTCDate() == today.getDate()) {
382 if (currentDate && prevMonth.valueOf() === currentDate) {
383 clsName += ' active';
386 if (prevMonth.valueOf() < this.startDate || prevMonth.valueOf() > this.endDate ||
387 this.daysOfWeekDisabled.indexOf(prevMonth.getUTCDay()) !== -1) {
388 clsName += ' disabled';
393 cls: 'day ' + clsName,
394 html: prevMonth.getDate()
397 prevMonth.setDate(prevMonth.getDate()+1);
400 var currentYear = this.date && this.date.getUTCFullYear();
401 var currentMonth = this.date && this.date.getUTCMonth();
403 this.picker().select('>.datepicker-months th.switch',true).first().dom.innerHTML = year;
405 Roo.each(this.picker().select('>.datepicker-months tbody span',true).elements, function(v,k){
406 v.removeClass('active');
408 if(currentYear === year && k === currentMonth){
409 v.addClass('active');
412 if (year < startYear || year > endYear || (year == startYear && k < startMonth) || (year == endYear && k > endMonth)) {
413 v.addClass('disabled');
419 year = parseInt(year/10, 10) * 10;
421 this.picker().select('>.datepicker-years th.switch', true).first().dom.innerHTML = year + '-' + (year + 9);
423 this.picker().select('>.datepicker-years tbody td',true).first().dom.innerHTML = '';
426 for (var i = -1; i < 11; i++) {
427 this.picker().select('>.datepicker-years tbody td',true).first().createChild({
429 cls: 'year' + (i === -1 || i === 10 ? ' old' : '') + (currentYear === year ? ' active' : '') + (year < startYear || year > endYear ? ' disabled' : ''),
437 showMode: function(dir)
440 this.viewMode = Math.max(this.minViewMode, Math.min(2, this.viewMode + dir));
443 Roo.each(this.picker().select('>div',true).elements, function(v){
444 v.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
447 this.picker().select('>.datepicker-'+Roo.bootstrap.DateField.modes[this.viewMode].clsName, true).first().show();
452 if(this.isInline) return;
454 this.picker().removeClass(['bottom', 'top']);
456 if((Roo.lib.Dom.getViewHeight() + Roo.get(document.body).getScroll().top) - (this.inputEl().getBottom() + this.picker().getHeight()) < 0){
458 * place to the top of element!
462 this.picker().addClass('top');
463 this.picker().setTop(this.inputEl().getTop() - this.picker().getHeight()).setLeft(this.inputEl().getLeft());
468 this.picker().addClass('bottom');
470 this.picker().setTop(this.inputEl().getBottom()).setLeft(this.inputEl().getLeft());
473 parseDate : function(value)
475 if(!value || value instanceof Date){
478 var v = Date.parseDate(value, this.format);
479 if (!v && (this.useIso || value.match(/^(\d{4})-0?(\d+)-0?(\d+)/))) {
480 v = Date.parseDate(value, 'Y-m-d');
482 if(!v && this.altFormats){
483 if(!this.altFormatsArray){
484 this.altFormatsArray = this.altFormats.split("|");
486 for(var i = 0, len = this.altFormatsArray.length; i < len && !v; i++){
487 v = Date.parseDate(value, this.altFormatsArray[i]);
493 formatDate : function(date, fmt)
495 return (!date || !(date instanceof Date)) ?
496 date : date.dateFormat(fmt || this.format);
501 Roo.bootstrap.DateField.superclass.onFocus.call(this);
507 Roo.bootstrap.DateField.superclass.onBlur.call(this);
509 var d = this.inputEl().getValue();
518 this.picker().show();
522 this.fireEvent('show', this, this.date);
527 if(this.isInline) return;
528 this.picker().hide();
529 this.viewMode = this.startViewMode;
532 this.fireEvent('hide', this, this.date);
536 onMousedown: function(e)
544 Roo.bootstrap.DateField.superclass.keyup.call(this);
548 setValue: function(v)
551 // v can be a string or a date..
554 var d = new Date(this.parseDate(v) ).clearTime();
556 if(isNaN(d.getTime())){
557 this.date = this.viewDate = '';
558 Roo.bootstrap.DateField.superclass.setValue.call(this, '');
562 v = this.formatDate(d);
564 Roo.bootstrap.DateField.superclass.setValue.call(this, v);
566 this.date = new Date(d.getTime() - d.getTimezoneOffset()*60000);
570 this.fireEvent('select', this, this.date);
576 return this.formatDate(this.date);
581 if (!this.picker().isVisible()){
582 if (e.keyCode == 27) // allow escape to hide and re-show picker
587 var dateChanged = false,
589 newDate, newViewDate;
598 if (!this.keyboardNavigation) break;
599 dir = e.keyCode == 37 ? -1 : 1;
602 newDate = this.moveYear(this.date, dir);
603 newViewDate = this.moveYear(this.viewDate, dir);
604 } else if (e.shiftKey){
605 newDate = this.moveMonth(this.date, dir);
606 newViewDate = this.moveMonth(this.viewDate, dir);
608 newDate = new Date(this.date);
609 newDate.setUTCDate(this.date.getUTCDate() + dir);
610 newViewDate = new Date(this.viewDate);
611 newViewDate.setUTCDate(this.viewDate.getUTCDate() + dir);
613 if (this.dateWithinRange(newDate)){
615 this.viewDate = newViewDate;
616 this.setValue(this.formatDate(this.date));
624 if (!this.keyboardNavigation) break;
625 dir = e.keyCode == 38 ? -1 : 1;
627 newDate = this.moveYear(this.date, dir);
628 newViewDate = this.moveYear(this.viewDate, dir);
629 } else if (e.shiftKey){
630 newDate = this.moveMonth(this.date, dir);
631 newViewDate = this.moveMonth(this.viewDate, dir);
633 newDate = new Date(this.date);
634 newDate.setUTCDate(this.date.getUTCDate() + dir * 7);
635 newViewDate = new Date(this.viewDate);
636 newViewDate.setUTCDate(this.viewDate.getUTCDate() + dir * 7);
638 if (this.dateWithinRange(newDate)){
640 this.viewDate = newViewDate;
641 this.setValue(this.formatDate(this.date));
648 this.setValue(this.formatDate(this.date));
653 this.setValue(this.formatDate(this.date));
672 var target = e.getTarget();
674 if(target.nodeName.toLowerCase() === 'i'){
675 target = Roo.get(target).dom.parentNode;
678 var nodeName = target.nodeName;
679 var className = target.className;
680 var html = target.innerHTML;
683 switch(nodeName.toLowerCase()) {
691 var dir = Roo.bootstrap.DateField.modes[this.viewMode].navStep * (className == 'prev' ? -1 : 1);
692 switch(this.viewMode){
694 this.viewDate = this.moveMonth(this.viewDate, dir);
698 this.viewDate = this.moveYear(this.viewDate, dir);
704 var date = new Date();
705 this.date = this.UTCDate(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0);
707 this.setValue(this.formatDate(this.date));
714 if (className.indexOf('disabled') < 0) {
715 this.viewDate.setUTCDate(1);
716 if (className.indexOf('month') > -1) {
717 this.viewDate.setUTCMonth(Roo.bootstrap.DateField.dates[this.language].monthsShort.indexOf(html));
719 var year = parseInt(html, 10) || 0;
720 this.viewDate.setUTCFullYear(year);
725 this.setValue(this.formatDate(this.viewDate));
736 //Roo.log(className);
737 if (className.indexOf('day') > -1 && className.indexOf('disabled') < 0 ){
738 var day = parseInt(html, 10) || 1;
739 var year = this.viewDate.getUTCFullYear(),
740 month = this.viewDate.getUTCMonth();
742 if (className.indexOf('old') > -1) {
749 } else if (className.indexOf('new') > -1) {
757 //Roo.log([year,month,day]);
758 this.date = this.UTCDate(year, month, day,0,0,0,0);
759 this.viewDate = this.UTCDate(year, month, Math.min(28, day),0,0,0,0);
761 //Roo.log(this.formatDate(this.date));
762 this.setValue(this.formatDate(this.date));
769 setStartDate: function(startDate)
771 this.startDate = startDate || -Infinity;
772 if (this.startDate !== -Infinity) {
773 this.startDate = this.parseDate(this.startDate);
776 this.updateNavArrows();
779 setEndDate: function(endDate)
781 this.endDate = endDate || Infinity;
782 if (this.endDate !== Infinity) {
783 this.endDate = this.parseDate(this.endDate);
786 this.updateNavArrows();
789 setDaysOfWeekDisabled: function(daysOfWeekDisabled)
791 this.daysOfWeekDisabled = daysOfWeekDisabled || [];
792 if (typeof(this.daysOfWeekDisabled) !== 'object') {
793 this.daysOfWeekDisabled = this.daysOfWeekDisabled.split(/,\s*/);
795 this.daysOfWeekDisabled = this.daysOfWeekDisabled.map(function (d) {
796 return parseInt(d, 10);
799 this.updateNavArrows();
802 updateNavArrows: function()
808 var d = new Date(this.viewDate),
809 year = d.getUTCFullYear(),
810 month = d.getUTCMonth();
812 Roo.each(this.picker().select('.prev', true).elements, function(v){
814 switch (this.viewMode) {
817 if (this.startDate !== -Infinity && year <= this.startDate.getUTCFullYear() && month <= this.startDate.getUTCMonth()) {
823 if (this.startDate !== -Infinity && year <= this.startDate.getUTCFullYear()) {
830 Roo.each(this.picker().select('.next', true).elements, function(v){
832 switch (this.viewMode) {
835 if (this.endDate !== Infinity && year >= this.endDate.getUTCFullYear() && month >= this.endDate.getUTCMonth()) {
841 if (this.endDate !== Infinity && year >= this.endDate.getUTCFullYear()) {
849 moveMonth: function(date, dir)
851 if (!dir) return date;
852 var new_date = new Date(date.valueOf()),
853 day = new_date.getUTCDate(),
854 month = new_date.getUTCMonth(),
857 dir = dir > 0 ? 1 : -1;
860 // If going back one month, make sure month is not current month
861 // (eg, Mar 31 -> Feb 31 == Feb 28, not Mar 02)
863 return new_date.getUTCMonth() == month;
865 // If going forward one month, make sure month is as expected
866 // (eg, Jan 31 -> Feb 31 == Feb 28, not Mar 02)
868 return new_date.getUTCMonth() != new_month;
870 new_month = month + dir;
871 new_date.setUTCMonth(new_month);
872 // Dec -> Jan (12) or Jan -> Dec (-1) -- limit expected date to 0-11
873 if (new_month < 0 || new_month > 11)
874 new_month = (new_month + 12) % 12;
876 // For magnitudes >1, move one month at a time...
877 for (var i=0; i<mag; i++)
878 // ...which might decrease the day (eg, Jan 31 to Feb 28, etc)...
879 new_date = this.moveMonth(new_date, dir);
880 // ...then reset the day, keeping it in the new month
881 new_month = new_date.getUTCMonth();
882 new_date.setUTCDate(day);
884 return new_month != new_date.getUTCMonth();
887 // Common date-resetting loop -- if date is beyond end of month, make it
890 new_date.setUTCDate(--day);
891 new_date.setUTCMonth(new_month);
896 moveYear: function(date, dir)
898 return this.moveMonth(date, dir*12);
901 dateWithinRange: function(date)
903 return date >= this.startDate && date <= this.endDate;
909 this.picker().remove();
914 Roo.apply(Roo.bootstrap.DateField, {
925 html: '<i class="fa fa-arrow-left"/>'
935 html: '<i class="fa fa-arrow-right"/>'
977 days: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"],
978 daysShort: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],
979 daysMin: ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa", "Su"],
980 months: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
981 monthsShort: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
1004 Roo.apply(Roo.bootstrap.DateField, {
1008 cls: 'datepicker dropdown-menu',
1012 cls: 'datepicker-days',
1016 cls: 'table-condensed',
1018 Roo.bootstrap.DateField.head,
1022 Roo.bootstrap.DateField.footer
1029 cls: 'datepicker-months',
1033 cls: 'table-condensed',
1035 Roo.bootstrap.DateField.head,
1036 Roo.bootstrap.DateField.content,
1037 Roo.bootstrap.DateField.footer
1044 cls: 'datepicker-years',
1048 cls: 'table-condensed',
1050 Roo.bootstrap.DateField.head,
1051 Roo.bootstrap.DateField.content,
1052 Roo.bootstrap.DateField.footer