[SPIP] +2.1.12
[velocampus/web/www.git] / www / plugins / auto / spip-bonux / formulaires / dateur / jquery.datePicker.js
1 /**
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.
5 *
6 * $Id: jquery.datePicker.js 3739 2007-10-25 13:55:30Z kelvin.luck $
7 **/
8
9 (function($){
10
11 $.fn.extend({
12 /**
13 * Render a calendar table into any matched elements.
14 *
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.
21 * @type jQuery
22 * @name renderCalendar
23 * @cat plugins/datePicker
24 * @author Kelvin Luck (http://www.kelvinluck.com/)
25 *
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.
28 *
29 * @example
30 * var testCallback = function($td, thisDate, month, year)
31 * {
32 * if ($td.is('.current-month') && thisDate.getDay() == 4) {
33 * var d = thisDate.getDate();
34 * $td.bind(
35 * 'click',
36 * function()
37 * {
38 * alert('You clicked on ' + d + '/' + (Number(month)+1) + '/' + year);
39 * }
40 * ).addClass('thursday');
41 * } else if (thisDate.getDay() == 5) {
42 * $td.html('Friday the ' + $td.html() + 'th');
43 * }
44 * }
45 * $('#calendar-me').renderCalendar({month:0, year:2007, renderCallback:testCallback});
46 *
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.
48 **/
49 renderCalendar : function(s)
50 {
51 var dc = function(a)
52 {
53 return document.createElement(a);
54 };
55
56 s = $.extend(
57 {
58 month : null,
59 year : null,
60 renderCallback : null,
61 showHeader : $.dpConst.SHOW_HEADER_SHORT,
62 dpController : null,
63 hoverClass : 'dp-hover'
64 }
65 , s
66 );
67
68 if (s.showHeader != $.dpConst.SHOW_HEADER_NONE) {
69 var headRow = $(dc('tr'));
70 for (var i=Date.firstDayOfWeek; i<Date.firstDayOfWeek+7; i++) {
71 var weekday = i%7;
72 var day = Date.dayNames[weekday];
73 headRow.append(
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)
75 );
76 }
77 };
78
79 var calendarTable = $(dc('table'))
80 .attr(
81 {
82 'cellspacing':2,
83 'className':'jCalendar'
84 }
85 )
86 .append(
87 (s.showHeader != $.dpConst.SHOW_HEADER_NONE ?
88 $(dc('thead'))
89 .append(headRow)
90 :
91 dc('thead')
92 )
93 );
94 var tbody = $(dc('tbody'));
95
96 var today = (new Date()).zeroTime();
97
98 var month = s.month == undefined ? today.getMonth() : s.month;
99 var year = s.year || today.getFullYear();
100
101 var currentDate = new Date(year, month, 1);
102
103
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);
108
109 var doHover = function()
110 {
111 if (s.hoverClass) {
112 $(this).addClass(s.hoverClass);
113 }
114 };
115 var unHover = function()
116 {
117 if (s.hoverClass) {
118 $(this).removeClass(s.hoverClass);
119 }
120 };
121
122 var w = 0;
123 while (w++<weeksToDraw) {
124 var r = jQuery(dc('tr'));
125 for (var i=0; i<7; i++) {
126 var thisMonth = currentDate.getMonth() == month;
127 var d = $(dc('td'))
128 .text(currentDate.getDate() + '')
129 .attr('className', (thisMonth ? 'current-month ' : 'other-month ') +
130 (currentDate.isWeekend() ? 'weekend ' : 'weekday ') +
131 (thisMonth && currentDate.getTime() == today.getTime() ? 'today ' : '')
132 )
133 .hover(doHover, unHover)
134 ;
135 if (s.renderCallback) {
136 s.renderCallback(d, currentDate, month, year);
137 }
138 r.append(d);
139 currentDate.addDays(1);
140 }
141 tbody.append(r);
142 }
143 calendarTable.append(tbody);
144
145 return this.each(
146 function()
147 {
148 $(this).empty().append(calendarTable);
149 }
150 );
151 },
152 /**
153 * Create a datePicker associated with each of the matched elements.
154 *
155 * The matched element will receive a few custom events with the following signatures:
156 *
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)
159 *
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.
162 *
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.
165 *
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.
168 *
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.
187 * @type jQuery
188 * @name datePicker
189 * @cat plugins/datePicker
190 * @author Kelvin Luck (http://www.kelvinluck.com/)
191 *
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).
194 *
195 * @example demo/index.html
196 * @desc See the projects homepage for many more complex examples...
197 **/
198 datePicker : function(s)
199 {
200 if (!$.event._dpCache) $.event._dpCache = [];
201
202 // initialise the date picker controller with the relevant settings...
203 s = $.extend(
204 {
205 month : undefined,
206 year : undefined,
207 startDate : undefined,
208 endDate : undefined,
209 inline : false,
210 renderCallback : [],
211 createButton : true,
212 showYearNavigation : true,
213 closeOnSelect : true,
214 displayClose : false,
215 selectMultiple : false,
216 clickInput : false,
217 verticalPosition : $.dpConst.POS_TOP,
218 horizontalPosition : $.dpConst.POS_LEFT,
219 verticalOffset : 0,
220 horizontalOffset : 0,
221 hoverClass : 'dp-hover'
222 }
223 , s
224 );
225
226 return this.each(
227 function()
228 {
229 var $this = $(this);
230 var alreadyExists = true;
231
232 if (!this._dpId) {
233 this._dpId = $.event.guid++;
234 $.event._dpCache[this._dpId] = new DatePicker(this);
235 alreadyExists = false;
236 }
237
238 if (s.inline) {
239 s.createButton = false;
240 s.displayClose = false;
241 s.closeOnSelect = false;
242 $this.empty();
243 }
244
245 var controller = $.event._dpCache[this._dpId];
246
247 controller.init(s);
248
249 if (!alreadyExists && s.createButton) {
250 // create it!
251 controller.button = $('<a href="#" class="dp-choose-date" title="' + $.dpText.TITLE_CHOOSE_DATE + '">' + $.dpText.TEXT_CHOOSE_DATE + '</a>')
252 .bind(
253 'click',
254 function()
255 {
256 $this.dpDisplay(this);
257 this.blur();
258 return false;
259 }
260 );
261 $this.after(controller.button);
262 }
263
264 if (!alreadyExists && $this.is(':text')) {
265 $this
266 .bind(
267 'dateSelected',
268 function(e, selectedDate, $td)
269 {
270 this.value = selectedDate.asString();
271 }
272 ).bind(
273 'change',
274 function()
275 {
276 var d = Date.fromString(this.value);
277 if (d) {
278 controller.setSelected(d, true, true);
279 }
280 }
281 );
282 if (s.clickInput) {
283 $this.bind(
284 'click',
285 function()
286 {
287 $this.dpDisplay();
288 }
289 );
290 }
291 var d = Date.fromString(this.value);
292 if (this.value != '' && d) {
293 controller.setSelected(d, true, true);
294 }
295 }
296
297 $this.addClass('dp-applied');
298
299 }
300 )
301 },
302 /**
303 * Disables or enables this date picker
304 *
305 * @param Boolean s Whether to disable (true) or enable (false) this datePicker
306 * @type jQuery
307 * @name dpSetDisabled
308 * @cat plugins/datePicker
309 * @author Kelvin Luck (http://www.kelvinluck.com/)
310 *
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.
314 **/
315 dpSetDisabled : function(s)
316 {
317 return _w.call(this, 'setDisabled', s);
318 },
319 /**
320 * Updates the first selectable date for any date pickers on any matched elements.
321 *
322 * @param String d A string representing the first selectable date (formatted according to Date.format).
323 * @type jQuery
324 * @name dpSetStartDate
325 * @cat plugins/datePicker
326 * @author Kelvin Luck (http://www.kelvinluck.com/)
327 *
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.
331 **/
332 dpSetStartDate : function(d)
333 {
334 return _w.call(this, 'setStartDate', d);
335 },
336 /**
337 * Updates the last selectable date for any date pickers on any matched elements.
338 *
339 * @param String d A string representing the last selectable date (formatted according to Date.format).
340 * @type jQuery
341 * @name dpSetEndDate
342 * @cat plugins/datePicker
343 * @author Kelvin Luck (http://www.kelvinluck.com/)
344 *
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.
348 **/
349 dpSetEndDate : function(d)
350 {
351 return _w.call(this, 'setEndDate', d);
352 },
353 /**
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.
355 *
356 * @type Array
357 * @name dpGetSelected
358 * @cat plugins/datePicker
359 * @author Kelvin Luck (http://www.kelvinluck.com/)
360 *
361 * @example $('.date-picker').datePicker();
362 * alert($('.date-picker').dpGetSelected());
363 * @desc Will alert an empty array (as nothing is selected yet)
364 **/
365 dpGetSelected : function()
366 {
367 var c = _getController(this[0]);
368 if (c) {
369 return c.getSelected();
370 }
371 return null;
372 },
373 /**
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.
375 *
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.
379 * @type jQuery
380 * @name dpSetSelected
381 * @cat plugins/datePicker
382 * @author Kelvin Luck (http://www.kelvinluck.com/)
383 *
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.
387 **/
388 dpSetSelected : function(d, v, m)
389 {
390 if (v == undefined) v=true;
391 if (m == undefined) m=true;
392 return _w.call(this, 'setSelected', Date.fromString(d), v, m);
393 },
394 /**
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.
396 *
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.
399 * @type jQuery
400 * @name dpSetDisplayedMonth
401 * @cat plugins/datePicker
402 * @author Kelvin Luck (http://www.kelvinluck.com/)
403 *
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.
407 **/
408 dpSetDisplayedMonth : function(m, y)
409 {
410 return _w.call(this, 'setDisplayedMonth', Number(m), Number(y));
411 },
412 /**
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.
414 *
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.
416 * @type jQuery
417 * @name dpDisplay
418 * @cat plugins/datePicker
419 * @author Kelvin Luck (http://www.kelvinluck.com/)
420 *
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.
424 **/
425 dpDisplay : function(e)
426 {
427 return _w.call(this, 'display', e);
428 },
429 /**
430 * Sets a function or array of functions that is called when each TD of the date picker popup is rendered to the page
431 *
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.
433 * @type jQuery
434 * @name dpSetRenderCallback
435 * @cat plugins/datePicker
436 * @author Kelvin Luck (http://www.kelvinluck.com/)
437 *
438 * @example $('#date-picker').datePicker();
439 * $('#date-picker').dpSetRenderCallback(function($td, thisDate, month, year)
440 * {
441 * // do stuff as each td is rendered dependant on the date in the td and the displayed month and year
442 * });
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.
444 **/
445 dpSetRenderCallback : function(a)
446 {
447 return _w.call(this, 'setRenderCallback', a);
448 },
449 /**
450 * Sets the position that the datePicker will pop up (relative to it's associated element)
451 *
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
454 * @type jQuery
455 * @name dpSetPosition
456 * @cat plugins/datePicker
457 * @author Kelvin Luck (http://www.kelvinluck.com/)
458 *
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.
462 **/
463 dpSetPosition : function(v, h)
464 {
465 return _w.call(this, 'setPosition', v, h);
466 },
467 /**
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)
469 *
470 * @param Number v The vertical offset of the created date picker.
471 * @param Number h The horizontal offset of the created date picker.
472 * @type jQuery
473 * @name dpSetOffset
474 * @cat plugins/datePicker
475 * @author Kelvin Luck (http://www.kelvinluck.com/)
476 *
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.
480 **/
481 dpSetOffset : function(v, h)
482 {
483 return _w.call(this, 'setOffset', v, h);
484 },
485 /**
486 * Closes the open date picker associated with this element.
487 *
488 * @type jQuery
489 * @name dpClose
490 * @cat plugins/datePicker
491 * @author Kelvin Luck (http://www.kelvinluck.com/)
492 *
493 * @example $('.date-pick')
494 * .datePicker()
495 * .bind(
496 * 'focus',
497 * function()
498 * {
499 * $(this).dpDisplay();
500 * }
501 * ).bind(
502 * 'blur',
503 * function()
504 * {
505 * $(this).dpClose();
506 * }
507 * );
508 * @desc Creates a date picker and makes it appear when the relevant element is focused and disappear when it is blurred.
509 **/
510 dpClose : function()
511 {
512 return _w.call(this, '_closeCalendar', false, this[0]);
513 },
514 // private function called on unload to clean up any expandos etc and prevent memory links...
515 _dpDestroy : function()
516 {
517 // TODO - implement this?
518 }
519 });
520
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)
524 {
525 return this.each(
526 function()
527 {
528 var c = _getController(this);
529 if (c) {
530 c[f](a1, a2, a3);
531 }
532 }
533 );
534 };
535
536 function DatePicker(ele)
537 {
538 this.ele = ele;
539
540 // initial values...
541 this.displayedMonth = null;
542 this.displayedYear = null;
543 this.startDate = null;
544 this.endDate = 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;
553 this.button = null;
554 this.renderCallback = [];
555 this.selectedDates = {};
556 this.inline = null;
557 this.context = '#dp-popup';
558 };
559 $.extend(
560 DatePicker.prototype,
561 {
562 init : function(s)
563 {
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;
577 if (this.inline) {
578 this.context = this.ele;
579 this.display();
580 }
581 },
582 setStartDate : function(d)
583 {
584 if (d) {
585 this.startDate = Date.fromString(d);
586 }
587 if (!this.startDate) {
588 this.startDate = (new Date()).zeroTime();
589 }
590 this.setDisplayedMonth(this.displayedMonth, this.displayedYear);
591 },
592 setEndDate : function(d)
593 {
594 if (d) {
595 this.endDate = Date.fromString(d);
596 }
597 if (!this.endDate) {
598 this.endDate = (new Date('12/31/2999')); // using the JS Date.parse function which expects mm/dd/yyyy
599 }
600 if (this.endDate.getTime() < this.startDate.getTime()) {
601 this.endDate = this.startDate;
602 }
603 this.setDisplayedMonth(this.displayedMonth, this.displayedYear);
604 },
605 setPosition : function(v, h)
606 {
607 this.verticalPosition = v;
608 this.horizontalPosition = h;
609 },
610 setOffset : function(v, h)
611 {
612 this.verticalOffset = parseInt(v) || 0;
613 this.horizontalOffset = parseInt(h) || 0;
614 },
615 setDisabled : function(s)
616 {
617 $e = $(this.ele);
618 $e[s ? 'addClass' : 'removeClass']('dp-disabled');
619 if (this.button) {
620 $but = $(this.button);
621 $but[s ? 'addClass' : 'removeClass']('dp-disabled');
622 $but.attr('title', s ? '' : $.dpText.TEXT_CHOOSE_DATE);
623 }
624 if ($e.is(':text')) {
625 $e.attr('disabled', s ? 'disabled' : '');
626 }
627 },
628 setDisplayedMonth : function(m, y)
629 {
630 if (this.startDate == undefined || this.endDate == undefined) {
631 return;
632 }
633 var s = new Date(this.startDate.getTime());
634 s.setDate(1);
635 var e = new Date(this.endDate.getTime());
636 e.setDate(1);
637
638 var t;
639 if ((!m && !y) || (isNaN(m) && isNaN(y))) {
640 // no month or year passed - default to current month
641 t = new Date().zeroTime();
642 t.setDate(1);
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);
649 } else {
650 // year and month passed in - that's the date we want!
651 t = new Date(y, m, 1)
652 }
653
654 // check if the desired date is within the range of our defined startDate and endDate
655 if (t.getTime() < s.getTime()) {
656 t = s;
657 } else if (t.getTime() > e.getTime()) {
658 t = e;
659 }
660 this.displayedMonth = t.getMonth();
661 this.displayedYear = t.getFullYear();
662 /* Patch Cedric Morin 2008-09-13 pour les controleurs inline */
663 if (this.inline){
664 this._clearCalendar();
665 this._renderCalendar();
666 }
667 },
668 setSelected : function(d, v, moveToMonth)
669 {
670 if (this.selectMultiple == false) {
671 this.selectedDates = {};
672 $('td.selected', this.context).removeClass('selected');
673 }
674 if (moveToMonth) {
675 this.setDisplayedMonth(d.getMonth(), d.getFullYear());
676 }
677 this.selectedDates[d.toString()] = v;
678 },
679 isSelected : function(d)
680 {
681 return this.selectedDates[d.toString()];
682 },
683 getSelected : function()
684 {
685 var r = [];
686 for(s in this.selectedDates) {
687 if (this.selectedDates[s] == true) {
688 r.push(Date.parse(s));
689 }
690 }
691 return r;
692 },
693 display : function(eleAlignTo)
694 {
695 if ($(this.ele).is('.dp-disabled')) return;
696
697 eleAlignTo = eleAlignTo || this.ele;
698 var c = this;
699 var $ele = $(eleAlignTo);
700 var eleOffset = $ele.offset();
701
702 var $createIn;
703 var attrs;
704 var attrsCalendarHolder;
705 var cssRules;
706
707 if (c.inline) {
708 $createIn = $(this.ele);
709 attrs = {
710 'id' : 'calendar-' + this.ele._dpId,
711 'className' : 'dp-popup dp-popup-inline'
712 };
713 cssRules = {
714 };
715 } else {
716 $createIn = $('body');
717 attrs = {
718 'id' : 'dp-popup',
719 'className' : 'dp-popup'
720 };
721 cssRules = {
722 'top' : eleOffset.top + c.verticalOffset,
723 'left' : eleOffset.left + c.horizontalOffset
724 };
725
726 var _checkMouse = function(e)
727 {
728 var el = e.target;
729 var cal = $('#dp-popup')[0];
730
731 while (true){
732 if (el == cal) {
733 return true;
734 } else if (el == document) {
735 c._closeCalendar();
736 return false;
737 } else {
738 el = $(el).parent()[0];
739 }
740 }
741 };
742 this._checkMouse = _checkMouse;
743
744 this._closeCalendar(true);
745 }
746
747
748 $createIn
749 .append(
750 $('<div></div>')
751 .attr(attrs)
752 .css(cssRules)
753 .append(
754 $('<h2></h2>'),
755 $('<div class="dp-nav-prev"></div>')
756 .append(
757 $('<a class="dp-nav-prev-year" href="#" title="' + $.dpText.TEXT_PREV_YEAR + '">&lt;&lt;</a>')
758 .bind(
759 'click',
760 function()
761 {
762 return c._displayNewMonth.call(c, this, 0, -1);
763 }
764 ),
765 $('<a class="dp-nav-prev-month" href="#" title="' + $.dpText.TEXT_PREV_MONTH + '">&lt;</a>')
766 .bind(
767 'click',
768 function()
769 {
770 return c._displayNewMonth.call(c, this, -1, 0);
771 }
772 )
773 ),
774 $('<div class="dp-nav-next"></div>')
775 .append(
776 $('<a class="dp-nav-next-year" href="#" title="' + $.dpText.TEXT_NEXT_YEAR + '">&gt;&gt;</a>')
777 .bind(
778 'click',
779 function()
780 {
781 return c._displayNewMonth.call(c, this, 0, 1);
782 }
783 ),
784 $('<a class="dp-nav-next-month" href="#" title="' + $.dpText.TEXT_NEXT_MONTH + '">&gt;</a>')
785 .bind(
786 'click',
787 function()
788 {
789 return c._displayNewMonth.call(c, this, 1, 0);
790 }
791 )
792 ),
793 $('<div></div>')
794 .attr('className', 'dp-calendar')
795 )
796 .bgIframe()
797 );
798
799 var $pop = this.inline ? $('.dp-popup', this.context) : $('#dp-popup');
800
801 if (this.showYearNavigation == false) {
802 $('.dp-nav-prev-year, .dp-nav-next-year', c.context).css('display', 'none');
803 }
804 if (this.displayClose) {
805 $pop.append(
806 $('<a href="#" id="dp-close">' + $.dpText.TEXT_CLOSE + '</a>')
807 .bind(
808 'click',
809 function()
810 {
811 c._closeCalendar();
812 return false;
813 }
814 )
815 );
816 }
817 c._renderCalendar();
818
819 $(this.ele).trigger('dpDisplayed', $pop);
820
821 if (!c.inline) {
822 if (this.verticalPosition == $.dpConst.POS_BOTTOM) {
823 $pop.css('top', eleOffset.top + $ele.height() - $pop.height() + c.verticalOffset);
824 }
825 if (this.horizontalPosition == $.dpConst.POS_RIGHT) {
826 $pop.css('left', eleOffset.left + $ele.width() - $pop.width() + c.horizontalOffset);
827 }
828 $(document).bind('mousedown', this._checkMouse);
829 }
830 },
831 setRenderCallback : function(a)
832 {
833 if (a && typeof(a) == 'function') {
834 a = [a];
835 }
836 this.renderCallback = this.renderCallback.concat(a);
837 },
838 cellRender : function ($td, thisDate, month, year) {
839 var c = this.dpController;
840 var d = new Date(thisDate.getTime());
841
842 // add our click handlers to deal with it when the days are clicked...
843
844 $td.bind(
845 'click',
846 function()
847 {
848 var $this = $(this);
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) {
855 c._closeCalendar();
856 } else {
857 $this[s ? 'addClass' : 'removeClass']('selected');
858 }
859 }
860 }
861 );
862
863 if (c.isSelected(d)) {
864 $td.addClass('selected');
865 }
866
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);
870 }
871
872
873 },
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)
877 {
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]);
883 }
884 ele.blur();
885 return false;
886 },
887 _renderCalendar : function()
888 {
889 // set the title...
890 $('h2', this.context).html(Date.monthNames[this.displayedMonth] + ' ' + this.displayedYear);
891
892 // render the calendar...
893 $('.dp-calendar', this.context).renderCalendar(
894 {
895 month : this.displayedMonth,
896 year : this.displayedYear,
897 renderCallback : this.cellRender,
898 dpController : this,
899 hoverClass : this.hoverClass
900 }
901 );
902
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(
909 function()
910 {
911 var $this = $(this);
912 if (Number($this.text()) > 20) {
913 $this.addClass('disabled');
914 }
915 }
916 );
917 var d = this.startDate.getDate();
918 $('.dp-calendar td.current-month', this.context).each(
919 function()
920 {
921 var $this = $(this);
922 if (Number($this.text()) < d) {
923 $this.addClass('disabled');
924 }
925 }
926 );
927 } else {
928 $('.dp-nav-prev-year', this.context).removeClass('disabled');
929 $('.dp-nav-prev-month', this.context).removeClass('disabled');
930 var d = this.startDate.getDate();
931 if (d > 20) {
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());
934 sd.addMonths(1);
935 if (this.displayedYear == sd.getFullYear() && this.displayedMonth == sd.getMonth()) {
936 $('dp-calendar td.other-month', this.context).each(
937 function()
938 {
939 var $this = $(this);
940 if (Number($this.text()) < d) {
941 $this.addClass('disabled');
942 }
943 }
944 );
945 }
946 }
947 }
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(
952 function()
953 {
954 var $this = $(this);
955 if (Number($this.text()) < 14) {
956 $this.addClass('disabled');
957 }
958 }
959 );
960 var d = this.endDate.getDate();
961 $('.dp-calendar td.current-month', this.context).each(
962 function()
963 {
964 var $this = $(this);
965 if (Number($this.text()) > d) {
966 $this.addClass('disabled');
967 }
968 }
969 );
970 } else {
971 $('.dp-nav-next-year', this.context).removeClass('disabled');
972 $('.dp-nav-next-month', this.context).removeClass('disabled');
973 var d = this.endDate.getDate();
974 if (d < 13) {
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());
977 ed.addMonths(-1);
978 if (this.displayedYear == ed.getFullYear() && this.displayedMonth == ed.getMonth()) {
979 $('.dp-calendar td.other-month', this.context).each(
980 function()
981 {
982 var $this = $(this);
983 if (Number($this.text()) > d) {
984 $this.addClass('disabled');
985 }
986 }
987 );
988 }
989 }
990 }
991 },
992 _closeCalendar : function(programatic, ele)
993 {
994 if (!ele || ele == this.ele)
995 {
996 $(document).unbind('mousedown', this._checkMouse);
997 this._clearCalendar();
998 $('#dp-popup a').unbind();
999 $('#dp-popup').empty().remove();
1000 if (!programatic) {
1001 $(this.ele).trigger('dpClosed', [this.getSelected()]);
1002 }
1003 }
1004 },
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()
1008 {
1009 // TODO.
1010 $('.dp-calendar td', this.context).unbind();
1011 $('.dp-calendar', this.context).empty();
1012 }
1013 }
1014 );
1015
1016 // static constants
1017 $.dpConst = {
1018 SHOW_HEADER_NONE : 0,
1019 SHOW_HEADER_SHORT : 1,
1020 SHOW_HEADER_LONG : 2,
1021 POS_TOP : 0,
1022 POS_BOTTOM : 1,
1023 POS_LEFT : 0,
1024 POS_RIGHT : 1
1025 };
1026 // localisable text
1027 $.dpText = {
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'
1034 };
1035 // version
1036 $.dpVersion = '$Id: jquery.datePicker.js 3739 2007-10-25 13:55:30Z kelvin.luck $';
1037
1038 function _getController(ele)
1039 {
1040 if (ele._dpId) return $.event._dpCache[ele._dpId];
1041 return false;
1042 };
1043
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; };
1048 };
1049
1050
1051 // clean-up
1052 $(window)
1053 .bind('unload', function() {
1054 var els = $.event._dpCache || [];
1055 for (var i in els) {
1056 $(els[i].ele)._dpDestroy();
1057 }
1058 });
1059
1060
1061 })(jQuery);