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('run1!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!');
192 Roo.log(this.picker().select('tbody > tr > td', true).first());
193 this.picker().select('tbody > tr > td', true).first().setStyle('width', '189px');
196 Roo.each(this.picker().select('tfoot th.today', true).elements, function(v){
197 if(!this.calendarWeeks){
202 v.dom.innerHTML = Roo.bootstrap.DateField.dates[this.language].today
203 v.attr('colspan', function(i, val){
204 return parseInt(val) + 1;
209 this.weekEnd = this.weekStart === 0 ? 6 : this.weekStart - 1;
211 this.setStartDate(this.startDate);
212 this.setEndDate(this.endDate);
214 this.setDaysOfWeekDisabled(this.daysOfWeekDisabled);
228 return this.pickerEl;
229 // return this.el.select('.datepicker', true).first();
234 var dowCnt = this.weekStart;
243 if(this.calendarWeeks){
251 while (dowCnt < this.weekStart + 7) {
255 html: Roo.bootstrap.DateField.dates[this.language].daysMin[(dowCnt++)%7]
259 this.picker().select('>.datepicker-days thead', true).first().createChild(dow);
262 fillMonths: function()
265 var months = this.picker().select('>.datepicker-months td', true).first();
267 months.dom.innerHTML = '';
273 html: Roo.bootstrap.DateField.dates[this.language].monthsShort[i++]
276 months.createChild(month);
283 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;
285 if (this.date < this.startDate) {
286 this.viewDate = new Date(this.startDate);
287 } else if (this.date > this.endDate) {
288 this.viewDate = new Date(this.endDate);
290 this.viewDate = new Date(this.date);
298 var d = new Date(this.viewDate),
299 year = d.getUTCFullYear(),
300 month = d.getUTCMonth(),
301 startYear = this.startDate !== -Infinity ? this.startDate.getUTCFullYear() : -Infinity,
302 startMonth = this.startDate !== -Infinity ? this.startDate.getUTCMonth() : -Infinity,
303 endYear = this.endDate !== Infinity ? this.endDate.getUTCFullYear() : Infinity,
304 endMonth = this.endDate !== Infinity ? this.endDate.getUTCMonth() : Infinity,
305 currentDate = this.date && this.date.valueOf(),
306 today = this.UTCToday();
308 this.picker().select('>.datepicker-days thead th.switch', true).first().dom.innerHTML = Roo.bootstrap.DateField.dates[this.language].months[month]+' '+year;
310 // this.picker().select('>tfoot th.today', true).first().dom.innerHTML = Roo.bootstrap.DateField.dates[this.language].today;
312 // this.picker.select('>tfoot th.today').
313 // .text(dates[this.language].today)
314 // .toggle(this.todayBtn !== false);
316 this.updateNavArrows();
319 var prevMonth = this.UTCDate(year, month-1, 28,0,0,0,0),
321 day = prevMonth.getDaysInMonth(prevMonth.getUTCFullYear(), prevMonth.getUTCMonth());
323 prevMonth.setUTCDate(day);
325 prevMonth.setUTCDate(day - (prevMonth.getUTCDay() - this.weekStart + 7)%7);
327 var nextMonth = new Date(prevMonth);
329 nextMonth.setUTCDate(nextMonth.getUTCDate() + 42);
331 nextMonth = nextMonth.valueOf();
333 var fillMonths = false;
335 this.picker().select('>.datepicker-days tbody',true).first().dom.innerHTML = '';
337 while(prevMonth.valueOf() < nextMonth) {
340 if (prevMonth.getUTCDay() === this.weekStart) {
342 this.picker().select('>.datepicker-days tbody',true).first().createChild(fillMonths);
350 if(this.calendarWeeks){
351 // ISO 8601: First week contains first thursday.
352 // ISO also states week starts on Monday, but we can be more abstract here.
354 // Start of current week: based on weekstart/current date
355 ws = new Date(+prevMonth + (this.weekStart - prevMonth.getUTCDay() - 7) % 7 * 864e5),
356 // Thursday of this week
357 th = new Date(+ws + (7 + 4 - ws.getUTCDay()) % 7 * 864e5),
358 // First Thursday of year, year from thursday
359 yth = new Date(+(yth = this.UTCDate(th.getUTCFullYear(), 0, 1)) + (7 + 4 - yth.getUTCDay())%7*864e5),
360 // Calendar week: ms between thursdays, div ms per day, div 7 days
361 calWeek = (th - yth) / 864e5 / 7 + 1;
371 if (prevMonth.getUTCFullYear() < year || (prevMonth.getUTCFullYear() == year && prevMonth.getUTCMonth() < month)) {
373 } else if (prevMonth.getUTCFullYear() > year || (prevMonth.getUTCFullYear() == year && prevMonth.getUTCMonth() > month)) {
376 if (this.todayHighlight &&
377 prevMonth.getUTCFullYear() == today.getFullYear() &&
378 prevMonth.getUTCMonth() == today.getMonth() &&
379 prevMonth.getUTCDate() == today.getDate()) {
383 if (currentDate && prevMonth.valueOf() === currentDate) {
384 clsName += ' active';
387 if (prevMonth.valueOf() < this.startDate || prevMonth.valueOf() > this.endDate ||
388 this.daysOfWeekDisabled.indexOf(prevMonth.getUTCDay()) !== -1) {
389 clsName += ' disabled';
394 cls: 'day ' + clsName,
395 html: prevMonth.getDate()
398 prevMonth.setDate(prevMonth.getDate()+1);
401 var currentYear = this.date && this.date.getUTCFullYear();
402 var currentMonth = this.date && this.date.getUTCMonth();
404 this.picker().select('>.datepicker-months th.switch',true).first().dom.innerHTML = year;
406 Roo.each(this.picker().select('>.datepicker-months tbody span',true).elements, function(v,k){
407 v.removeClass('active');
409 if(currentYear === year && k === currentMonth){
410 v.addClass('active');
413 if (year < startYear || year > endYear || (year == startYear && k < startMonth) || (year == endYear && k > endMonth)) {
414 v.addClass('disabled');
420 year = parseInt(year/10, 10) * 10;
422 this.picker().select('>.datepicker-years th.switch', true).first().dom.innerHTML = year + '-' + (year + 9);
424 this.picker().select('>.datepicker-years tbody td',true).first().dom.innerHTML = '';
427 for (var i = -1; i < 11; i++) {
428 this.picker().select('>.datepicker-years tbody td',true).first().createChild({
430 cls: 'year' + (i === -1 || i === 10 ? ' old' : '') + (currentYear === year ? ' active' : '') + (year < startYear || year > endYear ? ' disabled' : ''),
438 showMode: function(dir)
441 this.viewMode = Math.max(this.minViewMode, Math.min(2, this.viewMode + dir));
444 Roo.each(this.picker().select('>div',true).elements, function(v){
445 v.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
448 this.picker().select('>.datepicker-'+Roo.bootstrap.DateField.modes[this.viewMode].clsName, true).first().show();
453 if(this.isInline) return;
455 this.picker().removeClass(['bottom', 'top']);
457 if((Roo.lib.Dom.getViewHeight() + Roo.get(document.body).getScroll().top) - (this.inputEl().getBottom() + this.picker().getHeight()) < 0){
459 * place to the top of element!
463 this.picker().addClass('top');
464 this.picker().setTop(this.inputEl().getTop() - this.picker().getHeight()).setLeft(this.inputEl().getLeft());
469 this.picker().addClass('bottom');
471 this.picker().setTop(this.inputEl().getBottom()).setLeft(this.inputEl().getLeft());
474 parseDate : function(value)
476 if(!value || value instanceof Date){
479 var v = Date.parseDate(value, this.format);
480 if (!v && (this.useIso || value.match(/^(\d{4})-0?(\d+)-0?(\d+)/))) {
481 v = Date.parseDate(value, 'Y-m-d');
483 if(!v && this.altFormats){
484 if(!this.altFormatsArray){
485 this.altFormatsArray = this.altFormats.split("|");
487 for(var i = 0, len = this.altFormatsArray.length; i < len && !v; i++){
488 v = Date.parseDate(value, this.altFormatsArray[i]);
494 formatDate : function(date, fmt)
496 return (!date || !(date instanceof Date)) ?
497 date : date.dateFormat(fmt || this.format);
502 Roo.bootstrap.DateField.superclass.onFocus.call(this);
508 Roo.bootstrap.DateField.superclass.onBlur.call(this);
510 var d = this.inputEl().getValue();
519 this.picker().show();
523 this.fireEvent('show', this, this.date);
528 if(this.isInline) return;
529 this.picker().hide();
530 this.viewMode = this.startViewMode;
533 this.fireEvent('hide', this, this.date);
537 onMousedown: function(e)
545 Roo.bootstrap.DateField.superclass.keyup.call(this);
549 setValue: function(v)
552 // v can be a string or a date..
555 var d = new Date(this.parseDate(v) ).clearTime();
557 if(isNaN(d.getTime())){
558 this.date = this.viewDate = '';
559 Roo.bootstrap.DateField.superclass.setValue.call(this, '');
563 v = this.formatDate(d);
565 Roo.bootstrap.DateField.superclass.setValue.call(this, v);
567 this.date = new Date(d.getTime() - d.getTimezoneOffset()*60000);
571 this.fireEvent('select', this, this.date);
577 return this.formatDate(this.date);
582 if (!this.picker().isVisible()){
583 if (e.keyCode == 27) // allow escape to hide and re-show picker
588 var dateChanged = false,
590 newDate, newViewDate;
599 if (!this.keyboardNavigation) break;
600 dir = e.keyCode == 37 ? -1 : 1;
603 newDate = this.moveYear(this.date, dir);
604 newViewDate = this.moveYear(this.viewDate, dir);
605 } else if (e.shiftKey){
606 newDate = this.moveMonth(this.date, dir);
607 newViewDate = this.moveMonth(this.viewDate, dir);
609 newDate = new Date(this.date);
610 newDate.setUTCDate(this.date.getUTCDate() + dir);
611 newViewDate = new Date(this.viewDate);
612 newViewDate.setUTCDate(this.viewDate.getUTCDate() + dir);
614 if (this.dateWithinRange(newDate)){
616 this.viewDate = newViewDate;
617 this.setValue(this.formatDate(this.date));
625 if (!this.keyboardNavigation) break;
626 dir = e.keyCode == 38 ? -1 : 1;
628 newDate = this.moveYear(this.date, dir);
629 newViewDate = this.moveYear(this.viewDate, dir);
630 } else if (e.shiftKey){
631 newDate = this.moveMonth(this.date, dir);
632 newViewDate = this.moveMonth(this.viewDate, dir);
634 newDate = new Date(this.date);
635 newDate.setUTCDate(this.date.getUTCDate() + dir * 7);
636 newViewDate = new Date(this.viewDate);
637 newViewDate.setUTCDate(this.viewDate.getUTCDate() + dir * 7);
639 if (this.dateWithinRange(newDate)){
641 this.viewDate = newViewDate;
642 this.setValue(this.formatDate(this.date));
649 this.setValue(this.formatDate(this.date));
654 this.setValue(this.formatDate(this.date));
673 var target = e.getTarget();
675 if(target.nodeName.toLowerCase() === 'i'){
676 target = Roo.get(target).dom.parentNode;
679 var nodeName = target.nodeName;
680 var className = target.className;
681 var html = target.innerHTML;
684 switch(nodeName.toLowerCase()) {
692 var dir = Roo.bootstrap.DateField.modes[this.viewMode].navStep * (className == 'prev' ? -1 : 1);
693 switch(this.viewMode){
695 this.viewDate = this.moveMonth(this.viewDate, dir);
699 this.viewDate = this.moveYear(this.viewDate, dir);
705 var date = new Date();
706 this.date = this.UTCDate(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0);
708 this.setValue(this.formatDate(this.date));
715 if (className.indexOf('disabled') < 0) {
716 this.viewDate.setUTCDate(1);
717 if (className.indexOf('month') > -1) {
718 this.viewDate.setUTCMonth(Roo.bootstrap.DateField.dates[this.language].monthsShort.indexOf(html));
720 var year = parseInt(html, 10) || 0;
721 this.viewDate.setUTCFullYear(year);
726 this.setValue(this.formatDate(this.viewDate));
737 //Roo.log(className);
738 if (className.indexOf('day') > -1 && className.indexOf('disabled') < 0 ){
739 var day = parseInt(html, 10) || 1;
740 var year = this.viewDate.getUTCFullYear(),
741 month = this.viewDate.getUTCMonth();
743 if (className.indexOf('old') > -1) {
750 } else if (className.indexOf('new') > -1) {
758 //Roo.log([year,month,day]);
759 this.date = this.UTCDate(year, month, day,0,0,0,0);
760 this.viewDate = this.UTCDate(year, month, Math.min(28, day),0,0,0,0);
762 //Roo.log(this.formatDate(this.date));
763 this.setValue(this.formatDate(this.date));
770 setStartDate: function(startDate)
772 this.startDate = startDate || -Infinity;
773 if (this.startDate !== -Infinity) {
774 this.startDate = this.parseDate(this.startDate);
777 this.updateNavArrows();
780 setEndDate: function(endDate)
782 this.endDate = endDate || Infinity;
783 if (this.endDate !== Infinity) {
784 this.endDate = this.parseDate(this.endDate);
787 this.updateNavArrows();
790 setDaysOfWeekDisabled: function(daysOfWeekDisabled)
792 this.daysOfWeekDisabled = daysOfWeekDisabled || [];
793 if (typeof(this.daysOfWeekDisabled) !== 'object') {
794 this.daysOfWeekDisabled = this.daysOfWeekDisabled.split(/,\s*/);
796 this.daysOfWeekDisabled = this.daysOfWeekDisabled.map(function (d) {
797 return parseInt(d, 10);
800 this.updateNavArrows();
803 updateNavArrows: function()
809 var d = new Date(this.viewDate),
810 year = d.getUTCFullYear(),
811 month = d.getUTCMonth();
813 Roo.each(this.picker().select('.prev', true).elements, function(v){
815 switch (this.viewMode) {
818 if (this.startDate !== -Infinity && year <= this.startDate.getUTCFullYear() && month <= this.startDate.getUTCMonth()) {
824 if (this.startDate !== -Infinity && year <= this.startDate.getUTCFullYear()) {
831 Roo.each(this.picker().select('.next', true).elements, function(v){
833 switch (this.viewMode) {
836 if (this.endDate !== Infinity && year >= this.endDate.getUTCFullYear() && month >= this.endDate.getUTCMonth()) {
842 if (this.endDate !== Infinity && year >= this.endDate.getUTCFullYear()) {
850 moveMonth: function(date, dir)
852 if (!dir) return date;
853 var new_date = new Date(date.valueOf()),
854 day = new_date.getUTCDate(),
855 month = new_date.getUTCMonth(),
858 dir = dir > 0 ? 1 : -1;
861 // If going back one month, make sure month is not current month
862 // (eg, Mar 31 -> Feb 31 == Feb 28, not Mar 02)
864 return new_date.getUTCMonth() == month;
866 // If going forward one month, make sure month is as expected
867 // (eg, Jan 31 -> Feb 31 == Feb 28, not Mar 02)
869 return new_date.getUTCMonth() != new_month;
871 new_month = month + dir;
872 new_date.setUTCMonth(new_month);
873 // Dec -> Jan (12) or Jan -> Dec (-1) -- limit expected date to 0-11
874 if (new_month < 0 || new_month > 11)
875 new_month = (new_month + 12) % 12;
877 // For magnitudes >1, move one month at a time...
878 for (var i=0; i<mag; i++)
879 // ...which might decrease the day (eg, Jan 31 to Feb 28, etc)...
880 new_date = this.moveMonth(new_date, dir);
881 // ...then reset the day, keeping it in the new month
882 new_month = new_date.getUTCMonth();
883 new_date.setUTCDate(day);
885 return new_month != new_date.getUTCMonth();
888 // Common date-resetting loop -- if date is beyond end of month, make it
891 new_date.setUTCDate(--day);
892 new_date.setUTCMonth(new_month);
897 moveYear: function(date, dir)
899 return this.moveMonth(date, dir*12);
902 dateWithinRange: function(date)
904 return date >= this.startDate && date <= this.endDate;
910 this.picker().remove();
915 Roo.apply(Roo.bootstrap.DateField, {
926 html: '<i class="fa fa-arrow-left"/>'
936 html: '<i class="fa fa-arrow-right"/>'
978 days: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"],
979 daysShort: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],
980 daysMin: ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa", "Su"],
981 months: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
982 monthsShort: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
1005 Roo.apply(Roo.bootstrap.DateField, {
1009 cls: 'datepicker dropdown-menu',
1013 cls: 'datepicker-days',
1017 cls: 'table-condensed',
1019 Roo.bootstrap.DateField.head,
1023 Roo.bootstrap.DateField.footer
1030 cls: 'datepicker-months',
1034 cls: 'table-condensed',
1036 Roo.bootstrap.DateField.head,
1037 Roo.bootstrap.DateField.content,
1038 Roo.bootstrap.DateField.footer
1045 cls: 'datepicker-years',
1049 cls: 'table-condensed',
1051 Roo.bootstrap.DateField.head,
1052 Roo.bootstrap.DateField.content,
1053 Roo.bootstrap.DateField.footer