2 * Copyright (c) 2007 Kelvin Luck (http://www.kelvinluck.com/)
3 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
4 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
6 * $Id: jquery.datePicker.js 3739 2007-10-25 13:55:30Z kelvin.luck $
13 * Render a calendar table into any matched elements.
15 * @param Object s (optional) Customize your calendars.
16 * @option Number month The month to render (NOTE that months are zero based). Default is today's month.
17 * @option Number year The year to render. Default is today's year.
18 * @option Function renderCallback A reference to a function that is called as each cell is rendered and which can add classes and event listeners to the created nodes. Default is no callback.
19 * @option Number showHeader Whether or not to show the header row, possible values are: $.dpConst.SHOW_HEADER_NONE (no header), $.dpConst.SHOW_HEADER_SHORT (first letter of each day) and $.dpConst.SHOW_HEADER_LONG (full name of each day). Default is $.dpConst.SHOW_HEADER_SHORT.
20 * @option String hoverClass The class to attach to each cell when you hover over it (to allow you to use hover effects in IE6 which doesn't support the :hover pseudo-class on elements other than links). Default is dp-hover. Pass false if you don't want a hover class.
22 * @name renderCalendar
23 * @cat plugins/datePicker
24 * @author Kelvin Luck (http://www.kelvinluck.com/)
26 * @example $('#calendar-me').renderCalendar({month:0, year:2007});
27 * @desc Renders a calendar displaying January 2007 into the element with an id of calendar-me.
30 * var testCallback = function($td, thisDate, month, year)
32 * if ($td.is('.current-month') && thisDate.getDay() == 4) {
33 * var d = thisDate.getDate();
38 * alert('You clicked on ' + d + '/' + (Number(month)+1) + '/' + year);
40 * ).addClass('thursday');
41 * } else if (thisDate.getDay() == 5) {
42 * $td.html('Friday the ' + $td.html() + 'th');
45 * $('#calendar-me').renderCalendar({month:0, year:2007, renderCallback:testCallback});
47 * @desc Renders a calendar displaying January 2007 into the element with an id of calendar-me. Every Thursday in the current month has a class of "thursday" applied to it, is clickable and shows an alert when clicked. Every Friday on the calendar has the number inside replaced with text.
49 renderCalendar : function(s
)
53 return document
.createElement(a
);
60 renderCallback
: null,
61 showHeader
: $.dpConst
.SHOW_HEADER_SHORT
,
63 hoverClass
: 'dp-hover'
68 if (s
.showHeader
!= $.dpConst
.SHOW_HEADER_NONE
) {
69 var headRow
= $(dc('tr'));
70 for (var i
=Date
.firstDayOfWeek
; i
<Date
.firstDayOfWeek
+7; i
++) {
72 var day
= Date
.dayNames
[weekday
];
74 jQuery(dc('th')).attr({'scope':'col', 'abbr':day
, 'title':day
, 'class':(weekday
== 0 || weekday
== 6 ? 'weekend' : 'weekday')}).html(s
.showHeader
== $.dpConst
.SHOW_HEADER_SHORT
? day
.substr(0, 1) : day
)
79 var calendarTable
= $(dc('table'))
83 'className':'jCalendar'
87 (s
.showHeader
!= $.dpConst
.SHOW_HEADER_NONE
?
94 var tbody
= $(dc('tbody'));
96 var today
= (new Date()).zeroTime();
98 var month
= s
.month
== undefined ? today
.getMonth() : s
.month
;
99 var year
= s
.year
|| today
.getFullYear();
101 var currentDate
= new Date(year
, month
, 1);
104 var firstDayOffset
= Date
.firstDayOfWeek
- currentDate
.getDay() + 1;
105 if (firstDayOffset
> 1) firstDayOffset
-= 7;
106 var weeksToDraw
= Math
.ceil(( (-1*firstDayOffset
+1) + currentDate
.getDaysInMonth() ) /7);
107 currentDate
.addDays(firstDayOffset
-1);
109 var doHover = function()
112 $(this).addClass(s
.hoverClass
);
115 var unHover = function()
118 $(this).removeClass(s
.hoverClass
);
123 while (w
++<weeksToDraw
) {
124 var r
= jQuery(dc('tr'));
125 for (var i
=0; i
<7; i
++) {
126 var thisMonth
= currentDate
.getMonth() == month
;
128 .text(currentDate
.getDate() + '')
129 .attr('className', (thisMonth
? 'current-month ' : 'other-month ') +
130 (currentDate
.isWeekend() ? 'weekend ' : 'weekday ') +
131 (thisMonth
&& currentDate
.getTime() == today
.getTime() ? 'today ' : '')
133 .hover(doHover
, unHover
)
135 if (s
.renderCallback
) {
136 s
.renderCallback(d
, currentDate
, month
, year
);
139 currentDate
.addDays(1);
143 calendarTable
.append(tbody
);
148 $(this).empty().append(calendarTable
);
153 * Create a datePicker associated with each of the matched elements.
155 * The matched element will receive a few custom events with the following signatures:
157 * dateSelected(event, date, $td, status)
158 * Triggered when a date is selected. event is a reference to the event, date is the Date selected, $td is a jquery object wrapped around the TD that was clicked on and status is whether the date was selected (true) or deselected (false)
160 * dpClosed(event, selected)
161 * Triggered when the date picker is closed. event is a reference to the event and selected is an Array containing Date objects.
163 * dpMonthChanged(event, displayedMonth, displayedYear)
164 * Triggered when the month of the popped up calendar is changed. event is a reference to the event, displayedMonth is the number of the month now displayed (zero based) and displayedYear is the year of the month.
166 * dpDisplayed(event, $datePickerDiv)
167 * Triggered when the date picker is created. $datePickerDiv is the div containing the date picker. Use this event to add custom content/ listeners to the popped up date picker.
169 * @param Object s (optional) Customize your date pickers.
170 * @option Number month The month to render when the date picker is opened (NOTE that months are zero based). Default is today's month.
171 * @option Number year The year to render when the date picker is opened. Default is today's year.
172 * @option String startDate The first date date can be selected.
173 * @option String endDate The last date that can be selected.
174 * @option Boolean inline Whether to create the datePicker as inline (e.g. always on the page) or as a model popup. Default is false (== modal popup)
175 * @option Boolean createButton Whether to create a .dp-choose-date anchor directly after the matched element which when clicked will trigger the showing of the date picker. Default is true.
176 * @option Boolean showYearNavigation Whether to display buttons which allow the user to navigate through the months a year at a time. Default is true.
177 * @option Boolean closeOnSelect Whether to close the date picker when a date is selected. Default is true.
178 * @option Boolean displayClose Whether to create a "Close" button within the date picker popup. Default is false.
179 * @option Boolean selectMultiple Whether a user should be able to select multiple dates with this date picker. Default is false.
180 * @option Boolean clickInput If the matched element is an input type="text" and this option is true then clicking on the input will cause the date picker to appear.
181 * @option Number verticalPosition The vertical alignment of the popped up date picker to the matched element. One of $.dpConst.POS_TOP and $.dpConst.POS_BOTTOM. Default is $.dpConst.POS_TOP.
182 * @option Number horizontalPosition The horizontal alignment of the popped up date picker to the matched element. One of $.dpConst.POS_LEFT and $.dpConst.POS_RIGHT.
183 * @option Number verticalOffset The number of pixels offset from the defined verticalPosition of this date picker that it should pop up in. Default in 0.
184 * @option Number horizontalOffset The number of pixels offset from the defined horizontalPosition of this date picker that it should pop up in. Default in 0.
185 * @option (Function|Array) renderCallback A reference to a function (or an array of seperate functions) that is called as each cell is rendered and which can add classes and event listeners to the created nodes. Each callback function will receive four arguments; a jquery object wrapping the created TD, a Date object containing the date this TD represents, a number giving the currently rendered month and a number giving the currently rendered year. Default is no callback.
186 * @option String hoverClass The class to attach to each cell when you hover over it (to allow you to use hover effects in IE6 which doesn't support the :hover pseudo-class on elements other than links). Default is dp-hover. Pass false if you don't want a hover class.
189 * @cat plugins/datePicker
190 * @author Kelvin Luck (http://www.kelvinluck.com/)
192 * @example $('input.date-picker').datePicker();
193 * @desc Creates a date picker button next to all matched input elements. When the button is clicked on the value of the selected date will be placed in the corresponding input (formatted according to Date.format).
195 * @example demo/index.html
196 * @desc See the projects homepage for many more complex examples...
198 datePicker : function(s
)
200 if (!$.event
._dpCache
) $.event
._dpCache
= [];
202 // initialise the date picker controller with the relevant settings...
207 startDate
: undefined,
212 showYearNavigation
: true,
213 closeOnSelect
: true,
214 displayClose
: false,
215 selectMultiple
: false,
217 verticalPosition
: $.dpConst
.POS_TOP
,
218 horizontalPosition
: $.dpConst
.POS_LEFT
,
220 horizontalOffset
: 0,
221 hoverClass
: 'dp-hover'
230 var alreadyExists
= true;
233 this._dpId
= $.event
.guid
++;
234 $.event
._dpCache
[this._dpId
] = new DatePicker(this);
235 alreadyExists
= false;
239 s
.createButton
= false;
240 s
.displayClose
= false;
241 s
.closeOnSelect
= false;
245 var controller
= $.event
._dpCache
[this._dpId
];
249 if (!alreadyExists
&& s
.createButton
) {
251 controller
.button
= $('<a href="#" class="dp-choose-date" title="' + $.dpText
.TITLE_CHOOSE_DATE
+ '">' + $.dpText
.TEXT_CHOOSE_DATE
+ '</a>')
256 $this.dpDisplay(this);
261 $this.after(controller
.button
);
264 if (!alreadyExists
&& $this.is(':text')) {
268 function(e
, selectedDate
, $td
)
270 this.value
= selectedDate
.asString();
276 var d
= Date
.fromString(this.value
);
278 controller
.setSelected(d
, true, true);
291 var d
= Date
.fromString(this.value
);
292 if (this.value
!= '' && d
) {
293 controller
.setSelected(d
, true, true);
297 $this.addClass('dp-applied');
303 * Disables or enables this date picker
305 * @param Boolean s Whether to disable (true) or enable (false) this datePicker
307 * @name dpSetDisabled
308 * @cat plugins/datePicker
309 * @author Kelvin Luck (http://www.kelvinluck.com/)
311 * @example $('.date-picker').datePicker();
312 * $('.date-picker').dpSetDisabled(true);
313 * @desc Prevents this date picker from displaying and adds a class of dp-disabled to it (and it's associated button if it has one) for styling purposes. If the matched element is an input field then it will also set the disabled attribute to stop people directly editing the field.
315 dpSetDisabled : function(s
)
317 return _w
.call(this, 'setDisabled', s
);
320 * Updates the first selectable date for any date pickers on any matched elements.
322 * @param String d A string representing the first selectable date (formatted according to Date.format).
324 * @name dpSetStartDate
325 * @cat plugins/datePicker
326 * @author Kelvin Luck (http://www.kelvinluck.com/)
328 * @example $('.date-picker').datePicker();
329 * $('.date-picker').dpSetStartDate('01/01/2000');
330 * @desc Creates a date picker associated with all elements with a class of "date-picker" then sets the first selectable date for each of these to the first day of the millenium.
332 dpSetStartDate : function(d
)
334 return _w
.call(this, 'setStartDate', d
);
337 * Updates the last selectable date for any date pickers on any matched elements.
339 * @param String d A string representing the last selectable date (formatted according to Date.format).
342 * @cat plugins/datePicker
343 * @author Kelvin Luck (http://www.kelvinluck.com/)
345 * @example $('.date-picker').datePicker();
346 * $('.date-picker').dpSetEndDate('01/01/2010');
347 * @desc Creates a date picker associated with all elements with a class of "date-picker" then sets the last selectable date for each of these to the first Janurary 2010.
349 dpSetEndDate : function(d
)
351 return _w
.call(this, 'setEndDate', d
);
354 * Gets a list of Dates currently selected by this datePicker. This will be an empty array if no dates are currently selected or NULL if there is no datePicker associated with the matched element.
357 * @name dpGetSelected
358 * @cat plugins/datePicker
359 * @author Kelvin Luck (http://www.kelvinluck.com/)
361 * @example $('.date-picker').datePicker();
362 * alert($('.date-picker').dpGetSelected());
363 * @desc Will alert an empty array (as nothing is selected yet)
365 dpGetSelected : function()
367 var c
= _getController(this[0]);
369 return c
.getSelected();
374 * Selects or deselects a date on any matched element's date pickers. Deselcting is only useful on date pickers where selectMultiple==true. Selecting will only work if the passed date is within the startDate and endDate boundries for a given date picker.
376 * @param String d A string representing the date you want to select (formatted according to Date.format).
377 * @param Boolean v Whether you want to select (true) or deselect (false) this date. Optional - default = true.
378 * @param Boolean m Whether you want the date picker to open up on the month of this date when it is next opened. Optional - default = true.
380 * @name dpSetSelected
381 * @cat plugins/datePicker
382 * @author Kelvin Luck (http://www.kelvinluck.com/)
384 * @example $('.date-picker').datePicker();
385 * $('.date-picker').dpSetSelected('01/01/2010');
386 * @desc Creates a date picker associated with all elements with a class of "date-picker" then sets the selected date on these date pickers to the first Janurary 2010. When the date picker is next opened it will display Janurary 2010.
388 dpSetSelected : function(d
, v
, m
)
390 if (v
== undefined) v
=true;
391 if (m
== undefined) m
=true;
392 return _w
.call(this, 'setSelected', Date
.fromString(d
), v
, m
);
395 * Sets the month that will be displayed when the date picker is next opened. If the passed month is before startDate then the month containing startDate will be displayed instead. If the passed month is after endDate then the month containing the endDate will be displayed instead.
397 * @param Number m The month you want the date picker to display. Optional - defaults to the currently displayed month.
398 * @param Number y The year you want the date picker to display. Optional - defaults to the currently displayed year.
400 * @name dpSetDisplayedMonth
401 * @cat plugins/datePicker
402 * @author Kelvin Luck (http://www.kelvinluck.com/)
404 * @example $('.date-picker').datePicker();
405 * $('.date-picker').dpSetDisplayedMonth(10, 2008);
406 * @desc Creates a date picker associated with all elements with a class of "date-picker" then sets the selected date on these date pickers to the first Janurary 2010. When the date picker is next opened it will display Janurary 2010.
408 dpSetDisplayedMonth : function(m
, y
)
410 return _w
.call(this, 'setDisplayedMonth', Number(m
), Number(y
));
413 * Displays the date picker associated with the matched elements. Since only one date picker can be displayed at once then the date picker associated with the last matched element will be the one that is displayed.
415 * @param HTMLElement e An element that you want the date picker to pop up relative in position to. Optional - default behaviour is to pop up next to the element associated with this date picker.
418 * @cat plugins/datePicker
419 * @author Kelvin Luck (http://www.kelvinluck.com/)
421 * @example $('#date-picker').datePicker();
422 * $('#date-picker').dpDisplay();
423 * @desc Creates a date picker associated with the element with an id of date-picker and then causes it to pop up.
425 dpDisplay : function(e
)
427 return _w
.call(this, 'display', e
);
430 * Sets a function or array of functions that is called when each TD of the date picker popup is rendered to the page
432 * @param (Function|Array) a A function or an array of functions that are called when each td is rendered. Each function will receive four arguments; a jquery object wrapping the created TD, a Date object containing the date this TD represents, a number giving the currently rendered month and a number giving the currently rendered year.
434 * @name dpSetRenderCallback
435 * @cat plugins/datePicker
436 * @author Kelvin Luck (http://www.kelvinluck.com/)
438 * @example $('#date-picker').datePicker();
439 * $('#date-picker').dpSetRenderCallback(function($td, thisDate, month, year)
441 * // do stuff as each td is rendered dependant on the date in the td and the displayed month and year
443 * @desc Creates a date picker associated with the element with an id of date-picker and then creates a function which is called as each td is rendered when this date picker is displayed.
445 dpSetRenderCallback : function(a
)
447 return _w
.call(this, 'setRenderCallback', a
);
450 * Sets the position that the datePicker will pop up (relative to it's associated element)
452 * @param Number v The vertical alignment of the created date picker to it's associated element. Possible values are $.dpConst.POS_TOP and $.dpConst.POS_BOTTOM
453 * @param Number h The horizontal alignment of the created date picker to it's associated element. Possible values are $.dpConst.POS_LEFT and $.dpConst.POS_RIGHT
455 * @name dpSetPosition
456 * @cat plugins/datePicker
457 * @author Kelvin Luck (http://www.kelvinluck.com/)
459 * @example $('#date-picker').datePicker();
460 * $('#date-picker').dpSetPosition($.dpConst.POS_BOTTOM, $.dpConst.POS_RIGHT);
461 * @desc Creates a date picker associated with the element with an id of date-picker and makes it so that when this date picker pops up it will be bottom and right aligned to the #date-picker element.
463 dpSetPosition : function(v
, h
)
465 return _w
.call(this, 'setPosition', v
, h
);
468 * Sets the offset that the popped up date picker will have from it's default position relative to it's associated element (as set by dpSetPosition)
470 * @param Number v The vertical offset of the created date picker.
471 * @param Number h The horizontal offset of the created date picker.
474 * @cat plugins/datePicker
475 * @author Kelvin Luck (http://www.kelvinluck.com/)
477 * @example $('#date-picker').datePicker();
478 * $('#date-picker').dpSetOffset(-20, 200);
479 * @desc Creates a date picker associated with the element with an id of date-picker and makes it so that when this date picker pops up it will be 20 pixels above and 200 pixels to the right of it's default position.
481 dpSetOffset : function(v
, h
)
483 return _w
.call(this, 'setOffset', v
, h
);
486 * Closes the open date picker associated with this element.
490 * @cat plugins/datePicker
491 * @author Kelvin Luck (http://www.kelvinluck.com/)
493 * @example $('.date-pick')
499 * $(this).dpDisplay();
508 * @desc Creates a date picker and makes it appear when the relevant element is focused and disappear when it is blurred.
512 return _w
.call(this, '_closeCalendar', false, this[0]);
514 // private function called on unload to clean up any expandos etc and prevent memory links...
515 _dpDestroy : function()
517 // TODO - implement this?
521 // private internal function to cut down on the amount of code needed where we forward
522 // dp* methods on the jQuery object on to the relevant DatePicker controllers...
523 var _w = function(f
, a1
, a2
, a3
)
528 var c
= _getController(this);
536 function DatePicker(ele
)
541 this.displayedMonth
= null;
542 this.displayedYear
= null;
543 this.startDate
= null;
545 this.showYearNavigation
= null;
546 this.closeOnSelect
= null;
547 this.displayClose
= null;
548 this.selectMultiple
= null;
549 this.verticalPosition
= null;
550 this.horizontalPosition
= null;
551 this.verticalOffset
= null;
552 this.horizontalOffset
= null;
554 this.renderCallback
= [];
555 this.selectedDates
= {};
557 this.context
= '#dp-popup';
560 DatePicker
.prototype,
564 this.setStartDate(s
.startDate
);
565 this.setEndDate(s
.endDate
);
566 this.setDisplayedMonth(Number(s
.month
), Number(s
.year
));
567 this.setRenderCallback(s
.renderCallback
);
568 this.showYearNavigation
= s
.showYearNavigation
;
569 this.closeOnSelect
= s
.closeOnSelect
;
570 this.displayClose
= s
.displayClose
;
571 this.selectMultiple
= s
.selectMultiple
;
572 this.verticalPosition
= s
.verticalPosition
;
573 this.horizontalPosition
= s
.horizontalPosition
;
574 this.hoverClass
= s
.hoverClass
;
575 this.setOffset(s
.verticalOffset
, s
.horizontalOffset
);
576 this.inline
= s
.inline
;
578 this.context
= this.ele
;
582 setStartDate : function(d
)
585 this.startDate
= Date
.fromString(d
);
587 if (!this.startDate
) {
588 this.startDate
= (new Date()).zeroTime();
590 this.setDisplayedMonth(this.displayedMonth
, this.displayedYear
);
592 setEndDate : function(d
)
595 this.endDate
= Date
.fromString(d
);
598 this.endDate
= (new Date('12/31/2999')); // using the JS Date.parse function which expects mm/dd/yyyy
600 if (this.endDate
.getTime() < this.startDate
.getTime()) {
601 this.endDate
= this.startDate
;
603 this.setDisplayedMonth(this.displayedMonth
, this.displayedYear
);
605 setPosition : function(v
, h
)
607 this.verticalPosition
= v
;
608 this.horizontalPosition
= h
;
610 setOffset : function(v
, h
)
612 this.verticalOffset
= parseInt(v
) || 0;
613 this.horizontalOffset
= parseInt(h
) || 0;
615 setDisabled : function(s
)
618 $e
[s
? 'addClass' : 'removeClass']('dp-disabled');
620 $but
= $(this.button
);
621 $but
[s
? 'addClass' : 'removeClass']('dp-disabled');
622 $but
.attr('title', s
? '' : $.dpText
.TEXT_CHOOSE_DATE
);
624 if ($e
.is(':text')) {
625 $e
.attr('disabled', s
? 'disabled' : '');
628 setDisplayedMonth : function(m
, y
)
630 if (this.startDate
== undefined || this.endDate
== undefined) {
633 var s
= new Date(this.startDate
.getTime());
635 var e
= new Date(this.endDate
.getTime());
639 if ((!m
&& !y
) || (isNaN(m
) && isNaN(y
))) {
640 // no month or year passed - default to current month
641 t
= new Date().zeroTime();
643 } else if (isNaN(m
)) {
644 // just year passed in - presume we want the displayedMonth
645 t
= new Date(y
, this.displayedMonth
, 1);
646 } else if (isNaN(y
)) {
647 // just month passed in - presume we want the displayedYear
648 t
= new Date(this.displayedYear
, m
, 1);
650 // year and month passed in - that's the date we want!
651 t
= new Date(y
, m
, 1)
654 // check if the desired date is within the range of our defined startDate and endDate
655 if (t
.getTime() < s
.getTime()) {
657 } else if (t
.getTime() > e
.getTime()) {
660 this.displayedMonth
= t
.getMonth();
661 this.displayedYear
= t
.getFullYear();
662 /* Patch Cedric Morin 2008-09-13 pour les controleurs inline */
664 this._clearCalendar();
665 this._renderCalendar();
668 setSelected : function(d
, v
, moveToMonth
)
670 if (this.selectMultiple
== false) {
671 this.selectedDates
= {};
672 $('td.selected', this.context
).removeClass('selected');
675 this.setDisplayedMonth(d
.getMonth(), d
.getFullYear());
677 this.selectedDates
[d
.toString()] = v
;
679 isSelected : function(d
)
681 return this.selectedDates
[d
.toString()];
683 getSelected : function()
686 for(s
in this.selectedDates
) {
687 if (this.selectedDates
[s
] == true) {
688 r
.push(Date
.parse(s
));
693 display : function(eleAlignTo
)
695 if ($(this.ele
).is('.dp-disabled')) return;
697 eleAlignTo
= eleAlignTo
|| this.ele
;
699 var $ele
= $(eleAlignTo
);
700 var eleOffset
= $ele
.offset();
704 var attrsCalendarHolder
;
708 $createIn
= $(this.ele
);
710 'id' : 'calendar-' + this.ele
._dpId
,
711 'className' : 'dp-popup dp-popup-inline'
716 $createIn
= $('body');
719 'className' : 'dp-popup'
722 'top' : eleOffset
.top
+ c
.verticalOffset
,
723 'left' : eleOffset
.left
+ c
.horizontalOffset
726 var _checkMouse = function(e
)
729 var cal
= $('#dp-popup')[0];
734 } else if (el
== document
) {
738 el
= $(el
).parent()[0];
742 this._checkMouse
= _checkMouse
;
744 this._closeCalendar(true);
755 $('<div class="dp-nav-prev"></div>')
757 $('<a class="dp-nav-prev-year" href="#" title="' + $.dpText
.TEXT_PREV_YEAR
+ '"><<</a>')
762 return c
._displayNewMonth
.call(c
, this, 0, -1);
765 $('<a class="dp-nav-prev-month" href="#" title="' + $.dpText
.TEXT_PREV_MONTH
+ '"><</a>')
770 return c
._displayNewMonth
.call(c
, this, -1, 0);
774 $('<div class="dp-nav-next"></div>')
776 $('<a class="dp-nav-next-year" href="#" title="' + $.dpText
.TEXT_NEXT_YEAR
+ '">>></a>')
781 return c
._displayNewMonth
.call(c
, this, 0, 1);
784 $('<a class="dp-nav-next-month" href="#" title="' + $.dpText
.TEXT_NEXT_MONTH
+ '">></a>')
789 return c
._displayNewMonth
.call(c
, this, 1, 0);
794 .attr('className', 'dp-calendar')
799 var $pop
= this.inline
? $('.dp-popup', this.context
) : $('#dp-popup');
801 if (this.showYearNavigation
== false) {
802 $('.dp-nav-prev-year, .dp-nav-next-year', c
.context
).css('display', 'none');
804 if (this.displayClose
) {
806 $('<a href="#" id="dp-close">' + $.dpText
.TEXT_CLOSE
+ '</a>')
819 $(this.ele
).trigger('dpDisplayed', $pop
);
822 if (this.verticalPosition
== $.dpConst
.POS_BOTTOM
) {
823 $pop
.css('top', eleOffset
.top
+ $ele
.height() - $pop
.height() + c
.verticalOffset
);
825 if (this.horizontalPosition
== $.dpConst
.POS_RIGHT
) {
826 $pop
.css('left', eleOffset
.left
+ $ele
.width() - $pop
.width() + c
.horizontalOffset
);
828 $(document
).bind('mousedown', this._checkMouse
);
831 setRenderCallback : function(a
)
833 if (a
&& typeof(a
) == 'function') {
836 this.renderCallback
= this.renderCallback
.concat(a
);
838 cellRender : function ($td
, thisDate
, month
, year
) {
839 var c
= this.dpController
;
840 var d
= new Date(thisDate
.getTime());
842 // add our click handlers to deal with it when the days are clicked...
849 if (!$this.is('.disabled')) {
850 c
.setSelected(d
, !$this.is('.selected') || !c
.selectMultiple
);
851 var s
= c
.isSelected(d
);
852 $(c
.ele
).trigger('dateSelected', [d
, $td
, s
]);
853 $(c
.ele
).trigger('change');
854 if (c
.closeOnSelect
) {
857 $this[s
? 'addClass' : 'removeClass']('selected');
863 if (c
.isSelected(d
)) {
864 $td
.addClass('selected');
867 // call any extra renderCallbacks that were passed in
868 for (var i
=0; i
<c
.renderCallback
.length
; i
++) {
869 c
.renderCallback
[i
].apply(this, arguments
);
874 // ele is the clicked button - only proceed if it doesn't have the class disabled...
875 // m and y are -1, 0 or 1 depending which direction we want to go in...
876 _displayNewMonth : function(ele
, m
, y
)
878 if (!$(ele
).is('.disabled')) {
879 this.setDisplayedMonth(this.displayedMonth
+ m
, this.displayedYear
+ y
);
880 this._clearCalendar();
881 this._renderCalendar();
882 $(this.ele
).trigger('dpMonthChanged', [this.displayedMonth
, this.displayedYear
]);
887 _renderCalendar : function()
890 $('h2', this.context
).html(Date
.monthNames
[this.displayedMonth
] + ' ' + this.displayedYear
);
892 // render the calendar...
893 $('.dp-calendar', this.context
).renderCalendar(
895 month
: this.displayedMonth
,
896 year
: this.displayedYear
,
897 renderCallback
: this.cellRender
,
899 hoverClass
: this.hoverClass
903 // update the status of the control buttons and disable dates before startDate or after endDate...
904 // TODO: When should the year buttons be disabled? When you can't go forward a whole year from where you are or is that annoying?
905 if (this.displayedYear
== this.startDate
.getFullYear() && this.displayedMonth
== this.startDate
.getMonth()) {
906 $('.dp-nav-prev-year', this.context
).addClass('disabled');
907 $('.dp-nav-prev-month', this.context
).addClass('disabled');
908 $('.dp-calendar td.other-month', this.context
).each(
912 if (Number($this.text()) > 20) {
913 $this.addClass('disabled');
917 var d
= this.startDate
.getDate();
918 $('.dp-calendar td.current-month', this.context
).each(
922 if (Number($this.text()) < d
) {
923 $this.addClass('disabled');
928 $('.dp-nav-prev-year', this.context
).removeClass('disabled');
929 $('.dp-nav-prev-month', this.context
).removeClass('disabled');
930 var d
= this.startDate
.getDate();
932 // check if the startDate is last month as we might need to add some disabled classes...
933 var sd
= new Date(this.startDate
.getTime());
935 if (this.displayedYear
== sd
.getFullYear() && this.displayedMonth
== sd
.getMonth()) {
936 $('dp-calendar td.other-month', this.context
).each(
940 if (Number($this.text()) < d
) {
941 $this.addClass('disabled');
948 if (this.displayedYear
== this.endDate
.getFullYear() && this.displayedMonth
== this.endDate
.getMonth()) {
949 $('.dp-nav-next-year', this.context
).addClass('disabled');
950 $('.dp-nav-next-month', this.context
).addClass('disabled');
951 $('.dp-calendar td.other-month', this.context
).each(
955 if (Number($this.text()) < 14) {
956 $this.addClass('disabled');
960 var d
= this.endDate
.getDate();
961 $('.dp-calendar td.current-month', this.context
).each(
965 if (Number($this.text()) > d
) {
966 $this.addClass('disabled');
971 $('.dp-nav-next-year', this.context
).removeClass('disabled');
972 $('.dp-nav-next-month', this.context
).removeClass('disabled');
973 var d
= this.endDate
.getDate();
975 // check if the endDate is next month as we might need to add some disabled classes...
976 var ed
= new Date(this.endDate
.getTime());
978 if (this.displayedYear
== ed
.getFullYear() && this.displayedMonth
== ed
.getMonth()) {
979 $('.dp-calendar td.other-month', this.context
).each(
983 if (Number($this.text()) > d
) {
984 $this.addClass('disabled');
992 _closeCalendar : function(programatic
, ele
)
994 if (!ele
|| ele
== this.ele
)
996 $(document
).unbind('mousedown', this._checkMouse
);
997 this._clearCalendar();
998 $('#dp-popup a').unbind();
999 $('#dp-popup').empty().remove();
1001 $(this.ele
).trigger('dpClosed', [this.getSelected()]);
1005 // empties the current dp-calendar div and makes sure that all events are unbound
1006 // and expandos removed to avoid memory leaks...
1007 _clearCalendar : function()
1010 $('.dp-calendar td', this.context
).unbind();
1011 $('.dp-calendar', this.context
).empty();
1018 SHOW_HEADER_NONE
: 0,
1019 SHOW_HEADER_SHORT
: 1,
1020 SHOW_HEADER_LONG
: 2,
1028 TEXT_PREV_YEAR
: 'Previous year',
1029 TEXT_PREV_MONTH
: 'Previous month',
1030 TEXT_NEXT_YEAR
: 'Next year',
1031 TEXT_NEXT_MONTH
: 'Next month',
1032 TEXT_CLOSE
: 'Close',
1033 TEXT_CHOOSE_DATE
: 'Choose date'
1036 $.dpVersion
= '$Id: jquery.datePicker.js 3739 2007-10-25 13:55:30Z kelvin.luck $';
1038 function _getController(ele
)
1040 if (ele
._dpId
) return $.event
._dpCache
[ele
._dpId
];
1044 // make it so that no error is thrown if bgIframe plugin isn't included (allows you to use conditional
1045 // comments to only include bgIframe where it is needed in IE without breaking this plugin).
1046 if ($.fn
.bgIframe
== undefined) {
1047 $.fn
.bgIframe = function() {return this; };
1053 .bind('unload', function() {
1054 var els
= $.event
._dpCache
|| [];
1055 for (var i
in els
) {
1056 $(els
[i
].ele
)._dpDestroy();