4 * Copyright(c) 2006-2007, Ext JS, LLC.
6 * Originally Released Under LGPL - original licence link has changed is not relivant.
9 * <script type="text/javascript">
13 * @class Roo.DatePicker
14 * @extends Roo.Component
15 * Simple date picker class.
17 * Create a new DatePicker
18 * @param {Object} config The config object
20 Roo.DatePicker = function(config){
21 Roo.DatePicker.superclass.constructor.call(this, config);
23 this.value = config && config.value ?
24 config.value.clearTime() : new Date().clearTime();
29 * Fires when a date is selected
30 * @param {DatePicker} this
31 * @param {Date} date The selected date
36 * Fires when the displayed month changes
37 * @param {DatePicker} this
38 * @param {Date} date The selected month
44 this.on("select", this.handler, this.scope || this);
46 // build the disabledDatesRE
47 if(!this.disabledDatesRE && this.disabledDates){
48 var dd = this.disabledDates;
50 for(var i = 0; i < dd.length; i++){
52 if(i != dd.length-1) re += "|";
54 this.disabledDatesRE = new RegExp(re + ")");
58 Roo.extend(Roo.DatePicker, Roo.Component, {
60 * @cfg {String} todayText
61 * The text to display on the button that selects the current date (defaults to "Today")
65 * @cfg {String} okText
66 * The text to display on the ok button
68 okText : " OK ", //   to give the user extra clicking room
70 * @cfg {String} cancelText
71 * The text to display on the cancel button
73 cancelText : "Cancel",
75 * @cfg {String} todayTip
76 * The tooltip to display for the button that selects the current date (defaults to "{current date} (Spacebar)")
78 todayTip : "{0} (Spacebar)",
81 * Minimum allowable date (JavaScript date object, defaults to null)
86 * Maximum allowable date (JavaScript date object, defaults to null)
90 * @cfg {String} minText
91 * The error text to display if the minDate validation fails (defaults to "This date is before the minimum date")
93 minText : "This date is before the minimum date",
95 * @cfg {String} maxText
96 * The error text to display if the maxDate validation fails (defaults to "This date is after the maximum date")
98 maxText : "This date is after the maximum date",
100 * @cfg {String} format
101 * The default date format string which can be overriden for localization support. The format must be
102 * valid according to {@link Date#parseDate} (defaults to 'm/d/y').
106 * @cfg {Array} disabledDays
107 * An array of days to disable, 0-based. For example, [0, 6] disables Sunday and Saturday (defaults to null).
111 * @cfg {String} disabledDaysText
112 * The tooltip to display when the date falls on a disabled day (defaults to "")
114 disabledDaysText : "",
116 * @cfg {RegExp} disabledDatesRE
117 * JavaScript regular expression used to disable a pattern of dates (defaults to null)
119 disabledDatesRE : null,
121 * @cfg {String} disabledDatesText
122 * The tooltip text to display when the date falls on a disabled date (defaults to "")
124 disabledDatesText : "",
126 * @cfg {Boolean} constrainToViewport
127 * True to constrain the date picker to the viewport (defaults to true)
129 constrainToViewport : true,
131 * @cfg {Array} monthNames
132 * An array of textual month names which can be overriden for localization support (defaults to Date.monthNames)
134 monthNames : Date.monthNames,
136 * @cfg {Array} dayNames
137 * An array of textual day names which can be overriden for localization support (defaults to Date.dayNames)
139 dayNames : Date.dayNames,
141 * @cfg {String} nextText
142 * The next month navigation button tooltip (defaults to 'Next Month (Control+Right)')
144 nextText: 'Next Month (Control+Right)',
146 * @cfg {String} prevText
147 * The previous month navigation button tooltip (defaults to 'Previous Month (Control+Left)')
149 prevText: 'Previous Month (Control+Left)',
151 * @cfg {String} monthYearText
152 * The header month selector tooltip (defaults to 'Choose a month (Control+Up/Down to move years)')
154 monthYearText: 'Choose a month (Control+Up/Down to move years)',
156 * @cfg {Number} startDay
157 * Day index at which the week should begin, 0-based (defaults to 0, which is Sunday)
161 * @cfg {Bool} showClear
162 * Show a clear button (usefull for date form elements that can be blank.)
168 * Sets the value of the date field
169 * @param {Date} value The date to set
171 setValue : function(value){
172 var old = this.value;
173 if (typeof(value) == 'string') {
174 value = Date.parseDate(value, this.format);
177 this.value = value.clearTime(true);
179 this.update(this.value);
184 * Gets the current selected value of the date field
185 * @return {Date} The selected date
187 getValue : function(){
194 this.update(this.activeDate);
199 onRender : function(container, position){
202 '<table cellspacing="0">',
203 '<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>',
204 '<tr><td colspan="3"><table class="x-date-inner" cellspacing="0"><thead><tr>'];
205 var dn = this.dayNames;
206 for(var i = 0; i < 7; i++){
207 var d = this.startDay+i;
211 m.push("<th><span>", dn[d].substr(0,1), "</span></th>");
213 m[m.length] = "</tr></thead><tbody><tr>";
214 for(var i = 0; i < 42; i++) {
215 if(i % 7 == 0 && i != 0){
216 m[m.length] = "</tr><tr>";
218 m[m.length] = '<td><a href="#" hidefocus="on" class="x-date-date" tabIndex="1"><em><span></span></em></a></td>';
220 m[m.length] = '</tr></tbody></table></td></tr><tr>'+
221 '<td colspan="3" class="x-date-bottom" align="center"></td></tr></table><div class="x-date-mp"></div>';
223 var el = document.createElement("div");
224 el.className = "x-date-picker";
225 el.innerHTML = m.join("");
227 container.dom.insertBefore(el, position);
229 this.el = Roo.get(el);
230 this.eventEl = Roo.get(el.firstChild);
232 new Roo.util.ClickRepeater(this.el.child("td.x-date-left a"), {
233 handler: this.showPrevMonth,
239 new Roo.util.ClickRepeater(this.el.child("td.x-date-right a"), {
240 handler: this.showNextMonth,
246 this.eventEl.on("mousewheel", this.handleMouseWheel, this);
248 this.monthPicker = this.el.down('div.x-date-mp');
249 this.monthPicker.enableDisplayMode('block');
251 var kn = new Roo.KeyNav(this.eventEl, {
252 "left" : function(e){
254 this.showPrevMonth() :
255 this.update(this.activeDate.add("d", -1));
258 "right" : function(e){
260 this.showNextMonth() :
261 this.update(this.activeDate.add("d", 1));
266 this.showNextYear() :
267 this.update(this.activeDate.add("d", -7));
270 "down" : function(e){
272 this.showPrevYear() :
273 this.update(this.activeDate.add("d", 7));
276 "pageUp" : function(e){
277 this.showNextMonth();
280 "pageDown" : function(e){
281 this.showPrevMonth();
284 "enter" : function(e){
292 this.eventEl.on("click", this.handleDateClick, this, {delegate: "a.x-date-date"});
294 this.eventEl.addKeyListener(Roo.EventObject.SPACE, this.selectToday, this);
296 this.el.unselectable();
298 this.cells = this.el.select("table.x-date-inner tbody td");
299 this.textNodes = this.el.query("table.x-date-inner tbody span");
301 this.mbtn = new Roo.Button(this.el.child("td.x-date-middle", true), {
303 tooltip: this.monthYearText
306 this.mbtn.on('click', this.showMonthPicker, this);
307 this.mbtn.el.child(this.mbtn.menuClassTarget).addClass("x-btn-with-menu");
310 var today = (new Date()).dateFormat(this.format);
312 var baseTb = new Roo.Toolbar(this.el.child("td.x-date-bottom", true));
313 if (this.showClear) {
314 baseTb.add( new Roo.Toolbar.Fill());
317 text: String.format(this.todayText, today),
318 tooltip: String.format(this.todayTip, today),
319 handler: this.selectToday,
323 //var todayBtn = new Roo.Button(this.el.child("td.x-date-bottom", true), {
326 if (this.showClear) {
328 baseTb.add( new Roo.Toolbar.Fill());
331 cls: 'x-btn-icon x-btn-clear',
332 handler: function() {
334 this.fireEvent("select", this, '');
344 this.update(this.value);
347 createMonthPicker : function(){
348 if(!this.monthPicker.dom.firstChild){
349 var buf = ['<table border="0" cellspacing="0">'];
350 for(var i = 0; i < 6; i++){
352 '<tr><td class="x-date-mp-month"><a href="#">', this.monthNames[i].substr(0, 3), '</a></td>',
353 '<td class="x-date-mp-month x-date-mp-sep"><a href="#">', this.monthNames[i+6].substr(0, 3), '</a></td>',
355 '<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>' :
356 '<td class="x-date-mp-year"><a href="#"></a></td><td class="x-date-mp-year"><a href="#"></a></td></tr>'
360 '<tr class="x-date-mp-btns"><td colspan="4"><button type="button" class="x-date-mp-ok">',
362 '</button><button type="button" class="x-date-mp-cancel">',
364 '</button></td></tr>',
367 this.monthPicker.update(buf.join(''));
368 this.monthPicker.on('click', this.onMonthClick, this);
369 this.monthPicker.on('dblclick', this.onMonthDblClick, this);
371 this.mpMonths = this.monthPicker.select('td.x-date-mp-month');
372 this.mpYears = this.monthPicker.select('td.x-date-mp-year');
374 this.mpMonths.each(function(m, a, i){
377 m.dom.xmonth = 5 + Math.round(i * .5);
379 m.dom.xmonth = Math.round((i-1) * .5);
385 showMonthPicker : function(){
386 this.createMonthPicker();
387 var size = this.el.getSize();
388 this.monthPicker.setSize(size);
389 this.monthPicker.child('table').setSize(size);
391 this.mpSelMonth = (this.activeDate || this.value).getMonth();
392 this.updateMPMonth(this.mpSelMonth);
393 this.mpSelYear = (this.activeDate || this.value).getFullYear();
394 this.updateMPYear(this.mpSelYear);
396 this.monthPicker.slideIn('t', {duration:.2});
399 updateMPYear : function(y){
401 var ys = this.mpYears.elements;
402 for(var i = 1; i <= 10; i++){
403 var td = ys[i-1], y2;
405 y2 = y + Math.round(i * .5);
406 td.firstChild.innerHTML = y2;
409 y2 = y - (5-Math.round(i * .5));
410 td.firstChild.innerHTML = y2;
413 this.mpYears.item(i-1)[y2 == this.mpSelYear ? 'addClass' : 'removeClass']('x-date-mp-sel');
417 updateMPMonth : function(sm){
418 this.mpMonths.each(function(m, a, i){
419 m[m.dom.xmonth == sm ? 'addClass' : 'removeClass']('x-date-mp-sel');
423 selectMPMonth: function(m){
427 onMonthClick : function(e, t){
429 var el = new Roo.Element(t), pn;
430 if(el.is('button.x-date-mp-cancel')){
431 this.hideMonthPicker();
433 else if(el.is('button.x-date-mp-ok')){
434 this.update(new Date(this.mpSelYear, this.mpSelMonth, (this.activeDate || this.value).getDate()));
435 this.hideMonthPicker();
437 else if(pn = el.up('td.x-date-mp-month', 2)){
438 this.mpMonths.removeClass('x-date-mp-sel');
439 pn.addClass('x-date-mp-sel');
440 this.mpSelMonth = pn.dom.xmonth;
442 else if(pn = el.up('td.x-date-mp-year', 2)){
443 this.mpYears.removeClass('x-date-mp-sel');
444 pn.addClass('x-date-mp-sel');
445 this.mpSelYear = pn.dom.xyear;
447 else if(el.is('a.x-date-mp-prev')){
448 this.updateMPYear(this.mpyear-10);
450 else if(el.is('a.x-date-mp-next')){
451 this.updateMPYear(this.mpyear+10);
455 onMonthDblClick : function(e, t){
457 var el = new Roo.Element(t), pn;
458 if(pn = el.up('td.x-date-mp-month', 2)){
459 this.update(new Date(this.mpSelYear, pn.dom.xmonth, (this.activeDate || this.value).getDate()));
460 this.hideMonthPicker();
462 else if(pn = el.up('td.x-date-mp-year', 2)){
463 this.update(new Date(pn.dom.xyear, this.mpSelMonth, (this.activeDate || this.value).getDate()));
464 this.hideMonthPicker();
468 hideMonthPicker : function(disableAnim){
469 if(this.monthPicker){
470 if(disableAnim === true){
471 this.monthPicker.hide();
473 this.monthPicker.slideOut('t', {duration:.2});
479 showPrevMonth : function(e){
480 this.update(this.activeDate.add("mo", -1));
484 showNextMonth : function(e){
485 this.update(this.activeDate.add("mo", 1));
489 showPrevYear : function(){
490 this.update(this.activeDate.add("y", -1));
494 showNextYear : function(){
495 this.update(this.activeDate.add("y", 1));
499 handleMouseWheel : function(e){
500 var delta = e.getWheelDelta();
502 this.showPrevMonth();
504 } else if(delta < 0){
505 this.showNextMonth();
511 handleDateClick : function(e, t){
513 if(t.dateValue && !Roo.fly(t.parentNode).hasClass("x-date-disabled")){
514 this.setValue(new Date(t.dateValue));
515 this.fireEvent("select", this, this.value);
520 selectToday : function(){
521 this.setValue(new Date().clearTime());
522 this.fireEvent("select", this, this.value);
526 update : function(date)
528 var vd = this.activeDate;
529 this.activeDate = date;
531 var t = date.getTime();
532 if(vd.getMonth() == date.getMonth() && vd.getFullYear() == date.getFullYear()){
533 this.cells.removeClass("x-date-selected");
534 this.cells.each(function(c){
535 if(c.dom.firstChild.dateValue == t){
536 c.addClass("x-date-selected");
537 setTimeout(function(){
538 try{c.dom.firstChild.focus();}catch(e){}
547 var days = date.getDaysInMonth();
548 var firstOfMonth = date.getFirstDateOfMonth();
549 var startingPos = firstOfMonth.getDay()-this.startDay;
551 if(startingPos <= this.startDay){
555 var pm = date.add("mo", -1);
556 var prevStart = pm.getDaysInMonth()-startingPos;
558 var cells = this.cells.elements;
559 var textEls = this.textNodes;
562 // convert everything to numbers so it's fast
564 var d = (new Date(pm.getFullYear(), pm.getMonth(), prevStart)).clearTime();
565 var today = new Date().clearTime().getTime();
566 var sel = date.clearTime().getTime();
567 var min = this.minDate ? this.minDate.clearTime() : Number.NEGATIVE_INFINITY;
568 var max = this.maxDate ? this.maxDate.clearTime() : Number.POSITIVE_INFINITY;
569 var ddMatch = this.disabledDatesRE;
570 var ddText = this.disabledDatesText;
571 var ddays = this.disabledDays ? this.disabledDays.join("") : false;
572 var ddaysText = this.disabledDaysText;
573 var format = this.format;
575 var setCellClass = function(cal, cell){
578 cell.firstChild.dateValue = t;
580 cell.className += " x-date-today";
581 cell.title = cal.todayText;
584 cell.className += " x-date-selected";
585 setTimeout(function(){
586 try{cell.firstChild.focus();}catch(e){}
591 cell.className = " x-date-disabled";
592 cell.title = cal.minText;
596 cell.className = " x-date-disabled";
597 cell.title = cal.maxText;
601 if(ddays.indexOf(d.getDay()) != -1){
602 cell.title = ddaysText;
603 cell.className = " x-date-disabled";
606 if(ddMatch && format){
607 var fvalue = d.dateFormat(format);
608 if(ddMatch.test(fvalue)){
609 cell.title = ddText.replace("%0", fvalue);
610 cell.className = " x-date-disabled";
616 for(; i < startingPos; i++) {
617 textEls[i].innerHTML = (++prevStart);
618 d.setDate(d.getDate()+1);
619 cells[i].className = "x-date-prevday";
620 setCellClass(this, cells[i]);
622 for(; i < days; i++){
623 intDay = i - startingPos + 1;
624 textEls[i].innerHTML = (intDay);
625 d.setDate(d.getDate()+1);
626 cells[i].className = "x-date-active";
627 setCellClass(this, cells[i]);
631 textEls[i].innerHTML = (++extraDays);
632 d.setDate(d.getDate()+1);
633 cells[i].className = "x-date-nextday";
634 setCellClass(this, cells[i]);
637 this.mbtn.setText(this.monthNames[date.getMonth()] + " " + date.getFullYear());
638 this.fireEvent('monthchange', this, date);
640 if(!this.internalRender){
641 var main = this.el.dom.firstChild;
642 var w = main.offsetWidth;
643 this.el.setWidth(w + this.el.getBorderWidth("lr"));
644 Roo.fly(main).setWidth(w);
645 this.internalRender = true;
646 // opera does not respect the auto grow header center column
647 // then, after it gets a width opera refuses to recalculate
648 // without a second pass
649 if(Roo.isOpera && !this.secondPass){
650 main.rows[0].cells[1].style.width = (w - (main.rows[0].cells[0].offsetWidth+main.rows[0].cells[2].offsetWidth)) + "px";
651 this.secondPass = true;
652 this.update.defer(10, this, [date]);