3 * Copyright(c) 2006-2010 Ext JS, Inc.
5 * http://www.extjs.com/license
8 * @class Ext.DatePicker
9 * @extends Ext.Component
10 * <p>A popup date picker. This class is used by the {@link Ext.form.DateField DateField} class
11 * to allow browsing and selection of valid dates.</p>
12 * <p>All the string values documented below may be overridden by including an Ext locale file in
15 * Create a new DatePicker
16 * @param {Object} config The config object
19 Ext.DatePicker = Ext.extend(Ext.BoxComponent, {
21 * @cfg {String} todayText
22 * The text to display on the button that selects the current date (defaults to <code>'Today'</code>)
26 * @cfg {String} okText
27 * The text to display on the ok button (defaults to <code>' OK '</code> to give the user extra clicking room)
29 okText : ' OK ',
31 * @cfg {String} cancelText
32 * The text to display on the cancel button (defaults to <code>'Cancel'</code>)
34 cancelText : 'Cancel',
36 * @cfg {Function} handler
37 * Optional. A function that will handle the select event of this picker.
38 * The handler is passed the following parameters:<div class="mdetail-params"><ul>
39 * <li><code>picker</code> : DatePicker<div class="sub-desc">This DatePicker.</div></li>
40 * <li><code>date</code> : Date<div class="sub-desc">The selected date.</div></li>
45 * The scope (<code><b>this</b></code> reference) in which the <code>{@link #handler}</code>
46 * function will be called. Defaults to this DatePicker instance.
49 * @cfg {String} todayTip
50 * A string used to format the message for displaying in a tooltip over the button that
51 * selects the current date. Defaults to <code>'{0} (Spacebar)'</code> where
52 * the <code>{0}</code> token is replaced by today's date.
54 todayTip : '{0} (Spacebar)',
56 * @cfg {String} minText
57 * The error text to display if the minDate validation fails (defaults to <code>'This date is before the minimum date'</code>)
59 minText : 'This date is before the minimum date',
61 * @cfg {String} maxText
62 * The error text to display if the maxDate validation fails (defaults to <code>'This date is after the maximum date'</code>)
64 maxText : 'This date is after the maximum date',
66 * @cfg {String} format
67 * The default date format string which can be overriden for localization support. The format must be
68 * valid according to {@link Date#parseDate} (defaults to <code>'m/d/y'</code>).
72 * @cfg {String} disabledDaysText
73 * The tooltip to display when the date falls on a disabled day (defaults to <code>'Disabled'</code>)
75 disabledDaysText : 'Disabled',
77 * @cfg {String} disabledDatesText
78 * The tooltip text to display when the date falls on a disabled date (defaults to <code>'Disabled'</code>)
80 disabledDatesText : 'Disabled',
82 * @cfg {Array} monthNames
83 * An array of textual month names which can be overriden for localization support (defaults to Date.monthNames)
85 monthNames : Date.monthNames,
87 * @cfg {Array} dayNames
88 * An array of textual day names which can be overriden for localization support (defaults to Date.dayNames)
90 dayNames : Date.dayNames,
92 * @cfg {String} nextText
93 * The next month navigation button tooltip (defaults to <code>'Next Month (Control+Right)'</code>)
95 nextText : 'Next Month (Control+Right)',
97 * @cfg {String} prevText
98 * The previous month navigation button tooltip (defaults to <code>'Previous Month (Control+Left)'</code>)
100 prevText : 'Previous Month (Control+Left)',
102 * @cfg {String} monthYearText
103 * The header month selector tooltip (defaults to <code>'Choose a month (Control+Up/Down to move years)'</code>)
105 monthYearText : 'Choose a month (Control+Up/Down to move years)',
107 * @cfg {Number} startDay
108 * Day index at which the week should begin, 0-based (defaults to 0, which is Sunday)
112 * @cfg {Boolean} showToday
113 * False to hide the footer area containing the Today button and disable the keyboard handler for spacebar
114 * that selects the current date (defaults to <code>true</code>).
118 * @cfg {Date} minDate
119 * Minimum allowable date (JavaScript date object, defaults to null)
122 * @cfg {Date} maxDate
123 * Maximum allowable date (JavaScript date object, defaults to null)
126 * @cfg {Array} disabledDays
127 * An array of days to disable, 0-based. For example, [0, 6] disables Sunday and Saturday (defaults to null).
130 * @cfg {RegExp} disabledDatesRE
131 * JavaScript regular expression used to disable a pattern of dates (defaults to null). The {@link #disabledDates}
132 * config will generate this regex internally, but if you specify disabledDatesRE it will take precedence over the
133 * disabledDates value.
136 * @cfg {Array} disabledDates
137 * An array of 'dates' to disable, as strings. These strings will be used to build a dynamic regular
138 * expression so they are very powerful. Some examples:
140 * <li>['03/08/2003', '09/16/2003'] would disable those exact dates</li>
141 * <li>['03/08', '09/16'] would disable those days for every year</li>
142 * <li>['^03/08'] would only match the beginning (useful if you are using short years)</li>
143 * <li>['03/../2006'] would disable every day in March 2006</li>
144 * <li>['^03'] would disable every day in every March</li>
146 * Note that the format of the dates included in the array should exactly match the {@link #format} config.
147 * In order to support regular expressions, if you are using a date format that has '.' in it, you will have to
148 * escape the dot when restricting dates. For example: ['03\\.08\\.03'].
152 // Set by other components to stop the picker focus being updated when the value changes.
155 // default value used to initialise each date in the DatePicker
156 // (note: 12 noon was chosen because it steers well clear of all DST timezone changes)
157 initHour: 12, // 24-hour format
160 initComponent : function(){
161 Ext.DatePicker.superclass.initComponent.call(this);
163 this.value = this.value ?
164 this.value.clearTime(true) : new Date().clearTime();
169 * Fires when a date is selected
170 * @param {DatePicker} this DatePicker
171 * @param {Date} date The selected date
177 this.on('select', this.handler, this.scope || this);
180 this.initDisabledDays();
184 initDisabledDays : function(){
185 if(!this.disabledDatesRE && this.disabledDates){
186 var dd = this.disabledDates,
190 Ext.each(dd, function(d, i){
191 re += Ext.isDate(d) ? '^' + Ext.escapeRe(d.dateFormat(this.format)) + '$' : dd[i];
196 this.disabledDatesRE = new RegExp(re + ')');
201 * Replaces any existing disabled dates with new values and refreshes the DatePicker.
202 * @param {Array/RegExp} disabledDates An array of date strings (see the {@link #disabledDates} config
203 * for details on supported values), or a JavaScript regular expression used to disable a pattern of dates.
205 setDisabledDates : function(dd){
207 this.disabledDates = dd;
208 this.disabledDatesRE = null;
210 this.disabledDatesRE = dd;
212 this.initDisabledDays();
213 this.update(this.value, true);
217 * Replaces any existing disabled days (by index, 0-6) with new values and refreshes the DatePicker.
218 * @param {Array} disabledDays An array of disabled day indexes. See the {@link #disabledDays} config
219 * for details on supported values.
221 setDisabledDays : function(dd){
222 this.disabledDays = dd;
223 this.update(this.value, true);
227 * Replaces any existing {@link #minDate} with the new value and refreshes the DatePicker.
228 * @param {Date} value The minimum date that can be selected
230 setMinDate : function(dt){
232 this.update(this.value, true);
236 * Replaces any existing {@link #maxDate} with the new value and refreshes the DatePicker.
237 * @param {Date} value The maximum date that can be selected
239 setMaxDate : function(dt){
241 this.update(this.value, true);
245 * Sets the value of the date field
246 * @param {Date} value The date to set
248 setValue : function(value){
249 this.value = value.clearTime(true);
250 this.update(this.value);
254 * Gets the current selected value of the date field
255 * @return {Date} The selected date
257 getValue : function(){
263 this.update(this.activeDate);
267 onEnable: function(initial){
268 Ext.DatePicker.superclass.onEnable.call(this);
269 this.doDisabled(false);
270 this.update(initial ? this.value : this.activeDate);
278 onDisable : function(){
279 Ext.DatePicker.superclass.onDisable.call(this);
280 this.doDisabled(true);
281 if(Ext.isIE && !Ext.isIE8){
282 /* Really strange problem in IE6/7, when disabled, have to explicitly
283 * repaint each of the nodes to get them to display correctly, simply
284 * calling repaint on the main element doesn't appear to be enough.
286 Ext.each([].concat(this.textNodes, this.el.query('th span')), function(el){
287 Ext.fly(el).repaint();
293 doDisabled : function(disabled){
294 this.keyNav.setDisabled(disabled);
295 this.prevRepeater.setDisabled(disabled);
296 this.nextRepeater.setDisabled(disabled);
298 this.todayKeyListener.setDisabled(disabled);
299 this.todayBtn.setDisabled(disabled);
304 onRender : function(container, position){
306 '<table cellspacing="0">',
307 '<tr><td class="x-date-left"><a href="#" title="', this.prevText ,'"> </a></td><td class="x-date-middle" align="center"></td><td class="x-date-right"><a href="#" title="', this.nextText ,'"> </a></td></tr>',
308 '<tr><td colspan="3"><table class="x-date-inner" cellspacing="0"><thead><tr>'],
311 for(i = 0; i < 7; i++){
312 var d = this.startDay+i;
316 m.push('<th><span>', dn[d].substr(0,1), '</span></th>');
318 m[m.length] = '</tr></thead><tbody><tr>';
319 for(i = 0; i < 42; i++) {
320 if(i % 7 === 0 && i !== 0){
321 m[m.length] = '</tr><tr>';
323 m[m.length] = '<td><a href="#" hidefocus="on" class="x-date-date" tabIndex="1"><em><span></span></em></a></td>';
325 m.push('</tr></tbody></table></td></tr>',
326 this.showToday ? '<tr><td colspan="3" class="x-date-bottom" align="center"></td></tr>' : '',
327 '</table><div class="x-date-mp"></div>');
329 var el = document.createElement('div');
330 el.className = 'x-date-picker';
331 el.innerHTML = m.join('');
333 container.dom.insertBefore(el, position);
335 this.el = Ext.get(el);
336 this.eventEl = Ext.get(el.firstChild);
338 this.prevRepeater = new Ext.util.ClickRepeater(this.el.child('td.x-date-left a'), {
339 handler: this.showPrevMonth,
345 this.nextRepeater = new Ext.util.ClickRepeater(this.el.child('td.x-date-right a'), {
346 handler: this.showNextMonth,
352 this.monthPicker = this.el.down('div.x-date-mp');
353 this.monthPicker.enableDisplayMode('block');
355 this.keyNav = new Ext.KeyNav(this.eventEl, {
356 'left' : function(e){
358 this.showPrevMonth();
360 this.update(this.activeDate.add('d', -1));
364 'right' : function(e){
366 this.showNextMonth();
368 this.update(this.activeDate.add('d', 1));
376 this.update(this.activeDate.add('d', -7));
380 'down' : function(e){
384 this.update(this.activeDate.add('d', 7));
388 'pageUp' : function(e){
389 this.showNextMonth();
392 'pageDown' : function(e){
393 this.showPrevMonth();
396 'enter' : function(e){
404 this.el.unselectable();
406 this.cells = this.el.select('table.x-date-inner tbody td');
407 this.textNodes = this.el.query('table.x-date-inner tbody span');
409 this.mbtn = new Ext.Button({
411 tooltip: this.monthYearText,
412 renderTo: this.el.child('td.x-date-middle', true)
414 this.mbtn.el.child('em').addClass('x-btn-arrow');
417 this.todayKeyListener = this.eventEl.addKeyListener(Ext.EventObject.SPACE, this.selectToday, this);
418 var today = (new Date()).dateFormat(this.format);
419 this.todayBtn = new Ext.Button({
420 renderTo: this.el.child('td.x-date-bottom', true),
421 text: String.format(this.todayText, today),
422 tooltip: String.format(this.todayTip, today),
423 handler: this.selectToday,
427 this.mon(this.eventEl, 'mousewheel', this.handleMouseWheel, this);
428 this.mon(this.eventEl, 'click', this.handleDateClick, this, {delegate: 'a.x-date-date'});
429 this.mon(this.mbtn, 'click', this.showMonthPicker, this);
434 createMonthPicker : function(){
435 if(!this.monthPicker.dom.firstChild){
436 var buf = ['<table border="0" cellspacing="0">'];
437 for(var i = 0; i < 6; i++){
439 '<tr><td class="x-date-mp-month"><a href="#">', Date.getShortMonthName(i), '</a></td>',
440 '<td class="x-date-mp-month x-date-mp-sep"><a href="#">', Date.getShortMonthName(i + 6), '</a></td>',
442 '<td class="x-date-mp-ybtn" align="center"><a class="x-date-mp-prev"></a></td><td class="x-date-mp-ybtn" align="center"><a class="x-date-mp-next"></a></td></tr>' :
443 '<td class="x-date-mp-year"><a href="#"></a></td><td class="x-date-mp-year"><a href="#"></a></td></tr>'
447 '<tr class="x-date-mp-btns"><td colspan="4"><button type="button" class="x-date-mp-ok">',
449 '</button><button type="button" class="x-date-mp-cancel">',
451 '</button></td></tr>',
454 this.monthPicker.update(buf.join(''));
456 this.mon(this.monthPicker, 'click', this.onMonthClick, this);
457 this.mon(this.monthPicker, 'dblclick', this.onMonthDblClick, this);
459 this.mpMonths = this.monthPicker.select('td.x-date-mp-month');
460 this.mpYears = this.monthPicker.select('td.x-date-mp-year');
462 this.mpMonths.each(function(m, a, i){
465 m.dom.xmonth = 5 + Math.round(i * 0.5);
467 m.dom.xmonth = Math.round((i-1) * 0.5);
474 showMonthPicker : function(){
476 this.createMonthPicker();
477 var size = this.el.getSize();
478 this.monthPicker.setSize(size);
479 this.monthPicker.child('table').setSize(size);
481 this.mpSelMonth = (this.activeDate || this.value).getMonth();
482 this.updateMPMonth(this.mpSelMonth);
483 this.mpSelYear = (this.activeDate || this.value).getFullYear();
484 this.updateMPYear(this.mpSelYear);
486 this.monthPicker.slideIn('t', {duration:0.2});
491 updateMPYear : function(y){
493 var ys = this.mpYears.elements;
494 for(var i = 1; i <= 10; i++){
495 var td = ys[i-1], y2;
497 y2 = y + Math.round(i * 0.5);
498 td.firstChild.innerHTML = y2;
501 y2 = y - (5-Math.round(i * 0.5));
502 td.firstChild.innerHTML = y2;
505 this.mpYears.item(i-1)[y2 == this.mpSelYear ? 'addClass' : 'removeClass']('x-date-mp-sel');
510 updateMPMonth : function(sm){
511 this.mpMonths.each(function(m, a, i){
512 m[m.dom.xmonth == sm ? 'addClass' : 'removeClass']('x-date-mp-sel');
517 selectMPMonth : function(m){
522 onMonthClick : function(e, t){
524 var el = new Ext.Element(t), pn;
525 if(el.is('button.x-date-mp-cancel')){
526 this.hideMonthPicker();
528 else if(el.is('button.x-date-mp-ok')){
529 var d = new Date(this.mpSelYear, this.mpSelMonth, (this.activeDate || this.value).getDate());
530 if(d.getMonth() != this.mpSelMonth){
531 // 'fix' the JS rolling date conversion if needed
532 d = new Date(this.mpSelYear, this.mpSelMonth, 1).getLastDateOfMonth();
535 this.hideMonthPicker();
537 else if((pn = el.up('td.x-date-mp-month', 2))){
538 this.mpMonths.removeClass('x-date-mp-sel');
539 pn.addClass('x-date-mp-sel');
540 this.mpSelMonth = pn.dom.xmonth;
542 else if((pn = el.up('td.x-date-mp-year', 2))){
543 this.mpYears.removeClass('x-date-mp-sel');
544 pn.addClass('x-date-mp-sel');
545 this.mpSelYear = pn.dom.xyear;
547 else if(el.is('a.x-date-mp-prev')){
548 this.updateMPYear(this.mpyear-10);
550 else if(el.is('a.x-date-mp-next')){
551 this.updateMPYear(this.mpyear+10);
556 onMonthDblClick : function(e, t){
558 var el = new Ext.Element(t), pn;
559 if((pn = el.up('td.x-date-mp-month', 2))){
560 this.update(new Date(this.mpSelYear, pn.dom.xmonth, (this.activeDate || this.value).getDate()));
561 this.hideMonthPicker();
563 else if((pn = el.up('td.x-date-mp-year', 2))){
564 this.update(new Date(pn.dom.xyear, this.mpSelMonth, (this.activeDate || this.value).getDate()));
565 this.hideMonthPicker();
570 hideMonthPicker : function(disableAnim){
571 if(this.monthPicker){
572 if(disableAnim === true){
573 this.monthPicker.hide();
575 this.monthPicker.slideOut('t', {duration:0.2});
581 showPrevMonth : function(e){
582 this.update(this.activeDate.add('mo', -1));
586 showNextMonth : function(e){
587 this.update(this.activeDate.add('mo', 1));
591 showPrevYear : function(){
592 this.update(this.activeDate.add('y', -1));
596 showNextYear : function(){
597 this.update(this.activeDate.add('y', 1));
601 handleMouseWheel : function(e){
604 var delta = e.getWheelDelta();
606 this.showPrevMonth();
607 } else if(delta < 0){
608 this.showNextMonth();
614 handleDateClick : function(e, t){
616 if(!this.disabled && t.dateValue && !Ext.fly(t.parentNode).hasClass('x-date-disabled')){
617 this.cancelFocus = this.focusOnSelect === false;
618 this.setValue(new Date(t.dateValue));
619 delete this.cancelFocus;
620 this.fireEvent('select', this, this.value);
625 selectToday : function(){
626 if(this.todayBtn && !this.todayBtn.disabled){
627 this.setValue(new Date().clearTime());
628 this.fireEvent('select', this, this.value);
633 update : function(date, forceRefresh){
635 var vd = this.activeDate, vis = this.isVisible();
636 this.activeDate = date;
637 if(!forceRefresh && vd && this.el){
638 var t = date.getTime();
639 if(vd.getMonth() == date.getMonth() && vd.getFullYear() == date.getFullYear()){
640 this.cells.removeClass('x-date-selected');
641 this.cells.each(function(c){
642 if(c.dom.firstChild.dateValue == t){
643 c.addClass('x-date-selected');
644 if(vis && !this.cancelFocus){
645 Ext.fly(c.dom.firstChild).focus(50);
653 var days = date.getDaysInMonth(),
654 firstOfMonth = date.getFirstDateOfMonth(),
655 startingPos = firstOfMonth.getDay()-this.startDay;
662 var pm = date.add('mo', -1),
663 prevStart = pm.getDaysInMonth()-startingPos,
664 cells = this.cells.elements,
665 textEls = this.textNodes,
666 // convert everything to numbers so it's fast
667 d = (new Date(pm.getFullYear(), pm.getMonth(), prevStart, this.initHour)),
668 today = new Date().clearTime().getTime(),
669 sel = date.clearTime(true).getTime(),
670 min = this.minDate ? this.minDate.clearTime(true) : Number.NEGATIVE_INFINITY,
671 max = this.maxDate ? this.maxDate.clearTime(true) : Number.POSITIVE_INFINITY,
672 ddMatch = this.disabledDatesRE,
673 ddText = this.disabledDatesText,
674 ddays = this.disabledDays ? this.disabledDays.join('') : false,
675 ddaysText = this.disabledDaysText,
676 format = this.format;
679 var td = new Date().clearTime(),
680 disable = (td < min || td > max ||
681 (ddMatch && format && ddMatch.test(td.dateFormat(format))) ||
682 (ddays && ddays.indexOf(td.getDay()) != -1));
685 this.todayBtn.setDisabled(disable);
686 this.todayKeyListener[disable ? 'disable' : 'enable']();
690 var setCellClass = function(cal, cell){
692 var t = d.clearTime(true).getTime();
693 cell.firstChild.dateValue = t;
695 cell.className += ' x-date-today';
696 cell.title = cal.todayText;
699 cell.className += ' x-date-selected';
701 Ext.fly(cell.firstChild).focus(50);
706 cell.className = ' x-date-disabled';
707 cell.title = cal.minText;
711 cell.className = ' x-date-disabled';
712 cell.title = cal.maxText;
716 if(ddays.indexOf(d.getDay()) != -1){
717 cell.title = ddaysText;
718 cell.className = ' x-date-disabled';
721 if(ddMatch && format){
722 var fvalue = d.dateFormat(format);
723 if(ddMatch.test(fvalue)){
724 cell.title = ddText.replace('%0', fvalue);
725 cell.className = ' x-date-disabled';
731 for(; i < startingPos; i++) {
732 textEls[i].innerHTML = (++prevStart);
733 d.setDate(d.getDate()+1);
734 cells[i].className = 'x-date-prevday';
735 setCellClass(this, cells[i]);
737 for(; i < days; i++){
738 var intDay = i - startingPos + 1;
739 textEls[i].innerHTML = (intDay);
740 d.setDate(d.getDate()+1);
741 cells[i].className = 'x-date-active';
742 setCellClass(this, cells[i]);
746 textEls[i].innerHTML = (++extraDays);
747 d.setDate(d.getDate()+1);
748 cells[i].className = 'x-date-nextday';
749 setCellClass(this, cells[i]);
752 this.mbtn.setText(this.monthNames[date.getMonth()] + ' ' + date.getFullYear());
754 if(!this.internalRender){
755 var main = this.el.dom.firstChild,
756 w = main.offsetWidth;
757 this.el.setWidth(w + this.el.getBorderWidth('lr'));
758 Ext.fly(main).setWidth(w);
759 this.internalRender = true;
760 // opera does not respect the auto grow header center column
761 // then, after it gets a width opera refuses to recalculate
762 // without a second pass
763 if(Ext.isOpera && !this.secondPass){
764 main.rows[0].cells[1].style.width = (w - (main.rows[0].cells[0].offsetWidth+main.rows[0].cells[2].offsetWidth)) + 'px';
765 this.secondPass = true;
766 this.update.defer(10, this, [date]);
773 beforeDestroy : function() {
785 delete this.textNodes;
786 delete this.cells.elements;
791 * @cfg {String} autoEl @hide
795 Ext.reg('datepicker', Ext.DatePicker);