3 * Copyright(c) 2006-2009 Ext JS, LLC
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.
156 initComponent : function(){
157 Ext.DatePicker.superclass.initComponent.call(this);
159 this.value = this.value ?
160 this.value.clearTime(true) : new Date().clearTime();
165 * Fires when a date is selected
166 * @param {DatePicker} this DatePicker
167 * @param {Date} date The selected date
173 this.on('select', this.handler, this.scope || this);
176 this.initDisabledDays();
180 initDisabledDays : function(){
181 if(!this.disabledDatesRE && this.disabledDates){
182 var dd = this.disabledDates,
186 Ext.each(dd, function(d, i){
187 re += Ext.isDate(d) ? '^' + Ext.escapeRe(d.dateFormat(this.format)) + '$' : dd[i];
192 this.disabledDatesRE = new RegExp(re + ')');
197 * Replaces any existing disabled dates with new values and refreshes the DatePicker.
198 * @param {Array/RegExp} disabledDates An array of date strings (see the {@link #disabledDates} config
199 * for details on supported values), or a JavaScript regular expression used to disable a pattern of dates.
201 setDisabledDates : function(dd){
203 this.disabledDates = dd;
204 this.disabledDatesRE = null;
206 this.disabledDatesRE = dd;
208 this.initDisabledDays();
209 this.update(this.value, true);
213 * Replaces any existing disabled days (by index, 0-6) with new values and refreshes the DatePicker.
214 * @param {Array} disabledDays An array of disabled day indexes. See the {@link #disabledDays} config
215 * for details on supported values.
217 setDisabledDays : function(dd){
218 this.disabledDays = dd;
219 this.update(this.value, true);
223 * Replaces any existing {@link #minDate} with the new value and refreshes the DatePicker.
224 * @param {Date} value The minimum date that can be selected
226 setMinDate : function(dt){
228 this.update(this.value, true);
232 * Replaces any existing {@link #maxDate} with the new value and refreshes the DatePicker.
233 * @param {Date} value The maximum date that can be selected
235 setMaxDate : function(dt){
237 this.update(this.value, true);
241 * Sets the value of the date field
242 * @param {Date} value The date to set
244 setValue : function(value){
245 this.value = value.clearTime(true);
246 this.update(this.value);
250 * Gets the current selected value of the date field
251 * @return {Date} The selected date
253 getValue : function(){
259 this.update(this.activeDate);
263 onEnable: function(initial){
264 Ext.DatePicker.superclass.onEnable.call(this);
265 this.doDisabled(false);
266 this.update(initial ? this.value : this.activeDate);
274 onDisable : function(){
275 Ext.DatePicker.superclass.onDisable.call(this);
276 this.doDisabled(true);
277 if(Ext.isIE && !Ext.isIE8){
278 /* Really strange problem in IE6/7, when disabled, have to explicitly
279 * repaint each of the nodes to get them to display correctly, simply
280 * calling repaint on the main element doesn't appear to be enough.
282 Ext.each([].concat(this.textNodes, this.el.query('th span')), function(el){
283 Ext.fly(el).repaint();
289 doDisabled : function(disabled){
290 this.keyNav.setDisabled(disabled);
291 this.prevRepeater.setDisabled(disabled);
292 this.nextRepeater.setDisabled(disabled);
294 this.todayKeyListener.setDisabled(disabled);
295 this.todayBtn.setDisabled(disabled);
300 onRender : function(container, position){
302 '<table cellspacing="0">',
303 '<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>',
304 '<tr><td colspan="3"><table class="x-date-inner" cellspacing="0"><thead><tr>'],
307 for(i = 0; i < 7; i++){
308 var d = this.startDay+i;
312 m.push('<th><span>', dn[d].substr(0,1), '</span></th>');
314 m[m.length] = '</tr></thead><tbody><tr>';
315 for(i = 0; i < 42; i++) {
316 if(i % 7 === 0 && i !== 0){
317 m[m.length] = '</tr><tr>';
319 m[m.length] = '<td><a href="#" hidefocus="on" class="x-date-date" tabIndex="1"><em><span></span></em></a></td>';
321 m.push('</tr></tbody></table></td></tr>',
322 this.showToday ? '<tr><td colspan="3" class="x-date-bottom" align="center"></td></tr>' : '',
323 '</table><div class="x-date-mp"></div>');
325 var el = document.createElement('div');
326 el.className = 'x-date-picker';
327 el.innerHTML = m.join('');
329 container.dom.insertBefore(el, position);
331 this.el = Ext.get(el);
332 this.eventEl = Ext.get(el.firstChild);
334 this.prevRepeater = new Ext.util.ClickRepeater(this.el.child('td.x-date-left a'), {
335 handler: this.showPrevMonth,
341 this.nextRepeater = new Ext.util.ClickRepeater(this.el.child('td.x-date-right a'), {
342 handler: this.showNextMonth,
348 this.monthPicker = this.el.down('div.x-date-mp');
349 this.monthPicker.enableDisplayMode('block');
351 this.keyNav = new Ext.KeyNav(this.eventEl, {
352 'left' : function(e){
354 this.showPrevMonth();
356 this.update(this.activeDate.add('d', -1));
360 'right' : function(e){
362 this.showNextMonth();
364 this.update(this.activeDate.add('d', 1));
372 this.update(this.activeDate.add('d', -7));
376 'down' : function(e){
380 this.update(this.activeDate.add('d', 7));
384 'pageUp' : function(e){
385 this.showNextMonth();
388 'pageDown' : function(e){
389 this.showPrevMonth();
392 'enter' : function(e){
400 this.el.unselectable();
402 this.cells = this.el.select('table.x-date-inner tbody td');
403 this.textNodes = this.el.query('table.x-date-inner tbody span');
405 this.mbtn = new Ext.Button({
407 tooltip: this.monthYearText,
408 renderTo: this.el.child('td.x-date-middle', true)
410 this.mbtn.el.child('em').addClass('x-btn-arrow');
413 this.todayKeyListener = this.eventEl.addKeyListener(Ext.EventObject.SPACE, this.selectToday, this);
414 var today = (new Date()).dateFormat(this.format);
415 this.todayBtn = new Ext.Button({
416 renderTo: this.el.child('td.x-date-bottom', true),
417 text: String.format(this.todayText, today),
418 tooltip: String.format(this.todayTip, today),
419 handler: this.selectToday,
423 this.mon(this.eventEl, 'mousewheel', this.handleMouseWheel, this);
424 this.mon(this.eventEl, 'click', this.handleDateClick, this, {delegate: 'a.x-date-date'});
425 this.mon(this.mbtn, 'click', this.showMonthPicker, this);
430 createMonthPicker : function(){
431 if(!this.monthPicker.dom.firstChild){
432 var buf = ['<table border="0" cellspacing="0">'];
433 for(var i = 0; i < 6; i++){
435 '<tr><td class="x-date-mp-month"><a href="#">', Date.getShortMonthName(i), '</a></td>',
436 '<td class="x-date-mp-month x-date-mp-sep"><a href="#">', Date.getShortMonthName(i + 6), '</a></td>',
438 '<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>' :
439 '<td class="x-date-mp-year"><a href="#"></a></td><td class="x-date-mp-year"><a href="#"></a></td></tr>'
443 '<tr class="x-date-mp-btns"><td colspan="4"><button type="button" class="x-date-mp-ok">',
445 '</button><button type="button" class="x-date-mp-cancel">',
447 '</button></td></tr>',
450 this.monthPicker.update(buf.join(''));
452 this.mon(this.monthPicker, 'click', this.onMonthClick, this);
453 this.mon(this.monthPicker, 'dblclick', this.onMonthDblClick, this);
455 this.mpMonths = this.monthPicker.select('td.x-date-mp-month');
456 this.mpYears = this.monthPicker.select('td.x-date-mp-year');
458 this.mpMonths.each(function(m, a, i){
461 m.dom.xmonth = 5 + Math.round(i * 0.5);
463 m.dom.xmonth = Math.round((i-1) * 0.5);
470 showMonthPicker : function(){
472 this.createMonthPicker();
473 var size = this.el.getSize();
474 this.monthPicker.setSize(size);
475 this.monthPicker.child('table').setSize(size);
477 this.mpSelMonth = (this.activeDate || this.value).getMonth();
478 this.updateMPMonth(this.mpSelMonth);
479 this.mpSelYear = (this.activeDate || this.value).getFullYear();
480 this.updateMPYear(this.mpSelYear);
482 this.monthPicker.slideIn('t', {duration:0.2});
487 updateMPYear : function(y){
489 var ys = this.mpYears.elements;
490 for(var i = 1; i <= 10; i++){
491 var td = ys[i-1], y2;
493 y2 = y + Math.round(i * 0.5);
494 td.firstChild.innerHTML = y2;
497 y2 = y - (5-Math.round(i * 0.5));
498 td.firstChild.innerHTML = y2;
501 this.mpYears.item(i-1)[y2 == this.mpSelYear ? 'addClass' : 'removeClass']('x-date-mp-sel');
506 updateMPMonth : function(sm){
507 this.mpMonths.each(function(m, a, i){
508 m[m.dom.xmonth == sm ? 'addClass' : 'removeClass']('x-date-mp-sel');
513 selectMPMonth : function(m){
518 onMonthClick : function(e, t){
520 var el = new Ext.Element(t), pn;
521 if(el.is('button.x-date-mp-cancel')){
522 this.hideMonthPicker();
524 else if(el.is('button.x-date-mp-ok')){
525 var d = new Date(this.mpSelYear, this.mpSelMonth, (this.activeDate || this.value).getDate());
526 if(d.getMonth() != this.mpSelMonth){
527 // 'fix' the JS rolling date conversion if needed
528 d = new Date(this.mpSelYear, this.mpSelMonth, 1).getLastDateOfMonth();
531 this.hideMonthPicker();
533 else if((pn = el.up('td.x-date-mp-month', 2))){
534 this.mpMonths.removeClass('x-date-mp-sel');
535 pn.addClass('x-date-mp-sel');
536 this.mpSelMonth = pn.dom.xmonth;
538 else if((pn = el.up('td.x-date-mp-year', 2))){
539 this.mpYears.removeClass('x-date-mp-sel');
540 pn.addClass('x-date-mp-sel');
541 this.mpSelYear = pn.dom.xyear;
543 else if(el.is('a.x-date-mp-prev')){
544 this.updateMPYear(this.mpyear-10);
546 else if(el.is('a.x-date-mp-next')){
547 this.updateMPYear(this.mpyear+10);
552 onMonthDblClick : function(e, t){
554 var el = new Ext.Element(t), pn;
555 if((pn = el.up('td.x-date-mp-month', 2))){
556 this.update(new Date(this.mpSelYear, pn.dom.xmonth, (this.activeDate || this.value).getDate()));
557 this.hideMonthPicker();
559 else if((pn = el.up('td.x-date-mp-year', 2))){
560 this.update(new Date(pn.dom.xyear, this.mpSelMonth, (this.activeDate || this.value).getDate()));
561 this.hideMonthPicker();
566 hideMonthPicker : function(disableAnim){
567 if(this.monthPicker){
568 if(disableAnim === true){
569 this.monthPicker.hide();
571 this.monthPicker.slideOut('t', {duration:0.2});
577 showPrevMonth : function(e){
578 this.update(this.activeDate.add('mo', -1));
582 showNextMonth : function(e){
583 this.update(this.activeDate.add('mo', 1));
587 showPrevYear : function(){
588 this.update(this.activeDate.add('y', -1));
592 showNextYear : function(){
593 this.update(this.activeDate.add('y', 1));
597 handleMouseWheel : function(e){
600 var delta = e.getWheelDelta();
602 this.showPrevMonth();
603 } else if(delta < 0){
604 this.showNextMonth();
610 handleDateClick : function(e, t){
612 if(!this.disabled && t.dateValue && !Ext.fly(t.parentNode).hasClass('x-date-disabled')){
613 this.cancelFocus = this.focusOnSelect === false;
614 this.setValue(new Date(t.dateValue));
615 delete this.cancelFocus;
616 this.fireEvent('select', this, this.value);
621 selectToday : function(){
622 if(this.todayBtn && !this.todayBtn.disabled){
623 this.setValue(new Date().clearTime());
624 this.fireEvent('select', this, this.value);
629 update : function(date, forceRefresh){
631 var vd = this.activeDate, vis = this.isVisible();
632 this.activeDate = date;
633 if(!forceRefresh && vd && this.el){
634 var t = date.getTime();
635 if(vd.getMonth() == date.getMonth() && vd.getFullYear() == date.getFullYear()){
636 this.cells.removeClass('x-date-selected');
637 this.cells.each(function(c){
638 if(c.dom.firstChild.dateValue == t){
639 c.addClass('x-date-selected');
640 if(vis && !this.cancelFocus){
641 Ext.fly(c.dom.firstChild).focus(50);
649 var days = date.getDaysInMonth(),
650 firstOfMonth = date.getFirstDateOfMonth(),
651 startingPos = firstOfMonth.getDay()-this.startDay;
658 var pm = date.add('mo', -1),
659 prevStart = pm.getDaysInMonth()-startingPos,
660 cells = this.cells.elements,
661 textEls = this.textNodes,
662 // convert everything to numbers so it's fast
664 d = (new Date(pm.getFullYear(), pm.getMonth(), prevStart)).clearTime(),
665 today = new Date().clearTime().getTime(),
666 sel = date.clearTime(true).getTime(),
667 min = this.minDate ? this.minDate.clearTime(true) : Number.NEGATIVE_INFINITY,
668 max = this.maxDate ? this.maxDate.clearTime(true) : Number.POSITIVE_INFINITY,
669 ddMatch = this.disabledDatesRE,
670 ddText = this.disabledDatesText,
671 ddays = this.disabledDays ? this.disabledDays.join('') : false,
672 ddaysText = this.disabledDaysText,
673 format = this.format;
676 var td = new Date().clearTime(),
677 disable = (td < min || td > max ||
678 (ddMatch && format && ddMatch.test(td.dateFormat(format))) ||
679 (ddays && ddays.indexOf(td.getDay()) != -1));
682 this.todayBtn.setDisabled(disable);
683 this.todayKeyListener[disable ? 'disable' : 'enable']();
687 var setCellClass = function(cal, cell){
690 cell.firstChild.dateValue = t;
692 cell.className += ' x-date-today';
693 cell.title = cal.todayText;
696 cell.className += ' x-date-selected';
698 Ext.fly(cell.firstChild).focus(50);
703 cell.className = ' x-date-disabled';
704 cell.title = cal.minText;
708 cell.className = ' x-date-disabled';
709 cell.title = cal.maxText;
713 if(ddays.indexOf(d.getDay()) != -1){
714 cell.title = ddaysText;
715 cell.className = ' x-date-disabled';
718 if(ddMatch && format){
719 var fvalue = d.dateFormat(format);
720 if(ddMatch.test(fvalue)){
721 cell.title = ddText.replace('%0', fvalue);
722 cell.className = ' x-date-disabled';
728 for(; i < startingPos; i++) {
729 textEls[i].innerHTML = (++prevStart);
730 d.setDate(d.getDate()+1);
731 cells[i].className = 'x-date-prevday';
732 setCellClass(this, cells[i]);
734 for(; i < days; i++){
735 var intDay = i - startingPos + 1;
736 textEls[i].innerHTML = (intDay);
737 d.setDate(d.getDate()+1);
738 cells[i].className = 'x-date-active';
739 setCellClass(this, cells[i]);
743 textEls[i].innerHTML = (++extraDays);
744 d.setDate(d.getDate()+1);
745 cells[i].className = 'x-date-nextday';
746 setCellClass(this, cells[i]);
749 this.mbtn.setText(this.monthNames[date.getMonth()] + ' ' + date.getFullYear());
751 if(!this.internalRender){
752 var main = this.el.dom.firstChild,
753 w = main.offsetWidth;
754 this.el.setWidth(w + this.el.getBorderWidth('lr'));
755 Ext.fly(main).setWidth(w);
756 this.internalRender = true;
757 // opera does not respect the auto grow header center column
758 // then, after it gets a width opera refuses to recalculate
759 // without a second pass
760 if(Ext.isOpera && !this.secondPass){
761 main.rows[0].cells[1].style.width = (w - (main.rows[0].cells[0].offsetWidth+main.rows[0].cells[2].offsetWidth)) + 'px';
762 this.secondPass = true;
763 this.update.defer(10, this, [date]);
770 beforeDestroy : function() {
782 delete this.textNodes;
783 delete this.cells.elements;
788 * @cfg {String} autoEl @hide
792 Ext.reg('datepicker', Ext.DatePicker);