Fixed 3183 onSelect and onClose have inconsistent signatures

This commit is contained in:
Keith Wood 2008-09-07 08:38:09 +00:00
parent 0db0ffe580
commit 41811fb8c3
3 changed files with 78 additions and 60 deletions

View File

@ -234,10 +234,10 @@ function customRange(input) {
options: [ options: [
{ desc: 'On select', source: '$("#callbacks").datepicker({onSelect: function(date) { alert("The chosen date is " + date); }, showOn: "both", buttonImage: "templates/images/calendar.gif", buttonImageOnly: true});' }, { desc: 'On select', source: '$("#callbacks").datepicker({onSelect: function(date) { alert("The chosen date is " + date); }, showOn: "both", buttonImage: "templates/images/calendar.gif", buttonImageOnly: true});' },
{ desc: 'On close', source: '$("#callbacks").datepicker({onClose: function(date) { alert("Closed with date " + date); }, showOn: "both", buttonImage: "templates/images/calendar.gif", buttonImageOnly: true});' }, { desc: 'On close', source: '$("#callbacks").datepicker({onClose: function(date) { alert("Closed with date " + date); }, showOn: "both", buttonImage: "templates/images/calendar.gif", buttonImageOnly: true});' },
{ desc: 'On change of month/year', source: '$("#callbacks").datepicker({onChangeMonthYear: function(date) { alert("Moved to month " + $.datepicker.formatDate("MM yy", date)); }, showOn: "both", buttonImage: "templates/images/calendar.gif", buttonImageOnly: true});' }, { desc: 'On change of month/year', source: '$("#callbacks").datepicker({onChangeMonthYear: function(year, month) { alert("Moved to month " + month + "/" + year); }, showOn: "both", buttonImage: "templates/images/calendar.gif", buttonImageOnly: true});' },
{ desc: 'On select for range', source: '$("#callbacks").datepicker({onSelect: function(date) { alert("The chosen dates are " + date); }, rangeSelect: true, numberOfMonths: 2, showOn: "both", buttonImage: "templates/images/calendar.gif", buttonImageOnly: true});' }, { desc: 'On select for range', source: '$("#callbacks").datepicker({onSelect: function(date) { alert("The chosen dates are " + date); }, rangeSelect: true, numberOfMonths: 2, showOn: "both", buttonImage: "templates/images/calendar.gif", buttonImageOnly: true});' },
{ desc: 'On close for range', source: '$("#callbacks").datepicker({onClose: function(date) { alert("Closed with dates " + date); }, rangeSelect: true, numberOfMonths: 2, showOn: "both", buttonImage: "templates/images/calendar.gif", buttonImageOnly: true});' }, { desc: 'On close for range', source: '$("#callbacks").datepicker({onClose: function(date) { alert("Closed with dates " + date); }, rangeSelect: true, numberOfMonths: 2, showOn: "both", buttonImage: "templates/images/calendar.gif", buttonImageOnly: true});' },
{ desc: 'On change of month/year for range', source: '$("#callbacks").datepicker({onChangeMonthYear: function(date) { alert("Moved to month " + $.datepicker.formatDate("MM yy", date)); }, rangeSelect: true, numberOfMonths: 2, showOn: "both", buttonImage: "templates/images/calendar.gif", buttonImageOnly: true});' } { desc: 'On change of month/year for range', source: '$("#callbacks").datepicker({onChangeMonthYear: function(year, month) { alert("Moved to month " + month + "/" + year); }, rangeSelect: true, numberOfMonths: 2, showOn: "both", buttonImage: "templates/images/calendar.gif", buttonImageOnly: true});' }
] ]
}, },

View File

@ -1031,21 +1031,21 @@ test('ranges', function() {
var date1 = new Date(); var date1 = new Date();
var date2 = new Date(); var date2 = new Date();
// Select today - today // Select today - today
inp.val('').datepicker('show'); inp.val('').datepicker('show').
inp.simulate('keydown', {keyCode: $.simulate.VK_ENTER}). simulate('keydown', {keyCode: $.simulate.VK_ENTER}).
simulate('keydown', {keyCode: $.simulate.VK_ENTER}); simulate('keydown', {keyCode: $.simulate.VK_ENTER});
equalsDateArray(inp.datepicker('getDate'), [date1, date1], equalsDateArray(inp.datepicker('getDate'), [date1, date1],
'Range - enter/enter'); 'Range - enter/enter');
// Can't select prior to start date // Can't select prior to start date
inp.val('').datepicker('show'); inp.val('').datepicker('show').
inp.simulate('keydown', {keyCode: $.simulate.VK_ENTER}). simulate('keydown', {keyCode: $.simulate.VK_ENTER}).
simulate('keydown', {ctrlKey: true, keyCode: $.simulate.VK_UP}). simulate('keydown', {ctrlKey: true, keyCode: $.simulate.VK_UP}).
simulate('keydown', {keyCode: $.simulate.VK_ENTER}); simulate('keydown', {keyCode: $.simulate.VK_ENTER});
equalsDateArray(inp.datepicker('getDate'), [date1, date1], equalsDateArray(inp.datepicker('getDate'), [date1, date1],
'Range - enter/ctrl+up/enter'); 'Range - enter/ctrl+up/enter');
// Can select after start date // Can select after start date
inp.val('').datepicker('show'); inp.val('').datepicker('show').
inp.simulate('keydown', {keyCode: $.simulate.VK_ENTER}). simulate('keydown', {keyCode: $.simulate.VK_ENTER}).
simulate('keydown', {ctrlKey: true, keyCode: $.simulate.VK_DOWN}). simulate('keydown', {ctrlKey: true, keyCode: $.simulate.VK_DOWN}).
simulate('keydown', {keyCode: $.simulate.VK_ENTER}); simulate('keydown', {keyCode: $.simulate.VK_ENTER});
date2.setDate(date2.getDate() + 7); date2.setDate(date2.getDate() + 7);
@ -1054,16 +1054,16 @@ test('ranges', function() {
equals(inp.val(), $.datepicker.formatDate('mm/dd/yy', date1) + ' - ' + equals(inp.val(), $.datepicker.formatDate('mm/dd/yy', date1) + ' - ' +
$.datepicker.formatDate('mm/dd/yy', date2), 'Range - value'); $.datepicker.formatDate('mm/dd/yy', date2), 'Range - value');
// Select then cancel defaults to first date // Select then cancel defaults to first date
inp.val('').datepicker('show'); inp.val('').datepicker('show').
inp.simulate('keydown', {keyCode: $.simulate.VK_ENTER}). simulate('keydown', {keyCode: $.simulate.VK_ENTER}).
simulate('keydown', {ctrlKey: true, keyCode: $.simulate.VK_DOWN}). simulate('keydown', {ctrlKey: true, keyCode: $.simulate.VK_DOWN}).
simulate('keydown', {keyCode: $.simulate.VK_ESC}); simulate('keydown', {keyCode: $.simulate.VK_ESC});
equalsDateArray(inp.datepicker('getDate'), [date1, date1], equalsDateArray(inp.datepicker('getDate'), [date1, date1],
'Range - enter/ctrl+down/esc'); 'Range - enter/ctrl+down/esc');
// Separator // Separator
inp.datepicker('change', {rangeSeparator: ' to '}). inp.datepicker('change', {rangeSeparator: ' to '}).
datepicker('hide').val('06/04/2008').datepicker('show'); datepicker('hide').val('06/04/2008').datepicker('show').
inp.simulate('keydown', {keyCode: $.simulate.VK_ENTER}). simulate('keydown', {keyCode: $.simulate.VK_ENTER}).
simulate('keydown', {ctrlKey: true, keyCode: $.simulate.VK_DOWN}). simulate('keydown', {ctrlKey: true, keyCode: $.simulate.VK_DOWN}).
simulate('keydown', {keyCode: $.simulate.VK_ENTER}); simulate('keydown', {keyCode: $.simulate.VK_ENTER});
equalsDateArray(inp.datepicker('getDate'), equalsDateArray(inp.datepicker('getDate'),
@ -1073,41 +1073,41 @@ test('ranges', function() {
'Range separator - value'); 'Range separator - value');
// Callbacks // Callbacks
inp.datepicker('change', {onSelect: callback, rangeSeparator: ' - '}). inp.datepicker('change', {onSelect: callback, rangeSeparator: ' - '}).
datepicker('hide').val('06/04/2008').datepicker('show'); datepicker('hide').val('06/04/2008').datepicker('show').
inp.simulate('keydown', {keyCode: $.simulate.VK_ENTER}). simulate('keydown', {keyCode: $.simulate.VK_ENTER}).
simulate('keydown', {ctrlKey: true, keyCode: $.simulate.VK_DOWN}). simulate('keydown', {ctrlKey: true, keyCode: $.simulate.VK_DOWN}).
simulate('keydown', {keyCode: $.simulate.VK_ENTER}); simulate('keydown', {keyCode: $.simulate.VK_ENTER});
equals(selectedDate, '06/04/2008 - 06/11/2008', equals(selectedDate, '06/04/2008 - 06/11/2008',
'Range onSelect - enter/ctrl+down/enter'); 'Range onSelect - enter/ctrl+down/enter');
inp.datepicker('change', {onChangeMonthYear: callback, onSelect: null}). inp.datepicker('change', {onChangeMonthYear: callback2, onSelect: null}).
datepicker('hide').val('05/04/2008').datepicker('show'); datepicker('hide').val('05/04/2008').datepicker('show').
inp.simulate('keydown', {keyCode: $.simulate.VK_PGUP}). simulate('keydown', {keyCode: $.simulate.VK_PGUP}).
simulate('keydown', {keyCode: $.simulate.VK_ENTER}). simulate('keydown', {keyCode: $.simulate.VK_ENTER}).
simulate('keydown', {ctrlKey: true, keyCode: $.simulate.VK_DOWN}). simulate('keydown', {ctrlKey: true, keyCode: $.simulate.VK_DOWN}).
simulate('keydown', {keyCode: $.simulate.VK_ENTER}); simulate('keydown', {keyCode: $.simulate.VK_ENTER});
equalsDate(selectedDate, new Date(2008, 4 - 1, 1), equals(selectedDate, '2008/4',
'Range onChangeMonthYear - enter/ctrl+down/enter'); 'Range onChangeMonthYear - enter/ctrl+down/enter');
inp.datepicker('change', {onClose: callback, onChangeMonthYear: null}). inp.datepicker('change', {onClose: callback, onChangeMonthYear: null}).
datepicker('hide').val('03/04/2008').datepicker('show'); datepicker('hide').val('03/04/2008').datepicker('show').
inp.simulate('keydown', {keyCode: $.simulate.VK_ENTER}). simulate('keydown', {keyCode: $.simulate.VK_ENTER}).
simulate('keydown', {ctrlKey: true, keyCode: $.simulate.VK_DOWN}). simulate('keydown', {ctrlKey: true, keyCode: $.simulate.VK_DOWN}).
simulate('keydown', {keyCode: $.simulate.VK_ENTER}); simulate('keydown', {keyCode: $.simulate.VK_ENTER});
equalsDateArray(selectedDate, [new Date(2008, 3 - 1, 4), equals(selectedDate, '03/04/2008 - 03/11/2008',
new Date(2008, 3 - 1, 11)], 'Range onClose - enter/ctrl+down/enter'); 'Range onClose - enter/ctrl+down/enter');
// Minimum/maximum // Minimum/maximum
date1 = new Date(2008, 5 - 1, 20); date1 = new Date(2008, 5 - 1, 20);
date2 = new Date(2008, 7 - 1, 2); date2 = new Date(2008, 7 - 1, 2);
inp.datepicker('change', {minDate: date1, maxDate: date2, onClose: null}). inp.datepicker('change', {minDate: date1, maxDate: date2, onClose: null}).
datepicker('hide').val('06/04/2008').datepicker('show'); datepicker('hide').val('06/04/2008').datepicker('show').
inp.simulate('keydown', {keyCode: $.simulate.VK_PGUP}). simulate('keydown', {keyCode: $.simulate.VK_PGUP}).
simulate('keydown', {keyCode: $.simulate.VK_ENTER}). simulate('keydown', {keyCode: $.simulate.VK_ENTER}).
simulate('keydown', {keyCode: $.simulate.VK_PGDN}). simulate('keydown', {keyCode: $.simulate.VK_PGDN}).
simulate('keydown', {keyCode: $.simulate.VK_PGDN}). simulate('keydown', {keyCode: $.simulate.VK_PGDN}).
simulate('keydown', {keyCode: $.simulate.VK_ENTER}); simulate('keydown', {keyCode: $.simulate.VK_ENTER});
equalsDateArray(inp.datepicker('getDate'), [date1, date2], equalsDateArray(inp.datepicker('getDate'), [date1, date2],
'Range min/max - pgup/enter/pgdn/pgdn/enter'); 'Range min/max - pgup/enter/pgdn/pgdn/enter');
inp.val('06/04/2008').datepicker('show'); inp.val('06/04/2008').datepicker('show').
inp.simulate('keydown', {ctrlKey: true, keyCode: $.simulate.VK_UP}). simulate('keydown', {ctrlKey: true, keyCode: $.simulate.VK_UP}).
simulate('keydown', {keyCode: $.simulate.VK_ENTER}). simulate('keydown', {keyCode: $.simulate.VK_ENTER}).
simulate('keydown', {ctrlKey: true, keyCode: $.simulate.VK_DOWN}). simulate('keydown', {ctrlKey: true, keyCode: $.simulate.VK_DOWN}).
simulate('keydown', {ctrlKey: true, keyCode: $.simulate.VK_DOWN}). simulate('keydown', {ctrlKey: true, keyCode: $.simulate.VK_DOWN}).
@ -1298,85 +1298,103 @@ function callback(date, inst) {
selectedInst = inst; selectedInst = inst;
} }
function callback2(year, month, inst) {
selectedThis = this;
selectedDate = year + '/' + month;
selectedInst = inst;
}
test('events', function() { test('events', function() {
var inp = init('#inp', {onSelect: callback}); var inp = init('#inp', {onSelect: callback});
var date = new Date(); var date = new Date();
// onSelect // onSelect
inp.val('').datepicker('show'); inp.val('').datepicker('show').
inp.simulate('keydown', {keyCode: $.simulate.VK_ENTER}); simulate('keydown', {keyCode: $.simulate.VK_ENTER});
equals(selectedThis, inp[0], 'Callback selected this'); equals(selectedThis, inp[0], 'Callback selected this');
equals(selectedInst, $.data(inp[0], PROP_NAME), 'Callback selected inst'); equals(selectedInst, $.data(inp[0], PROP_NAME), 'Callback selected inst');
equals(selectedDate, $.datepicker.formatDate('mm/dd/yy', date), equals(selectedDate, $.datepicker.formatDate('mm/dd/yy', date),
'Callback selected date'); 'Callback selected date');
inp.val('').datepicker('show'); inp.val('').datepicker('show').
inp.simulate('keydown', {ctrlKey: true, keyCode: $.simulate.VK_DOWN}). simulate('keydown', {ctrlKey: true, keyCode: $.simulate.VK_DOWN}).
simulate('keydown', {keyCode: $.simulate.VK_ENTER}); simulate('keydown', {keyCode: $.simulate.VK_ENTER});
date.setDate(date.getDate() + 7); date.setDate(date.getDate() + 7);
equals(selectedDate, $.datepicker.formatDate('mm/dd/yy', date), equals(selectedDate, $.datepicker.formatDate('mm/dd/yy', date),
'Callback selected date - ctrl+down'); 'Callback selected date - ctrl+down');
inp.val('').datepicker('show'); inp.val('').datepicker('show').
inp.simulate('keydown', {keyCode: $.simulate.VK_ESC}); simulate('keydown', {keyCode: $.simulate.VK_ESC});
equals(selectedDate, $.datepicker.formatDate('mm/dd/yy', date), equals(selectedDate, $.datepicker.formatDate('mm/dd/yy', date),
'Callback selected date - esc'); 'Callback selected date - esc');
// onChangeMonthYear // onChangeMonthYear
inp.datepicker('change', {onChangeMonthYear: callback, onSelect: null}). inp.datepicker('change', {onChangeMonthYear: callback2, onSelect: null}).
val('').datepicker('show'); val('').datepicker('show');
var newMonthYear = function(date) {
return date.getFullYear() + '/' + (date.getMonth() + 1);
};
date = new Date(); date = new Date();
date.setDate(1); date.setDate(1);
inp.simulate('keydown', {keyCode: $.simulate.VK_PGUP}); inp.simulate('keydown', {keyCode: $.simulate.VK_PGUP});
date.setMonth(date.getMonth() - 1); date.setMonth(date.getMonth() - 1);
equals(selectedThis, inp[0], 'Callback change month/year this'); equals(selectedThis, inp[0], 'Callback change month/year this');
equals(selectedInst, $.data(inp[0], PROP_NAME), 'Callback change month/year inst'); equals(selectedInst, $.data(inp[0], PROP_NAME), 'Callback change month/year inst');
equalsDate(selectedDate, date, 'Callback change month/year date - pgup'); equals(selectedDate, newMonthYear(date),
'Callback change month/year date - pgup');
inp.simulate('keydown', {keyCode: $.simulate.VK_PGDN}); inp.simulate('keydown', {keyCode: $.simulate.VK_PGDN});
date.setMonth(date.getMonth() + 1); date.setMonth(date.getMonth() + 1);
equalsDate(selectedDate, date, 'Callback change month/year date - pgdn'); equals(selectedDate, newMonthYear(date),
'Callback change month/year date - pgdn');
inp.simulate('keydown', {ctrlKey: true, keyCode: $.simulate.VK_PGUP}); inp.simulate('keydown', {ctrlKey: true, keyCode: $.simulate.VK_PGUP});
date.setFullYear(date.getFullYear() - 1); date.setFullYear(date.getFullYear() - 1);
equalsDate(selectedDate, date, 'Callback change month/year date - ctrl+pgup'); equals(selectedDate, newMonthYear(date),
'Callback change month/year date - ctrl+pgup');
inp.simulate('keydown', {ctrlKey: true, keyCode: $.simulate.VK_HOME}); inp.simulate('keydown', {ctrlKey: true, keyCode: $.simulate.VK_HOME});
date.setFullYear(date.getFullYear() + 1); date.setFullYear(date.getFullYear() + 1);
equalsDate(selectedDate, date, 'Callback change month/year date - ctrl+home'); equals(selectedDate, newMonthYear(date),
'Callback change month/year date - ctrl+home');
inp.simulate('keydown', {ctrlKey: true, keyCode: $.simulate.VK_PGDN}); inp.simulate('keydown', {ctrlKey: true, keyCode: $.simulate.VK_PGDN});
date.setFullYear(date.getFullYear() + 1); date.setFullYear(date.getFullYear() + 1);
equalsDate(selectedDate, date, 'Callback change month/year date - ctrl+pgdn'); equals(selectedDate, newMonthYear(date),
'Callback change month/year date - ctrl+pgdn');
inp.datepicker('setDate', new Date(2007, 1 - 1, 26)); inp.datepicker('setDate', new Date(2007, 1 - 1, 26));
equalsDate(selectedDate, new Date(2007, 1 - 1, 1), equals(selectedDate, '2007/1', 'Callback change month/year date - setDate');
'Callback change month/year date - setDate');
selectedDate = null; selectedDate = null;
inp.datepicker('setDate', new Date(2007, 1 - 1, 12)); inp.datepicker('setDate', new Date(2007, 1 - 1, 12));
ok(selectedDate == null, 'Callback change month/year date - setDate no change'); ok(selectedDate == null, 'Callback change month/year date - setDate no change');
// onChangeMonthYear step by 2 // onChangeMonthYear step by 2
inp.datepicker('change', {stepMonths: 2}). inp.datepicker('change', {stepMonths: 2}).
datepicker('hide').val('').datepicker('show'); datepicker('hide').val('').datepicker('show').
inp.simulate('keydown', {keyCode: $.simulate.VK_PGUP}); simulate('keydown', {keyCode: $.simulate.VK_PGUP});
date.setMonth(date.getMonth() - 14); date.setMonth(date.getMonth() - 14);
equalsDate(selectedDate, date, 'Callback change month/year by 2 date - pgup'); equals(selectedDate, newMonthYear(date),
'Callback change month/year by 2 date - pgup');
inp.simulate('keydown', {ctrlKey: true, keyCode: $.simulate.VK_PGUP}); inp.simulate('keydown', {ctrlKey: true, keyCode: $.simulate.VK_PGUP});
date.setMonth(date.getMonth() - 12); date.setMonth(date.getMonth() - 12);
equalsDate(selectedDate, date, 'Callback change month/year by 2 date - ctrl+pgup'); equals(selectedDate, newMonthYear(date),
'Callback change month/year by 2 date - ctrl+pgup');
inp.simulate('keydown', {keyCode: $.simulate.VK_PGDN}); inp.simulate('keydown', {keyCode: $.simulate.VK_PGDN});
date.setMonth(date.getMonth() + 2); date.setMonth(date.getMonth() + 2);
equalsDate(selectedDate, date, 'Callback change month/year by 2 date - pgdn'); equals(selectedDate, newMonthYear(date),
'Callback change month/year by 2 date - pgdn');
inp.simulate('keydown', {ctrlKey: true, keyCode: $.simulate.VK_PGDN}); inp.simulate('keydown', {ctrlKey: true, keyCode: $.simulate.VK_PGDN});
date.setMonth(date.getMonth() + 12); date.setMonth(date.getMonth() + 12);
equalsDate(selectedDate, date, 'Callback change month/year by 2 date - ctrl+pgdn'); equals(selectedDate, newMonthYear(date),
'Callback change month/year by 2 date - ctrl+pgdn');
// onClose // onClose
inp.datepicker('change', {onClose: callback, onChangeMonthYear: null, stepMonths: 1}). inp.datepicker('change', {onClose: callback, onChangeMonthYear: null, stepMonths: 1}).
val('').datepicker('show'); val('').datepicker('show').
inp.simulate('keydown', {keyCode: $.simulate.VK_ESC}); simulate('keydown', {keyCode: $.simulate.VK_ESC});
equals(selectedThis, inp[0], 'Callback close this'); equals(selectedThis, inp[0], 'Callback close this');
equals(selectedInst, $.data(inp[0], PROP_NAME), 'Callback close inst'); equals(selectedInst, $.data(inp[0], PROP_NAME), 'Callback close inst');
ok(selectedDate == null, 'Callback close date - esc'); equals(selectedDate, '', 'Callback close date - esc');
inp.val('').datepicker('show'); inp.val('').datepicker('show').
inp.simulate('keydown', {keyCode: $.simulate.VK_ENTER}); simulate('keydown', {keyCode: $.simulate.VK_ENTER});
equalsDate(selectedDate, new Date(), 'Callback close date - enter'); equals(selectedDate, $.datepicker.formatDate('mm/dd/yy', new Date()),
inp.val('02/04/2008').datepicker('show'); 'Callback close date - enter');
inp.simulate('keydown', {keyCode: $.simulate.VK_ESC}); inp.val('02/04/2008').datepicker('show').
equalsDate(selectedDate, new Date(2008, 2 - 1, 4), 'Callback close date - preset'); simulate('keydown', {keyCode: $.simulate.VK_ESC});
inp.val('02/04/2008').datepicker('show'); equals(selectedDate, '02/04/2008', 'Callback close date - preset');
inp.simulate('keydown', {ctrlKey: true, keyCode: $.simulate.VK_END}); inp.val('02/04/2008').datepicker('show').
ok(selectedDate == null, 'Callback close date - ctrl+end'); simulate('keydown', {ctrlKey: true, keyCode: $.simulate.VK_END});
equals(selectedDate, '', 'Callback close date - ctrl+end');
}); });
function highlight20(date, inst) { function highlight20(date, inst) {

View File

@ -665,7 +665,7 @@ $.extend(Datepicker.prototype, {
var onClose = this._get(inst, 'onClose'); var onClose = this._get(inst, 'onClose');
if (onClose) if (onClose)
onClose.apply((inst.input ? inst.input[0] : null), onClose.apply((inst.input ? inst.input[0] : null),
[this._getDate(inst), inst]); // trigger custom callback [(inst.input ? inst.input.val() : ''), inst]); // trigger custom callback
this._datepickerShowing = false; this._datepickerShowing = false;
this._lastInput = null; this._lastInput = null;
inst.settings.prompt = null; inst.settings.prompt = null;
@ -1577,7 +1577,7 @@ $.extend(Datepicker.prototype, {
var onChange = this._get(inst, 'onChangeMonthYear'); var onChange = this._get(inst, 'onChangeMonthYear');
if (onChange) if (onChange)
onChange.apply((inst.input ? inst.input[0] : null), onChange.apply((inst.input ? inst.input[0] : null),
[new Date(inst.selectedYear, inst.selectedMonth, 1), inst]); [inst.selectedYear, inst.selectedMonth + 1, inst]);
}, },
/* Determine the number of months to show. */ /* Determine the number of months to show. */