jquery.datepick.js 84KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093
  1. /* http://keith-wood.name/datepick.html
  2. Date picker for jQuery v4.0.6.
  3. Written by Keith Wood (kbwood{at}iinet.com.au) February 2010.
  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. /* Datepicker manager. */
  9. function Datepicker() {
  10. this._defaults = {
  11. pickerClass: '', // CSS class to add to this instance of the datepicker
  12. showOnFocus: true, // True for popup on focus, false for not
  13. showTrigger: null, // Element to be cloned for a trigger, null for none
  14. showAnim: 'show', // Name of jQuery animation for popup, '' for no animation
  15. showOptions: {}, // Options for enhanced animations
  16. showSpeed: 'normal', // Duration of display/closure
  17. popupContainer: null, // The element to which a popup calendar is added, null for body
  18. alignment: 'bottom', // Alignment of popup - with nominated corner of input:
  19. // 'top' or 'bottom' aligns depending on language direction,
  20. // 'topLeft', 'topRight', 'bottomLeft', 'bottomRight'
  21. fixedWeeks: false, // True to always show 6 weeks, false to only show as many as are needed
  22. firstDay: 0, // First day of the week, 0 = Sunday, 1 = Monday, ...
  23. calculateWeek: this.iso8601Week, // Calculate week of the year from a date, null for ISO8601
  24. monthsToShow: 1, // How many months to show, cols or [rows, cols]
  25. monthsOffset: 0, // How many months to offset the primary month by;
  26. // may be a function that takes the date and returns the offset
  27. monthsToStep: 1, // How many months to move when prev/next clicked
  28. monthsToJump: 12, // How many months to move when large prev/next clicked
  29. useMouseWheel: true, // True to use mousewheel if available, false to never use it
  30. changeMonth: true, // True to change month/year via drop-down, false for navigation only
  31. yearRange: 'c-10:c+10', // Range of years to show in drop-down: 'any' for direct text entry
  32. // or 'start:end', where start/end are '+-nn' for relative to today
  33. // or 'c+-nn' for relative to the currently selected date
  34. // or 'nnnn' for an absolute year
  35. shortYearCutoff: '+10', // Cutoff for two-digit year in the current century
  36. showOtherMonths: false, // True to show dates from other months, false to not show them
  37. selectOtherMonths: false, // True to allow selection of dates from other months too
  38. defaultDate: null, // Date to show if no other selected
  39. selectDefaultDate: false, // True to pre-select the default date if no other is chosen
  40. minDate: null, // The minimum selectable date
  41. maxDate: null, // The maximum selectable date
  42. dateFormat: 'mm/dd/yyyy', // Format for dates
  43. autoSize: false, // True to size the input field according to the date format
  44. rangeSelect: false, // Allows for selecting a date range on one date picker
  45. rangeSeparator: ' - ', // Text between two dates in a range
  46. multiSelect: 0, // Maximum number of selectable dates, zero for single select
  47. multiSeparator: ',', // Text between multiple dates
  48. onDate: null, // Callback as a date is added to the datepicker
  49. onShow: null, // Callback just before a datepicker is shown
  50. onChangeMonthYear: null, // Callback when a new month/year is selected
  51. onSelect: null, // Callback when a date is selected
  52. onClose: null, // Callback when a datepicker is closed
  53. altField: null, // Alternate field to update in synch with the datepicker
  54. altFormat: null, // Date format for alternate field, defaults to dateFormat
  55. constrainInput: true, // True to constrain typed input to dateFormat allowed characters
  56. commandsAsDateFormat: false, // True to apply formatDate to the command texts
  57. commands: this.commands // Command actions that may be added to a layout by name
  58. };
  59. this.regional = {
  60. '': { // US/English
  61. monthNames: ['January', 'February', 'March', 'April', 'May', 'June',
  62. 'July', 'August', 'September', 'October', 'November', 'December'],
  63. monthNamesShort: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
  64. dayNames: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
  65. dayNamesShort: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
  66. dayNamesMin: ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'],
  67. dateFormat: 'mm/dd/yyyy', // See options on formatDate
  68. firstDay: 0, // The first day of the week, Sun = 0, Mon = 1, ...
  69. renderer: this.defaultRenderer, // The rendering templates
  70. prevText: '<Prev', // Text for the previous month command
  71. prevStatus: 'Show the previous month', // Status text for the previous month command
  72. prevJumpText: '<<', // Text for the previous year command
  73. prevJumpStatus: 'Show the previous year', // Status text for the previous year command
  74. nextText: 'Next>', // Text for the next month command
  75. nextStatus: 'Show the next month', // Status text for the next month command
  76. nextJumpText: '>>', // Text for the next year command
  77. nextJumpStatus: 'Show the next year', // Status text for the next year command
  78. currentText: 'Current', // Text for the current month command
  79. currentStatus: 'Show the current month', // Status text for the current month command
  80. todayText: 'Today', // Text for the today's month command
  81. todayStatus: 'Show today\'s month', // Status text for the today's month command
  82. clearText: 'Clear', // Text for the clear command
  83. clearStatus: 'Clear all the dates', // Status text for the clear command
  84. closeText: 'Close', // Text for the close command
  85. closeStatus: 'Close the datepicker', // Status text for the close command
  86. yearStatus: 'Change the year', // Status text for year selection
  87. monthStatus: 'Change the month', // Status text for month selection
  88. weekText: 'Wk', // Text for week of the year column header
  89. weekStatus: 'Week of the year', // Status text for week of the year column header
  90. dayStatus: 'Select DD, M d, yyyy', // Status text for selectable days
  91. defaultStatus: 'Select a date', // Status text shown by default
  92. isRTL: false // True if language is right-to-left
  93. }};
  94. $.extend(this._defaults, this.regional['']);
  95. this._disabled = [];
  96. }
  97. $.extend(Datepicker.prototype, {
  98. dataName: 'datepick',
  99. /* Class name added to elements to indicate already configured with datepicker. */
  100. markerClass: 'hasDatepick',
  101. _popupClass: 'datepick-popup', // Marker for popup division
  102. _triggerClass: 'datepick-trigger', // Marker for trigger element
  103. _disableClass: 'datepick-disable', // Marker for disabled element
  104. _coverClass: 'datepick-cover', // Marker for iframe backing element
  105. _monthYearClass: 'datepick-month-year', // Marker for month/year inputs
  106. _curMonthClass: 'datepick-month-', // Marker for current month/year
  107. _anyYearClass: 'datepick-any-year', // Marker for year direct input
  108. _curDoWClass: 'datepick-dow-', // Marker for day of week
  109. commands: { // Command actions that may be added to a layout by name
  110. // name: { // The command name, use '{button:name}' or '{link:name}' in layouts
  111. // text: '', // The field in the regional settings for the displayed text
  112. // status: '', // The field in the regional settings for the status text
  113. // // The keystroke to trigger the action
  114. // keystroke: {keyCode: nn, ctrlKey: boolean, altKey: boolean, shiftKey: boolean},
  115. // enabled: fn, // The function that indicates the command is enabled
  116. // date: fn, // The function to get the date associated with this action
  117. // action: fn} // The function that implements the action
  118. prev: {text: 'prevText', status: 'prevStatus', // Previous month
  119. keystroke: {keyCode: 33}, // Page up
  120. enabled: function(inst) {
  121. var minDate = inst.curMinDate();
  122. return (!minDate || $.datepick.add($.datepick.day(
  123. $.datepick._applyMonthsOffset($.datepick.add($.datepick.newDate(inst.drawDate),
  124. 1 - inst.get('monthsToStep'), 'm'), inst), 1), -1, 'd').
  125. getTime() >= minDate.getTime()); },
  126. date: function(inst) {
  127. return $.datepick.day($.datepick._applyMonthsOffset($.datepick.add(
  128. $.datepick.newDate(inst.drawDate), -inst.get('monthsToStep'), 'm'), inst), 1); },
  129. action: function(inst) {
  130. $.datepick.changeMonth(this, -inst.get('monthsToStep')); }
  131. },
  132. prevJump: {text: 'prevJumpText', status: 'prevJumpStatus', // Previous year
  133. keystroke: {keyCode: 33, ctrlKey: true}, // Ctrl + Page up
  134. enabled: function(inst) {
  135. var minDate = inst.curMinDate();
  136. return (!minDate || $.datepick.add($.datepick.day(
  137. $.datepick._applyMonthsOffset($.datepick.add($.datepick.newDate(inst.drawDate),
  138. 1 - inst.get('monthsToJump'), 'm'), inst), 1), -1, 'd').
  139. getTime() >= minDate.getTime()); },
  140. date: function(inst) {
  141. return $.datepick.day($.datepick._applyMonthsOffset($.datepick.add(
  142. $.datepick.newDate(inst.drawDate), -inst.get('monthsToJump'), 'm'), inst), 1); },
  143. action: function(inst) {
  144. $.datepick.changeMonth(this, -inst.get('monthsToJump')); }
  145. },
  146. next: {text: 'nextText', status: 'nextStatus', // Next month
  147. keystroke: {keyCode: 34}, // Page down
  148. enabled: function(inst) {
  149. var maxDate = inst.get('maxDate');
  150. return (!maxDate || $.datepick.day($.datepick._applyMonthsOffset($.datepick.add(
  151. $.datepick.newDate(inst.drawDate), inst.get('monthsToStep'), 'm'), inst), 1).
  152. getTime() <= maxDate.getTime()); },
  153. date: function(inst) {
  154. return $.datepick.day($.datepick._applyMonthsOffset($.datepick.add(
  155. $.datepick.newDate(inst.drawDate), inst.get('monthsToStep'), 'm'), inst), 1); },
  156. action: function(inst) {
  157. $.datepick.changeMonth(this, inst.get('monthsToStep')); }
  158. },
  159. nextJump: {text: 'nextJumpText', status: 'nextJumpStatus', // Next year
  160. keystroke: {keyCode: 34, ctrlKey: true}, // Ctrl + Page down
  161. enabled: function(inst) {
  162. var maxDate = inst.get('maxDate');
  163. return (!maxDate || $.datepick.day($.datepick._applyMonthsOffset($.datepick.add(
  164. $.datepick.newDate(inst.drawDate), inst.get('monthsToJump'), 'm'), inst), 1).
  165. getTime() <= maxDate.getTime()); },
  166. date: function(inst) {
  167. return $.datepick.day($.datepick._applyMonthsOffset($.datepick.add(
  168. $.datepick.newDate(inst.drawDate), inst.get('monthsToJump'), 'm'), inst), 1); },
  169. action: function(inst) {
  170. $.datepick.changeMonth(this, inst.get('monthsToJump')); }
  171. },
  172. current: {text: 'currentText', status: 'currentStatus', // Current month
  173. keystroke: {keyCode: 36, ctrlKey: true}, // Ctrl + Home
  174. enabled: function(inst) {
  175. var minDate = inst.curMinDate();
  176. var maxDate = inst.get('maxDate');
  177. var curDate = inst.selectedDates[0] || $.datepick.today();
  178. return (!minDate || curDate.getTime() >= minDate.getTime()) &&
  179. (!maxDate || curDate.getTime() <= maxDate.getTime()); },
  180. date: function(inst) {
  181. return inst.selectedDates[0] || $.datepick.today(); },
  182. action: function(inst) {
  183. var curDate = inst.selectedDates[0] || $.datepick.today();
  184. $.datepick.showMonth(this, curDate.getFullYear(), curDate.getMonth() + 1); }
  185. },
  186. today: {text: 'todayText', status: 'todayStatus', // Today's month
  187. keystroke: {keyCode: 36, ctrlKey: true}, // Ctrl + Home
  188. enabled: function(inst) {
  189. var minDate = inst.curMinDate();
  190. var maxDate = inst.get('maxDate');
  191. return (!minDate || $.datepick.today().getTime() >= minDate.getTime()) &&
  192. (!maxDate || $.datepick.today().getTime() <= maxDate.getTime()); },
  193. date: function(inst) { return $.datepick.today(); },
  194. action: function(inst) { $.datepick.showMonth(this); }
  195. },
  196. clear: {text: 'clearText', status: 'clearStatus', // Clear the datepicker
  197. keystroke: {keyCode: 35, ctrlKey: true}, // Ctrl + End
  198. enabled: function(inst) { return true; },
  199. date: function(inst) { return null; },
  200. action: function(inst) { $.datepick.clear(this); }
  201. },
  202. close: {text: 'closeText', status: 'closeStatus', // Close the datepicker
  203. keystroke: {keyCode: 27}, // Escape
  204. enabled: function(inst) { return true; },
  205. date: function(inst) { return null; },
  206. action: function(inst) { $.datepick.hide(this); }
  207. },
  208. prevWeek: {text: 'prevWeekText', status: 'prevWeekStatus', // Previous week
  209. keystroke: {keyCode: 38, ctrlKey: true}, // Ctrl + Up
  210. enabled: function(inst) {
  211. var minDate = inst.curMinDate();
  212. return (!minDate || $.datepick.add($.datepick.newDate(inst.drawDate), -7, 'd').
  213. getTime() >= minDate.getTime()); },
  214. date: function(inst) { return $.datepick.add($.datepick.newDate(inst.drawDate), -7, 'd'); },
  215. action: function(inst) { $.datepick.changeDay(this, -7); }
  216. },
  217. prevDay: {text: 'prevDayText', status: 'prevDayStatus', // Previous day
  218. keystroke: {keyCode: 37, ctrlKey: true}, // Ctrl + Left
  219. enabled: function(inst) {
  220. var minDate = inst.curMinDate();
  221. return (!minDate || $.datepick.add($.datepick.newDate(inst.drawDate), -1, 'd').
  222. getTime() >= minDate.getTime()); },
  223. date: function(inst) { return $.datepick.add($.datepick.newDate(inst.drawDate), -1, 'd'); },
  224. action: function(inst) { $.datepick.changeDay(this, -1); }
  225. },
  226. nextDay: {text: 'nextDayText', status: 'nextDayStatus', // Next day
  227. keystroke: {keyCode: 39, ctrlKey: true}, // Ctrl + Right
  228. enabled: function(inst) {
  229. var maxDate = inst.get('maxDate');
  230. return (!maxDate || $.datepick.add($.datepick.newDate(inst.drawDate), 1, 'd').
  231. getTime() <= maxDate.getTime()); },
  232. date: function(inst) { return $.datepick.add($.datepick.newDate(inst.drawDate), 1, 'd'); },
  233. action: function(inst) { $.datepick.changeDay(this, 1); }
  234. },
  235. nextWeek: {text: 'nextWeekText', status: 'nextWeekStatus', // Next week
  236. keystroke: {keyCode: 40, ctrlKey: true}, // Ctrl + Down
  237. enabled: function(inst) {
  238. var maxDate = inst.get('maxDate');
  239. return (!maxDate || $.datepick.add($.datepick.newDate(inst.drawDate), 7, 'd').
  240. getTime() <= maxDate.getTime()); },
  241. date: function(inst) { return $.datepick.add($.datepick.newDate(inst.drawDate), 7, 'd'); },
  242. action: function(inst) { $.datepick.changeDay(this, 7); }
  243. }
  244. },
  245. /* Default template for generating a datepicker. */
  246. defaultRenderer: {
  247. // Anywhere: '{l10n:name}' to insert localised value for name,
  248. // '{link:name}' to insert a link trigger for command name,
  249. // '{button:name}' to insert a button trigger for command name,
  250. // '{popup:start}...{popup:end}' to mark a section for inclusion in a popup datepicker only,
  251. // '{inline:start}...{inline:end}' to mark a section for inclusion in an inline datepicker only
  252. // Overall structure: '{months}' to insert calendar months
  253. picker: '<div class="datepick">' +
  254. '<div class="datepick-nav">{link:prev}{link:today}{link:next}</div>{months}' +
  255. '{popup:start}<div class="datepick-ctrl">{link:clear}{link:close}</div>{popup:end}' +
  256. '<div class="datepick-clear-fix"></div></div>',
  257. // One row of months: '{months}' to insert calendar months
  258. monthRow: '<div class="datepick-month-row">{months}</div>',
  259. // A single month: '{monthHeader:dateFormat}' to insert the month header -
  260. // dateFormat is optional and defaults to 'MM yyyy',
  261. // '{weekHeader}' to insert a week header, '{weeks}' to insert the month's weeks
  262. month: '<div class="datepick-month"><div class="datepick-month-header">{monthHeader}</div>' +
  263. '<table><thead>{weekHeader}</thead><tbody>{weeks}</tbody></table></div>',
  264. // A week header: '{days}' to insert individual day names
  265. weekHeader: '<tr>{days}</tr>',
  266. // Individual day header: '{day}' to insert day name
  267. dayHeader: '<th>{day}</th>',
  268. // One week of the month: '{days}' to insert the week's days, '{weekOfYear}' to insert week of year
  269. week: '<tr>{days}</tr>',
  270. // An individual day: '{day}' to insert day value
  271. day: '<td>{day}</td>',
  272. // jQuery selector, relative to picker, for a single month
  273. monthSelector: '.datepick-month',
  274. // jQuery selector, relative to picker, for individual days
  275. daySelector: 'td',
  276. // Class for right-to-left (RTL) languages
  277. rtlClass: 'datepick-rtl',
  278. // Class for multi-month datepickers
  279. multiClass: 'datepick-multi',
  280. // Class for selectable dates
  281. defaultClass: '',
  282. // Class for currently selected dates
  283. selectedClass: 'datepick-selected',
  284. // Class for highlighted dates
  285. highlightedClass: 'datepick-highlight',
  286. // Class for today
  287. todayClass: 'datepick-today',
  288. // Class for days from other months
  289. otherMonthClass: 'datepick-other-month',
  290. // Class for days on weekends
  291. weekendClass: 'datepick-weekend',
  292. // Class prefix for commands
  293. commandClass: 'datepick-cmd',
  294. // Extra class(es) for commands that are buttons
  295. commandButtonClass: '',
  296. // Extra class(es) for commands that are links
  297. commandLinkClass: '',
  298. // Class for disabled commands
  299. disabledClass: 'datepick-disabled'
  300. },
  301. /* Override the default settings for all datepicker instances.
  302. @param settings (object) the new settings to use as defaults
  303. @return (Datepicker) this object */
  304. setDefaults: function(settings) {
  305. $.extend(this._defaults, settings || {});
  306. return this;
  307. },
  308. _ticksTo1970: (((1970 - 1) * 365 + Math.floor(1970 / 4) - Math.floor(1970 / 100) +
  309. Math.floor(1970 / 400)) * 24 * 60 * 60 * 10000000),
  310. _msPerDay: 24 * 60 * 60 * 1000,
  311. ATOM: 'yyyy-mm-dd', // RFC 3339/ISO 8601
  312. COOKIE: 'D, dd M yyyy',
  313. FULL: 'DD, MM d, yyyy',
  314. ISO_8601: 'yyyy-mm-dd',
  315. JULIAN: 'J',
  316. RFC_822: 'D, d M yy',
  317. RFC_850: 'DD, dd-M-yy',
  318. RFC_1036: 'D, d M yy',
  319. RFC_1123: 'D, d M yyyy',
  320. RFC_2822: 'D, d M yyyy',
  321. RSS: 'D, d M yy', // RFC 822
  322. TICKS: '!',
  323. TIMESTAMP: '@',
  324. W3C: 'yyyy-mm-dd', // ISO 8601
  325. /* Format a date object into a string value.
  326. The format can be combinations of the following:
  327. d - day of month (no leading zero)
  328. dd - day of month (two digit)
  329. o - day of year (no leading zeros)
  330. oo - day of year (three digit)
  331. D - day name short
  332. DD - day name long
  333. w - week of year (no leading zero)
  334. ww - week of year (two digit)
  335. m - month of year (no leading zero)
  336. mm - month of year (two digit)
  337. M - month name short
  338. MM - month name long
  339. yy - year (two digit)
  340. yyyy - year (four digit)
  341. @ - Unix timestamp (s since 01/01/1970)
  342. ! - Windows ticks (100ns since 01/01/0001)
  343. '...' - literal text
  344. '' - single quote
  345. @param format (string) the desired format of the date (optional, default datepicker format)
  346. @param date (Date) the date value to format
  347. @param settings (object) attributes include:
  348. dayNamesShort (string[]) abbreviated names of the days from Sunday (optional)
  349. dayNames (string[]) names of the days from Sunday (optional)
  350. monthNamesShort (string[]) abbreviated names of the months (optional)
  351. monthNames (string[]) names of the months (optional)
  352. calculateWeek (function) function that determines week of the year (optional)
  353. @return (string) the date in the above format */
  354. formatDate: function(format, date, settings) {
  355. if (typeof format != 'string') {
  356. settings = date;
  357. date = format;
  358. format = '';
  359. }
  360. if (!date) {
  361. return '';
  362. }
  363. format = format || this._defaults.dateFormat;
  364. settings = settings || {};
  365. var dayNamesShort = settings.dayNamesShort || this._defaults.dayNamesShort;
  366. var dayNames = settings.dayNames || this._defaults.dayNames;
  367. var monthNamesShort = settings.monthNamesShort || this._defaults.monthNamesShort;
  368. var monthNames = settings.monthNames || this._defaults.monthNames;
  369. var calculateWeek = settings.calculateWeek || this._defaults.calculateWeek;
  370. // Check whether a format character is doubled
  371. var doubled = function(match, step) {
  372. var matches = 1;
  373. while (iFormat + matches < format.length && format.charAt(iFormat + matches) == match) {
  374. matches++;
  375. }
  376. iFormat += matches - 1;
  377. return Math.floor(matches / (step || 1)) > 1;
  378. };
  379. // Format a number, with leading zeroes if necessary
  380. var formatNumber = function(match, value, len, step) {
  381. var num = '' + value;
  382. if (doubled(match, step)) {
  383. while (num.length < len) {
  384. num = '0' + num;
  385. }
  386. }
  387. return num;
  388. };
  389. // Format a name, short or long as requested
  390. var formatName = function(match, value, shortNames, longNames) {
  391. return (doubled(match) ? longNames[value] : shortNames[value]);
  392. };
  393. var output = '';
  394. var literal = false;
  395. for (var iFormat = 0; iFormat < format.length; iFormat++) {
  396. if (literal) {
  397. if (format.charAt(iFormat) == "'" && !doubled("'")) {
  398. literal = false;
  399. }
  400. else {
  401. output += format.charAt(iFormat);
  402. }
  403. }
  404. else {
  405. switch (format.charAt(iFormat)) {
  406. case 'd': output += formatNumber('d', date.getDate(), 2); break;
  407. case 'D': output += formatName('D', date.getDay(),
  408. dayNamesShort, dayNames); break;
  409. case 'o': output += formatNumber('o', this.dayOfYear(date), 3); break;
  410. case 'w': output += formatNumber('w', calculateWeek(date), 2); break;
  411. case 'm': output += formatNumber('m', date.getMonth() + 1, 2); break;
  412. case 'M': output += formatName('M', date.getMonth(),
  413. monthNamesShort, monthNames); break;
  414. case 'y':
  415. output += (doubled('y', 2) ? date.getFullYear() :
  416. (date.getFullYear() % 100 < 10 ? '0' : '') + date.getFullYear() % 100);
  417. break;
  418. case '@': output += Math.floor(date.getTime() / 1000); break;
  419. case '!': output += date.getTime() * 10000 + this._ticksTo1970; break;
  420. case "'":
  421. if (doubled("'")) {
  422. output += "'";
  423. }
  424. else {
  425. literal = true;
  426. }
  427. break;
  428. default:
  429. output += format.charAt(iFormat);
  430. }
  431. }
  432. }
  433. return output;
  434. },
  435. /* Parse a string value into a date object.
  436. See formatDate for the possible formats, plus:
  437. * - ignore rest of string
  438. @param format (string) the expected format of the date ('' for default datepicker format)
  439. @param value (string) the date in the above format
  440. @param settings (object) attributes include:
  441. shortYearCutoff (number) the cutoff year for determining the century (optional)
  442. dayNamesShort (string[]) abbreviated names of the days from Sunday (optional)
  443. dayNames (string[]) names of the days from Sunday (optional)
  444. monthNamesShort (string[]) abbreviated names of the months (optional)
  445. monthNames (string[]) names of the months (optional)
  446. @return (Date) the extracted date value or null if value is blank
  447. @throws errors if the format and/or value are missing,
  448. if the value doesn't match the format,
  449. or if the date is invalid */
  450. parseDate: function(format, value, settings) {
  451. if (value == null) {
  452. throw 'Invalid arguments';
  453. }
  454. value = (typeof value == 'object' ? value.toString() : value + '');
  455. if (value == '') {
  456. return null;
  457. }
  458. format = format || this._defaults.dateFormat;
  459. settings = settings || {};
  460. var shortYearCutoff = settings.shortYearCutoff || this._defaults.shortYearCutoff;
  461. shortYearCutoff = (typeof shortYearCutoff != 'string' ? shortYearCutoff :
  462. this.today().getFullYear() % 100 + parseInt(shortYearCutoff, 10));
  463. var dayNamesShort = settings.dayNamesShort || this._defaults.dayNamesShort;
  464. var dayNames = settings.dayNames || this._defaults.dayNames;
  465. var monthNamesShort = settings.monthNamesShort || this._defaults.monthNamesShort;
  466. var monthNames = settings.monthNames || this._defaults.monthNames;
  467. var year = -1;
  468. var month = -1;
  469. var day = -1;
  470. var doy = -1;
  471. var shortYear = false;
  472. var literal = false;
  473. // Check whether a format character is doubled
  474. var doubled = function(match, step) {
  475. var matches = 1;
  476. while (iFormat + matches < format.length && format.charAt(iFormat + matches) == match) {
  477. matches++;
  478. }
  479. iFormat += matches - 1;
  480. return Math.floor(matches / (step || 1)) > 1;
  481. };
  482. // Extract a number from the string value
  483. var getNumber = function(match, step) {
  484. var isDoubled = doubled(match, step);
  485. var size = [2, 3, isDoubled ? 4 : 2, 11, 20]['oy@!'.indexOf(match) + 1];
  486. var digits = new RegExp('^-?\\d{1,' + size + '}');
  487. var num = value.substring(iValue).match(digits);
  488. if (!num) {
  489. throw 'Missing number at position {0}'.replace(/\{0\}/, iValue);
  490. }
  491. iValue += num[0].length;
  492. return parseInt(num[0], 10);
  493. };
  494. // Extract a name from the string value and convert to an index
  495. var getName = function(match, shortNames, longNames, step) {
  496. var names = (doubled(match, step) ? longNames : shortNames);
  497. for (var i = 0; i < names.length; i++) {
  498. if (value.substr(iValue, names[i].length) == names[i]) {
  499. iValue += names[i].length;
  500. return i + 1;
  501. }
  502. }
  503. throw 'Unknown name at position {0}'.replace(/\{0\}/, iValue);
  504. };
  505. // Confirm that a literal character matches the string value
  506. var checkLiteral = function() {
  507. if (value.charAt(iValue) != format.charAt(iFormat)) {
  508. throw 'Unexpected literal at position {0}'.replace(/\{0\}/, iValue);
  509. }
  510. iValue++;
  511. };
  512. var iValue = 0;
  513. for (var iFormat = 0; iFormat < format.length; iFormat++) {
  514. if (literal) {
  515. if (format.charAt(iFormat) == "'" && !doubled("'")) {
  516. literal = false;
  517. }
  518. else {
  519. checkLiteral();
  520. }
  521. }
  522. else {
  523. switch (format.charAt(iFormat)) {
  524. case 'd': day = getNumber('d'); break;
  525. case 'D': getName('D', dayNamesShort, dayNames); break;
  526. case 'o': doy = getNumber('o'); break;
  527. case 'w': getNumber('w'); break;
  528. case 'm': month = getNumber('m'); break;
  529. case 'M': month = getName('M', monthNamesShort, monthNames); break;
  530. case 'y':
  531. var iSave = iFormat;
  532. shortYear = !doubled('y', 2);
  533. iFormat = iSave;
  534. year = getNumber('y', 2);
  535. break;
  536. case '@':
  537. var date = this._normaliseDate(new Date(getNumber('@') * 1000));
  538. year = date.getFullYear();
  539. month = date.getMonth() + 1;
  540. day = date.getDate();
  541. break;
  542. case '!':
  543. var date = this._normaliseDate(
  544. new Date((getNumber('!') - this._ticksTo1970) / 10000));
  545. year = date.getFullYear();
  546. month = date.getMonth() + 1;
  547. day = date.getDate();
  548. break;
  549. case '*': iValue = value.length; break;
  550. case "'":
  551. if (doubled("'")) {
  552. checkLiteral();
  553. }
  554. else {
  555. literal = true;
  556. }
  557. break;
  558. default: checkLiteral();
  559. }
  560. }
  561. }
  562. if (iValue < value.length) {
  563. throw 'Additional text found at end';
  564. }
  565. if (year == -1) {
  566. year = this.today().getFullYear();
  567. }
  568. else if (year < 100 && shortYear) {
  569. year += (shortYearCutoff == -1 ? 1900 : this.today().getFullYear() -
  570. this.today().getFullYear() % 100 - (year <= shortYearCutoff ? 0 : 100));
  571. }
  572. if (doy > -1) {
  573. month = 1;
  574. day = doy;
  575. for (var dim = this.daysInMonth(year, month); day > dim;
  576. dim = this.daysInMonth(year, month)) {
  577. month++;
  578. day -= dim;
  579. }
  580. }
  581. var date = this.newDate(year, month, day);
  582. if (date.getFullYear() != year || date.getMonth() + 1 != month || date.getDate() != day) {
  583. throw 'Invalid date';
  584. }
  585. return date;
  586. },
  587. /* A date may be specified as an exact value or a relative one.
  588. @param dateSpec (Date or number or string) the date as an object or string
  589. in the given format or an offset - numeric days from today,
  590. or string amounts and periods, e.g. '+1m +2w'
  591. @param defaultDate (Date) the date to use if no other supplied, may be null
  592. @param currentDate (Date) the current date as a possible basis for relative dates,
  593. if null today is used (optional)
  594. @param dateFormat (string) the expected date format - see formatDate above (optional)
  595. @param settings (object) attributes include:
  596. shortYearCutoff (number) the cutoff year for determining the century (optional)
  597. dayNamesShort (string[7]) abbreviated names of the days from Sunday (optional)
  598. dayNames (string[7]) names of the days from Sunday (optional)
  599. monthNamesShort (string[12]) abbreviated names of the months (optional)
  600. monthNames (string[12]) names of the months (optional)
  601. @return (Date) the decoded date */
  602. determineDate: function(dateSpec, defaultDate, currentDate, dateFormat, settings) {
  603. if (currentDate && typeof currentDate != 'object') {
  604. settings = dateFormat;
  605. dateFormat = currentDate;
  606. currentDate = null;
  607. }
  608. if (typeof dateFormat != 'string') {
  609. settings = dateFormat;
  610. dateFormat = '';
  611. }
  612. var offsetString = function(offset) {
  613. try {
  614. return $.datepick.parseDate(dateFormat, offset, settings);
  615. }
  616. catch (e) {
  617. // Ignore
  618. }
  619. offset = offset.toLowerCase();
  620. var date = (offset.match(/^c/) && currentDate ? $.datepick.newDate(currentDate) : null) ||
  621. $.datepick.today();
  622. var pattern = /([+-]?[0-9]+)\s*(d|w|m|y)?/g;
  623. var matches = pattern.exec(offset);
  624. while (matches) {
  625. date = $.datepick.add(date, parseInt(matches[1], 10), matches[2] || 'd');
  626. matches = pattern.exec(offset);
  627. }
  628. return date;
  629. };
  630. defaultDate = (defaultDate ? $.datepick.newDate(defaultDate) : null);
  631. dateSpec = (dateSpec == null ? defaultDate :
  632. (typeof dateSpec == 'string' ? offsetString(dateSpec) : (typeof dateSpec == 'number' ?
  633. (isNaN(dateSpec) || dateSpec == Infinity || dateSpec == -Infinity ? defaultDate :
  634. $.datepick.add($.datepick.today(), dateSpec, 'd')) : $.datepick.newDate(dateSpec))));
  635. return dateSpec;
  636. },
  637. /* Find the number of days in a given month.
  638. @param year (Date) the date to get days for or
  639. (number) the full year
  640. @param month (number) the month (1 to 12)
  641. @return (number) the number of days in this month */
  642. daysInMonth: function(year, month) {
  643. month = (year.getFullYear ? year.getMonth() + 1 : month);
  644. year = (year.getFullYear ? year.getFullYear() : year);
  645. return this.newDate(year, month + 1, 0).getDate();
  646. },
  647. /* Calculate the day of the year for a date.
  648. @param year (Date) the date to get the day-of-year for or
  649. (number) the full year
  650. @param month (number) the month (1-12)
  651. @param day (number) the day
  652. @return (number) the day of the year */
  653. dayOfYear: function(year, month, day) {
  654. var date = (year.getFullYear ? year : this.newDate(year, month, day));
  655. var newYear = this.newDate(date.getFullYear(), 1, 1);
  656. return Math.floor((date.getTime() - newYear.getTime()) / this._msPerDay) + 1;
  657. },
  658. /* Set as calculateWeek to determine the week of the year based on the ISO 8601 definition.
  659. @param year (Date) the date to get the week for or
  660. (number) the full year
  661. @param month (number) the month (1-12)
  662. @param day (number) the day
  663. @return (number) the number of the week within the year that contains this date */
  664. iso8601Week: function(year, month, day) {
  665. var checkDate = (year.getFullYear ?
  666. new Date(year.getTime()) : this.newDate(year, month, day));
  667. // Find Thursday of this week starting on Monday
  668. checkDate.setDate(checkDate.getDate() + 4 - (checkDate.getDay() || 7));
  669. var time = checkDate.getTime();
  670. checkDate.setMonth(0, 1); // Compare with Jan 1
  671. return Math.floor(Math.round((time - checkDate) / 86400000) / 7) + 1;
  672. },
  673. /* Return today's date.
  674. @return (Date) today */
  675. today: function() {
  676. return this._normaliseDate(new Date());
  677. },
  678. /* Return a new date.
  679. @param year (Date) the date to clone or
  680. (number) the year
  681. @param month (number) the month (1-12)
  682. @param day (number) the day
  683. @return (Date) the date */
  684. newDate: function(year, month, day) {
  685. return (!year ? null : (year.getFullYear ? this._normaliseDate(new Date(year.getTime())) :
  686. new Date(year, month - 1, day, 12)));
  687. },
  688. /* Standardise a date into a common format - time portion is 12 noon.
  689. @param date (Date) the date to standardise
  690. @return (Date) the normalised date */
  691. _normaliseDate: function(date) {
  692. if (date) {
  693. date.setHours(12, 0, 0, 0);
  694. }
  695. return date;
  696. },
  697. /* Set the year for a date.
  698. @param date (Date) the original date
  699. @param year (number) the new year
  700. @return the updated date */
  701. year: function(date, year) {
  702. date.setFullYear(year);
  703. return this._normaliseDate(date);
  704. },
  705. /* Set the month for a date.
  706. @param date (Date) the original date
  707. @param month (number) the new month (1-12)
  708. @return the updated date */
  709. month: function(date, month) {
  710. date.setMonth(month - 1);
  711. return this._normaliseDate(date);
  712. },
  713. /* Set the day for a date.
  714. @param date (Date) the original date
  715. @param day (number) the new day of the month
  716. @return the updated date */
  717. day: function(date, day) {
  718. date.setDate(day);
  719. return this._normaliseDate(date);
  720. },
  721. /* Add a number of periods to a date.
  722. @param date (Date) the original date
  723. @param amount (number) the number of periods
  724. @param period (string) the type of period d/w/m/y
  725. @return the updated date */
  726. add: function(date, amount, period) {
  727. if (period == 'd' || period == 'w') {
  728. this._normaliseDate(date);
  729. date.setDate(date.getDate() + amount * (period == 'w' ? 7 : 1));
  730. }
  731. else {
  732. var year = date.getFullYear() + (period == 'y' ? amount : 0);
  733. var month = date.getMonth() + (period == 'm' ? amount : 0);
  734. date.setTime($.datepick.newDate(year, month + 1,
  735. Math.min(date.getDate(), this.daysInMonth(year, month + 1))).getTime());
  736. }
  737. return date;
  738. },
  739. /* Apply the months offset value to a date.
  740. @param date (Date) the original date
  741. @param inst (object) the current instance settings
  742. @return (Date) the updated date */
  743. _applyMonthsOffset: function(date, inst) {
  744. var monthsOffset = inst.get('monthsOffset');
  745. if ($.isFunction(monthsOffset)) {
  746. monthsOffset = monthsOffset.apply(inst.target[0], [date]);
  747. }
  748. return $.datepick.add(date, -monthsOffset, 'm');
  749. },
  750. /* Attach the datepicker functionality to an input field.
  751. @param target (element) the control to affect
  752. @param settings (object) the custom options for this instance */
  753. _attachPicker: function(target, settings) {
  754. target = $(target);
  755. if (target.hasClass(this.markerClass)) {
  756. return;
  757. }
  758. target.addClass(this.markerClass);
  759. var inst = {target: target, selectedDates: [], drawDate: null, pickingRange: false,
  760. inline: ($.inArray(target[0].nodeName.toLowerCase(), ['div', 'span']) > -1),
  761. get: function(name) { // Get a setting value, defaulting if necessary
  762. var value = this.settings[name] !== undefined ?
  763. this.settings[name] : $.datepick._defaults[name];
  764. if ($.inArray(name, ['defaultDate', 'minDate', 'maxDate']) > -1) { // Decode date settings
  765. value = $.datepick.determineDate(
  766. value, null, this.selectedDates[0], this.get('dateFormat'), inst.getConfig());
  767. }
  768. return value;
  769. },
  770. curMinDate: function() {
  771. return (this.pickingRange ? this.selectedDates[0] : this.get('minDate'));
  772. },
  773. getConfig: function() {
  774. return {dayNamesShort: this.get('dayNamesShort'), dayNames: this.get('dayNames'),
  775. monthNamesShort: this.get('monthNamesShort'), monthNames: this.get('monthNames'),
  776. calculateWeek: this.get('calculateWeek'),
  777. shortYearCutoff: this.get('shortYearCutoff')};
  778. }
  779. };
  780. $.data(target[0], this.dataName, inst);
  781. var inlineSettings = ($.fn.metadata ? target.metadata() : {});
  782. inst.settings = $.extend({}, settings || {}, inlineSettings || {});
  783. if (inst.inline) {
  784. inst.drawDate = $.datepick._checkMinMax($.datepick.newDate(inst.selectedDates[0] ||
  785. inst.get('defaultDate') || $.datepick.today()), inst);
  786. inst.prevDate = $.datepick.newDate(inst.drawDate);
  787. this._update(target[0]);
  788. if ($.fn.mousewheel) {
  789. target.mousewheel(this._doMouseWheel);
  790. }
  791. }
  792. else {
  793. this._attachments(target, inst);
  794. target.bind('keydown.' + this.dataName, this._keyDown).
  795. bind('keypress.' + this.dataName, this._keyPress).
  796. bind('keyup.' + this.dataName, this._keyUp);
  797. if (target.attr('disabled')) {
  798. this.disable(target[0]);
  799. }
  800. }
  801. },
  802. /* Retrieve the settings for a datepicker control.
  803. @param target (element) the control to affect
  804. @param name (string) the name of the setting (optional)
  805. @return (object) the current instance settings (name == 'all') or
  806. (object) the default settings (no name) or
  807. (any) the setting value (name supplied) */
  808. options: function(target, name) {
  809. var inst = $.data(target, this.dataName);
  810. return (inst ? (name ? (name == 'all' ?
  811. inst.settings : inst.settings[name]) : $.datepick._defaults) : {});
  812. },
  813. /* Reconfigure the settings for a datepicker control.
  814. @param target (element) the control to affect
  815. @param settings (object) the new options for this instance or
  816. (string) an individual property name
  817. @param value (any) the individual property value (omit if settings is an object) */
  818. option: function(target, settings, value) {
  819. target = $(target);
  820. if (!target.hasClass(this.markerClass)) {
  821. return;
  822. }
  823. settings = settings || {};
  824. if (typeof settings == 'string') {
  825. var name = settings;
  826. settings = {};
  827. settings[name] = value;
  828. }
  829. var inst = $.data(target[0], this.dataName);
  830. var dates = inst.selectedDates;
  831. extendRemove(inst.settings, settings);
  832. this.setDate(target[0], dates, null, false, true);
  833. inst.pickingRange = false;
  834. inst.drawDate = $.datepick.newDate(this._checkMinMax(
  835. (settings.defaultDate ? inst.get('defaultDate') : inst.drawDate) ||
  836. inst.get('defaultDate') || $.datepick.today(), inst));
  837. if (!inst.inline) {
  838. this._attachments(target, inst);
  839. }
  840. if (inst.inline || inst.div) {
  841. this._update(target[0]);
  842. }
  843. },
  844. /* Attach events and trigger, if necessary.
  845. @param target (jQuery) the control to affect
  846. @param inst (object) the current instance settings */
  847. _attachments: function(target, inst) {
  848. target.unbind('focus.' + this.dataName);
  849. if (inst.get('showOnFocus')) {
  850. target.bind('focus.' + this.dataName, this.show);
  851. }
  852. if (inst.trigger) {
  853. inst.trigger.remove();
  854. }
  855. var trigger = inst.get('showTrigger');
  856. inst.trigger = (!trigger ? $([]) :
  857. $(trigger).clone().removeAttr('id').addClass(this._triggerClass)
  858. [inst.get('isRTL') ? 'insertBefore' : 'insertAfter'](target).
  859. click(function() {
  860. if (!$.datepick.isDisabled(target[0])) {
  861. $.datepick[$.datepick.curInst == inst ?
  862. 'hide' : 'show'](target[0]);
  863. }
  864. }));
  865. this._autoSize(target, inst);
  866. var dates = this._extractDates(inst, target.val());
  867. if (dates) {
  868. this.setDate(target[0], dates, null, true);
  869. }
  870. if (inst.get('selectDefaultDate') && inst.get('defaultDate') &&
  871. inst.selectedDates.length == 0) {
  872. this.setDate(target[0], $.datepick.newDate(inst.get('defaultDate') || $.datepick.today()));
  873. }
  874. },
  875. /* Apply the maximum length for the date format.
  876. @param inst (object) the current instance settings */
  877. _autoSize: function(target, inst) {
  878. if (inst.get('autoSize') && !inst.inline) {
  879. var date = $.datepick.newDate(2009, 10, 20); // Ensure double digits
  880. var dateFormat = inst.get('dateFormat');
  881. if (dateFormat.match(/[DM]/)) {
  882. var findMax = function(names) {
  883. var max = 0;
  884. var maxI = 0;
  885. for (var i = 0; i < names.length; i++) {
  886. if (names[i].length > max) {
  887. max = names[i].length;
  888. maxI = i;
  889. }
  890. }
  891. return maxI;
  892. };
  893. date.setMonth(findMax(inst.get(dateFormat.match(/MM/) ? // Longest month
  894. 'monthNames' : 'monthNamesShort')));
  895. date.setDate(findMax(inst.get(dateFormat.match(/DD/) ? // Longest day
  896. 'dayNames' : 'dayNamesShort')) + 20 - date.getDay());
  897. }
  898. inst.target.attr('size', $.datepick.formatDate(dateFormat, date, inst.getConfig()).length);
  899. }
  900. },
  901. /* Remove the datepicker functionality from a control.
  902. @param target (element) the control to affect */
  903. destroy: function(target) {
  904. target = $(target);
  905. if (!target.hasClass(this.markerClass)) {
  906. return;
  907. }
  908. var inst = $.data(target[0], this.dataName);
  909. if (inst.trigger) {
  910. inst.trigger.remove();
  911. }
  912. target.removeClass(this.markerClass).empty().unbind('.' + this.dataName);
  913. if (inst.inline && $.fn.mousewheel) {
  914. target.unmousewheel();
  915. }
  916. if (!inst.inline && inst.get('autoSize')) {
  917. target.removeAttr('size');
  918. }
  919. $.removeData(target[0], this.dataName);
  920. },
  921. /* Apply multiple event functions.
  922. Usage, for example: onShow: multipleEvents(fn1, fn2, ...)
  923. @param fns (function...) the functions to apply */
  924. multipleEvents: function(fns) {
  925. var funcs = arguments;
  926. return function(args) {
  927. for (var i = 0; i < funcs.length; i++) {
  928. funcs[i].apply(this, arguments);
  929. }
  930. };
  931. },
  932. /* Enable the datepicker and any associated trigger.
  933. @param target (element) the control to use */
  934. enable: function(target) {
  935. var $target = $(target);
  936. if (!$target.hasClass(this.markerClass)) {
  937. return;
  938. }
  939. var inst = $.data(target, this.dataName);
  940. if (inst.inline)
  941. $target.children('.' + this._disableClass).remove().end().
  942. find('button,select').attr('disabled', '').end().
  943. find('a').attr('href', 'javascript:void(0)');
  944. else {
  945. target.disabled = false;
  946. inst.trigger.filter('button.' + this._triggerClass).
  947. attr('disabled', '').end().
  948. filter('img.' + this._triggerClass).
  949. css({opacity: '1.0', cursor: ''});
  950. }
  951. this._disabled = $.map(this._disabled,
  952. function(value) { return (value == target ? null : value); }); // Delete entry
  953. },
  954. /* Disable the datepicker and any associated trigger.
  955. @param target (element) the control to use */
  956. disable: function(target) {
  957. var $target = $(target);
  958. if (!$target.hasClass(this.markerClass))
  959. return;
  960. var inst = $.data(target, this.dataName);
  961. if (inst.inline) {
  962. var inline = $target.children(':last');
  963. var offset = inline.offset();
  964. var relOffset = {left: 0, top: 0};
  965. inline.parents().each(function() {
  966. if ($(this).css('position') == 'relative') {
  967. relOffset = $(this).offset();
  968. return false;
  969. }
  970. });
  971. var zIndex = $target.css('zIndex');
  972. zIndex = (zIndex == 'auto' ? 0 : parseInt(zIndex, 10)) + 1;
  973. $target.prepend('<div class="' + this._disableClass + '" style="' +
  974. 'width: ' + inline.outerWidth() + 'px; height: ' + inline.outerHeight() +
  975. 'px; left: ' + (offset.left - relOffset.left) + 'px; top: ' +
  976. (offset.top - relOffset.top) + 'px; z-index: ' + zIndex + '"></div>').
  977. find('button,select').attr('disabled', 'disabled').end().
  978. find('a').removeAttr('href');
  979. }
  980. else {
  981. target.disabled = true;
  982. inst.trigger.filter('button.' + this._triggerClass).
  983. attr('disabled', 'disabled').end().
  984. filter('img.' + this._triggerClass).
  985. css({opacity: '0.5', cursor: 'default'});
  986. }
  987. this._disabled = $.map(this._disabled,
  988. function(value) { return (value == target ? null : value); }); // Delete entry
  989. this._disabled.push(target);
  990. },
  991. /* Is the first field in a jQuery collection disabled as a datepicker?
  992. @param target (element) the control to examine
  993. @return (boolean) true if disabled, false if enabled */
  994. isDisabled: function(target) {
  995. return (target && $.inArray(target, this._disabled) > -1);
  996. },
  997. /* Show a popup datepicker.
  998. @param target (event) a focus event or
  999. (element) the control to use */
  1000. show: function(target) {
  1001. target = target.target || target;
  1002. var inst = $.data(target, $.datepick.dataName);
  1003. if ($.datepick.curInst == inst) {
  1004. return;
  1005. }
  1006. if ($.datepick.curInst) {
  1007. $.datepick.hide($.datepick.curInst, true);
  1008. }
  1009. if (inst) {
  1010. // Retrieve existing date(s)
  1011. inst.lastVal = null;
  1012. inst.selectedDates = $.datepick._extractDates(inst, $(target).val());
  1013. inst.pickingRange = false;
  1014. inst.drawDate = $.datepick._checkMinMax($.datepick.newDate(inst.selectedDates[0] ||
  1015. inst.get('defaultDate') || $.datepick.today()), inst);
  1016. inst.prevDate = $.datepick.newDate(inst.drawDate);
  1017. $.datepick.curInst = inst;
  1018. // Generate content
  1019. $.datepick._update(target, true);
  1020. // Adjust position before showing
  1021. var offset = $.datepick._checkOffset(inst);
  1022. inst.div.css({left: offset.left, top: offset.top});
  1023. // And display
  1024. var showAnim = inst.get('showAnim');
  1025. var showSpeed = inst.get('showSpeed');
  1026. showSpeed = (showSpeed == 'normal' && $.ui && $.ui.version >= '1.8' ?
  1027. '_default' : showSpeed);
  1028. var postProcess = function() {
  1029. var cover = inst.div.find('.' + $.datepick._coverClass);
  1030. if (cover.length) {
  1031. var borders = $.datepick._getBorders(inst.div);
  1032. cover.css({left: -borders[0], top: -borders[1], // IE6- only
  1033. width: inst.div.outerWidth() + borders[0],
  1034. height: inst.div.outerHeight() + borders[1]});
  1035. }
  1036. };
  1037. if ($.effects && $.effects[showAnim]) {
  1038. var data = inst.div.data(); // Update old effects data
  1039. for (var key in data) {
  1040. if (key.match(/^ec\.storage\./)) {
  1041. data[key] = inst._mainDiv.css(key.replace(/ec\.storage\./, ''));
  1042. }
  1043. }
  1044. inst.div.data(data).show(showAnim, inst.get('showOptions'), showSpeed, postProcess);
  1045. }
  1046. else {
  1047. inst.div[showAnim || 'show']((showAnim ? showSpeed : ''), postProcess);
  1048. }
  1049. if (!showAnim) {
  1050. postProcess();
  1051. }
  1052. }
  1053. },
  1054. /* Extract possible dates from a string.
  1055. @param inst (object) the current instance settings
  1056. @param text (string) the text to extract from
  1057. @return (CDate[]) the extracted dates */
  1058. _extractDates: function(inst, datesText) {
  1059. if (datesText == inst.lastVal) {
  1060. return;
  1061. }
  1062. inst.lastVal = datesText;
  1063. var dateFormat = inst.get('dateFormat');
  1064. var multiSelect = inst.get('multiSelect');
  1065. var rangeSelect = inst.get('rangeSelect');
  1066. datesText = datesText.split(multiSelect ? inst.get('multiSeparator') :
  1067. (rangeSelect ? inst.get('rangeSeparator') : '\x00'));
  1068. var dates = [];
  1069. for (var i = 0; i < datesText.length; i++) {
  1070. try {
  1071. var date = $.datepick.parseDate(dateFormat, datesText[i], inst.getConfig());
  1072. if (date) {
  1073. var found = false;
  1074. for (var j = 0; j < dates.length; j++) {
  1075. if (dates[j].getTime() == date.getTime()) {
  1076. found = true;
  1077. break;
  1078. }
  1079. }
  1080. if (!found) {
  1081. dates.push(date);
  1082. }
  1083. }
  1084. }
  1085. catch (e) {
  1086. // Ignore
  1087. }
  1088. }
  1089. dates.splice(multiSelect || (rangeSelect ? 2 : 1), dates.length);
  1090. if (rangeSelect && dates.length == 1) {
  1091. dates[1] = dates[0];
  1092. }
  1093. return dates;
  1094. },
  1095. /* Update the datepicker display.
  1096. @param target (event) a focus event or
  1097. (element) the control to use
  1098. @param hidden (boolean) true to initially hide the datepicker */
  1099. _update: function(target, hidden) {
  1100. target = $(target.target || target);
  1101. var inst = $.data(target[0], $.datepick.dataName);
  1102. if (inst) {
  1103. if (inst.inline || $.datepick.curInst == inst) {
  1104. var onChange = inst.get('onChangeMonthYear');
  1105. if (onChange && (!inst.prevDate ||
  1106. inst.prevDate.getFullYear() != inst.drawDate.getFullYear() ||
  1107. inst.prevDate.getMonth() != inst.drawDate.getMonth())) {
  1108. onChange.apply(target[0], [inst.drawDate.getFullYear(), inst.drawDate.getMonth() + 1]);
  1109. }
  1110. }
  1111. if (inst.inline) {
  1112. target.html(this._generateContent(target[0], inst));
  1113. }
  1114. else if ($.datepick.curInst == inst) {
  1115. if (!inst.div) {
  1116. inst.div = $('<div></div>').addClass(this._popupClass).
  1117. css({display: (hidden ? 'none' : 'static'), position: 'absolute',
  1118. left: target.offset().left,
  1119. top: target.offset().top + target.outerHeight()}).
  1120. appendTo($(inst.get('popupContainer') || 'body'));
  1121. if ($.fn.mousewheel) {
  1122. inst.div.mousewheel(this._doMouseWheel);
  1123. }
  1124. }
  1125. inst.div.html(this._generateContent(target[0], inst));
  1126. target.focus();
  1127. }
  1128. }
  1129. },
  1130. /* Update the input field and any alternate field with the current dates.
  1131. @param target (element) the control to use
  1132. @param keyUp (boolean, internal) true if coming from keyUp processing */
  1133. _updateInput: function(target, keyUp) {
  1134. var inst = $.data(target, this.dataName);
  1135. if (inst) {
  1136. var value = '';
  1137. var altValue = '';
  1138. var sep = (inst.get('multiSelect') ? inst.get('multiSeparator') :
  1139. inst.get('rangeSeparator'));
  1140. var dateFormat = inst.get('dateFormat');
  1141. var altFormat = inst.get('altFormat') || dateFormat;
  1142. for (var i = 0; i < inst.selectedDates.length; i++) {
  1143. value += (keyUp ? '' : (i > 0 ? sep : '') + $.datepick.formatDate(
  1144. dateFormat, inst.selectedDates[i], inst.getConfig()));
  1145. altValue += (i > 0 ? sep : '') + $.datepick.formatDate(
  1146. altFormat, inst.selectedDates[i], inst.getConfig());
  1147. }
  1148. if (!inst.inline && !keyUp) {
  1149. $(target).val(value);
  1150. }
  1151. $(inst.get('altField')).val(altValue);
  1152. var onSelect = inst.get('onSelect');
  1153. if (onSelect && !keyUp && !inst.inSelect) {
  1154. inst.inSelect = true; // Prevent endless loops
  1155. onSelect.apply(target, [inst.selectedDates]);
  1156. inst.inSelect = false;
  1157. }
  1158. }
  1159. },
  1160. /* Retrieve the size of left and top borders for an element.
  1161. @param elem (jQuery) the element of interest
  1162. @return (number[2]) the left and top borders */
  1163. _getBorders: function(elem) {
  1164. var convert = function(value) {
  1165. var extra = ($.browser.msie ? 1 : 0);
  1166. return {thin: 1 + extra, medium: 3 + extra, thick: 5 + extra}[value] || value;
  1167. };
  1168. return [parseFloat(convert(elem.css('border-left-width'))),
  1169. parseFloat(convert(elem.css('border-top-width')))];
  1170. },
  1171. /* Check positioning to remain on the screen.
  1172. @param inst (object) the current instance settings
  1173. @return (object) the updated offset for the datepicker */
  1174. _checkOffset: function(inst) {
  1175. var base = (inst.target.is(':hidden') && inst.trigger ? inst.trigger : inst.target);
  1176. var offset = base.offset();
  1177. var isFixed = false;
  1178. $(inst.target).parents().each(function() {
  1179. isFixed |= $(this).css('position') == 'fixed';
  1180. return !isFixed;
  1181. });
  1182. if (isFixed && $.browser.opera) { // Correction for Opera when fixed and scrolled
  1183. offset.left -= document.documentElement.scrollLeft;
  1184. offset.top -= document.documentElement.scrollTop;
  1185. }
  1186. var browserWidth = (!$.browser.mozilla || document.doctype ?
  1187. document.documentElement.clientWidth : 0) || document.body.clientWidth;
  1188. var browserHeight = (!$.browser.mozilla || document.doctype ?
  1189. document.documentElement.clientHeight : 0) || document.body.clientHeight;
  1190. if (browserWidth == 0) {
  1191. return offset;
  1192. }
  1193. var alignment = inst.get('alignment');
  1194. var isRTL = inst.get('isRTL');
  1195. var scrollX = document.documentElement.scrollLeft || document.body.scrollLeft;
  1196. var scrollY = document.documentElement.scrollTop || document.body.scrollTop;
  1197. var above = offset.top - inst.div.outerHeight() -
  1198. (isFixed && $.browser.opera ? document.documentElement.scrollTop : 0);
  1199. var below = offset.top + base.outerHeight();
  1200. var alignL = offset.left;
  1201. var alignR = offset.left + base.outerWidth() - inst.div.outerWidth() -
  1202. (isFixed && $.browser.opera ? document.documentElement.scrollLeft : 0);
  1203. var tooWide = (offset.left + inst.div.outerWidth() - scrollX) > browserWidth;
  1204. var tooHigh = (offset.top + inst.target.outerHeight() + inst.div.outerHeight() -
  1205. scrollY) > browserHeight;
  1206. if (alignment == 'topLeft') {
  1207. offset = {left: alignL, top: above};
  1208. }
  1209. else if (alignment == 'topRight') {
  1210. offset = {left: alignR, top: above};
  1211. }
  1212. else if (alignment == 'bottomLeft') {
  1213. offset = {left: alignL, top: below};
  1214. }
  1215. else if (alignment == 'bottomRight') {
  1216. offset = {left: alignR, top: below};
  1217. }
  1218. else if (alignment == 'top') {
  1219. offset = {left: (isRTL || tooWide ? alignR : alignL), top: above};
  1220. }
  1221. else { // bottom
  1222. offset = {left: (isRTL || tooWide ? alignR : alignL),
  1223. top: (tooHigh ? above : below)};
  1224. }
  1225. offset.left = Math.max((isFixed ? 0 : scrollX), offset.left - (isFixed ? scrollX : 0));
  1226. offset.top = Math.max((isFixed ? 0 : scrollY), offset.top - (isFixed ? scrollY : 0));
  1227. return offset;
  1228. },
  1229. /* Close date picker if clicked elsewhere.
  1230. @param event (MouseEvent) the mouse click to check */
  1231. _checkExternalClick: function(event) {
  1232. if (!$.datepick.curInst) {
  1233. return;
  1234. }
  1235. var target = $(event.target);
  1236. if (!target.parents().andSelf().hasClass($.datepick._popupClass) &&
  1237. !target.hasClass($.datepick.markerClass) &&
  1238. !target.parents().andSelf().hasClass($.datepick._triggerClass)) {
  1239. $.datepick.hide($.datepick.curInst);
  1240. }
  1241. },
  1242. /* Hide a popup datepicker.
  1243. @param target (element) the control to use or
  1244. (object) the current instance settings
  1245. @param immediate (boolean) true to close immediately without animation */
  1246. hide: function(target, immediate) {
  1247. var inst = $.data(target, this.dataName) || target;
  1248. if (inst && inst == $.datepick.curInst) {
  1249. var showAnim = (immediate ? '' : inst.get('showAnim'));
  1250. var showSpeed = inst.get('showSpeed');
  1251. showSpeed = (showSpeed == 'normal' && $.ui && $.ui.version >= '1.8' ?
  1252. '_default' : showSpeed);
  1253. var postProcess = function() {
  1254. inst.div.remove();
  1255. inst.div = null;
  1256. $.datepick.curInst = null;
  1257. var onClose = inst.get('onClose');
  1258. if (onClose) {
  1259. onClose.apply(target, [inst.selectedDates]);
  1260. }
  1261. };
  1262. inst.div.stop();
  1263. if ($.effects && $.effects[showAnim]) {
  1264. inst.div.hide(showAnim, inst.get('showOptions'), showSpeed, postProcess);
  1265. }
  1266. else {
  1267. var hideAnim = (showAnim == 'slideDown' ? 'slideUp' :
  1268. (showAnim == 'fadeIn' ? 'fadeOut' : 'hide'));
  1269. inst.div[hideAnim]((showAnim ? showSpeed : ''), postProcess);
  1270. }
  1271. if (!showAnim) {
  1272. postProcess();
  1273. }
  1274. }
  1275. },
  1276. /* Handle keystrokes in the datepicker.
  1277. @param event (KeyEvent) the keystroke
  1278. @return (boolean) true if not handled, false if handled */
  1279. _keyDown: function(event) {
  1280. var target = event.target;
  1281. var inst = $.data(target, $.datepick.dataName);
  1282. var handled = false;
  1283. if (inst.div) {
  1284. if (event.keyCode == 9) { // Tab - close
  1285. $.datepick.hide(target);
  1286. }
  1287. else if (event.keyCode == 13) { // Enter - select
  1288. $.datepick.selectDate(target,
  1289. $('a.' + inst.get('renderer').highlightedClass, inst.div)[0]);
  1290. handled = true;
  1291. }
  1292. else { // Command keystrokes
  1293. var commands = inst.get('commands');
  1294. for (var name in commands) {
  1295. var command = commands[name];
  1296. if (command.keystroke.keyCode == event.keyCode &&
  1297. !!command.keystroke.ctrlKey == !!(event.ctrlKey || event.metaKey) &&
  1298. !!command.keystroke.altKey == event.altKey &&
  1299. !!command.keystroke.shiftKey == event.shiftKey) {
  1300. $.datepick.performAction(target, name);
  1301. handled = true;
  1302. break;
  1303. }
  1304. }
  1305. }
  1306. }
  1307. else { // Show on 'current' keystroke
  1308. var command = inst.get('commands').current;
  1309. if (command.keystroke.keyCode == event.keyCode &&
  1310. !!command.keystroke.ctrlKey == !!(event.ctrlKey || event.metaKey) &&
  1311. !!command.keystroke.altKey == event.altKey &&
  1312. !!command.keystroke.shiftKey == event.shiftKey) {
  1313. $.datepick.show(target);
  1314. handled = true;
  1315. }
  1316. }
  1317. inst.ctrlKey = ((event.keyCode < 48 && event.keyCode != 32) ||
  1318. event.ctrlKey || event.metaKey);
  1319. if (handled) {
  1320. event.preventDefault();
  1321. event.stopPropagation();
  1322. }
  1323. return !handled;
  1324. },
  1325. /* Filter keystrokes in the datepicker.
  1326. @param event (KeyEvent) the keystroke
  1327. @return (boolean) true if allowed, false if not allowed */
  1328. _keyPress: function(event) {
  1329. var target = event.target;
  1330. var inst = $.data(target, $.datepick.dataName);
  1331. if (inst && inst.get('constrainInput')) {
  1332. var ch = String.fromCharCode(event.keyCode || event.charCode);
  1333. var allowedChars = $.datepick._allowedChars(inst);
  1334. return (event.metaKey || inst.ctrlKey || ch < ' ' ||
  1335. !allowedChars || allowedChars.indexOf(ch) > -1);
  1336. }
  1337. return true;
  1338. },
  1339. /* Determine the set of characters allowed by the date format.
  1340. @param inst (object) the current instance settings
  1341. @return (string) the set of allowed characters, or null if anything allowed */
  1342. _allowedChars: function(inst) {
  1343. var dateFormat = inst.get('dateFormat');
  1344. var allowedChars = (inst.get('multiSelect') ? inst.get('multiSeparator') :
  1345. (inst.get('rangeSelect') ? inst.get('rangeSeparator') : ''));
  1346. var literal = false;
  1347. var hasNum = false;
  1348. for (var i = 0; i < dateFormat.length; i++) {
  1349. var ch = dateFormat.charAt(i);
  1350. if (literal) {
  1351. if (ch == "'" && dateFormat.charAt(i + 1) != "'") {
  1352. literal = false;
  1353. }
  1354. else {
  1355. allowedChars += ch;
  1356. }
  1357. }
  1358. else {
  1359. switch (ch) {
  1360. case 'd': case 'm': case 'o': case 'w':
  1361. allowedChars += (hasNum ? '' : '0123456789'); hasNum = true; break;
  1362. case 'y': case '@': case '!':
  1363. allowedChars += (hasNum ? '' : '0123456789') + '-'; hasNum = true; break;
  1364. case 'J':
  1365. allowedChars += (hasNum ? '' : '0123456789') + '-.'; hasNum = true; break;
  1366. case 'D': case 'M': case 'Y':
  1367. return null; // Accept anything
  1368. case "'":
  1369. if (dateFormat.charAt(i + 1) == "'") {
  1370. allowedChars += "'";
  1371. }
  1372. else {
  1373. literal = true;
  1374. }
  1375. break;
  1376. default:
  1377. allowedChars += ch;
  1378. }
  1379. }
  1380. }
  1381. return allowedChars;
  1382. },
  1383. /* Synchronise datepicker with the field.
  1384. @param event (KeyEvent) the keystroke
  1385. @return (boolean) true if allowed, false if not allowed */
  1386. _keyUp: function(event) {
  1387. var target = event.target;
  1388. var inst = $.data(target, $.datepick.dataName);
  1389. if (inst && !inst.ctrlKey && inst.lastVal != inst.target.val()) {
  1390. try {
  1391. var dates = $.datepick._extractDates(inst, inst.target.val());
  1392. if (dates.length > 0) {
  1393. $.datepick.setDate(target, dates, null, true);
  1394. }
  1395. }
  1396. catch (event) {
  1397. // Ignore
  1398. }
  1399. }
  1400. return true;
  1401. },
  1402. /* Increment/decrement month/year on mouse wheel activity.
  1403. @param event (event) the mouse wheel event
  1404. @param delta (number) the amount of change */
  1405. _doMouseWheel: function(event, delta) {
  1406. var target = ($.datepick.curInst && $.datepick.curInst.target[0]) ||
  1407. $(event.target).closest('.' + $.datepick.markerClass)[0];
  1408. if ($.datepick.isDisabled(target)) {
  1409. return;
  1410. }
  1411. var inst = $.data(target, $.datepick.dataName);
  1412. if (inst.get('useMouseWheel')) {
  1413. delta = ($.browser.opera ? -delta : delta);
  1414. delta = (delta < 0 ? -1 : +1);
  1415. $.datepick.changeMonth(target,
  1416. -inst.get(event.ctrlKey ? 'monthsToJump' : 'monthsToStep') * delta);
  1417. }
  1418. event.preventDefault();
  1419. },
  1420. /* Clear an input and close a popup datepicker.
  1421. @param target (element) the control to use */
  1422. clear: function(target) {
  1423. var inst = $.data(target, this.dataName);
  1424. if (inst) {
  1425. inst.selectedDates = [];
  1426. this.hide(target);
  1427. if (inst.get('selectDefaultDate') && inst.get('defaultDate')) {
  1428. this.setDate(target,
  1429. $.datepick.newDate(inst.get('defaultDate') ||$.datepick.today()));
  1430. }
  1431. else {
  1432. this._updateInput(target);
  1433. }
  1434. }
  1435. },
  1436. /* Retrieve the selected date(s) for a datepicker.
  1437. @param target (element) the control to examine
  1438. @return (CDate[]) the selected date(s) */
  1439. getDate: function(target) {
  1440. var inst = $.data(target, this.dataName);
  1441. return (inst ? inst.selectedDates : []);
  1442. },
  1443. /* Set the selected date(s) for a datepicker.
  1444. @param target (element) the control to examine
  1445. @param dates (CDate or number or string or [] of these) the selected date(s)
  1446. @param endDate (CDate or number or string) the ending date for a range (optional)
  1447. @param keyUp (boolean, internal) true if coming from keyUp processing
  1448. @param setOpt (boolean, internal) true if coming from option processing */
  1449. setDate: function(target, dates, endDate, keyUp, setOpt) {
  1450. var inst = $.data(target, this.dataName);
  1451. if (inst) {
  1452. if (!$.isArray(dates)) {
  1453. dates = [dates];
  1454. if (endDate) {
  1455. dates.push(endDate);
  1456. }
  1457. }
  1458. var dateFormat = inst.get('dateFormat');
  1459. var minDate = inst.get('minDate');
  1460. var maxDate = inst.get('maxDate');
  1461. var curDate = inst.selectedDates[0];
  1462. inst.selectedDates = [];
  1463. for (var i = 0; i < dates.length; i++) {
  1464. var date = $.datepick.determineDate(
  1465. dates[i], null, curDate, dateFormat, inst.getConfig());
  1466. if (date) {
  1467. if ((!minDate || date.getTime() >= minDate.getTime()) &&
  1468. (!maxDate || date.getTime() <= maxDate.getTime())) {
  1469. var found = false;
  1470. for (var j = 0; j < inst.selectedDates.length; j++) {
  1471. if (inst.selectedDates[j].getTime() == date.getTime()) {
  1472. found = true;
  1473. break;
  1474. }
  1475. }
  1476. if (!found) {
  1477. inst.selectedDates.push(date);
  1478. }
  1479. }
  1480. }
  1481. }
  1482. var rangeSelect = inst.get('rangeSelect');
  1483. inst.selectedDates.splice(inst.get('multiSelect') ||
  1484. (rangeSelect ? 2 : 1), inst.selectedDates.length);
  1485. if (rangeSelect) {
  1486. switch (inst.selectedDates.length) {
  1487. case 1: inst.selectedDates[1] = inst.selectedDates[0]; break;
  1488. case 2: inst.selectedDates[1] =
  1489. (inst.selectedDates[0].getTime() > inst.selectedDates[1].getTime() ?
  1490. inst.selectedDates[0] : inst.selectedDates[1]); break;
  1491. }
  1492. inst.pickingRange = false;
  1493. }
  1494. inst.prevDate = (inst.drawDate ? $.datepick.newDate(inst.drawDate) : null);
  1495. inst.drawDate = this._checkMinMax($.datepick.newDate(inst.selectedDates[0] ||
  1496. inst.get('defaultDate') || $.datepick.today()), inst);
  1497. if (!setOpt) {
  1498. this._update(target);
  1499. this._updateInput(target, keyUp);
  1500. }
  1501. }
  1502. },
  1503. /* Determine whether a date is selectable for this datepicker.
  1504. @param target (element) the control to check
  1505. @param date (Date or string or number) the date to check
  1506. @return (boolean) true if selectable, false if not */
  1507. isSelectable: function(target, date) {
  1508. var inst = $.data(target, this.dataName);
  1509. if (!inst) {
  1510. return false;
  1511. }
  1512. date = $.datepick.determineDate(date, inst.selectedDates[0] || this.today(), null,
  1513. inst.get('dateFormat'), inst.getConfig());
  1514. return this._isSelectable(target, date, inst.get('onDate'),
  1515. inst.get('minDate'), inst.get('maxDate'));
  1516. },
  1517. /* Internally determine whether a date is selectable for this datepicker.
  1518. @param target (element) the control to check
  1519. @param date (Date) the date to check
  1520. @param onDate (function or boolean) any onDate callback or callback.selectable
  1521. @param mindate (Date) the minimum allowed date
  1522. @param maxdate (Date) the maximum allowed date
  1523. @return (boolean) true if selectable, false if not */
  1524. _isSelectable: function(target, date, onDate, minDate, maxDate) {
  1525. var dateInfo = (typeof onDate == 'boolean' ? {selectable: onDate} :
  1526. (!onDate ? {} : onDate.apply(target, [date, true])));
  1527. return (dateInfo.selectable != false) &&
  1528. (!minDate || date.getTime() >= minDate.getTime()) &&
  1529. (!maxDate || date.getTime() <= maxDate.getTime());
  1530. },
  1531. /* Perform a named action for a datepicker.
  1532. @param target (element) the control to affect
  1533. @param action (string) the name of the action */
  1534. performAction: function(target, action) {
  1535. var inst = $.data(target, this.dataName);
  1536. if (inst && !this.isDisabled(target)) {
  1537. var commands = inst.get('commands');
  1538. if (commands[action] && commands[action].enabled.apply(target, [inst])) {
  1539. commands[action].action.apply(target, [inst]);
  1540. }
  1541. }
  1542. },
  1543. /* Set the currently shown month, defaulting to today's.
  1544. @param target (element) the control to affect
  1545. @param year (number) the year to show (optional)
  1546. @param month (number) the month to show (1-12) (optional)
  1547. @param day (number) the day to show (optional) */
  1548. showMonth: function(target, year, month, day) {
  1549. var inst = $.data(target, this.dataName);
  1550. if (inst && (day != null ||
  1551. (inst.drawDate.getFullYear() != year || inst.drawDate.getMonth() + 1 != month))) {
  1552. inst.prevDate = $.datepick.newDate(inst.drawDate);
  1553. var show = this._checkMinMax((year != null ?
  1554. $.datepick.newDate(year, month, 1) : $.datepick.today()), inst);
  1555. inst.drawDate = $.datepick.newDate(show.getFullYear(), show.getMonth() + 1,
  1556. (day != null ? day : Math.min(inst.drawDate.getDate(),
  1557. $.datepick.daysInMonth(show.getFullYear(), show.getMonth() + 1))));
  1558. this._update(target);
  1559. }
  1560. },
  1561. /* Adjust the currently shown month.
  1562. @param target (element) the control to affect
  1563. @param offset (number) the number of months to change by */
  1564. changeMonth: function(target, offset) {
  1565. var inst = $.data(target, this.dataName);
  1566. if (inst) {
  1567. var date = $.datepick.add($.datepick.newDate(inst.drawDate), offset, 'm');
  1568. this.showMonth(target, date.getFullYear(), date.getMonth() + 1);
  1569. }
  1570. },
  1571. /* Adjust the currently shown day.
  1572. @param target (element) the control to affect
  1573. @param offset (number) the number of days to change by */
  1574. changeDay: function(target, offset) {
  1575. var inst = $.data(target, this.dataName);
  1576. if (inst) {
  1577. var date = $.datepick.add($.datepick.newDate(inst.drawDate), offset, 'd');
  1578. this.showMonth(target, date.getFullYear(), date.getMonth() + 1, date.getDate());
  1579. }
  1580. },
  1581. /* Restrict a date to the minimum/maximum specified.
  1582. @param date (CDate) the date to check
  1583. @param inst (object) the current instance settings */
  1584. _checkMinMax: function(date, inst) {
  1585. var minDate = inst.get('minDate');
  1586. var maxDate = inst.get('maxDate');
  1587. date = (minDate && date.getTime() < minDate.getTime() ? $.datepick.newDate(minDate) : date);
  1588. date = (maxDate && date.getTime() > maxDate.getTime() ? $.datepick.newDate(maxDate) : date);
  1589. return date;
  1590. },
  1591. /* Retrieve the date associated with an entry in the datepicker.
  1592. @param target (element) the control to examine
  1593. @param elem (element) the selected datepicker element
  1594. @return (CDate) the corresponding date, or null */
  1595. retrieveDate: function(target, elem) {
  1596. var inst = $.data(target, this.dataName);
  1597. return (!inst ? null : this._normaliseDate(
  1598. new Date(parseInt(elem.className.replace(/^.*dp(-?\d+).*$/, '$1'), 10))));
  1599. },
  1600. /* Select a date for this datepicker.
  1601. @param target (element) the control to examine
  1602. @param elem (element) the selected datepicker element */
  1603. selectDate: function(target, elem) {
  1604. var inst = $.data(target, this.dataName);
  1605. if (inst && !this.isDisabled(target)) {
  1606. var date = this.retrieveDate(target, elem);
  1607. var multiSelect = inst.get('multiSelect');
  1608. var rangeSelect = inst.get('rangeSelect');
  1609. if (multiSelect) {
  1610. var found = false;
  1611. for (var i = 0; i < inst.selectedDates.length; i++) {
  1612. if (date.getTime() == inst.selectedDates[i].getTime()) {
  1613. inst.selectedDates.splice(i, 1);
  1614. found = true;
  1615. break;
  1616. }
  1617. }
  1618. if (!found && inst.selectedDates.length < multiSelect) {
  1619. inst.selectedDates.push(date);
  1620. }
  1621. }
  1622. else if (rangeSelect) {
  1623. if (inst.pickingRange) {
  1624. inst.selectedDates[1] = date;
  1625. }
  1626. else {
  1627. inst.selectedDates = [date, date];
  1628. }
  1629. inst.pickingRange = !inst.pickingRange;
  1630. }
  1631. else {
  1632. inst.selectedDates = [date];
  1633. }
  1634. inst.prevDate = $.datepick.newDate(date);
  1635. this._updateInput(target);
  1636. if (inst.inline || inst.pickingRange || inst.selectedDates.length <
  1637. (multiSelect || (rangeSelect ? 2 : 1))) {
  1638. this._update(target);
  1639. }
  1640. else {
  1641. this.hide(target);
  1642. }
  1643. }
  1644. },
  1645. /* Generate the datepicker content for this control.
  1646. @param target (element) the control to affect
  1647. @param inst (object) the current instance settings
  1648. @return (jQuery) the datepicker content */
  1649. _generateContent: function(target, inst) {
  1650. var renderer = inst.get('renderer');
  1651. var monthsToShow = inst.get('monthsToShow');
  1652. monthsToShow = ($.isArray(monthsToShow) ? monthsToShow : [1, monthsToShow]);
  1653. inst.drawDate = this._checkMinMax(
  1654. inst.drawDate || inst.get('defaultDate') || $.datepick.today(), inst);
  1655. var drawDate = $.datepick._applyMonthsOffset($.datepick.newDate(inst.drawDate), inst);
  1656. // Generate months
  1657. var monthRows = '';
  1658. for (var row = 0; row < monthsToShow[0]; row++) {
  1659. var months = '';
  1660. for (var col = 0; col < monthsToShow[1]; col++) {
  1661. months += this._generateMonth(target, inst, drawDate.getFullYear(),
  1662. drawDate.getMonth() + 1, renderer, (row == 0 && col == 0));
  1663. $.datepick.add(drawDate, 1, 'm');
  1664. }
  1665. monthRows += this._prepare(renderer.monthRow, inst).replace(/\{months\}/, months);
  1666. }
  1667. var picker = this._prepare(renderer.picker, inst).replace(/\{months\}/, monthRows).
  1668. replace(/\{weekHeader\}/g, this._generateDayHeaders(inst, renderer)) +
  1669. ($.browser.msie && parseInt($.browser.version, 10) < 7 && !inst.inline ?
  1670. '<iframe src="javascript:void(0);" class="' + this._coverClass + '"></iframe>' : '');
  1671. // Add commands
  1672. var commands = inst.get('commands');
  1673. var asDateFormat = inst.get('commandsAsDateFormat');
  1674. var addCommand = function(type, open, close, name, classes) {
  1675. if (picker.indexOf('{' + type + ':' + name + '}') == -1) {
  1676. return;
  1677. }
  1678. var command = commands[name];
  1679. var date = (asDateFormat ? command.date.apply(target, [inst]) : null);
  1680. picker = picker.replace(new RegExp('\\{' + type + ':' + name + '\\}', 'g'),
  1681. '<' + open +
  1682. (command.status ? ' title="' + inst.get(command.status) + '"' : '') +
  1683. ' class="' + renderer.commandClass + ' ' +
  1684. renderer.commandClass + '-' + name + ' ' + classes +
  1685. (command.enabled(inst) ? '' : ' ' + renderer.disabledClass) + '">' +
  1686. (date ? $.datepick.formatDate(inst.get(command.text), date, inst.getConfig()) :
  1687. inst.get(command.text)) + '</' + close + '>');
  1688. };
  1689. for (var name in commands) {
  1690. addCommand('button', 'button type="button"', 'button', name,
  1691. renderer.commandButtonClass);
  1692. addCommand('link', 'a href="javascript:void(0)"', 'a', name,
  1693. renderer.commandLinkClass);
  1694. }
  1695. picker = $(picker);
  1696. if (monthsToShow[1] > 1) {
  1697. var count = 0;
  1698. $(renderer.monthSelector, picker).each(function() {
  1699. var nth = ++count % monthsToShow[1];
  1700. $(this).addClass(nth == 1 ? 'first' : (nth == 0 ? 'last' : ''));
  1701. });
  1702. }
  1703. // Add datepicker behaviour
  1704. var self = this;
  1705. picker.find(renderer.daySelector + ' a').hover(
  1706. function() { $(this).addClass(renderer.highlightedClass); },
  1707. function() {
  1708. (inst.inline ? $(this).parents('.' + self.markerClass) : inst.div).
  1709. find(renderer.daySelector + ' a').
  1710. removeClass(renderer.highlightedClass);
  1711. }).
  1712. click(function() {
  1713. self.selectDate(target, this);
  1714. }).end().
  1715. find('select.' + this._monthYearClass + ':not(.' + this._anyYearClass + ')').
  1716. change(function() {
  1717. var monthYear = $(this).val().split('/');
  1718. self.showMonth(target, parseInt(monthYear[1], 10), parseInt(monthYear[0], 10));
  1719. }).end().
  1720. find('select.' + this._anyYearClass).
  1721. click(function() {
  1722. $(this).css('visibility', 'hidden').
  1723. next('input').css({left: this.offsetLeft, top: this.offsetTop,
  1724. width: this.offsetWidth, height: this.offsetHeight}).show().focus();
  1725. }).end().
  1726. find('input.' + self._monthYearClass).
  1727. change(function() {
  1728. try {
  1729. var year = parseInt($(this).val(), 10);
  1730. year = (isNaN(year) ? inst.drawDate.getFullYear() : year);
  1731. self.showMonth(target, year, inst.drawDate.getMonth() + 1, inst.drawDate.getDate());
  1732. }
  1733. catch (e) {
  1734. alert(e);
  1735. }
  1736. }).keydown(function(event) {
  1737. if (event.keyCode == 13) { // Enter
  1738. $(event.target).change();
  1739. }
  1740. else if (event.keyCode == 27) { // Escape
  1741. $(event.target).hide().prev('select').css('visibility', 'visible');
  1742. inst.target.focus();
  1743. }
  1744. });
  1745. // Add command behaviour
  1746. picker.find('.' + renderer.commandClass).click(function() {
  1747. if (!$(this).hasClass(renderer.disabledClass)) {
  1748. var action = this.className.replace(
  1749. new RegExp('^.*' + renderer.commandClass + '-([^ ]+).*$'), '$1');
  1750. $.datepick.performAction(target, action);
  1751. }
  1752. });
  1753. // Add classes
  1754. if (inst.get('isRTL')) {
  1755. picker.addClass(renderer.rtlClass);
  1756. }
  1757. if (monthsToShow[0] * monthsToShow[1] > 1) {
  1758. picker.addClass(renderer.multiClass);
  1759. }
  1760. var pickerClass = inst.get('pickerClass');
  1761. if (pickerClass) {
  1762. picker.addClass(pickerClass);
  1763. }
  1764. // Resize
  1765. $('body').append(picker);
  1766. var width = 0;
  1767. picker.find(renderer.monthSelector).each(function() {
  1768. width += $(this).outerWidth();
  1769. });
  1770. picker.width(width / monthsToShow[0]);
  1771. // Pre-show customisation
  1772. var onShow = inst.get('onShow');
  1773. if (onShow) {
  1774. onShow.apply(target, [picker, inst]);
  1775. }
  1776. return picker;
  1777. },
  1778. /* Generate the content for a single month.
  1779. @param target (element) the control to affect
  1780. @param inst (object) the current instance settings
  1781. @param year (number) the year to generate
  1782. @param month (number) the month to generate
  1783. @param renderer (object) the rendering templates
  1784. @param first (boolean) true if first of multiple months
  1785. @return (string) the month content */
  1786. _generateMonth: function(target, inst, year, month, renderer, first) {
  1787. var daysInMonth = $.datepick.daysInMonth(year, month);
  1788. var monthsToShow = inst.get('monthsToShow');
  1789. monthsToShow = ($.isArray(monthsToShow) ? monthsToShow : [1, monthsToShow]);
  1790. var fixedWeeks = inst.get('fixedWeeks') || (monthsToShow[0] * monthsToShow[1] > 1);
  1791. var firstDay = inst.get('firstDay');
  1792. var leadDays = ($.datepick.newDate(year, month, 1).getDay() - firstDay + 7) % 7;
  1793. var numWeeks = (fixedWeeks ? 6 : Math.ceil((leadDays + daysInMonth) / 7));
  1794. var showOtherMonths = inst.get('showOtherMonths');
  1795. var selectOtherMonths = inst.get('selectOtherMonths') && showOtherMonths;
  1796. var dayStatus = inst.get('dayStatus');
  1797. var minDate = (inst.pickingRange ? inst.selectedDates[0] : inst.get('minDate'));
  1798. var maxDate = inst.get('maxDate');
  1799. var rangeSelect = inst.get('rangeSelect');
  1800. var onDate = inst.get('onDate');
  1801. var showWeeks = renderer.week.indexOf('{weekOfYear}') > -1;
  1802. var calculateWeek = inst.get('calculateWeek');
  1803. var today = $.datepick.today();
  1804. var drawDate = $.datepick.newDate(year, month, 1);
  1805. $.datepick.add(drawDate, -leadDays - (fixedWeeks && (drawDate.getDay() == firstDay) ? 7 : 0), 'd');
  1806. var ts = drawDate.getTime();
  1807. // Generate weeks
  1808. var weeks = '';
  1809. for (var week = 0; week < numWeeks; week++) {
  1810. var weekOfYear = (!showWeeks ? '' : '<span class="dp' + ts + '">' +
  1811. (calculateWeek ? calculateWeek(drawDate) : 0) + '</span>');
  1812. var days = '';
  1813. for (var day = 0; day < 7; day++) {
  1814. var selected = false;
  1815. if (rangeSelect && inst.selectedDates.length > 0) {
  1816. selected = (drawDate.getTime() >= inst.selectedDates[0] &&
  1817. drawDate.getTime() <= inst.selectedDates[1]);
  1818. }
  1819. else {
  1820. for (var i = 0; i < inst.selectedDates.length; i++) {
  1821. if (inst.selectedDates[i].getTime() == drawDate.getTime()) {
  1822. selected = true;
  1823. break;
  1824. }
  1825. }
  1826. }
  1827. var dateInfo = (!onDate ? {} :
  1828. onDate.apply(target, [drawDate, drawDate.getMonth() + 1 == month]));
  1829. var selectable = (selectOtherMonths || drawDate.getMonth() + 1 == month) &&
  1830. this._isSelectable(target, drawDate, dateInfo.selectable, minDate, maxDate);
  1831. days += this._prepare(renderer.day, inst).replace(/\{day\}/g,
  1832. (selectable ? '<a href="javascript:void(0)"' : '<span') +
  1833. ' class="dp' + ts + ' ' + (dateInfo.dateClass || '') +
  1834. (selected && (selectOtherMonths || drawDate.getMonth() + 1 == month) ?
  1835. ' ' + renderer.selectedClass : '') +
  1836. (selectable ? ' ' + renderer.defaultClass : '') +
  1837. ((drawDate.getDay() || 7) < 6 ? '' : ' ' + renderer.weekendClass) +
  1838. (drawDate.getMonth() + 1 == month ? '' : ' ' + renderer.otherMonthClass) +
  1839. (drawDate.getTime() == today.getTime() && (drawDate.getMonth() + 1) == month ?
  1840. ' ' + renderer.todayClass : '') +
  1841. (drawDate.getTime() == inst.drawDate.getTime() && (drawDate.getMonth() + 1) == month ?
  1842. ' ' + renderer.highlightedClass : '') + '"' +
  1843. (dateInfo.title || (dayStatus && selectable) ? ' title="' +
  1844. (dateInfo.title || $.datepick.formatDate(
  1845. dayStatus, drawDate, inst.getConfig())) + '"' : '') + '>' +
  1846. (showOtherMonths || (drawDate.getMonth() + 1) == month ?
  1847. dateInfo.content || drawDate.getDate() : '&nbsp;') +
  1848. (selectable ? '</a>' : '</span>'));
  1849. $.datepick.add(drawDate, 1, 'd');
  1850. ts = drawDate.getTime();
  1851. }
  1852. weeks += this._prepare(renderer.week, inst).replace(/\{days\}/g, days).
  1853. replace(/\{weekOfYear\}/g, weekOfYear);
  1854. }
  1855. var monthHeader = this._prepare(renderer.month, inst).match(/\{monthHeader(:[^\}]+)?\}/);
  1856. monthHeader = (monthHeader[0].length <= 13 ? 'MM yyyy' :
  1857. monthHeader[0].substring(13, monthHeader[0].length - 1));
  1858. monthHeader = (first ? this._generateMonthSelection(
  1859. inst, year, month, minDate, maxDate, monthHeader, renderer) :
  1860. $.datepick.formatDate(monthHeader, $.datepick.newDate(year, month, 1), inst.getConfig()));
  1861. var weekHeader = this._prepare(renderer.weekHeader, inst).
  1862. replace(/\{days\}/g, this._generateDayHeaders(inst, renderer));
  1863. return this._prepare(renderer.month, inst).replace(/\{monthHeader(:[^\}]+)?\}/g, monthHeader).
  1864. replace(/\{weekHeader\}/g, weekHeader).replace(/\{weeks\}/g, weeks);
  1865. },
  1866. /* Generate the HTML for the day headers.
  1867. @param inst (object) the current instance settings
  1868. @param renderer (object) the rendering templates
  1869. @return (string) a week's worth of day headers */
  1870. _generateDayHeaders: function(inst, renderer) {
  1871. var firstDay = inst.get('firstDay');
  1872. var dayNames = inst.get('dayNames');
  1873. var dayNamesMin = inst.get('dayNamesMin');
  1874. var header = '';
  1875. for (var day = 0; day < 7; day++) {
  1876. var dow = (day + firstDay) % 7;
  1877. header += this._prepare(renderer.dayHeader, inst).replace(/\{day\}/g,
  1878. '<span class="' + this._curDoWClass + dow + '" title="' +
  1879. dayNames[dow] + '">' + dayNamesMin[dow] + '</span>');
  1880. }
  1881. return header;
  1882. },
  1883. /* Generate selection controls for month.
  1884. @param inst (object) the current instance settings
  1885. @param year (number) the year to generate
  1886. @param month (number) the month to generate
  1887. @param minDate (CDate) the minimum date allowed
  1888. @param maxDate (CDate) the maximum date allowed
  1889. @param monthHeader (string) the month/year format
  1890. @return (string) the month selection content */
  1891. _generateMonthSelection: function(inst, year, month, minDate, maxDate, monthHeader) {
  1892. if (!inst.get('changeMonth')) {
  1893. return $.datepick.formatDate(
  1894. monthHeader, $.datepick.newDate(year, month, 1), inst.getConfig());
  1895. }
  1896. // Months
  1897. var monthNames = inst.get('monthNames' + (monthHeader.match(/mm/i) ? '' : 'Short'));
  1898. var html = monthHeader.replace(/m+/i, '\\x2E').replace(/y+/i, '\\x2F');
  1899. var selector = '<select class="' + this._monthYearClass +
  1900. '" title="' + inst.get('monthStatus') + '">';
  1901. for (var m = 1; m <= 12; m++) {
  1902. if ((!minDate || $.datepick.newDate(year, m, $.datepick.daysInMonth(year, m)).
  1903. getTime() >= minDate.getTime()) &&
  1904. (!maxDate || $.datepick.newDate(year, m, 1).getTime() <= maxDate.getTime())) {
  1905. selector += '<option value="' + m + '/' + year + '"' +
  1906. (month == m ? ' selected="selected"' : '') + '>' +
  1907. monthNames[m - 1] + '</option>';
  1908. }
  1909. }
  1910. selector += '</select>';
  1911. html = html.replace(/\\x2E/, selector);
  1912. // Years
  1913. var yearRange = inst.get('yearRange');
  1914. if (yearRange == 'any') {
  1915. selector = '<select class="' + this._monthYearClass + ' ' + this._anyYearClass +
  1916. '" title="' + inst.get('yearStatus') + '">' +
  1917. '<option>' + year + '</option></select>' +
  1918. '<input class="' + this._monthYearClass + ' ' + this._curMonthClass +
  1919. month + '" value="' + year + '">';
  1920. }
  1921. else {
  1922. yearRange = yearRange.split(':');
  1923. var todayYear = $.datepick.today().getFullYear();
  1924. var start = (yearRange[0].match('c[+-].*') ? year + parseInt(yearRange[0].substring(1), 10) :
  1925. ((yearRange[0].match('[+-].*') ? todayYear : 0) + parseInt(yearRange[0], 10)));
  1926. var end = (yearRange[1].match('c[+-].*') ? year + parseInt(yearRange[1].substring(1), 10) :
  1927. ((yearRange[1].match('[+-].*') ? todayYear : 0) + parseInt(yearRange[1], 10)));
  1928. selector = '<select class="' + this._monthYearClass +
  1929. '" title="' + inst.get('yearStatus') + '">';
  1930. start = $.datepick.add($.datepick.newDate(start + 1, 1, 1), -1, 'd');
  1931. end = $.datepick.newDate(end, 1, 1);
  1932. var addYear = function(y) {
  1933. if (y != 0) {
  1934. selector += '<option value="' + month + '/' + y + '"' +
  1935. (year == y ? ' selected="selected"' : '') + '>' + y + '</option>';
  1936. }
  1937. };
  1938. if (start.getTime() < end.getTime()) {
  1939. start = (minDate && minDate.getTime() > start.getTime() ? minDate : start).getFullYear();
  1940. end = (maxDate && maxDate.getTime() < end.getTime() ? maxDate : end).getFullYear();
  1941. for (var y = start; y <= end; y++) {
  1942. addYear(y);
  1943. }
  1944. }
  1945. else {
  1946. start = (maxDate && maxDate.getTime() < start.getTime() ? maxDate : start).getFullYear();
  1947. end = (minDate && minDate.getTime() > end.getTime() ? minDate : end).getFullYear();
  1948. for (var y = start; y >= end; y--) {
  1949. addYear(y);
  1950. }
  1951. }
  1952. selector += '</select>';
  1953. }
  1954. html = html.replace(/\\x2F/, selector);
  1955. return html;
  1956. },
  1957. /* Prepare a render template for use.
  1958. Exclude popup/inline sections that are not applicable.
  1959. Localise text of the form: {l10n:name}.
  1960. @param text (string) the text to localise
  1961. @param inst (object) the current instance settings
  1962. @return (string) the localised text */
  1963. _prepare: function(text, inst) {
  1964. var replaceSection = function(type, retain) {
  1965. while (true) {
  1966. var start = text.indexOf('{' + type + ':start}');
  1967. if (start == -1) {
  1968. return;
  1969. }
  1970. var end = text.substring(start).indexOf('{' + type + ':end}');
  1971. if (end > -1) {
  1972. text = text.substring(0, start) +
  1973. (retain ? text.substr(start + type.length + 8, end - type.length - 8) : '') +
  1974. text.substring(start + end + type.length + 6);
  1975. }
  1976. }
  1977. };
  1978. replaceSection('inline', inst.inline);
  1979. replaceSection('popup', !inst.inline);
  1980. var pattern = /\{l10n:([^\}]+)\}/;
  1981. var matches = null;
  1982. while (matches = pattern.exec(text)) {
  1983. text = text.replace(matches[0], inst.get(matches[1]));
  1984. }
  1985. return text;
  1986. }
  1987. });
  1988. /* jQuery extend now ignores nulls!
  1989. @param target (object) the object to extend
  1990. @param props (object) the new settings
  1991. @return (object) the updated object */
  1992. function extendRemove(target, props) {
  1993. $.extend(target, props);
  1994. for (var name in props)
  1995. if (props[name] == null || props[name] == undefined)
  1996. target[name] = props[name];
  1997. return target;
  1998. };
  1999. /* Attach the datepicker functionality to a jQuery selection.
  2000. @param command (string) the command to run (optional, default 'attach')
  2001. @param options (object) the new settings to use for these instances (optional)
  2002. @return (jQuery) for chaining further calls */
  2003. $.fn.datepick = function(options) {
  2004. var otherArgs = Array.prototype.slice.call(arguments, 1);
  2005. if ($.inArray(options, ['getDate', 'isDisabled', 'isSelectable', 'options', 'retrieveDate']) > -1) {
  2006. return $.datepick[options].apply($.datepick, [this[0]].concat(otherArgs));
  2007. }
  2008. return this.each(function() {
  2009. if (typeof options == 'string') {
  2010. $.datepick[options].apply($.datepick, [this].concat(otherArgs));
  2011. }
  2012. else {
  2013. $.datepick._attachPicker(this, options || {});
  2014. }
  2015. });
  2016. };
  2017. /* Initialise the datepicker functionality. */
  2018. $.datepick = new Datepicker(); // singleton instance
  2019. $(function() {
  2020. $(document).mousedown($.datepick._checkExternalClick).
  2021. resize(function() { $.datepick.hide($.datepick.curInst); });
  2022. });
  2023. })(jQuery);