2
* Ext JS Library 3.0 RC2
3
* Copyright(c) 2006-2009, Ext JS, LLC.
6
* http://extjs.com/license
10
* @class Ext.DatePicker
11
* @extends Ext.Component
12
* Simple date picker class.
14
* Create a new DatePicker
15
* @param {Object} config The config object
18
Ext.DatePicker = Ext.extend(Ext.BoxComponent, {
20
* @cfg {String} todayText
21
* The text to display on the button that selects the current date (defaults to "Today")
25
* @cfg {String} okText
26
* The text to display on the ok button
28
okText : " OK ", //   to give the user extra clicking room
30
* @cfg {String} cancelText
31
* The text to display on the cancel button
33
cancelText : "Cancel",
35
* @cfg {String} todayTip
36
* The tooltip to display for the button that selects the current date (defaults to "{current date} (Spacebar)")
38
todayTip : "{0} (Spacebar)",
40
* @cfg {String} minText
41
* The error text to display if the minDate validation fails (defaults to "This date is before the minimum date")
43
minText : "This date is before the minimum date",
45
* @cfg {String} maxText
46
* The error text to display if the maxDate validation fails (defaults to "This date is after the maximum date")
48
maxText : "This date is after the maximum date",
50
* @cfg {String} format
51
* The default date format string which can be overriden for localization support. The format must be
52
* valid according to {@link Date#parseDate} (defaults to 'm/d/y').
56
* @cfg {String} disabledDaysText
57
* The tooltip to display when the date falls on a disabled day (defaults to "Disabled")
59
disabledDaysText : "Disabled",
61
* @cfg {String} disabledDatesText
62
* The tooltip text to display when the date falls on a disabled date (defaults to "Disabled")
64
disabledDatesText : "Disabled",
66
* @cfg {Array} monthNames
67
* An array of textual month names which can be overriden for localization support (defaults to Date.monthNames)
69
monthNames : Date.monthNames,
71
* @cfg {Array} dayNames
72
* An array of textual day names which can be overriden for localization support (defaults to Date.dayNames)
74
dayNames : Date.dayNames,
76
* @cfg {String} nextText
77
* The next month navigation button tooltip (defaults to 'Next Month (Control+Right)')
79
nextText: 'Next Month (Control+Right)',
81
* @cfg {String} prevText
82
* The previous month navigation button tooltip (defaults to 'Previous Month (Control+Left)')
84
prevText: 'Previous Month (Control+Left)',
86
* @cfg {String} monthYearText
87
* The header month selector tooltip (defaults to 'Choose a month (Control+Up/Down to move years)')
89
monthYearText: 'Choose a month (Control+Up/Down to move years)',
91
* @cfg {Number} startDay
92
* Day index at which the week should begin, 0-based (defaults to 0, which is Sunday)
96
* @cfg {Boolean} showToday
97
* False to hide the footer area containing the Today button and disable the keyboard handler for spacebar
98
* that selects the current date (defaults to true).
102
* @cfg {Date} minDate
103
* Minimum allowable date (JavaScript date object, defaults to null)
106
* @cfg {Date} maxDate
107
* Maximum allowable date (JavaScript date object, defaults to null)
110
* @cfg {Array} disabledDays
111
* An array of days to disable, 0-based. For example, [0, 6] disables Sunday and Saturday (defaults to null).
114
* @cfg {RegExp} disabledDatesRE
115
* JavaScript regular expression used to disable a pattern of dates (defaults to null). The {@link #disabledDates}
116
* config will generate this regex internally, but if you specify disabledDatesRE it will take precedence over the
117
* disabledDates value.
120
* @cfg {Array} disabledDates
121
* An array of "dates" to disable, as strings. These strings will be used to build a dynamic regular
122
* expression so they are very powerful. Some examples:
124
* <li>["03/08/2003", "09/16/2003"] would disable those exact dates</li>
125
* <li>["03/08", "09/16"] would disable those days for every year</li>
126
* <li>["^03/08"] would only match the beginning (useful if you are using short years)</li>
127
* <li>["03/../2006"] would disable every day in March 2006</li>
128
* <li>["^03"] would disable every day in every March</li>
130
* Note that the format of the dates included in the array should exactly match the {@link #format} config.
131
* In order to support regular expressions, if you are using a date format that has "." in it, you will have to
132
* escape the dot when restricting dates. For example: ["03\\.08\\.03"].
136
initComponent : function(){
137
Ext.DatePicker.superclass.initComponent.call(this);
139
this.value = this.value ?
140
this.value.clearTime() : new Date().clearTime();
145
* Fires when a date is selected
146
* @param {DatePicker} this
147
* @param {Date} date The selected date
153
this.on("select", this.handler, this.scope || this);
156
this.initDisabledDays();
160
initDisabledDays : function(){
161
if(!this.disabledDatesRE && this.disabledDates){
162
var dd = this.disabledDates;
164
for(var i = 0; i < dd.length; i++){
166
if(i != dd.length-1) re += "|";
168
this.disabledDatesRE = new RegExp(re + ")");
173
* Replaces any existing disabled dates with new values and refreshes the DatePicker.
174
* @param {Array/RegExp} disabledDates An array of date strings (see the {@link #disabledDates} config
175
* for details on supported values), or a JavaScript regular expression used to disable a pattern of dates.
177
setDisabledDates : function(dd){
179
this.disabledDates = dd;
180
this.disabledDatesRE = null;
182
this.disabledDatesRE = dd;
184
this.initDisabledDays();
185
this.update(this.value, true);
189
* Replaces any existing disabled days (by index, 0-6) with new values and refreshes the DatePicker.
190
* @param {Array} disabledDays An array of disabled day indexes. See the {@link #disabledDays} config
191
* for details on supported values.
193
setDisabledDays : function(dd){
194
this.disabledDays = dd;
195
this.update(this.value, true);
199
* Replaces any existing {@link #minDate} with the new value and refreshes the DatePicker.
200
* @param {Date} value The minimum date that can be selected
202
setMinDate : function(dt){
204
this.update(this.value, true);
208
* Replaces any existing {@link #maxDate} with the new value and refreshes the DatePicker.
209
* @param {Date} value The maximum date that can be selected
211
setMaxDate : function(dt){
213
this.update(this.value, true);
217
* Sets the value of the date field
218
* @param {Date} value The date to set
220
setValue : function(value){
221
var old = this.value;
222
this.value = value.clearTime(true);
224
this.update(this.value);
229
* Gets the current selected value of the date field
230
* @return {Date} The selected date
232
getValue : function(){
239
this.update(this.activeDate);
244
onRender : function(container, position){
246
'<table cellspacing="0">',
247
'<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>',
248
'<tr><td colspan="3"><table class="x-date-inner" cellspacing="0"><thead><tr>'];
249
var dn = this.dayNames;
250
for(var i = 0; i < 7; i++){
251
var d = this.startDay+i;
255
m.push("<th><span>", dn[d].substr(0,1), "</span></th>");
257
m[m.length] = "</tr></thead><tbody><tr>";
258
for(var i = 0; i < 42; i++) {
259
if(i % 7 == 0 && i != 0){
260
m[m.length] = "</tr><tr>";
262
m[m.length] = '<td><a href="#" hidefocus="on" class="x-date-date" tabIndex="1"><em><span></span></em></a></td>';
264
m.push('</tr></tbody></table></td></tr>',
265
this.showToday ? '<tr><td colspan="3" class="x-date-bottom" align="center"></td></tr>' : '',
266
'</table><div class="x-date-mp"></div>');
268
var el = document.createElement("div");
269
el.className = "x-date-picker";
270
el.innerHTML = m.join("");
272
container.dom.insertBefore(el, position);
274
this.el = Ext.get(el);
275
this.eventEl = Ext.get(el.firstChild);
277
new Ext.util.ClickRepeater(this.el.child("td.x-date-left a"), {
278
handler: this.showPrevMonth,
284
new Ext.util.ClickRepeater(this.el.child("td.x-date-right a"), {
285
handler: this.showNextMonth,
291
this.mon(this.eventEl, "mousewheel", this.handleMouseWheel, this);
293
this.monthPicker = this.el.down('div.x-date-mp');
294
this.monthPicker.enableDisplayMode('block');
296
var kn = new Ext.KeyNav(this.eventEl, {
297
"left" : function(e){
299
this.showPrevMonth() :
300
this.update(this.activeDate.add("d", -1));
303
"right" : function(e){
305
this.showNextMonth() :
306
this.update(this.activeDate.add("d", 1));
311
this.showNextYear() :
312
this.update(this.activeDate.add("d", -7));
315
"down" : function(e){
317
this.showPrevYear() :
318
this.update(this.activeDate.add("d", 7));
321
"pageUp" : function(e){
322
this.showNextMonth();
325
"pageDown" : function(e){
326
this.showPrevMonth();
329
"enter" : function(e){
337
this.mon(this.eventEl, "click", this.handleDateClick, this, {delegate: "a.x-date-date"});
339
this.el.unselectable();
341
this.cells = this.el.select("table.x-date-inner tbody td");
342
this.textNodes = this.el.query("table.x-date-inner tbody span");
344
this.mbtn = new Ext.Button({
346
tooltip: this.monthYearText,
347
renderTo: this.el.child("td.x-date-middle", true)
350
this.mon(this.mbtn, 'click', this.showMonthPicker, this);
351
this.mbtn.el.child('em').addClass("x-btn-arrow");
354
this.todayKeyListener = this.eventEl.addKeyListener(Ext.EventObject.SPACE, this.selectToday, this);
355
var today = (new Date()).dateFormat(this.format);
356
this.todayBtn = new Ext.Button({
357
renderTo: this.el.child("td.x-date-bottom", true),
358
text: String.format(this.todayText, today),
359
tooltip: String.format(this.todayTip, today),
360
handler: this.selectToday,
368
this.update(this.value);
372
createMonthPicker : function(){
373
if(!this.monthPicker.dom.firstChild){
374
var buf = ['<table border="0" cellspacing="0">'];
375
for(var i = 0; i < 6; i++){
377
'<tr><td class="x-date-mp-month"><a href="#">', this.monthNames[i].substr(0, 3), '</a></td>',
378
'<td class="x-date-mp-month x-date-mp-sep"><a href="#">', this.monthNames[i+6].substr(0, 3), '</a></td>',
380
'<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>' :
381
'<td class="x-date-mp-year"><a href="#"></a></td><td class="x-date-mp-year"><a href="#"></a></td></tr>'
385
'<tr class="x-date-mp-btns"><td colspan="4"><button type="button" class="x-date-mp-ok">',
387
'</button><button type="button" class="x-date-mp-cancel">',
389
'</button></td></tr>',
392
this.monthPicker.update(buf.join(''));
394
this.mon(this.monthPicker, 'click', this.onMonthClick, this);
395
this.mon(this.monthPicker, 'dblclick', this.onMonthDblClick, this);
397
this.mpMonths = this.monthPicker.select('td.x-date-mp-month');
398
this.mpYears = this.monthPicker.select('td.x-date-mp-year');
400
this.mpMonths.each(function(m, a, i){
403
m.dom.xmonth = 5 + Math.round(i * .5);
405
m.dom.xmonth = Math.round((i-1) * .5);
412
showMonthPicker : function(){
413
this.createMonthPicker();
414
var size = this.el.getSize();
415
this.monthPicker.setSize(size);
416
this.monthPicker.child('table').setSize(size);
418
this.mpSelMonth = (this.activeDate || this.value).getMonth();
419
this.updateMPMonth(this.mpSelMonth);
420
this.mpSelYear = (this.activeDate || this.value).getFullYear();
421
this.updateMPYear(this.mpSelYear);
423
this.monthPicker.slideIn('t', {duration:.2});
427
updateMPYear : function(y){
429
var ys = this.mpYears.elements;
430
for(var i = 1; i <= 10; i++){
431
var td = ys[i-1], y2;
433
y2 = y + Math.round(i * .5);
434
td.firstChild.innerHTML = y2;
437
y2 = y - (5-Math.round(i * .5));
438
td.firstChild.innerHTML = y2;
441
this.mpYears.item(i-1)[y2 == this.mpSelYear ? 'addClass' : 'removeClass']('x-date-mp-sel');
446
updateMPMonth : function(sm){
447
this.mpMonths.each(function(m, a, i){
448
m[m.dom.xmonth == sm ? 'addClass' : 'removeClass']('x-date-mp-sel');
453
selectMPMonth: function(m){
458
onMonthClick : function(e, t){
460
var el = new Ext.Element(t), pn;
461
if(el.is('button.x-date-mp-cancel')){
462
this.hideMonthPicker();
464
else if(el.is('button.x-date-mp-ok')){
465
var d = new Date(this.mpSelYear, this.mpSelMonth, (this.activeDate || this.value).getDate());
466
if(d.getMonth() != this.mpSelMonth){
467
// "fix" the JS rolling date conversion if needed
468
d = new Date(this.mpSelYear, this.mpSelMonth, 1).getLastDateOfMonth();
471
this.hideMonthPicker();
473
else if(pn = el.up('td.x-date-mp-month', 2)){
474
this.mpMonths.removeClass('x-date-mp-sel');
475
pn.addClass('x-date-mp-sel');
476
this.mpSelMonth = pn.dom.xmonth;
478
else if(pn = el.up('td.x-date-mp-year', 2)){
479
this.mpYears.removeClass('x-date-mp-sel');
480
pn.addClass('x-date-mp-sel');
481
this.mpSelYear = pn.dom.xyear;
483
else if(el.is('a.x-date-mp-prev')){
484
this.updateMPYear(this.mpyear-10);
486
else if(el.is('a.x-date-mp-next')){
487
this.updateMPYear(this.mpyear+10);
492
onMonthDblClick : function(e, t){
494
var el = new Ext.Element(t), pn;
495
if(pn = el.up('td.x-date-mp-month', 2)){
496
this.update(new Date(this.mpSelYear, pn.dom.xmonth, (this.activeDate || this.value).getDate()));
497
this.hideMonthPicker();
499
else if(pn = el.up('td.x-date-mp-year', 2)){
500
this.update(new Date(pn.dom.xyear, this.mpSelMonth, (this.activeDate || this.value).getDate()));
501
this.hideMonthPicker();
506
hideMonthPicker : function(disableAnim){
507
if(this.monthPicker){
508
if(disableAnim === true){
509
this.monthPicker.hide();
511
this.monthPicker.slideOut('t', {duration:.2});
517
showPrevMonth : function(e){
518
this.update(this.activeDate.add("mo", -1));
522
showNextMonth : function(e){
523
this.update(this.activeDate.add("mo", 1));
527
showPrevYear : function(){
528
this.update(this.activeDate.add("y", -1));
532
showNextYear : function(){
533
this.update(this.activeDate.add("y", 1));
537
handleMouseWheel : function(e){
538
var delta = e.getWheelDelta();
540
this.showPrevMonth();
542
} else if(delta < 0){
543
this.showNextMonth();
549
handleDateClick : function(e, t){
551
if(t.dateValue && !Ext.fly(t.parentNode).hasClass("x-date-disabled")){
552
this.setValue(new Date(t.dateValue));
553
this.fireEvent("select", this, this.value);
558
selectToday : function(){
559
if(this.todayBtn && !this.todayBtn.disabled){
560
this.setValue(new Date().clearTime());
561
this.fireEvent("select", this, this.value);
566
update : function(date, forceRefresh){
567
var vd = this.activeDate, vis = this.isVisible();
568
this.activeDate = date;
569
if(!forceRefresh && vd && this.el){
570
var t = date.getTime();
571
if(vd.getMonth() == date.getMonth() && vd.getFullYear() == date.getFullYear()){
572
this.cells.removeClass("x-date-selected");
573
this.cells.each(function(c){
574
if(c.dom.firstChild.dateValue == t){
575
c.addClass("x-date-selected");
577
Ext.fly(c.dom.firstChild).focus(50);
585
var days = date.getDaysInMonth();
586
var firstOfMonth = date.getFirstDateOfMonth();
587
var startingPos = firstOfMonth.getDay()-this.startDay;
589
if(startingPos <= this.startDay){
593
var pm = date.add("mo", -1);
594
var prevStart = pm.getDaysInMonth()-startingPos;
596
var cells = this.cells.elements;
597
var textEls = this.textNodes;
600
// convert everything to numbers so it's fast
602
var d = (new Date(pm.getFullYear(), pm.getMonth(), prevStart)).clearTime();
603
var today = new Date().clearTime().getTime();
604
var sel = date.clearTime().getTime();
605
var min = this.minDate ? this.minDate.clearTime() : Number.NEGATIVE_INFINITY;
606
var max = this.maxDate ? this.maxDate.clearTime() : Number.POSITIVE_INFINITY;
607
var ddMatch = this.disabledDatesRE;
608
var ddText = this.disabledDatesText;
609
var ddays = this.disabledDays ? this.disabledDays.join("") : false;
610
var ddaysText = this.disabledDaysText;
611
var format = this.format;
614
var td = new Date().clearTime();
615
var disable = (td < min || td > max ||
616
(ddMatch && format && ddMatch.test(td.dateFormat(format))) ||
617
(ddays && ddays.indexOf(td.getDay()) != -1));
619
this.todayBtn.setDisabled(disable);
620
this.todayKeyListener[disable ? 'disable' : 'enable']();
623
var setCellClass = function(cal, cell){
626
cell.firstChild.dateValue = t;
628
cell.className += " x-date-today";
629
cell.title = cal.todayText;
632
cell.className += " x-date-selected";
634
Ext.fly(cell.firstChild).focus(50);
639
cell.className = " x-date-disabled";
640
cell.title = cal.minText;
644
cell.className = " x-date-disabled";
645
cell.title = cal.maxText;
649
if(ddays.indexOf(d.getDay()) != -1){
650
cell.title = ddaysText;
651
cell.className = " x-date-disabled";
654
if(ddMatch && format){
655
var fvalue = d.dateFormat(format);
656
if(ddMatch.test(fvalue)){
657
cell.title = ddText.replace("%0", fvalue);
658
cell.className = " x-date-disabled";
664
for(; i < startingPos; i++) {
665
textEls[i].innerHTML = (++prevStart);
666
d.setDate(d.getDate()+1);
667
cells[i].className = "x-date-prevday";
668
setCellClass(this, cells[i]);
670
for(; i < days; i++){
671
var intDay = i - startingPos + 1;
672
textEls[i].innerHTML = (intDay);
673
d.setDate(d.getDate()+1);
674
cells[i].className = "x-date-active";
675
setCellClass(this, cells[i]);
679
textEls[i].innerHTML = (++extraDays);
680
d.setDate(d.getDate()+1);
681
cells[i].className = "x-date-nextday";
682
setCellClass(this, cells[i]);
685
this.mbtn.setText(this.monthNames[date.getMonth()] + " " + date.getFullYear());
687
if(!this.internalRender){
688
var main = this.el.dom.firstChild;
689
var w = main.offsetWidth;
690
this.el.setWidth(w + this.el.getBorderWidth("lr"));
691
Ext.fly(main).setWidth(w);
692
this.internalRender = true;
693
// opera does not respect the auto grow header center column
694
// then, after it gets a width opera refuses to recalculate
695
// without a second pass
696
if(Ext.isOpera && !this.secondPass){
697
main.rows[0].cells[1].style.width = (w - (main.rows[0].cells[0].offsetWidth+main.rows[0].cells[2].offsetWidth)) + "px";
698
this.secondPass = true;
699
this.update.defer(10, this, [date]);
705
beforeDestroy : function() {
719
* @cfg {String} autoEl @hide
722
Ext.reg('datepicker', Ext.DatePicker);
b'\\ No newline at end of file'