jquery.datepick.ext.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /* http://keith-wood.name/datepick.html
  2. Datepicker extensions for jQuery v4.0.6.
  3. Written by Keith Wood (kbwood{at}iinet.com.au) August 2009.
  4. Dual licensed under the GPL (http://dev.jquery.com/browser/trunk/jquery/GPL-LICENSE.txt) and
  5. MIT (http://dev.jquery.com/browser/trunk/jquery/MIT-LICENSE.txt) licenses.
  6. Please attribute the author if you use it. */
  7. (function($) { // Hide scope, no $ conflict
  8. var themeRollerRenderer = {
  9. picker: '<div{popup:start} id="ui-datepicker-div"{popup:end} class="ui-datepicker ui-widget ' +
  10. 'ui-widget-content ui-helper-clearfix ui-corner-all{inline:start} ui-datepicker-inline{inline:end}">' +
  11. '<div class="ui-datepicker-header ui-widget-header ui-helper-clearfix ui-corner-all">' +
  12. '{link:prev}{link:today}{link:next}</div>{months}' +
  13. '{popup:start}<div class="ui-datepicker-header ui-widget-header ui-helper-clearfix ' +
  14. 'ui-corner-all">{button:clear}{button:close}</div>{popup:end}' +
  15. '<div class="ui-helper-clearfix"></div></div>',
  16. monthRow: '<div class="ui-datepicker-row-break">{months}</div>',
  17. month: '<div class="ui-datepicker-group">' +
  18. '<div class="ui-datepicker-header ui-widget-header ui-helper-clearfix ui-corner-all">{monthHeader:MM yyyy}</div>' +
  19. '<table class="ui-datepicker-calendar"><thead>{weekHeader}</thead><tbody>{weeks}</tbody></table></div>',
  20. weekHeader: '<tr>{days}</tr>',
  21. dayHeader: '<th>{day}</th>',
  22. week: '<tr>{days}</tr>',
  23. day: '<td>{day}</td>',
  24. monthSelector: '.ui-datepicker-group',
  25. daySelector: 'td',
  26. rtlClass: 'ui-datepicker-rtl',
  27. multiClass: 'ui-datepicker-multi',
  28. defaultClass: 'ui-state-default',
  29. selectedClass: 'ui-state-active',
  30. highlightedClass: 'ui-state-hover',
  31. todayClass: 'ui-state-highlight',
  32. otherMonthClass: 'ui-datepicker-other-month',
  33. weekendClass: 'ui-datepicker-week-end',
  34. commandClass: 'ui-datepicker-cmd',
  35. commandButtonClass: 'ui-state-default ui-corner-all',
  36. commandLinkClass: '',
  37. disabledClass: 'ui-datepicker-disabled'
  38. };
  39. $.extend($.datepick, {
  40. // Template for generating a datepicker showing week of year.
  41. weekOfYearRenderer: $.extend({}, $.datepick.defaultRenderer, {
  42. weekHeader: '<tr><th class="datepick-week">' +
  43. '<span title="{l10n:weekStatus}">{l10n:weekText}</span></th>{days}</tr>',
  44. week: '<tr><td class="datepick-week">{weekOfYear}</td>{days}</tr>'
  45. }),
  46. // ThemeRoller template for generating a datepicker.
  47. themeRollerRenderer: themeRollerRenderer,
  48. // ThemeRoller template for generating a datepicker showing week of year.
  49. themeRollerWeekOfYearRenderer: $.extend({}, themeRollerRenderer, {
  50. weekHeader: '<tr><th class="ui-state-hover"><span>{l10n:weekText}</span></th>{days}</tr>',
  51. week: '<tr><td class="ui-state-hover">{weekOfYear}</td>{days}</tr>'
  52. }),
  53. /* Don't allow weekends to be selected.
  54. Usage: onDate: $.datepick.noWeekends.
  55. @param date (Date) the current date
  56. @return (object) information about this date */
  57. noWeekends: function(date) {
  58. return {selectable: (date.getDay() || 7) < 6};
  59. },
  60. /* Change the first day of the week by clicking on the day header.
  61. Usage: onShow: $.datepick.changeFirstDay.
  62. @param picker (jQuery) the completed datepicker division
  63. @param inst (object) the current instance settings */
  64. changeFirstDay: function(picker, inst) {
  65. var target = $(this);
  66. picker.find('th span').each(function() {
  67. var parent = $(this).parent();
  68. if (parent.is('.datepick-week') || parent.is('.ui-state-hover')) {
  69. return;
  70. }
  71. $('<a href="javascript:void(0)" class="' + this.className +
  72. '" title="Change first day of the week">' + $(this).text() + '</a>').
  73. click(function() {
  74. var dow = parseInt(this.className.replace(/^.*datepick-dow-(\d+).*$/, '$1'), 10);
  75. target.datepick('option', {firstDay: dow});
  76. }).
  77. replaceAll(this);
  78. });
  79. },
  80. /* Add a callback when hovering over dates.
  81. Usage: onShow: $.datepick.hoverCallback(handleHover).
  82. @param onHover (function) the callback when hovering,
  83. it receives the current date and a flag indicating selectability
  84. as parameters on entry, and no parameters on exit,
  85. this refers to the target input or division */
  86. hoverCallback: function(onHover) {
  87. return function(picker, inst) {
  88. var target = this;
  89. var renderer = inst.get('renderer');
  90. picker.find(renderer.daySelector + ' a, ' + renderer.daySelector + ' span').
  91. hover(function() {
  92. onHover.apply(target, [$.datepick.retrieveDate(target, this),
  93. this.nodeName.toLowerCase() == 'a']);
  94. },
  95. function() { onHover.apply(target, []); });
  96. };
  97. },
  98. /* Highlight the entire week when hovering over it.
  99. Usage: onShow: $.datepick.highlightWeek.
  100. @param picker (jQuery) the completed datepicker division
  101. @param inst (object) the current instance settings */
  102. highlightWeek: function(picker, inst) {
  103. var target = this;
  104. var renderer = inst.get('renderer');
  105. picker.find(renderer.daySelector + ' a, ' + renderer.daySelector + ' span').
  106. hover(function() {
  107. $(this).parents('tr').find(renderer.daySelector + ' *').
  108. addClass(renderer.highlightedClass);
  109. },
  110. function() {
  111. $(this).parents('tr').find(renderer.daySelector + ' *').
  112. removeClass(renderer.highlightedClass);
  113. });
  114. },
  115. /* Show a status bar with messages.
  116. Usage: onShow: $.datepick.showStatus.
  117. @param picker (jQuery) the completed datepicker division
  118. @param inst (object) the current instance settings */
  119. showStatus: function(picker, inst) {
  120. var renderer = inst.get('renderer');
  121. var isTR = (renderer.selectedClass == themeRollerRenderer.selectedClass);
  122. var defaultStatus = inst.get('defaultStatus') || '&nbsp;';
  123. var status = $('<div class="' + (!isTR ? 'datepick-status' :
  124. 'ui-datepicker-status ui-widget-header ui-helper-clearfix ui-corner-all') + '">' +
  125. defaultStatus + '</div>').
  126. insertAfter(picker.find('.datepick-month-row:last,.ui-datepicker-row-break:last'));
  127. picker.find('*[title]').each(function() {
  128. var title = $(this).attr('title');
  129. $(this).removeAttr('title').hover(
  130. function() { status.text(title || defaultStatus); },
  131. function() { status.text(defaultStatus); });
  132. });
  133. },
  134. /* Allow easier navigation by month/year.
  135. Usage: onShow: $.datepick.monthNavigation.
  136. @param picker (jQuery) the completed datepicker division
  137. @param inst (object) the current instance settings */
  138. monthNavigation: function(picker, inst) {
  139. var target = $(this);
  140. var renderer = inst.get('renderer');
  141. var isTR = (renderer.selectedClass == themeRollerRenderer.selectedClass);
  142. var minDate = inst.curMinDate();
  143. var maxDate = inst.get('maxDate');
  144. var monthNames = inst.get('monthNames');
  145. var monthNamesShort = inst.get('monthNamesShort');
  146. var month = inst.drawDate.getMonth();
  147. var year = inst.drawDate.getFullYear();
  148. var html = '<div class="' + (!isTR ? 'datepick-month-nav' : 'ui-datepicker-month-nav') + '"' +
  149. ' style="display: none;">';
  150. for (var i = 0; i < monthNames.length; i++) {
  151. var inRange = ((!minDate || new Date(year, i + 1, 0).getTime() >= minDate.getTime()) &&
  152. (!maxDate || new Date(year, i, 1).getTime() <= maxDate.getTime()));
  153. html += '<div>' +
  154. (inRange ? '<a href="#" class="dp' + new Date(year, i, 1).getTime() + '"' : '<span') +
  155. ' title="' + monthNames[i] + '">' + monthNamesShort[i] +
  156. (inRange ? '</a>' : '</span>') + '</div>';
  157. }
  158. for (var i = -6; i <= 6; i++) {
  159. if (i == 0) {
  160. continue;
  161. }
  162. var inRange =
  163. ((!minDate || new Date(year + i, 12 - 1, 31).getTime() >= minDate.getTime()) &&
  164. (!maxDate || new Date(year + i, 1 - 1, 1).getTime() <= maxDate.getTime()));
  165. html += '<div>' + (inRange ? '<a href="#" class="dp' +
  166. new Date(year + i, month, 1).getTime() + '"' : '<span') +
  167. ' title="' + (year + i) + '">' + (year + i) +
  168. (inRange ? '</a>' : '</span>') + '</div>';
  169. }
  170. html += '</div>';
  171. html = $(html).insertAfter(picker.find('div.datepick-nav,div.ui-datepicker-header:first'));
  172. html.find('a').click(function() {
  173. var date = $.datepick.retrieveDate(target[0], this);
  174. target.datepick('showMonth', date.getFullYear(), date.getMonth() + 1);
  175. return false;
  176. });
  177. picker.find('div.datepick-month-header,div.ui-datepicker-month-header').click(function() {
  178. html.slideToggle();
  179. }).css('cursor', 'pointer');
  180. },
  181. /* Select an entire week when clicking on a week number.
  182. Use in conjunction with weekOfYearRenderer or themeRollerWeekOfYearRenderer.
  183. Usage: onShow: $.datepick.selectWeek.
  184. @param picker (jQuery) the completed datepicker division
  185. @param inst (object) the current instance settings */
  186. selectWeek: function(picker, inst) {
  187. var target = $(this);
  188. picker.find('td.datepick-week span,td.ui-state-hover span').each(function() {
  189. $('<a href="javascript:void(0)" class="' +
  190. this.className + '" title="Select the entire week">' +
  191. $(this).text() + '</a>').
  192. click(function() {
  193. var date = target.datepick('retrieveDate', this);
  194. var dates = [date];
  195. for (var i = 1; i < 7; i++) {
  196. dates.push(date = $.datepick.add($.datepick.newDate(date), 1, 'd'));
  197. }
  198. if (inst.get('rangeSelect')) {
  199. dates.splice(1, dates.length - 2);
  200. }
  201. target.datepick('setDate', dates).datepick('hide');
  202. }).
  203. replaceAll(this);
  204. });
  205. },
  206. /* Select an entire month when clicking on the week header.
  207. Use in conjunction with weekOfYearRenderer or themeRollerWeekOfYearRenderer.
  208. Usage: onShow: $.datepick.selectMonth.
  209. @param picker (jQuery) the completed datepicker division
  210. @param inst (object) the current instance settings */
  211. selectMonth: function(picker, inst) {
  212. var target = $(this);
  213. picker.find('th.datepick-week span,th.ui-state-hover span').each(function() {
  214. $('<a href="javascript:void(0)" title="Select the entire month">' +
  215. $(this).text() + '</a>').
  216. click(function() {
  217. var date = target.datepick('retrieveDate', $(this).parents('table').
  218. find('td:not(.datepick-week):not(.ui-state-hover) ' +
  219. '*:not(.datepick-other-month):not(.ui-datepicker-other-month)')[0]);
  220. var dates = [date];
  221. var dim = $.datepick.daysInMonth(date);
  222. for (var i = 1; i < dim; i++) {
  223. dates.push(date = $.datepick.add($.datepick.newDate(date), 1, 'd'));
  224. }
  225. if (inst.get('rangeSelect')) {
  226. dates.splice(1, dates.length - 2);
  227. }
  228. target.datepick('setDate', dates).datepick('hide');
  229. }).
  230. replaceAll(this);
  231. });
  232. },
  233. /* Select a month only instead of a single day.
  234. Usage: onShow: $.datepick.monthOnly.
  235. @param picker (jQuery) the completed datepicker division
  236. @param inst (object) the current instance settings */
  237. monthOnly: function(picker, inst) {
  238. var target = $(this);
  239. var selectMonth = $('<div style="text-align: center;"><button type="button">Select</button></div>').
  240. insertAfter(picker.find('.datepick-month-row:last,.ui-datepicker-row-break:last')).
  241. children().click(function() {
  242. var monthYear = picker.find('.datepick-month-year:first').val().split('/');
  243. target.datepick('setDate', $.datepick.newDate(
  244. parseInt(monthYear[1], 10), parseInt(monthYear[0], 10), 1)).
  245. datepick('hide');
  246. });
  247. picker.find('.datepick-month-row table,.ui-datepicker-row-break table').remove();
  248. }
  249. });
  250. })(jQuery);