dpSetDisabled : function(s)
{
return _w.call(this, 'setDisabled', s);
},
dpSetStartDate : function(d)
{
return _w.call(this, 'setStartDate', d);
},
dpSetEndDate : function(d)
{
return _w.call(this, 'setEndDate', d);
},
dpGetSelected : function()
{
var c = _getController(this[0]);
if (c) {
return c.getSelected();
}
return null;
},
dpSetSelected : function(d, v, m, e)
{
if (v == undefined) v=true;
if (m == undefined) m=true;
if (e == undefined) e=true;
return _w.call(this, 'setSelected', Date.fromString(d), v, m, e);
},
dpSetDisplayedMonth : function(m, y)
{
return _w.call(this, 'setDisplayedMonth', Number(m), Number(y), true);
},
dpDisplay : function(e)
{
return _w.call(this, 'display', e);
},
dpSetRenderCallback : function(a)
{
return _w.call(this, 'setRenderCallback', a);
},
dpSetPosition : function(v, h)
{
return _w.call(this, 'setPosition', v, h);
},
dpSetOffset : function(v, h)
{
return _w.call(this, 'setOffset', v, h);
},
dpClose : function()
{
return _w.call(this, '_closeCalendar', false, this[0]);
},
dpRerenderCalendar : function()
{
return _w.call(this, '_rerenderCalendar');
},
// private function called on unload to clean up any expandos etc and prevent memory links...
_dpDestroy : function()
{
// TODO - implement this?
}
});
var _w = function(f, a1, a2, a3, a4)
{
return this.each(
function()
{
var c = _getController(this);
if (c) {
c[f](a1, a2, a3, a4);
}
}
);
};
function DatePicker(ele)
{
this.ele = ele;
// initial values...
this.displayedMonth = null;
this.displayedYear = null;
this.startDate = null;
this.endDate = null;
this.showYearNavigation = null;
this.closeOnSelect = null;
this.displayClose = null;
this.rememberViewedMonth= null;
this.selectMultiple = null;
this.numSelectable = null;
this.numSelected = null;
this.verticalPosition = null;
this.horizontalPosition = null;
this.verticalOffset = null;
this.horizontalOffset = null;
this.button = null;
this.renderCallback = [];
this.selectedDates = {};
this.inline = null;
this.context = '#dp-popup';
this.settings = {};
};
$.extend(
DatePicker.prototype,
{
init : function(s)
{
this.setStartDate(s.startDate);
this.setEndDate(s.endDate);
this.setDisplayedMonth(Number(s.month), Number(s.year));
this.setRenderCallback(s.renderCallback);
this.showYearNavigation = s.showYearNavigation;
this.closeOnSelect = s.closeOnSelect;
this.displayClose = s.displayClose;
this.rememberViewedMonth = s.rememberViewedMonth;
this.selectMultiple = s.selectMultiple;
this.numSelectable = s.selectMultiple ? s.numSelectable : 1;
this.numSelected = 0;
this.verticalPosition = s.verticalPosition;
this.horizontalPosition = s.horizontalPosition;
this.hoverClass = s.hoverClass;
this.setOffset(s.verticalOffset, s.horizontalOffset);
this.inline = s.inline;
this.settings = s;
if (this.inline) {
this.context = this.ele;
this.display();
}
},
setStartDate : function(d)
{
if (d) {
if (d instanceof Date) {
this.startDate = d;
} else {
this.startDate = Date.fromString(d);
}
}
if (!this.startDate) {
this.startDate = (new Date()).zeroTime();
}
this.setDisplayedMonth(this.displayedMonth, this.displayedYear);
},
setEndDate : function(d)
{
if (d) {
if (d instanceof Date) {
this.endDate = d;
} else {
this.endDate = Date.fromString(d);
}
}
if (!this.endDate) {
this.endDate = (new Date('12/31/2999')); // using the JS Date.parse function which expects mm/dd/yyyy
}
if (this.endDate.getTime() < this.startDate.getTime()) {
this.endDate = this.startDate;
}
this.setDisplayedMonth(this.displayedMonth, this.displayedYear);
},
setPosition : function(v, h)
{
this.verticalPosition = v;
this.horizontalPosition = h;
},
setOffset : function(v, h)
{
this.verticalOffset = parseInt(v) || 0;
this.horizontalOffset = parseInt(h) || 0;
},
setDisabled : function(s)
{
$e = $(this.ele);
$e[s ? 'addClass' : 'removeClass']('dp-disabled');
if (this.button) {
$but = $(this.button);
$but[s ? 'addClass' : 'removeClass']('dp-disabled');
$but.attr('title', s ? '' : $.dpText.TEXT_CHOOSE_DATE);
}
if ($e.is(':text')) {
$e.attr('disabled', s ? 'disabled' : '');
}
},
setDisplayedMonth : function(m, y, rerender)
{
if (this.startDate == undefined || this.endDate == undefined) {
return;
}
var s = new Date(this.startDate.getTime());
s.setDate(1);
var e = new Date(this.endDate.getTime());
e.setDate(1);
var t;
if ((!m && !y) || (isNaN(m) && isNaN(y))) {
// no month or year passed - default to current month
t = new Date().zeroTime();
t.setDate(1);
} else if (isNaN(m)) {
// just year passed in - presume we want the displayedMonth
t = new Date(y, this.displayedMonth, 1);
} else if (isNaN(y)) {
// just month passed in - presume we want the displayedYear
t = new Date(this.displayedYear, m, 1);
} else {
// year and month passed in - that's the date we want!
t = new Date(y, m, 1)
}
// check if the desired date is within the range of our defined startDate and endDate
if (t.getTime() < s.getTime()) {
t = s;
} else if (t.getTime() > e.getTime()) {
t = e;
}
var oldMonth = this.displayedMonth;
var oldYear = this.displayedYear;
this.displayedMonth = t.getMonth();
this.displayedYear = t.getFullYear();
if (rerender && (this.displayedMonth != oldMonth || this.displayedYear != oldYear))
{
this._rerenderCalendar();
$(this.ele).trigger('dpMonthChanged', [this.displayedMonth, this.displayedYear]);
}
},
setSelected : function(d, v, moveToMonth, dispatchEvents)
{
if (d < this.startDate || d.zeroTime() > this.endDate.zeroTime()) {
// Don't allow people to select dates outside range...
return;
}
var s = this.settings;
if (s.selectWeek)
{
d = d.addDays(- (d.getDay() - Date.firstDayOfWeek + 7) % 7);
if (d < this.startDate) // The first day of this week is before the start date so is unselectable...
{
return;
}
}
if (v == this.isSelected(d)) // this date is already un/selected
{
return;
}
if (this.selectMultiple == false) {
this.clearSelected();
} else if (v && this.numSelected == this.numSelectable) {
// can't select any more dates...
return;
}
if (moveToMonth && (this.displayedMonth != d.getMonth() || this.displayedYear != d.getFullYear())) {
this.setDisplayedMonth(d.getMonth(), d.getFullYear(), true);
}
this.selectedDates[d.asString()] = v;
this.numSelected += v ? 1 : -1;
var selectorString = 'td.' + (d.getMonth() == this.displayedMonth ? 'current-month' : 'other-month');
var $td;
$(selectorString, this.context).each(
function()
{
if ($(this).data('datePickerDate') == d.asString()) {
$td = $(this);
if (s.selectWeek)
{
$td.parent()[v ? 'addClass' : 'removeClass']('selectedWeek');
}
$td[v ? 'addClass' : 'removeClass']('selected');
}
}
);
$('td', this.context).not('.selected')[this.selectMultiple && this.numSelected == this.numSelectable ? 'addClass' : 'removeClass']('unselectable');
if (dispatchEvents)
{
var s = this.isSelected(d);
$e = $(this.ele);
var dClone = Date.fromString(d.asString());
$e.trigger('dateSelected', [dClone, $td, s]);
$e.trigger('change');
}
},
isSelected : function(d)
{
return this.selectedDates[d.asString()];
},
getSelected : function()
{
var r = [];
for(var s in this.selectedDates) {
if (this.selectedDates[s] == true) {
r.push(Date.fromString(s));
}
}
return r;
},
clearSelected : function()
{
this.selectedDates = {};
this.numSelected = 0;
$('td.selected', this.context).removeClass('selected').parent().removeClass('selectedWeek');
},