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) {
56 this.disabledDatesRE = new RegExp(re + ")");
60 Roo.extend(Roo.DatePicker, Roo.Component, {
62 * @cfg {String} todayText
63 * The text to display on the button that selects the current date (defaults to "Today")
67 * @cfg {String} okText
68 * The text to display on the ok button
70 okText : " OK ", //   to give the user extra clicking room
72 * @cfg {String} cancelText
73 * The text to display on the cancel button
75 cancelText : "Cancel",
77 * @cfg {String} todayTip
78 * The tooltip to display for the button that selects the current date (defaults to "{current date} (Spacebar)")
80 todayTip : "{0} (Spacebar)",
83 * Minimum allowable date (JavaScript date object, defaults to null)
88 * Maximum allowable date (JavaScript date object, defaults to null)
92 * @cfg {String} minText
93 * The error text to display if the minDate validation fails (defaults to "This date is before the minimum date")
95 minText : "This date is before the minimum date",
97 * @cfg {String} maxText
98 * The error text to display if the maxDate validation fails (defaults to "This date is after the maximum date")
100 maxText : "This date is after the maximum date",
102 * @cfg {String} format
103 * The default date format string which can be overriden for localization support. The format must be
104 * valid according to {@link Date#parseDate} (defaults to 'm/d/y').
108 * @cfg {Array} disabledDays
109 * An array of days to disable, 0-based. For example, [0, 6] disables Sunday and Saturday (defaults to null).
113 * @cfg {String} disabledDaysText
114 * The tooltip to display when the date falls on a disabled day (defaults to "")
116 disabledDaysText : "",
118 * @cfg {RegExp} disabledDatesRE
119 * JavaScript regular expression used to disable a pattern of dates (defaults to null)
121 disabledDatesRE : null,
123 * @cfg {String} disabledDatesText
124 * The tooltip text to display when the date falls on a disabled date (defaults to "")
126 disabledDatesText : "",
128 * @cfg {Boolean} constrainToViewport
129 * True to constrain the date picker to the viewport (defaults to true)
131 constrainToViewport : true,
133 * @cfg {Array} monthNames
134 * An array of textual month names which can be overriden for localization support (defaults to Date.monthNames)
136 monthNames : Date.monthNames,
138 * @cfg {Array} dayNames
139 * An array of textual day names which can be overriden for localization support (defaults to Date.dayNames)
141 dayNames : Date.dayNames,
143 * @cfg {String} nextText
144 * The next month navigation button tooltip (defaults to 'Next Month (Control+Right)')
146 nextText: 'Next Month (Control+Right)',
148 * @cfg {String} prevText
149 * The previous month navigation button tooltip (defaults to 'Previous Month (Control+Left)')
151 prevText: 'Previous Month (Control+Left)',
153 * @cfg {String} monthYearText
154 * The header month selector tooltip (defaults to 'Choose a month (Control+Up/Down to move years)')
156 monthYearText: 'Choose a month (Control+Up/Down to move years)',
158 * @cfg {Number} startDay
159 * Day index at which the week should begin, 0-based (defaults to 0, which is Sunday)
163 * @cfg {Bool} showClear
164 * Show a clear button (usefull for date form elements that can be blank.)
170 * Sets the value of the date field
171 * @param {Date} value The date to set
173 setValue : function(value){
174 var old = this.value;
176 if (typeof(value) == 'string') {
178 value = Date.parseDate(value, this.format);
184 this.value = value.clearTime(true);
186 this.update(this.value);
191 * Gets the current selected value of the date field
192 * @return {Date} The selected date
194 getValue : function(){
201 this.update(this.activeDate);
206 onRender : function(container, position){
209 '<table cellspacing="0">',
210 '<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>',
211 '<tr><td colspan="3"><table class="x-date-inner" cellspacing="0"><thead><tr>'];
212 var dn = this.dayNames;
213 for(var i = 0; i < 7; i++){
214 var d = this.startDay+i;
218 m.push("<th><span>", dn[d].substr(0,1), "</span></th>");
220 m[m.length] = "</tr></thead><tbody><tr>";
221 for(var i = 0; i < 42; i++) {
222 if(i % 7 == 0 && i != 0){
223 m[m.length] = "</tr><tr>";
225 m[m.length] = '<td><a href="#" hidefocus="on" class="x-date-date" tabIndex="1"><em><span></span></em></a></td>';
227 m[m.length] = '</tr></tbody></table></td></tr><tr>'+
228 '<td colspan="3" class="x-date-bottom" align="center"></td></tr></table><div class="x-date-mp"></div>';
230 var el = document.createElement("div");
231 el.className = "x-date-picker";
232 el.innerHTML = m.join("");
234 container.dom.insertBefore(el, position);
236 this.el = Roo.get(el);
237 this.eventEl = Roo.get(el.firstChild);
239 new Roo.util.ClickRepeater(this.el.child("td.x-date-left a"), {
240 handler: this.showPrevMonth,
246 new Roo.util.ClickRepeater(this.el.child("td.x-date-right a"), {
247 handler: this.showNextMonth,
253 this.eventEl.on("mousewheel", this.handleMouseWheel, this);
255 this.monthPicker = this.el.down('div.x-date-mp');
256 this.monthPicker.enableDisplayMode('block');
258 var kn = new Roo.KeyNav(this.eventEl, {
259 "left" : function(e){
261 this.showPrevMonth() :
262 this.update(this.activeDate.add("d", -1));
265 "right" : function(e){
267 this.showNextMonth() :
268 this.update(this.activeDate.add("d", 1));
273 this.showNextYear() :
274 this.update(this.activeDate.add("d", -7));
277 "down" : function(e){
279 this.showPrevYear() :
280 this.update(this.activeDate.add("d", 7));
283 "pageUp" : function(e){
284 this.showNextMonth();
287 "pageDown" : function(e){
288 this.showPrevMonth();
291 "enter" : function(e){
299 this.eventEl.on("click", this.handleDateClick, this, {delegate: "a.x-date-date"});
301 this.eventEl.addKeyListener(Roo.EventObject.SPACE, this.selectToday, this);
303 this.el.unselectable();
305 this.cells = this.el.select("table.x-date-inner tbody td");
306 this.textNodes = this.el.query("table.x-date-inner tbody span");
308 this.mbtn = new Roo.Button(this.el.child("td.x-date-middle", true), {
310 tooltip: this.monthYearText
313 this.mbtn.on('click', this.showMonthPicker, this);
314 this.mbtn.el.child(this.mbtn.menuClassTarget).addClass("x-btn-with-menu");
317 var today = (new Date()).dateFormat(this.format);
319 var baseTb = new Roo.Toolbar(this.el.child("td.x-date-bottom", true));
320 if (this.showClear) {
321 baseTb.add( new Roo.Toolbar.Fill());
324 text: String.format(this.todayText, today),
325 tooltip: String.format(this.todayTip, today),
326 handler: this.selectToday,
330 //var todayBtn = new Roo.Button(this.el.child("td.x-date-bottom", true), {
333 if (this.showClear) {
335 baseTb.add( new Roo.Toolbar.Fill());
338 cls: 'x-btn-icon x-btn-clear',
339 handler: function() {
341 this.fireEvent("select", this, '');
351 this.update(this.value);
354 createMonthPicker : function(){
355 if(!this.monthPicker.dom.firstChild){
356 var buf = ['<table border="0" cellspacing="0">'];
357 for(var i = 0; i < 6; i++){
359 '<tr><td class="x-date-mp-month"><a href="#">', this.monthNames[i].substr(0, 3), '</a></td>',
360 '<td class="x-date-mp-month x-date-mp-sep"><a href="#">', this.monthNames[i+6].substr(0, 3), '</a></td>',
362 '<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>' :
363 '<td class="x-date-mp-year"><a href="#"></a></td><td class="x-date-mp-year"><a href="#"></a></td></tr>'
367 '<tr class="x-date-mp-btns"><td colspan="4"><button type="button" class="x-date-mp-ok">',
369 '</button><button type="button" class="x-date-mp-cancel">',
371 '</button></td></tr>',
374 this.monthPicker.update(buf.join(''));
375 this.monthPicker.on('click', this.onMonthClick, this);
376 this.monthPicker.on('dblclick', this.onMonthDblClick, this);
378 this.mpMonths = this.monthPicker.select('td.x-date-mp-month');
379 this.mpYears = this.monthPicker.select('td.x-date-mp-year');
381 this.mpMonths.each(function(m, a, i){
384 m.dom.xmonth = 5 + Math.round(i * .5);
386 m.dom.xmonth = Math.round((i-1) * .5);
392 showMonthPicker : function(){
393 this.createMonthPicker();
394 var size = this.el.getSize();
395 this.monthPicker.setSize(size);
396 this.monthPicker.child('table').setSize(size);
398 this.mpSelMonth = (this.activeDate || this.value).getMonth();
399 this.updateMPMonth(this.mpSelMonth);
400 this.mpSelYear = (this.activeDate || this.value).getFullYear();
401 this.updateMPYear(this.mpSelYear);
403 this.monthPicker.slideIn('t', {duration:.2});
406 updateMPYear : function(y){
408 var ys = this.mpYears.elements;
409 for(var i = 1; i <= 10; i++){
410 var td = ys[i-1], y2;
412 y2 = y + Math.round(i * .5);
413 td.firstChild.innerHTML = y2;
416 y2 = y - (5-Math.round(i * .5));
417 td.firstChild.innerHTML = y2;
420 this.mpYears.item(i-1)[y2 == this.mpSelYear ? 'addClass' : 'removeClass']('x-date-mp-sel');
424 updateMPMonth : function(sm){
425 this.mpMonths.each(function(m, a, i){
426 m[m.dom.xmonth == sm ? 'addClass' : 'removeClass']('x-date-mp-sel');
430 selectMPMonth: function(m){
434 onMonthClick : function(e, t){
436 var el = new Roo.Element(t), pn;
437 if(el.is('button.x-date-mp-cancel')){
438 this.hideMonthPicker();
440 else if(el.is('button.x-date-mp-ok')){
441 this.update(new Date(this.mpSelYear, this.mpSelMonth, (this.activeDate || this.value).getDate()));
442 this.hideMonthPicker();
444 else if(pn = el.up('td.x-date-mp-month', 2)){
445 this.mpMonths.removeClass('x-date-mp-sel');
446 pn.addClass('x-date-mp-sel');
447 this.mpSelMonth = pn.dom.xmonth;
449 else if(pn = el.up('td.x-date-mp-year', 2)){
450 this.mpYears.removeClass('x-date-mp-sel');
451 pn.addClass('x-date-mp-sel');
452 this.mpSelYear = pn.dom.xyear;
454 else if(el.is('a.x-date-mp-prev')){
455 this.updateMPYear(this.mpyear-10);
457 else if(el.is('a.x-date-mp-next')){
458 this.updateMPYear(this.mpyear+10);
462 onMonthDblClick : function(e, t){
464 var el = new Roo.Element(t), pn;
465 if(pn = el.up('td.x-date-mp-month', 2)){
466 this.update(new Date(this.mpSelYear, pn.dom.xmonth, (this.activeDate || this.value).getDate()));
467 this.hideMonthPicker();
469 else if(pn = el.up('td.x-date-mp-year', 2)){
470 this.update(new Date(pn.dom.xyear, this.mpSelMonth, (this.activeDate || this.value).getDate()));
471 this.hideMonthPicker();
475 hideMonthPicker : function(disableAnim){
476 if(this.monthPicker){
477 if(disableAnim === true){
478 this.monthPicker.hide();
480 this.monthPicker.slideOut('t', {duration:.2});
486 showPrevMonth : function(e){
487 this.update(this.activeDate.add("mo", -1));
491 showNextMonth : function(e){
492 this.update(this.activeDate.add("mo", 1));
496 showPrevYear : function(){
497 this.update(this.activeDate.add("y", -1));
501 showNextYear : function(){
502 this.update(this.activeDate.add("y", 1));
506 handleMouseWheel : function(e){
507 var delta = e.getWheelDelta();
509 this.showPrevMonth();
511 } else if(delta < 0){
512 this.showNextMonth();
518 handleDateClick : function(e, t){
520 if(t.dateValue && !Roo.fly(t.parentNode).hasClass("x-date-disabled")){
521 this.setValue(new Date(t.dateValue));
522 this.fireEvent("select", this, this.value);
527 selectToday : function(){
528 this.setValue(new Date().clearTime());
529 this.fireEvent("select", this, this.value);
533 update : function(date)
535 var vd = this.activeDate;
536 this.activeDate = date;
538 var t = date.getTime();
539 if(vd.getMonth() == date.getMonth() && vd.getFullYear() == date.getFullYear()){
540 this.cells.removeClass("x-date-selected");
541 this.cells.each(function(c){
542 if(c.dom.firstChild.dateValue == t){
543 c.addClass("x-date-selected");
544 setTimeout(function(){
545 try{c.dom.firstChild.focus();}catch(e){}
554 var days = date.getDaysInMonth();
555 var firstOfMonth = date.getFirstDateOfMonth();
556 var startingPos = firstOfMonth.getDay()-this.startDay;
558 if(startingPos <= this.startDay){
562 var pm = date.add("mo", -1);
563 var prevStart = pm.getDaysInMonth()-startingPos;
565 var cells = this.cells.elements;
566 var textEls = this.textNodes;
569 // convert everything to numbers so it's fast
571 var d = (new Date(pm.getFullYear(), pm.getMonth(), prevStart)).clearTime();
572 var today = new Date().clearTime().getTime();
573 var sel = date.clearTime().getTime();
574 var min = this.minDate ? this.minDate.clearTime() : Number.NEGATIVE_INFINITY;
575 var max = this.maxDate ? this.maxDate.clearTime() : Number.POSITIVE_INFINITY;
576 var ddMatch = this.disabledDatesRE;
577 var ddText = this.disabledDatesText;
578 var ddays = this.disabledDays ? this.disabledDays.join("") : false;
579 var ddaysText = this.disabledDaysText;
580 var format = this.format;
582 var setCellClass = function(cal, cell){
585 cell.firstChild.dateValue = t;
587 cell.className += " x-date-today";
588 cell.title = cal.todayText;
591 cell.className += " x-date-selected";
592 setTimeout(function(){
593 try{cell.firstChild.focus();}catch(e){}
598 cell.className = " x-date-disabled";
599 cell.title = cal.minText;
603 cell.className = " x-date-disabled";
604 cell.title = cal.maxText;
608 if(ddays.indexOf(d.getDay()) != -1){
609 cell.title = ddaysText;
610 cell.className = " x-date-disabled";
613 if(ddMatch && format){
614 var fvalue = d.dateFormat(format);
615 if(ddMatch.test(fvalue)){
616 cell.title = ddText.replace("%0", fvalue);
617 cell.className = " x-date-disabled";
623 for(; i < startingPos; i++) {
624 textEls[i].innerHTML = (++prevStart);
625 d.setDate(d.getDate()+1);
626 cells[i].className = "x-date-prevday";
627 setCellClass(this, cells[i]);
629 for(; i < days; i++){
630 intDay = i - startingPos + 1;
631 textEls[i].innerHTML = (intDay);
632 d.setDate(d.getDate()+1);
633 cells[i].className = "x-date-active";
634 setCellClass(this, cells[i]);
638 textEls[i].innerHTML = (++extraDays);
639 d.setDate(d.getDate()+1);
640 cells[i].className = "x-date-nextday";
641 setCellClass(this, cells[i]);
644 this.mbtn.setText(this.monthNames[date.getMonth()] + " " + date.getFullYear());
645 this.fireEvent('monthchange', this, date);
647 if(!this.internalRender){
648 var main = this.el.dom.firstChild;
649 var w = main.offsetWidth;
650 this.el.setWidth(w + this.el.getBorderWidth("lr"));
651 Roo.fly(main).setWidth(w);
652 this.internalRender = true;
653 // opera does not respect the auto grow header center column
654 // then, after it gets a width opera refuses to recalculate
655 // without a second pass
656 if(Roo.isOpera && !this.secondPass){
657 main.rows[0].cells[1].style.width = (w - (main.rows[0].cells[0].offsetWidth+main.rows[0].cells[2].offsetWidth)) + "px";
658 this.secondPass = true;
659 this.update.defer(10, this, [date]);