jquery.datepick.ext.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /* http://keith-wood.name/datepick.html
  2. Datepicker extensions for jQuery v4.1.0.
  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. if ($.isFunction(onHover)) {
  89. var target = this;
  90. picker.find(inst.options.renderer.daySelector + ' a, ' +
  91. inst.options.renderer.daySelector + ' span').
  92. hover(function() {
  93. onHover.apply(target, [$(target).datepick('retrieveDate', this),
  94. this.nodeName.toLowerCase() == 'a']);
  95. },
  96. function() { onHover.apply(target, []); });
  97. }
  98. };
  99. },
  100. /* Highlight the entire week when hovering over it.
  101. Usage: onShow: $.datepick.highlightWeek.
  102. @param picker (jQuery) the completed datepicker division
  103. @param inst (object) the current instance settings */
  104. highlightWeek: function(picker, inst) {
  105. var target = this;
  106. var renderer = inst.options.renderer;
  107. picker.find(renderer.daySelector + ' a, ' + renderer.daySelector + ' span').
  108. hover(function() {
  109. $(this).parents('tr').find(renderer.daySelector + ' *').
  110. addClass(renderer.highlightedClass);
  111. },
  112. function() {
  113. $(this).parents('tr').find(renderer.daySelector + ' *').
  114. removeClass(renderer.highlightedClass);
  115. });
  116. },
  117. /* Show a status bar with messages.
  118. Usage: onShow: $.datepick.showStatus.
  119. @param picker (jQuery) the completed datepicker division
  120. @param inst (object) the current instance settings */
  121. showStatus: function(picker, inst) {
  122. var isTR = (inst.options.renderer.selectedClass == themeRollerRenderer.selectedClass);
  123. var defaultStatus = inst.options.defaultStatus || '&nbsp;';
  124. var status = $('<div class="' + (!isTR ? 'datepick-status' :
  125. 'ui-datepicker-status ui-widget-header ui-helper-clearfix ui-corner-all') + '">' +
  126. defaultStatus + '</div>').
  127. insertAfter(picker.find('.datepick-month-row:last,.ui-datepicker-row-break:last'));
  128. picker.find('*[title]').each(function() {
  129. var title = $(this).attr('title');
  130. $(this).removeAttr('title').hover(
  131. function() { status.text(title || defaultStatus); },
  132. function() { status.text(defaultStatus); });
  133. });
  134. },
  135. /* Allow easier navigation by month/year.
  136. Usage: onShow: $.datepick.monthNavigation.
  137. @param picker (jQuery) the completed datepicker division
  138. @param inst (object) the current instance settings */
  139. monthNavigation: function(picker, inst) {
  140. var target = $(this);
  141. var isTR = (inst.options.renderer.selectedClass == themeRollerRenderer.selectedClass);
  142. var minDate = inst.curMinDate();
  143. var maxDate = inst.get('maxDate');
  144. var month = inst.drawDate.getMonth();
  145. var year = inst.drawDate.getFullYear();
  146. var html = '<div class="' + (!isTR ? 'datepick-month-nav' : 'ui-datepicker-month-nav') + '"' +
  147. ' style="display: none;">';
  148. for (var i = 0; i < inst.options.monthNames.length; i++) {
  149. var inRange = ((!minDate || new Date(year, i + 1, 0).getTime() >= minDate.getTime()) &&
  150. (!maxDate || new Date(year, i, 1).getTime() <= maxDate.getTime()));
  151. html += '<div>' +
  152. (inRange ? '<a href="#" class="dp' + new Date(year, i, 1).getTime() + '"' : '<span') +
  153. ' title="' + inst.options.monthNames[i] + '">' + inst.options.monthNamesShort[i] +
  154. (inRange ? '</a>' : '</span>') + '</div>';
  155. }
  156. for (var i = -6; i <= 6; i++) {
  157. if (i == 0) {
  158. continue;
  159. }
  160. var inRange =
  161. ((!minDate || new Date(year + i, 12 - 1, 31).getTime() >= minDate.getTime()) &&
  162. (!maxDate || new Date(year + i, 1 - 1, 1).getTime() <= maxDate.getTime()));
  163. html += '<div>' + (inRange ? '<a href="#" class="dp' +
  164. new Date(year + i, month, 1).getTime() + '"' : '<span') +
  165. ' title="' + (year + i) + '">' + (year + i) +
  166. (inRange ? '</a>' : '</span>') + '</div>';
  167. }
  168. html += '</div>';
  169. html = $(html).insertAfter(picker.find('div.datepick-nav,div.ui-datepicker-header:first'));
  170. html.find('a').click(function() {
  171. var date = target.datepick('retrieveDate', this);
  172. target.datepick('showMonth', date.getFullYear(), date.getMonth() + 1);
  173. return false;
  174. });
  175. picker.find('div.datepick-month-header,div.ui-datepicker-month-header').click(function() {
  176. html.slideToggle();
  177. }).css('cursor', 'pointer');
  178. },
  179. /* Select an entire week when clicking on a week number.
  180. Use in conjunction with weekOfYearRenderer or themeRollerWeekOfYearRenderer.
  181. Usage: onShow: $.datepick.selectWeek.
  182. @param picker (jQuery) the completed datepicker division
  183. @param inst (object) the current instance settings */
  184. selectWeek: function(picker, inst) {
  185. var target = $(this);
  186. picker.find('td.datepick-week span,td.ui-state-hover span').each(function() {
  187. $('<a href="javascript:void(0)" class="' +
  188. this.className + '" title="Select the entire week">' +
  189. $(this).text() + '</a>').
  190. click(function() {
  191. var date = target.datepick('retrieveDate', this);
  192. var dates = [date];
  193. for (var i = 1; i < 7; i++) {
  194. dates.push(date = $.datepick.add($.datepick.newDate(date), 1, 'd'));
  195. }
  196. if (inst.options.rangeSelect) {
  197. dates.splice(1, dates.length - 2);
  198. }
  199. target.datepick('setDate', dates).datepick('hide');
  200. }).
  201. replaceAll(this);
  202. });
  203. },
  204. /* Select an entire month when clicking on the week header.
  205. Use in conjunction with weekOfYearRenderer or themeRollerWeekOfYearRenderer.
  206. Usage: onShow: $.datepick.selectMonth.
  207. @param picker (jQuery) the completed datepicker division
  208. @param inst (object) the current instance settings */
  209. selectMonth: function(picker, inst) {
  210. var target = $(this);
  211. picker.find('th.datepick-week span,th.ui-state-hover span').each(function() {
  212. $('<a href="javascript:void(0)" title="Select the entire month">' +
  213. $(this).text() + '</a>').
  214. click(function() {
  215. var date = target.datepick('retrieveDate', $(this).parents('table').
  216. find('td:not(.datepick-week):not(.ui-state-hover) ' +
  217. '*:not(.datepick-other-month):not(.ui-datepicker-other-month)')[0]);
  218. var dates = [date];
  219. var dim = $.datepick.daysInMonth(date);
  220. for (var i = 1; i < dim; i++) {
  221. dates.push(date = $.datepick.add($.datepick.newDate(date), 1, 'd'));
  222. }
  223. if (inst.options.rangeSelect) {
  224. dates.splice(1, dates.length - 2);
  225. }
  226. target.datepick('setDate', dates).datepick('hide');
  227. }).
  228. replaceAll(this);
  229. });
  230. },
  231. /* Select a month only instead of a single day.
  232. Usage: onShow: $.datepick.monthOnly.
  233. @param picker (jQuery) the completed datepicker division
  234. @param inst (object) the current instance settings */
  235. monthOnly: function(picker, inst) {
  236. var target = $(this);
  237. var selectMonth = $('<div style="text-align: center;"><button type="button">Select</button></div>').
  238. insertAfter(picker.find('.datepick-month-row:last,.ui-datepicker-row-break:last')).
  239. children().click(function() {
  240. var monthYear = picker.find('.datepick-month-year:first').val().split('/');
  241. target.datepick('setDate', $.datepick.newDate(
  242. parseInt(monthYear[1], 10), parseInt(monthYear[0], 10), 1)).
  243. datepick('hide');
  244. });
  245. picker.find('.datepick-month-row table,.ui-datepicker-row-break table').remove();
  246. }
  247. });
  248. })(jQuery);