1
0

datepicker.js 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084
  1. /*!
  2. * jQuery UI Datepicker 1.11.4
  3. * http://jqueryui.com
  4. *
  5. * Copyright jQuery Foundation and other contributors
  6. * Released under the MIT license.
  7. * http://jquery.org/license
  8. *
  9. * http://api.jqueryui.com/datepicker/
  10. */
  11. (function( factory ) {
  12. if ( typeof define === "function" && define.amd ) {
  13. // AMD. Register as an anonymous module.
  14. define([
  15. "jquery",
  16. "./core"
  17. ], factory );
  18. } else {
  19. // Browser globals
  20. factory( jQuery );
  21. }
  22. }(function( $ ) {
  23. $.extend($.ui, { datepicker: { version: "1.11.4" } });
  24. var datepicker_instActive;
  25. function datepicker_getZindex( elem ) {
  26. var position, value;
  27. while ( elem.length && elem[ 0 ] !== document ) {
  28. // Ignore z-index if position is set to a value where z-index is ignored by the browser
  29. // This makes behavior of this function consistent across browsers
  30. // WebKit always returns auto if the element is positioned
  31. position = elem.css( "position" );
  32. if ( position === "absolute" || position === "relative" || position === "fixed" ) {
  33. // IE returns 0 when zIndex is not specified
  34. // other browsers return a string
  35. // we ignore the case of nested elements with an explicit value of 0
  36. // <div style="z-index: -10;"><div style="z-index: 0;"></div></div>
  37. value = parseInt( elem.css( "zIndex" ), 10 );
  38. if ( !isNaN( value ) && value !== 0 ) {
  39. return value;
  40. }
  41. }
  42. elem = elem.parent();
  43. }
  44. return 0;
  45. }
  46. /* Date picker manager.
  47. Use the singleton instance of this class, $.datepicker, to interact with the date picker.
  48. Settings for (groups of) date pickers are maintained in an instance object,
  49. allowing multiple different settings on the same page. */
  50. function Datepicker() {
  51. this._curInst = null; // The current instance in use
  52. this._keyEvent = false; // If the last event was a key event
  53. this._disabledInputs = []; // List of date picker inputs that have been disabled
  54. this._datepickerShowing = false; // True if the popup picker is showing , false if not
  55. this._inDialog = false; // True if showing within a "dialog", false if not
  56. this._mainDivId = "ui-datepicker-div"; // The ID of the main datepicker division
  57. this._inlineClass = "ui-datepicker-inline"; // The name of the inline marker class
  58. this._appendClass = "ui-datepicker-append"; // The name of the append marker class
  59. this._triggerClass = "ui-datepicker-trigger"; // The name of the trigger marker class
  60. this._dialogClass = "ui-datepicker-dialog"; // The name of the dialog marker class
  61. this._disableClass = "ui-datepicker-disabled"; // The name of the disabled covering marker class
  62. this._unselectableClass = "ui-datepicker-unselectable"; // The name of the unselectable cell marker class
  63. this._currentClass = "ui-datepicker-current-day"; // The name of the current day marker class
  64. this._dayOverClass = "ui-datepicker-days-cell-over"; // The name of the day hover marker class
  65. this.regional = []; // Available regional settings, indexed by language code
  66. this.regional[""] = { // Default regional settings
  67. closeText: "Done", // Display text for close link
  68. prevText: "Prev", // Display text for previous month link
  69. nextText: "Next", // Display text for next month link
  70. currentText: "Today", // Display text for current month link
  71. monthNames: ["January","February","March","April","May","June",
  72. "July","August","September","October","November","December"], // Names of months for drop-down and formatting
  73. monthNamesShort: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"], // For formatting
  74. dayNames: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"], // For formatting
  75. dayNamesShort: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"], // For formatting
  76. dayNamesMin: ["Su","Mo","Tu","We","Th","Fr","Sa"], // Column headings for days starting at Sunday
  77. weekHeader: "Wk", // Column header for week of the year
  78. dateFormat: "mm/dd/yy", // See format options on parseDate
  79. firstDay: 0, // The first day of the week, Sun = 0, Mon = 1, ...
  80. isRTL: false, // True if right-to-left language, false if left-to-right
  81. showMonthAfterYear: false, // True if the year select precedes month, false for month then year
  82. yearSuffix: "" // Additional text to append to the year in the month headers
  83. };
  84. this._defaults = { // Global defaults for all the date picker instances
  85. showOn: "focus", // "focus" for popup on focus,
  86. // "button" for trigger button, or "both" for either
  87. showAnim: "fadeIn", // Name of jQuery animation for popup
  88. showOptions: {}, // Options for enhanced animations
  89. defaultDate: null, // Used when field is blank: actual date,
  90. // +/-number for offset from today, null for today
  91. appendText: "", // Display text following the input box, e.g. showing the format
  92. buttonText: "...", // Text for trigger button
  93. buttonImage: "", // URL for trigger button image
  94. buttonImageOnly: false, // True if the image appears alone, false if it appears on a button
  95. hideIfNoPrevNext: false, // True to hide next/previous month links
  96. // if not applicable, false to just disable them
  97. navigationAsDateFormat: false, // True if date formatting applied to prev/today/next links
  98. gotoCurrent: false, // True if today link goes back to current selection instead
  99. changeMonth: false, // True if month can be selected directly, false if only prev/next
  100. changeYear: false, // True if year can be selected directly, false if only prev/next
  101. yearRange: "c-10:c+10", // Range of years to display in drop-down,
  102. // either relative to today's year (-nn:+nn), relative to currently displayed year
  103. // (c-nn:c+nn), absolute (nnnn:nnnn), or a combination of the above (nnnn:-n)
  104. showOtherMonths: false, // True to show dates in other months, false to leave blank
  105. selectOtherMonths: false, // True to allow selection of dates in other months, false for unselectable
  106. showWeek: false, // True to show week of the year, false to not show it
  107. calculateWeek: this.iso8601Week, // How to calculate the week of the year,
  108. // takes a Date and returns the number of the week for it
  109. shortYearCutoff: "+10", // Short year values < this are in the current century,
  110. // > this are in the previous century,
  111. // string value starting with "+" for current year + value
  112. minDate: null, // The earliest selectable date, or null for no limit
  113. maxDate: null, // The latest selectable date, or null for no limit
  114. duration: "fast", // Duration of display/closure
  115. beforeShowDay: null, // Function that takes a date and returns an array with
  116. // [0] = true if selectable, false if not, [1] = custom CSS class name(s) or "",
  117. // [2] = cell title (optional), e.g. $.datepicker.noWeekends
  118. beforeShow: null, // Function that takes an input field and
  119. // returns a set of custom settings for the date picker
  120. onSelect: null, // Define a callback function when a date is selected
  121. onChangeMonthYear: null, // Define a callback function when the month or year is changed
  122. onClose: null, // Define a callback function when the datepicker is closed
  123. numberOfMonths: 1, // Number of months to show at a time
  124. showCurrentAtPos: 0, // The position in multipe months at which to show the current month (starting at 0)
  125. stepMonths: 1, // Number of months to step back/forward
  126. stepBigMonths: 12, // Number of months to step back/forward for the big links
  127. altField: "", // Selector for an alternate field to store selected dates into
  128. altFormat: "", // The date format to use for the alternate field
  129. constrainInput: true, // The input is constrained by the current date format
  130. showButtonPanel: false, // True to show button panel, false to not show it
  131. autoSize: false, // True to size the input for the date format, false to leave as is
  132. disabled: false // The initial disabled state
  133. };
  134. $.extend(this._defaults, this.regional[""]);
  135. this.regional.en = $.extend( true, {}, this.regional[ "" ]);
  136. this.regional[ "en-US" ] = $.extend( true, {}, this.regional.en );
  137. this.dpDiv = datepicker_bindHover($("<div id='" + this._mainDivId + "' class='ui-datepicker ui-widget ui-widget-content ui-helper-clearfix ui-corner-all'></div>"));
  138. }
  139. $.extend(Datepicker.prototype, {
  140. /* Class name added to elements to indicate already configured with a date picker. */
  141. markerClassName: "hasDatepicker",
  142. //Keep track of the maximum number of rows displayed (see #7043)
  143. maxRows: 4,
  144. // TODO rename to "widget" when switching to widget factory
  145. _widgetDatepicker: function() {
  146. return this.dpDiv;
  147. },
  148. /* Override the default settings for all instances of the date picker.
  149. * @param settings object - the new settings to use as defaults (anonymous object)
  150. * @return the manager object
  151. */
  152. setDefaults: function(settings) {
  153. datepicker_extendRemove(this._defaults, settings || {});
  154. return this;
  155. },
  156. /* Attach the date picker to a jQuery selection.
  157. * @param target element - the target input field or division or span
  158. * @param settings object - the new settings to use for this date picker instance (anonymous)
  159. */
  160. _attachDatepicker: function(target, settings) {
  161. var nodeName, inline, inst;
  162. nodeName = target.nodeName.toLowerCase();
  163. inline = (nodeName === "div" || nodeName === "span");
  164. if (!target.id) {
  165. this.uuid += 1;
  166. target.id = "dp" + this.uuid;
  167. }
  168. inst = this._newInst($(target), inline);
  169. inst.settings = $.extend({}, settings || {});
  170. if (nodeName === "input") {
  171. this._connectDatepicker(target, inst);
  172. } else if (inline) {
  173. this._inlineDatepicker(target, inst);
  174. }
  175. },
  176. /* Create a new instance object. */
  177. _newInst: function(target, inline) {
  178. var id = target[0].id.replace(/([^A-Za-z0-9_\-])/g, "\\\\$1"); // escape jQuery meta chars
  179. return {id: id, input: target, // associated target
  180. selectedDay: 0, selectedMonth: 0, selectedYear: 0, // current selection
  181. drawMonth: 0, drawYear: 0, // month being drawn
  182. inline: inline, // is datepicker inline or not
  183. dpDiv: (!inline ? this.dpDiv : // presentation div
  184. datepicker_bindHover($("<div class='" + this._inlineClass + " ui-datepicker ui-widget ui-widget-content ui-helper-clearfix ui-corner-all'></div>")))};
  185. },
  186. /* Attach the date picker to an input field. */
  187. _connectDatepicker: function(target, inst) {
  188. var input = $(target);
  189. inst.append = $([]);
  190. inst.trigger = $([]);
  191. if (input.hasClass(this.markerClassName)) {
  192. return;
  193. }
  194. this._attachments(input, inst);
  195. input.addClass(this.markerClassName).keydown(this._doKeyDown).
  196. keypress(this._doKeyPress).keyup(this._doKeyUp);
  197. this._autoSize(inst);
  198. $.data(target, "datepicker", inst);
  199. //If disabled option is true, disable the datepicker once it has been attached to the input (see ticket #5665)
  200. if( inst.settings.disabled ) {
  201. this._disableDatepicker( target );
  202. }
  203. },
  204. /* Make attachments based on settings. */
  205. _attachments: function(input, inst) {
  206. var showOn, buttonText, buttonImage,
  207. appendText = this._get(inst, "appendText"),
  208. isRTL = this._get(inst, "isRTL");
  209. if (inst.append) {
  210. inst.append.remove();
  211. }
  212. if (appendText) {
  213. inst.append = $("<span class='" + this._appendClass + "'>" + appendText + "</span>");
  214. input[isRTL ? "before" : "after"](inst.append);
  215. }
  216. input.unbind("focus", this._showDatepicker);
  217. if (inst.trigger) {
  218. inst.trigger.remove();
  219. }
  220. showOn = this._get(inst, "showOn");
  221. if (showOn === "focus" || showOn === "both") { // pop-up date picker when in the marked field
  222. input.focus(this._showDatepicker);
  223. }
  224. if (showOn === "button" || showOn === "both") { // pop-up date picker when button clicked
  225. buttonText = this._get(inst, "buttonText");
  226. buttonImage = this._get(inst, "buttonImage");
  227. inst.trigger = $(this._get(inst, "buttonImageOnly") ?
  228. $("<img/>").addClass(this._triggerClass).
  229. attr({ src: buttonImage, alt: buttonText, title: buttonText }) :
  230. $("<button type='button'></button>").addClass(this._triggerClass).
  231. html(!buttonImage ? buttonText : $("<img/>").attr(
  232. { src:buttonImage, alt:buttonText, title:buttonText })));
  233. input[isRTL ? "before" : "after"](inst.trigger);
  234. inst.trigger.click(function() {
  235. if ($.datepicker._datepickerShowing && $.datepicker._lastInput === input[0]) {
  236. $.datepicker._hideDatepicker();
  237. } else if ($.datepicker._datepickerShowing && $.datepicker._lastInput !== input[0]) {
  238. $.datepicker._hideDatepicker();
  239. $.datepicker._showDatepicker(input[0]);
  240. } else {
  241. $.datepicker._showDatepicker(input[0]);
  242. }
  243. return false;
  244. });
  245. }
  246. },
  247. /* Apply the maximum length for the date format. */
  248. _autoSize: function(inst) {
  249. if (this._get(inst, "autoSize") && !inst.inline) {
  250. var findMax, max, maxI, i,
  251. date = new Date(2009, 12 - 1, 20), // Ensure double digits
  252. dateFormat = this._get(inst, "dateFormat");
  253. if (dateFormat.match(/[DM]/)) {
  254. findMax = function(names) {
  255. max = 0;
  256. maxI = 0;
  257. for (i = 0; i < names.length; i++) {
  258. if (names[i].length > max) {
  259. max = names[i].length;
  260. maxI = i;
  261. }
  262. }
  263. return maxI;
  264. };
  265. date.setMonth(findMax(this._get(inst, (dateFormat.match(/MM/) ?
  266. "monthNames" : "monthNamesShort"))));
  267. date.setDate(findMax(this._get(inst, (dateFormat.match(/DD/) ?
  268. "dayNames" : "dayNamesShort"))) + 20 - date.getDay());
  269. }
  270. inst.input.attr("size", this._formatDate(inst, date).length);
  271. }
  272. },
  273. /* Attach an inline date picker to a div. */
  274. _inlineDatepicker: function(target, inst) {
  275. var divSpan = $(target);
  276. if (divSpan.hasClass(this.markerClassName)) {
  277. return;
  278. }
  279. divSpan.addClass(this.markerClassName).append(inst.dpDiv);
  280. $.data(target, "datepicker", inst);
  281. this._setDate(inst, this._getDefaultDate(inst), true);
  282. this._updateDatepicker(inst);
  283. this._updateAlternate(inst);
  284. //If disabled option is true, disable the datepicker before showing it (see ticket #5665)
  285. if( inst.settings.disabled ) {
  286. this._disableDatepicker( target );
  287. }
  288. // Set display:block in place of inst.dpDiv.show() which won't work on disconnected elements
  289. // http://bugs.jqueryui.com/ticket/7552 - A Datepicker created on a detached div has zero height
  290. inst.dpDiv.css( "display", "block" );
  291. },
  292. /* Pop-up the date picker in a "dialog" box.
  293. * @param input element - ignored
  294. * @param date string or Date - the initial date to display
  295. * @param onSelect function - the function to call when a date is selected
  296. * @param settings object - update the dialog date picker instance's settings (anonymous object)
  297. * @param pos int[2] - coordinates for the dialog's position within the screen or
  298. * event - with x/y coordinates or
  299. * leave empty for default (screen centre)
  300. * @return the manager object
  301. */
  302. _dialogDatepicker: function(input, date, onSelect, settings, pos) {
  303. var id, browserWidth, browserHeight, scrollX, scrollY,
  304. inst = this._dialogInst; // internal instance
  305. if (!inst) {
  306. this.uuid += 1;
  307. id = "dp" + this.uuid;
  308. this._dialogInput = $("<input type='text' id='" + id +
  309. "' style='position: absolute; top: -100px; width: 0px;'/>");
  310. this._dialogInput.keydown(this._doKeyDown);
  311. $("body").append(this._dialogInput);
  312. inst = this._dialogInst = this._newInst(this._dialogInput, false);
  313. inst.settings = {};
  314. $.data(this._dialogInput[0], "datepicker", inst);
  315. }
  316. datepicker_extendRemove(inst.settings, settings || {});
  317. date = (date && date.constructor === Date ? this._formatDate(inst, date) : date);
  318. this._dialogInput.val(date);
  319. this._pos = (pos ? (pos.length ? pos : [pos.pageX, pos.pageY]) : null);
  320. if (!this._pos) {
  321. browserWidth = document.documentElement.clientWidth;
  322. browserHeight = document.documentElement.clientHeight;
  323. scrollX = document.documentElement.scrollLeft || document.body.scrollLeft;
  324. scrollY = document.documentElement.scrollTop || document.body.scrollTop;
  325. this._pos = // should use actual width/height below
  326. [(browserWidth / 2) - 100 + scrollX, (browserHeight / 2) - 150 + scrollY];
  327. }
  328. // move input on screen for focus, but hidden behind dialog
  329. this._dialogInput.css("left", (this._pos[0] + 20) + "px").css("top", this._pos[1] + "px");
  330. inst.settings.onSelect = onSelect;
  331. this._inDialog = true;
  332. this.dpDiv.addClass(this._dialogClass);
  333. this._showDatepicker(this._dialogInput[0]);
  334. if ($.blockUI) {
  335. $.blockUI(this.dpDiv);
  336. }
  337. $.data(this._dialogInput[0], "datepicker", inst);
  338. return this;
  339. },
  340. /* Detach a datepicker from its control.
  341. * @param target element - the target input field or division or span
  342. */
  343. _destroyDatepicker: function(target) {
  344. var nodeName,
  345. $target = $(target),
  346. inst = $.data(target, "datepicker");
  347. if (!$target.hasClass(this.markerClassName)) {
  348. return;
  349. }
  350. nodeName = target.nodeName.toLowerCase();
  351. $.removeData(target, "datepicker");
  352. if (nodeName === "input") {
  353. inst.append.remove();
  354. inst.trigger.remove();
  355. $target.removeClass(this.markerClassName).
  356. unbind("focus", this._showDatepicker).
  357. unbind("keydown", this._doKeyDown).
  358. unbind("keypress", this._doKeyPress).
  359. unbind("keyup", this._doKeyUp);
  360. } else if (nodeName === "div" || nodeName === "span") {
  361. $target.removeClass(this.markerClassName).empty();
  362. }
  363. if ( datepicker_instActive === inst ) {
  364. datepicker_instActive = null;
  365. }
  366. },
  367. /* Enable the date picker to a jQuery selection.
  368. * @param target element - the target input field or division or span
  369. */
  370. _enableDatepicker: function(target) {
  371. var nodeName, inline,
  372. $target = $(target),
  373. inst = $.data(target, "datepicker");
  374. if (!$target.hasClass(this.markerClassName)) {
  375. return;
  376. }
  377. nodeName = target.nodeName.toLowerCase();
  378. if (nodeName === "input") {
  379. target.disabled = false;
  380. inst.trigger.filter("button").
  381. each(function() { this.disabled = false; }).end().
  382. filter("img").css({opacity: "1.0", cursor: ""});
  383. } else if (nodeName === "div" || nodeName === "span") {
  384. inline = $target.children("." + this._inlineClass);
  385. inline.children().removeClass("ui-state-disabled");
  386. inline.find("select.ui-datepicker-month, select.ui-datepicker-year").
  387. prop("disabled", false);
  388. }
  389. this._disabledInputs = $.map(this._disabledInputs,
  390. function(value) { return (value === target ? null : value); }); // delete entry
  391. },
  392. /* Disable the date picker to a jQuery selection.
  393. * @param target element - the target input field or division or span
  394. */
  395. _disableDatepicker: function(target) {
  396. var nodeName, inline,
  397. $target = $(target),
  398. inst = $.data(target, "datepicker");
  399. if (!$target.hasClass(this.markerClassName)) {
  400. return;
  401. }
  402. nodeName = target.nodeName.toLowerCase();
  403. if (nodeName === "input") {
  404. target.disabled = true;
  405. inst.trigger.filter("button").
  406. each(function() { this.disabled = true; }).end().
  407. filter("img").css({opacity: "0.5", cursor: "default"});
  408. } else if (nodeName === "div" || nodeName === "span") {
  409. inline = $target.children("." + this._inlineClass);
  410. inline.children().addClass("ui-state-disabled");
  411. inline.find("select.ui-datepicker-month, select.ui-datepicker-year").
  412. prop("disabled", true);
  413. }
  414. this._disabledInputs = $.map(this._disabledInputs,
  415. function(value) { return (value === target ? null : value); }); // delete entry
  416. this._disabledInputs[this._disabledInputs.length] = target;
  417. },
  418. /* Is the first field in a jQuery collection disabled as a datepicker?
  419. * @param target element - the target input field or division or span
  420. * @return boolean - true if disabled, false if enabled
  421. */
  422. _isDisabledDatepicker: function(target) {
  423. if (!target) {
  424. return false;
  425. }
  426. for (var i = 0; i < this._disabledInputs.length; i++) {
  427. if (this._disabledInputs[i] === target) {
  428. return true;
  429. }
  430. }
  431. return false;
  432. },
  433. /* Retrieve the instance data for the target control.
  434. * @param target element - the target input field or division or span
  435. * @return object - the associated instance data
  436. * @throws error if a jQuery problem getting data
  437. */
  438. _getInst: function(target) {
  439. try {
  440. return $.data(target, "datepicker");
  441. }
  442. catch (err) {
  443. throw "Missing instance data for this datepicker";
  444. }
  445. },
  446. /* Update or retrieve the settings for a date picker attached to an input field or division.
  447. * @param target element - the target input field or division or span
  448. * @param name object - the new settings to update or
  449. * string - the name of the setting to change or retrieve,
  450. * when retrieving also "all" for all instance settings or
  451. * "defaults" for all global defaults
  452. * @param value any - the new value for the setting
  453. * (omit if above is an object or to retrieve a value)
  454. */
  455. _optionDatepicker: function(target, name, value) {
  456. var settings, date, minDate, maxDate,
  457. inst = this._getInst(target);
  458. if (arguments.length === 2 && typeof name === "string") {
  459. return (name === "defaults" ? $.extend({}, $.datepicker._defaults) :
  460. (inst ? (name === "all" ? $.extend({}, inst.settings) :
  461. this._get(inst, name)) : null));
  462. }
  463. settings = name || {};
  464. if (typeof name === "string") {
  465. settings = {};
  466. settings[name] = value;
  467. }
  468. if (inst) {
  469. if (this._curInst === inst) {
  470. this._hideDatepicker();
  471. }
  472. date = this._getDateDatepicker(target, true);
  473. minDate = this._getMinMaxDate(inst, "min");
  474. maxDate = this._getMinMaxDate(inst, "max");
  475. datepicker_extendRemove(inst.settings, settings);
  476. // reformat the old minDate/maxDate values if dateFormat changes and a new minDate/maxDate isn't provided
  477. if (minDate !== null && settings.dateFormat !== undefined && settings.minDate === undefined) {
  478. inst.settings.minDate = this._formatDate(inst, minDate);
  479. }
  480. if (maxDate !== null && settings.dateFormat !== undefined && settings.maxDate === undefined) {
  481. inst.settings.maxDate = this._formatDate(inst, maxDate);
  482. }
  483. if ( "disabled" in settings ) {
  484. if ( settings.disabled ) {
  485. this._disableDatepicker(target);
  486. } else {
  487. this._enableDatepicker(target);
  488. }
  489. }
  490. this._attachments($(target), inst);
  491. this._autoSize(inst);
  492. this._setDate(inst, date);
  493. this._updateAlternate(inst);
  494. this._updateDatepicker(inst);
  495. }
  496. },
  497. // change method deprecated
  498. _changeDatepicker: function(target, name, value) {
  499. this._optionDatepicker(target, name, value);
  500. },
  501. /* Redraw the date picker attached to an input field or division.
  502. * @param target element - the target input field or division or span
  503. */
  504. _refreshDatepicker: function(target) {
  505. var inst = this._getInst(target);
  506. if (inst) {
  507. this._updateDatepicker(inst);
  508. }
  509. },
  510. /* Set the dates for a jQuery selection.
  511. * @param target element - the target input field or division or span
  512. * @param date Date - the new date
  513. */
  514. _setDateDatepicker: function(target, date) {
  515. var inst = this._getInst(target);
  516. if (inst) {
  517. this._setDate(inst, date);
  518. this._updateDatepicker(inst);
  519. this._updateAlternate(inst);
  520. }
  521. },
  522. /* Get the date(s) for the first entry in a jQuery selection.
  523. * @param target element - the target input field or division or span
  524. * @param noDefault boolean - true if no default date is to be used
  525. * @return Date - the current date
  526. */
  527. _getDateDatepicker: function(target, noDefault) {
  528. var inst = this._getInst(target);
  529. if (inst && !inst.inline) {
  530. this._setDateFromField(inst, noDefault);
  531. }
  532. return (inst ? this._getDate(inst) : null);
  533. },
  534. /* Handle keystrokes. */
  535. _doKeyDown: function(event) {
  536. var onSelect, dateStr, sel,
  537. inst = $.datepicker._getInst(event.target),
  538. handled = true,
  539. isRTL = inst.dpDiv.is(".ui-datepicker-rtl");
  540. inst._keyEvent = true;
  541. if ($.datepicker._datepickerShowing) {
  542. switch (event.keyCode) {
  543. case 9: $.datepicker._hideDatepicker();
  544. handled = false;
  545. break; // hide on tab out
  546. case 13: sel = $("td." + $.datepicker._dayOverClass + ":not(." +
  547. $.datepicker._currentClass + ")", inst.dpDiv);
  548. if (sel[0]) {
  549. $.datepicker._selectDay(event.target, inst.selectedMonth, inst.selectedYear, sel[0]);
  550. }
  551. onSelect = $.datepicker._get(inst, "onSelect");
  552. if (onSelect) {
  553. dateStr = $.datepicker._formatDate(inst);
  554. // trigger custom callback
  555. onSelect.apply((inst.input ? inst.input[0] : null), [dateStr, inst]);
  556. } else {
  557. $.datepicker._hideDatepicker();
  558. }
  559. return false; // don't submit the form
  560. case 27: $.datepicker._hideDatepicker();
  561. break; // hide on escape
  562. case 33: $.datepicker._adjustDate(event.target, (event.ctrlKey ?
  563. -$.datepicker._get(inst, "stepBigMonths") :
  564. -$.datepicker._get(inst, "stepMonths")), "M");
  565. break; // previous month/year on page up/+ ctrl
  566. case 34: $.datepicker._adjustDate(event.target, (event.ctrlKey ?
  567. +$.datepicker._get(inst, "stepBigMonths") :
  568. +$.datepicker._get(inst, "stepMonths")), "M");
  569. break; // next month/year on page down/+ ctrl
  570. case 35: if (event.ctrlKey || event.metaKey) {
  571. $.datepicker._clearDate(event.target);
  572. }
  573. handled = event.ctrlKey || event.metaKey;
  574. break; // clear on ctrl or command +end
  575. case 36: if (event.ctrlKey || event.metaKey) {
  576. $.datepicker._gotoToday(event.target);
  577. }
  578. handled = event.ctrlKey || event.metaKey;
  579. break; // current on ctrl or command +home
  580. case 37: if (event.ctrlKey || event.metaKey) {
  581. $.datepicker._adjustDate(event.target, (isRTL ? +1 : -1), "D");
  582. }
  583. handled = event.ctrlKey || event.metaKey;
  584. // -1 day on ctrl or command +left
  585. if (event.originalEvent.altKey) {
  586. $.datepicker._adjustDate(event.target, (event.ctrlKey ?
  587. -$.datepicker._get(inst, "stepBigMonths") :
  588. -$.datepicker._get(inst, "stepMonths")), "M");
  589. }
  590. // next month/year on alt +left on Mac
  591. break;
  592. case 38: if (event.ctrlKey || event.metaKey) {
  593. $.datepicker._adjustDate(event.target, -7, "D");
  594. }
  595. handled = event.ctrlKey || event.metaKey;
  596. break; // -1 week on ctrl or command +up
  597. case 39: if (event.ctrlKey || event.metaKey) {
  598. $.datepicker._adjustDate(event.target, (isRTL ? -1 : +1), "D");
  599. }
  600. handled = event.ctrlKey || event.metaKey;
  601. // +1 day on ctrl or command +right
  602. if (event.originalEvent.altKey) {
  603. $.datepicker._adjustDate(event.target, (event.ctrlKey ?
  604. +$.datepicker._get(inst, "stepBigMonths") :
  605. +$.datepicker._get(inst, "stepMonths")), "M");
  606. }
  607. // next month/year on alt +right
  608. break;
  609. case 40: if (event.ctrlKey || event.metaKey) {
  610. $.datepicker._adjustDate(event.target, +7, "D");
  611. }
  612. handled = event.ctrlKey || event.metaKey;
  613. break; // +1 week on ctrl or command +down
  614. default: handled = false;
  615. }
  616. } else if (event.keyCode === 36 && event.ctrlKey) { // display the date picker on ctrl+home
  617. $.datepicker._showDatepicker(this);
  618. } else {
  619. handled = false;
  620. }
  621. if (handled) {
  622. event.preventDefault();
  623. event.stopPropagation();
  624. }
  625. },
  626. /* Filter entered characters - based on date format. */
  627. _doKeyPress: function(event) {
  628. var chars, chr,
  629. inst = $.datepicker._getInst(event.target);
  630. if ($.datepicker._get(inst, "constrainInput")) {
  631. chars = $.datepicker._possibleChars($.datepicker._get(inst, "dateFormat"));
  632. chr = String.fromCharCode(event.charCode == null ? event.keyCode : event.charCode);
  633. return event.ctrlKey || event.metaKey || (chr < " " || !chars || chars.indexOf(chr) > -1);
  634. }
  635. },
  636. /* Synchronise manual entry and field/alternate field. */
  637. _doKeyUp: function(event) {
  638. var date,
  639. inst = $.datepicker._getInst(event.target);
  640. if (inst.input.val() !== inst.lastVal) {
  641. try {
  642. date = $.datepicker.parseDate($.datepicker._get(inst, "dateFormat"),
  643. (inst.input ? inst.input.val() : null),
  644. $.datepicker._getFormatConfig(inst));
  645. if (date) { // only if valid
  646. $.datepicker._setDateFromField(inst);
  647. $.datepicker._updateAlternate(inst);
  648. $.datepicker._updateDatepicker(inst);
  649. }
  650. }
  651. catch (err) {
  652. }
  653. }
  654. return true;
  655. },
  656. /* Pop-up the date picker for a given input field.
  657. * If false returned from beforeShow event handler do not show.
  658. * @param input element - the input field attached to the date picker or
  659. * event - if triggered by focus
  660. */
  661. _showDatepicker: function(input) {
  662. input = input.target || input;
  663. if (input.nodeName.toLowerCase() !== "input") { // find from button/image trigger
  664. input = $("input", input.parentNode)[0];
  665. }
  666. if ($.datepicker._isDisabledDatepicker(input) || $.datepicker._lastInput === input) { // already here
  667. return;
  668. }
  669. var inst, beforeShow, beforeShowSettings, isFixed,
  670. offset, showAnim, duration;
  671. inst = $.datepicker._getInst(input);
  672. if ($.datepicker._curInst && $.datepicker._curInst !== inst) {
  673. $.datepicker._curInst.dpDiv.stop(true, true);
  674. if ( inst && $.datepicker._datepickerShowing ) {
  675. $.datepicker._hideDatepicker( $.datepicker._curInst.input[0] );
  676. }
  677. }
  678. beforeShow = $.datepicker._get(inst, "beforeShow");
  679. beforeShowSettings = beforeShow ? beforeShow.apply(input, [input, inst]) : {};
  680. if(beforeShowSettings === false){
  681. return;
  682. }
  683. datepicker_extendRemove(inst.settings, beforeShowSettings);
  684. inst.lastVal = null;
  685. $.datepicker._lastInput = input;
  686. $.datepicker._setDateFromField(inst);
  687. if ($.datepicker._inDialog) { // hide cursor
  688. input.value = "";
  689. }
  690. if (!$.datepicker._pos) { // position below input
  691. $.datepicker._pos = $.datepicker._findPos(input);
  692. $.datepicker._pos[1] += input.offsetHeight; // add the height
  693. }
  694. isFixed = false;
  695. $(input).parents().each(function() {
  696. isFixed |= $(this).css("position") === "fixed";
  697. return !isFixed;
  698. });
  699. offset = {left: $.datepicker._pos[0], top: $.datepicker._pos[1]};
  700. $.datepicker._pos = null;
  701. //to avoid flashes on Firefox
  702. inst.dpDiv.empty();
  703. // determine sizing offscreen
  704. inst.dpDiv.css({position: "absolute", display: "block", top: "-1000px"});
  705. $.datepicker._updateDatepicker(inst);
  706. // fix width for dynamic number of date pickers
  707. // and adjust position before showing
  708. offset = $.datepicker._checkOffset(inst, offset, isFixed);
  709. inst.dpDiv.css({position: ($.datepicker._inDialog && $.blockUI ?
  710. "static" : (isFixed ? "fixed" : "absolute")), display: "none",
  711. left: offset.left + "px", top: offset.top + "px"});
  712. if (!inst.inline) {
  713. showAnim = $.datepicker._get(inst, "showAnim");
  714. duration = $.datepicker._get(inst, "duration");
  715. inst.dpDiv.css( "z-index", datepicker_getZindex( $( input ) ) + 1 );
  716. $.datepicker._datepickerShowing = true;
  717. if ( $.effects && $.effects.effect[ showAnim ] ) {
  718. inst.dpDiv.show(showAnim, $.datepicker._get(inst, "showOptions"), duration);
  719. } else {
  720. inst.dpDiv[showAnim || "show"](showAnim ? duration : null);
  721. }
  722. if ( $.datepicker._shouldFocusInput( inst ) ) {
  723. inst.input.focus();
  724. }
  725. $.datepicker._curInst = inst;
  726. }
  727. },
  728. /* Generate the date picker content. */
  729. _updateDatepicker: function(inst) {
  730. this.maxRows = 4; //Reset the max number of rows being displayed (see #7043)
  731. datepicker_instActive = inst; // for delegate hover events
  732. inst.dpDiv.empty().append(this._generateHTML(inst));
  733. this._attachHandlers(inst);
  734. var origyearshtml,
  735. numMonths = this._getNumberOfMonths(inst),
  736. cols = numMonths[1],
  737. width = 17,
  738. activeCell = inst.dpDiv.find( "." + this._dayOverClass + " a" );
  739. if ( activeCell.length > 0 ) {
  740. datepicker_handleMouseover.apply( activeCell.get( 0 ) );
  741. }
  742. inst.dpDiv.removeClass("ui-datepicker-multi-2 ui-datepicker-multi-3 ui-datepicker-multi-4").width("");
  743. if (cols > 1) {
  744. inst.dpDiv.addClass("ui-datepicker-multi-" + cols).css("width", (width * cols) + "em");
  745. }
  746. inst.dpDiv[(numMonths[0] !== 1 || numMonths[1] !== 1 ? "add" : "remove") +
  747. "Class"]("ui-datepicker-multi");
  748. inst.dpDiv[(this._get(inst, "isRTL") ? "add" : "remove") +
  749. "Class"]("ui-datepicker-rtl");
  750. if (inst === $.datepicker._curInst && $.datepicker._datepickerShowing && $.datepicker._shouldFocusInput( inst ) ) {
  751. inst.input.focus();
  752. }
  753. // deffered render of the years select (to avoid flashes on Firefox)
  754. if( inst.yearshtml ){
  755. origyearshtml = inst.yearshtml;
  756. setTimeout(function(){
  757. //assure that inst.yearshtml didn't change.
  758. if( origyearshtml === inst.yearshtml && inst.yearshtml ){
  759. inst.dpDiv.find("select.ui-datepicker-year:first").replaceWith(inst.yearshtml);
  760. }
  761. origyearshtml = inst.yearshtml = null;
  762. }, 0);
  763. }
  764. },
  765. // #6694 - don't focus the input if it's already focused
  766. // this breaks the change event in IE
  767. // Support: IE and jQuery <1.9
  768. _shouldFocusInput: function( inst ) {
  769. return inst.input && inst.input.is( ":visible" ) && !inst.input.is( ":disabled" ) && !inst.input.is( ":focus" );
  770. },
  771. /* Check positioning to remain on screen. */
  772. _checkOffset: function(inst, offset, isFixed) {
  773. var dpWidth = inst.dpDiv.outerWidth(),
  774. dpHeight = inst.dpDiv.outerHeight(),
  775. inputWidth = inst.input ? inst.input.outerWidth() : 0,
  776. inputHeight = inst.input ? inst.input.outerHeight() : 0,
  777. viewWidth = document.documentElement.clientWidth + (isFixed ? 0 : $(document).scrollLeft()),
  778. viewHeight = document.documentElement.clientHeight + (isFixed ? 0 : $(document).scrollTop());
  779. offset.left -= (this._get(inst, "isRTL") ? (dpWidth - inputWidth) : 0);
  780. offset.left -= (isFixed && offset.left === inst.input.offset().left) ? $(document).scrollLeft() : 0;
  781. offset.top -= (isFixed && offset.top === (inst.input.offset().top + inputHeight)) ? $(document).scrollTop() : 0;
  782. // now check if datepicker is showing outside window viewport - move to a better place if so.
  783. offset.left -= Math.min(offset.left, (offset.left + dpWidth > viewWidth && viewWidth > dpWidth) ?
  784. Math.abs(offset.left + dpWidth - viewWidth) : 0);
  785. offset.top -= Math.min(offset.top, (offset.top + dpHeight > viewHeight && viewHeight > dpHeight) ?
  786. Math.abs(dpHeight + inputHeight) : 0);
  787. return offset;
  788. },
  789. /* Find an object's position on the screen. */
  790. _findPos: function(obj) {
  791. var position,
  792. inst = this._getInst(obj),
  793. isRTL = this._get(inst, "isRTL");
  794. while (obj && (obj.type === "hidden" || obj.nodeType !== 1 || $.expr.filters.hidden(obj))) {
  795. obj = obj[isRTL ? "previousSibling" : "nextSibling"];
  796. }
  797. position = $(obj).offset();
  798. return [position.left, position.top];
  799. },
  800. /* Hide the date picker from view.
  801. * @param input element - the input field attached to the date picker
  802. */
  803. _hideDatepicker: function(input) {
  804. var showAnim, duration, postProcess, onClose,
  805. inst = this._curInst;
  806. if (!inst || (input && inst !== $.data(input, "datepicker"))) {
  807. return;
  808. }
  809. if (this._datepickerShowing) {
  810. showAnim = this._get(inst, "showAnim");
  811. duration = this._get(inst, "duration");
  812. postProcess = function() {
  813. $.datepicker._tidyDialog(inst);
  814. };
  815. // DEPRECATED: after BC for 1.8.x $.effects[ showAnim ] is not needed
  816. if ( $.effects && ( $.effects.effect[ showAnim ] || $.effects[ showAnim ] ) ) {
  817. inst.dpDiv.hide(showAnim, $.datepicker._get(inst, "showOptions"), duration, postProcess);
  818. } else {
  819. inst.dpDiv[(showAnim === "slideDown" ? "slideUp" :
  820. (showAnim === "fadeIn" ? "fadeOut" : "hide"))]((showAnim ? duration : null), postProcess);
  821. }
  822. if (!showAnim) {
  823. postProcess();
  824. }
  825. this._datepickerShowing = false;
  826. onClose = this._get(inst, "onClose");
  827. if (onClose) {
  828. onClose.apply((inst.input ? inst.input[0] : null), [(inst.input ? inst.input.val() : ""), inst]);
  829. }
  830. this._lastInput = null;
  831. if (this._inDialog) {
  832. this._dialogInput.css({ position: "absolute", left: "0", top: "-100px" });
  833. if ($.blockUI) {
  834. $.unblockUI();
  835. $("body").append(this.dpDiv);
  836. }
  837. }
  838. this._inDialog = false;
  839. }
  840. },
  841. /* Tidy up after a dialog display. */
  842. _tidyDialog: function(inst) {
  843. inst.dpDiv.removeClass(this._dialogClass).unbind(".ui-datepicker-calendar");
  844. },
  845. /* Close date picker if clicked elsewhere. */
  846. _checkExternalClick: function(event) {
  847. if (!$.datepicker._curInst) {
  848. return;
  849. }
  850. var $target = $(event.target),
  851. inst = $.datepicker._getInst($target[0]);
  852. if ( ( ( $target[0].id !== $.datepicker._mainDivId &&
  853. $target.parents("#" + $.datepicker._mainDivId).length === 0 &&
  854. !$target.hasClass($.datepicker.markerClassName) &&
  855. !$target.closest("." + $.datepicker._triggerClass).length &&
  856. $.datepicker._datepickerShowing && !($.datepicker._inDialog && $.blockUI) ) ) ||
  857. ( $target.hasClass($.datepicker.markerClassName) && $.datepicker._curInst !== inst ) ) {
  858. $.datepicker._hideDatepicker();
  859. }
  860. },
  861. /* Adjust one of the date sub-fields. */
  862. _adjustDate: function(id, offset, period) {
  863. var target = $(id),
  864. inst = this._getInst(target[0]);
  865. if (this._isDisabledDatepicker(target[0])) {
  866. return;
  867. }
  868. this._adjustInstDate(inst, offset +
  869. (period === "M" ? this._get(inst, "showCurrentAtPos") : 0), // undo positioning
  870. period);
  871. this._updateDatepicker(inst);
  872. },
  873. /* Action for current link. */
  874. _gotoToday: function(id) {
  875. var date,
  876. target = $(id),
  877. inst = this._getInst(target[0]);
  878. if (this._get(inst, "gotoCurrent") && inst.currentDay) {
  879. inst.selectedDay = inst.currentDay;
  880. inst.drawMonth = inst.selectedMonth = inst.currentMonth;
  881. inst.drawYear = inst.selectedYear = inst.currentYear;
  882. } else {
  883. date = new Date();
  884. inst.selectedDay = date.getDate();
  885. inst.drawMonth = inst.selectedMonth = date.getMonth();
  886. inst.drawYear = inst.selectedYear = date.getFullYear();
  887. }
  888. this._notifyChange(inst);
  889. this._adjustDate(target);
  890. },
  891. /* Action for selecting a new month/year. */
  892. _selectMonthYear: function(id, select, period) {
  893. var target = $(id),
  894. inst = this._getInst(target[0]);
  895. inst["selected" + (period === "M" ? "Month" : "Year")] =
  896. inst["draw" + (period === "M" ? "Month" : "Year")] =
  897. parseInt(select.options[select.selectedIndex].value,10);
  898. this._notifyChange(inst);
  899. this._adjustDate(target);
  900. },
  901. /* Action for selecting a day. */
  902. _selectDay: function(id, month, year, td) {
  903. var inst,
  904. target = $(id);
  905. if ($(td).hasClass(this._unselectableClass) || this._isDisabledDatepicker(target[0])) {
  906. return;
  907. }
  908. inst = this._getInst(target[0]);
  909. inst.selectedDay = inst.currentDay = $("a", td).html();
  910. inst.selectedMonth = inst.currentMonth = month;
  911. inst.selectedYear = inst.currentYear = year;
  912. this._selectDate(id, this._formatDate(inst,
  913. inst.currentDay, inst.currentMonth, inst.currentYear));
  914. },
  915. /* Erase the input field and hide the date picker. */
  916. _clearDate: function(id) {
  917. var target = $(id);
  918. this._selectDate(target, "");
  919. },
  920. /* Update the input field with the selected date. */
  921. _selectDate: function(id, dateStr) {
  922. var onSelect,
  923. target = $(id),
  924. inst = this._getInst(target[0]);
  925. dateStr = (dateStr != null ? dateStr : this._formatDate(inst));
  926. if (inst.input) {
  927. inst.input.val(dateStr);
  928. }
  929. this._updateAlternate(inst);
  930. onSelect = this._get(inst, "onSelect");
  931. if (onSelect) {
  932. onSelect.apply((inst.input ? inst.input[0] : null), [dateStr, inst]); // trigger custom callback
  933. } else if (inst.input) {
  934. inst.input.trigger("change"); // fire the change event
  935. }
  936. if (inst.inline){
  937. this._updateDatepicker(inst);
  938. } else {
  939. this._hideDatepicker();
  940. this._lastInput = inst.input[0];
  941. if (typeof(inst.input[0]) !== "object") {
  942. inst.input.focus(); // restore focus
  943. }
  944. this._lastInput = null;
  945. }
  946. },
  947. /* Update any alternate field to synchronise with the main field. */
  948. _updateAlternate: function(inst) {
  949. var altFormat, date, dateStr,
  950. altField = this._get(inst, "altField");
  951. if (altField) { // update alternate field too
  952. altFormat = this._get(inst, "altFormat") || this._get(inst, "dateFormat");
  953. date = this._getDate(inst);
  954. dateStr = this.formatDate(altFormat, date, this._getFormatConfig(inst));
  955. $(altField).each(function() { $(this).val(dateStr); });
  956. }
  957. },
  958. /* Set as beforeShowDay function to prevent selection of weekends.
  959. * @param date Date - the date to customise
  960. * @return [boolean, string] - is this date selectable?, what is its CSS class?
  961. */
  962. noWeekends: function(date) {
  963. var day = date.getDay();
  964. return [(day > 0 && day < 6), ""];
  965. },
  966. /* Set as calculateWeek to determine the week of the year based on the ISO 8601 definition.
  967. * @param date Date - the date to get the week for
  968. * @return number - the number of the week within the year that contains this date
  969. */
  970. iso8601Week: function(date) {
  971. var time,
  972. checkDate = new Date(date.getTime());
  973. // Find Thursday of this week starting on Monday
  974. checkDate.setDate(checkDate.getDate() + 4 - (checkDate.getDay() || 7));
  975. time = checkDate.getTime();
  976. checkDate.setMonth(0); // Compare with Jan 1
  977. checkDate.setDate(1);
  978. return Math.floor(Math.round((time - checkDate) / 86400000) / 7) + 1;
  979. },
  980. /* Parse a string value into a date object.
  981. * See formatDate below for the possible formats.
  982. *
  983. * @param format string - the expected format of the date
  984. * @param value string - the date in the above format
  985. * @param settings Object - attributes include:
  986. * shortYearCutoff number - the cutoff year for determining the century (optional)
  987. * dayNamesShort string[7] - abbreviated names of the days from Sunday (optional)
  988. * dayNames string[7] - names of the days from Sunday (optional)
  989. * monthNamesShort string[12] - abbreviated names of the months (optional)
  990. * monthNames string[12] - names of the months (optional)
  991. * @return Date - the extracted date value or null if value is blank
  992. */
  993. parseDate: function (format, value, settings) {
  994. if (format == null || value == null) {
  995. throw "Invalid arguments";
  996. }
  997. value = (typeof value === "object" ? value.toString() : value + "");
  998. if (value === "") {
  999. return null;
  1000. }
  1001. var iFormat, dim, extra,
  1002. iValue = 0,
  1003. shortYearCutoffTemp = (settings ? settings.shortYearCutoff : null) || this._defaults.shortYearCutoff,
  1004. shortYearCutoff = (typeof shortYearCutoffTemp !== "string" ? shortYearCutoffTemp :
  1005. new Date().getFullYear() % 100 + parseInt(shortYearCutoffTemp, 10)),
  1006. dayNamesShort = (settings ? settings.dayNamesShort : null) || this._defaults.dayNamesShort,
  1007. dayNames = (settings ? settings.dayNames : null) || this._defaults.dayNames,
  1008. monthNamesShort = (settings ? settings.monthNamesShort : null) || this._defaults.monthNamesShort,
  1009. monthNames = (settings ? settings.monthNames : null) || this._defaults.monthNames,
  1010. year = -1,
  1011. month = -1,
  1012. day = -1,
  1013. doy = -1,
  1014. literal = false,
  1015. date,
  1016. // Check whether a format character is doubled
  1017. lookAhead = function(match) {
  1018. var matches = (iFormat + 1 < format.length && format.charAt(iFormat + 1) === match);
  1019. if (matches) {
  1020. iFormat++;
  1021. }
  1022. return matches;
  1023. },
  1024. // Extract a number from the string value
  1025. getNumber = function(match) {
  1026. var isDoubled = lookAhead(match),
  1027. size = (match === "@" ? 14 : (match === "!" ? 20 :
  1028. (match === "y" && isDoubled ? 4 : (match === "o" ? 3 : 2)))),
  1029. minSize = (match === "y" ? size : 1),
  1030. digits = new RegExp("^\\d{" + minSize + "," + size + "}"),
  1031. num = value.substring(iValue).match(digits);
  1032. if (!num) {
  1033. throw "Missing number at position " + iValue;
  1034. }
  1035. iValue += num[0].length;
  1036. return parseInt(num[0], 10);
  1037. },
  1038. // Extract a name from the string value and convert to an index
  1039. getName = function(match, shortNames, longNames) {
  1040. var index = -1,
  1041. names = $.map(lookAhead(match) ? longNames : shortNames, function (v, k) {
  1042. return [ [k, v] ];
  1043. }).sort(function (a, b) {
  1044. return -(a[1].length - b[1].length);
  1045. });
  1046. $.each(names, function (i, pair) {
  1047. var name = pair[1];
  1048. if (value.substr(iValue, name.length).toLowerCase() === name.toLowerCase()) {
  1049. index = pair[0];
  1050. iValue += name.length;
  1051. return false;
  1052. }
  1053. });
  1054. if (index !== -1) {
  1055. return index + 1;
  1056. } else {
  1057. throw "Unknown name at position " + iValue;
  1058. }
  1059. },
  1060. // Confirm that a literal character matches the string value
  1061. checkLiteral = function() {
  1062. if (value.charAt(iValue) !== format.charAt(iFormat)) {
  1063. throw "Unexpected literal at position " + iValue;
  1064. }
  1065. iValue++;
  1066. };
  1067. for (iFormat = 0; iFormat < format.length; iFormat++) {
  1068. if (literal) {
  1069. if (format.charAt(iFormat) === "'" && !lookAhead("'")) {
  1070. literal = false;
  1071. } else {
  1072. checkLiteral();
  1073. }
  1074. } else {
  1075. switch (format.charAt(iFormat)) {
  1076. case "d":
  1077. day = getNumber("d");
  1078. break;
  1079. case "D":
  1080. getName("D", dayNamesShort, dayNames);
  1081. break;
  1082. case "o":
  1083. doy = getNumber("o");
  1084. break;
  1085. case "m":
  1086. month = getNumber("m");
  1087. break;
  1088. case "M":
  1089. month = getName("M", monthNamesShort, monthNames);
  1090. break;
  1091. case "y":
  1092. year = getNumber("y");
  1093. break;
  1094. case "@":
  1095. date = new Date(getNumber("@"));
  1096. year = date.getFullYear();
  1097. month = date.getMonth() + 1;
  1098. day = date.getDate();
  1099. break;
  1100. case "!":
  1101. date = new Date((getNumber("!") - this._ticksTo1970) / 10000);
  1102. year = date.getFullYear();
  1103. month = date.getMonth() + 1;
  1104. day = date.getDate();
  1105. break;
  1106. case "'":
  1107. if (lookAhead("'")){
  1108. checkLiteral();
  1109. } else {
  1110. literal = true;
  1111. }
  1112. break;
  1113. default:
  1114. checkLiteral();
  1115. }
  1116. }
  1117. }
  1118. if (iValue < value.length){
  1119. extra = value.substr(iValue);
  1120. if (!/^\s+/.test(extra)) {
  1121. throw "Extra/unparsed characters found in date: " + extra;
  1122. }
  1123. }
  1124. if (year === -1) {
  1125. year = new Date().getFullYear();
  1126. } else if (year < 100) {
  1127. year += new Date().getFullYear() - new Date().getFullYear() % 100 +
  1128. (year <= shortYearCutoff ? 0 : -100);
  1129. }
  1130. if (doy > -1) {
  1131. month = 1;
  1132. day = doy;
  1133. do {
  1134. dim = this._getDaysInMonth(year, month - 1);
  1135. if (day <= dim) {
  1136. break;
  1137. }
  1138. month++;
  1139. day -= dim;
  1140. } while (true);
  1141. }
  1142. date = this._daylightSavingAdjust(new Date(year, month - 1, day));
  1143. if (date.getFullYear() !== year || date.getMonth() + 1 !== month || date.getDate() !== day) {
  1144. throw "Invalid date"; // E.g. 31/02/00
  1145. }
  1146. return date;
  1147. },
  1148. /* Standard date formats. */
  1149. ATOM: "yy-mm-dd", // RFC 3339 (ISO 8601)
  1150. COOKIE: "D, dd M yy",
  1151. ISO_8601: "yy-mm-dd",
  1152. RFC_822: "D, d M y",
  1153. RFC_850: "DD, dd-M-y",
  1154. RFC_1036: "D, d M y",
  1155. RFC_1123: "D, d M yy",
  1156. RFC_2822: "D, d M yy",
  1157. RSS: "D, d M y", // RFC 822
  1158. TICKS: "!",
  1159. TIMESTAMP: "@",
  1160. W3C: "yy-mm-dd", // ISO 8601
  1161. _ticksTo1970: (((1970 - 1) * 365 + Math.floor(1970 / 4) - Math.floor(1970 / 100) +
  1162. Math.floor(1970 / 400)) * 24 * 60 * 60 * 10000000),
  1163. /* Format a date object into a string value.
  1164. * The format can be combinations of the following:
  1165. * d - day of month (no leading zero)
  1166. * dd - day of month (two digit)
  1167. * o - day of year (no leading zeros)
  1168. * oo - day of year (three digit)
  1169. * D - day name short
  1170. * DD - day name long
  1171. * m - month of year (no leading zero)
  1172. * mm - month of year (two digit)
  1173. * M - month name short
  1174. * MM - month name long
  1175. * y - year (two digit)
  1176. * yy - year (four digit)
  1177. * @ - Unix timestamp (ms since 01/01/1970)
  1178. * ! - Windows ticks (100ns since 01/01/0001)
  1179. * "..." - literal text
  1180. * '' - single quote
  1181. *
  1182. * @param format string - the desired format of the date
  1183. * @param date Date - the date value to format
  1184. * @param settings Object - attributes include:
  1185. * dayNamesShort string[7] - abbreviated names of the days from Sunday (optional)
  1186. * dayNames string[7] - names of the days from Sunday (optional)
  1187. * monthNamesShort string[12] - abbreviated names of the months (optional)
  1188. * monthNames string[12] - names of the months (optional)
  1189. * @return string - the date in the above format
  1190. */
  1191. formatDate: function (format, date, settings) {
  1192. if (!date) {
  1193. return "";
  1194. }
  1195. var iFormat,
  1196. dayNamesShort = (settings ? settings.dayNamesShort : null) || this._defaults.dayNamesShort,
  1197. dayNames = (settings ? settings.dayNames : null) || this._defaults.dayNames,
  1198. monthNamesShort = (settings ? settings.monthNamesShort : null) || this._defaults.monthNamesShort,
  1199. monthNames = (settings ? settings.monthNames : null) || this._defaults.monthNames,
  1200. // Check whether a format character is doubled
  1201. lookAhead = function(match) {
  1202. var matches = (iFormat + 1 < format.length && format.charAt(iFormat + 1) === match);
  1203. if (matches) {
  1204. iFormat++;
  1205. }
  1206. return matches;
  1207. },
  1208. // Format a number, with leading zero if necessary
  1209. formatNumber = function(match, value, len) {
  1210. var num = "" + value;
  1211. if (lookAhead(match)) {
  1212. while (num.length < len) {
  1213. num = "0" + num;
  1214. }
  1215. }
  1216. return num;
  1217. },
  1218. // Format a name, short or long as requested
  1219. formatName = function(match, value, shortNames, longNames) {
  1220. return (lookAhead(match) ? longNames[value] : shortNames[value]);
  1221. },
  1222. output = "",
  1223. literal = false;
  1224. if (date) {
  1225. for (iFormat = 0; iFormat < format.length; iFormat++) {
  1226. if (literal) {
  1227. if (format.charAt(iFormat) === "'" && !lookAhead("'")) {
  1228. literal = false;
  1229. } else {
  1230. output += format.charAt(iFormat);
  1231. }
  1232. } else {
  1233. switch (format.charAt(iFormat)) {
  1234. case "d":
  1235. output += formatNumber("d", date.getDate(), 2);
  1236. break;
  1237. case "D":
  1238. output += formatName("D", date.getDay(), dayNamesShort, dayNames);
  1239. break;
  1240. case "o":
  1241. output += formatNumber("o",
  1242. Math.round((new Date(date.getFullYear(), date.getMonth(), date.getDate()).getTime() - new Date(date.getFullYear(), 0, 0).getTime()) / 86400000), 3);
  1243. break;
  1244. case "m":
  1245. output += formatNumber("m", date.getMonth() + 1, 2);
  1246. break;
  1247. case "M":
  1248. output += formatName("M", date.getMonth(), monthNamesShort, monthNames);
  1249. break;
  1250. case "y":
  1251. output += (lookAhead("y") ? date.getFullYear() :
  1252. (date.getYear() % 100 < 10 ? "0" : "") + date.getYear() % 100);
  1253. break;
  1254. case "@":
  1255. output += date.getTime();
  1256. break;
  1257. case "!":
  1258. output += date.getTime() * 10000 + this._ticksTo1970;
  1259. break;
  1260. case "'":
  1261. if (lookAhead("'")) {
  1262. output += "'";
  1263. } else {
  1264. literal = true;
  1265. }
  1266. break;
  1267. default:
  1268. output += format.charAt(iFormat);
  1269. }
  1270. }
  1271. }
  1272. }
  1273. return output;
  1274. },
  1275. /* Extract all possible characters from the date format. */
  1276. _possibleChars: function (format) {
  1277. var iFormat,
  1278. chars = "",
  1279. literal = false,
  1280. // Check whether a format character is doubled
  1281. lookAhead = function(match) {
  1282. var matches = (iFormat + 1 < format.length && format.charAt(iFormat + 1) === match);
  1283. if (matches) {
  1284. iFormat++;
  1285. }
  1286. return matches;
  1287. };
  1288. for (iFormat = 0; iFormat < format.length; iFormat++) {
  1289. if (literal) {
  1290. if (format.charAt(iFormat) === "'" && !lookAhead("'")) {
  1291. literal = false;
  1292. } else {
  1293. chars += format.charAt(iFormat);
  1294. }
  1295. } else {
  1296. switch (format.charAt(iFormat)) {
  1297. case "d": case "m": case "y": case "@":
  1298. chars += "0123456789";
  1299. break;
  1300. case "D": case "M":
  1301. return null; // Accept anything
  1302. case "'":
  1303. if (lookAhead("'")) {
  1304. chars += "'";
  1305. } else {
  1306. literal = true;
  1307. }
  1308. break;
  1309. default:
  1310. chars += format.charAt(iFormat);
  1311. }
  1312. }
  1313. }
  1314. return chars;
  1315. },
  1316. /* Get a setting value, defaulting if necessary. */
  1317. _get: function(inst, name) {
  1318. return inst.settings[name] !== undefined ?
  1319. inst.settings[name] : this._defaults[name];
  1320. },
  1321. /* Parse existing date and initialise date picker. */
  1322. _setDateFromField: function(inst, noDefault) {
  1323. if (inst.input.val() === inst.lastVal) {
  1324. return;
  1325. }
  1326. var dateFormat = this._get(inst, "dateFormat"),
  1327. dates = inst.lastVal = inst.input ? inst.input.val() : null,
  1328. defaultDate = this._getDefaultDate(inst),
  1329. date = defaultDate,
  1330. settings = this._getFormatConfig(inst);
  1331. try {
  1332. date = this.parseDate(dateFormat, dates, settings) || defaultDate;
  1333. } catch (event) {
  1334. dates = (noDefault ? "" : dates);
  1335. }
  1336. inst.selectedDay = date.getDate();
  1337. inst.drawMonth = inst.selectedMonth = date.getMonth();
  1338. inst.drawYear = inst.selectedYear = date.getFullYear();
  1339. inst.currentDay = (dates ? date.getDate() : 0);
  1340. inst.currentMonth = (dates ? date.getMonth() : 0);
  1341. inst.currentYear = (dates ? date.getFullYear() : 0);
  1342. this._adjustInstDate(inst);
  1343. },
  1344. /* Retrieve the default date shown on opening. */
  1345. _getDefaultDate: function(inst) {
  1346. return this._restrictMinMax(inst,
  1347. this._determineDate(inst, this._get(inst, "defaultDate"), new Date()));
  1348. },
  1349. /* A date may be specified as an exact value or a relative one. */
  1350. _determineDate: function(inst, date, defaultDate) {
  1351. var offsetNumeric = function(offset) {
  1352. var date = new Date();
  1353. date.setDate(date.getDate() + offset);
  1354. return date;
  1355. },
  1356. offsetString = function(offset) {
  1357. try {
  1358. return $.datepicker.parseDate($.datepicker._get(inst, "dateFormat"),
  1359. offset, $.datepicker._getFormatConfig(inst));
  1360. }
  1361. catch (e) {
  1362. // Ignore
  1363. }
  1364. var date = (offset.toLowerCase().match(/^c/) ?
  1365. $.datepicker._getDate(inst) : null) || new Date(),
  1366. year = date.getFullYear(),
  1367. month = date.getMonth(),
  1368. day = date.getDate(),
  1369. pattern = /([+\-]?[0-9]+)\s*(d|D|w|W|m|M|y|Y)?/g,
  1370. matches = pattern.exec(offset);
  1371. while (matches) {
  1372. switch (matches[2] || "d") {
  1373. case "d" : case "D" :
  1374. day += parseInt(matches[1],10); break;
  1375. case "w" : case "W" :
  1376. day += parseInt(matches[1],10) * 7; break;
  1377. case "m" : case "M" :
  1378. month += parseInt(matches[1],10);
  1379. day = Math.min(day, $.datepicker._getDaysInMonth(year, month));
  1380. break;
  1381. case "y": case "Y" :
  1382. year += parseInt(matches[1],10);
  1383. day = Math.min(day, $.datepicker._getDaysInMonth(year, month));
  1384. break;
  1385. }
  1386. matches = pattern.exec(offset);
  1387. }
  1388. return new Date(year, month, day);
  1389. },
  1390. newDate = (date == null || date === "" ? defaultDate : (typeof date === "string" ? offsetString(date) :
  1391. (typeof date === "number" ? (isNaN(date) ? defaultDate : offsetNumeric(date)) : new Date(date.getTime()))));
  1392. newDate = (newDate && newDate.toString() === "Invalid Date" ? defaultDate : newDate);
  1393. if (newDate) {
  1394. newDate.setHours(0);
  1395. newDate.setMinutes(0);
  1396. newDate.setSeconds(0);
  1397. newDate.setMilliseconds(0);
  1398. }
  1399. return this._daylightSavingAdjust(newDate);
  1400. },
  1401. /* Handle switch to/from daylight saving.
  1402. * Hours may be non-zero on daylight saving cut-over:
  1403. * > 12 when midnight changeover, but then cannot generate
  1404. * midnight datetime, so jump to 1AM, otherwise reset.
  1405. * @param date (Date) the date to check
  1406. * @return (Date) the corrected date
  1407. */
  1408. _daylightSavingAdjust: function(date) {
  1409. if (!date) {
  1410. return null;
  1411. }
  1412. date.setHours(date.getHours() > 12 ? date.getHours() + 2 : 0);
  1413. return date;
  1414. },
  1415. /* Set the date(s) directly. */
  1416. _setDate: function(inst, date, noChange) {
  1417. var clear = !date,
  1418. origMonth = inst.selectedMonth,
  1419. origYear = inst.selectedYear,
  1420. newDate = this._restrictMinMax(inst, this._determineDate(inst, date, new Date()));
  1421. inst.selectedDay = inst.currentDay = newDate.getDate();
  1422. inst.drawMonth = inst.selectedMonth = inst.currentMonth = newDate.getMonth();
  1423. inst.drawYear = inst.selectedYear = inst.currentYear = newDate.getFullYear();
  1424. if ((origMonth !== inst.selectedMonth || origYear !== inst.selectedYear) && !noChange) {
  1425. this._notifyChange(inst);
  1426. }
  1427. this._adjustInstDate(inst);
  1428. if (inst.input) {
  1429. inst.input.val(clear ? "" : this._formatDate(inst));
  1430. }
  1431. },
  1432. /* Retrieve the date(s) directly. */
  1433. _getDate: function(inst) {
  1434. var startDate = (!inst.currentYear || (inst.input && inst.input.val() === "") ? null :
  1435. this._daylightSavingAdjust(new Date(
  1436. inst.currentYear, inst.currentMonth, inst.currentDay)));
  1437. return startDate;
  1438. },
  1439. /* Attach the onxxx handlers. These are declared statically so
  1440. * they work with static code transformers like Caja.
  1441. */
  1442. _attachHandlers: function(inst) {
  1443. var stepMonths = this._get(inst, "stepMonths"),
  1444. id = "#" + inst.id.replace( /\\\\/g, "\\" );
  1445. inst.dpDiv.find("[data-handler]").map(function () {
  1446. var handler = {
  1447. prev: function () {
  1448. $.datepicker._adjustDate(id, -stepMonths, "M");
  1449. },
  1450. next: function () {
  1451. $.datepicker._adjustDate(id, +stepMonths, "M");
  1452. },
  1453. hide: function () {
  1454. $.datepicker._hideDatepicker();
  1455. },
  1456. today: function () {
  1457. $.datepicker._gotoToday(id);
  1458. },
  1459. selectDay: function () {
  1460. $.datepicker._selectDay(id, +this.getAttribute("data-month"), +this.getAttribute("data-year"), this);
  1461. return false;
  1462. },
  1463. selectMonth: function () {
  1464. $.datepicker._selectMonthYear(id, this, "M");
  1465. return false;
  1466. },
  1467. selectYear: function () {
  1468. $.datepicker._selectMonthYear(id, this, "Y");
  1469. return false;
  1470. }
  1471. };
  1472. $(this).bind(this.getAttribute("data-event"), handler[this.getAttribute("data-handler")]);
  1473. });
  1474. },
  1475. /* Generate the HTML for the current state of the date picker. */
  1476. _generateHTML: function(inst) {
  1477. var maxDraw, prevText, prev, nextText, next, currentText, gotoDate,
  1478. controls, buttonPanel, firstDay, showWeek, dayNames, dayNamesMin,
  1479. monthNames, monthNamesShort, beforeShowDay, showOtherMonths,
  1480. selectOtherMonths, defaultDate, html, dow, row, group, col, selectedDate,
  1481. cornerClass, calender, thead, day, daysInMonth, leadDays, curRows, numRows,
  1482. printDate, dRow, tbody, daySettings, otherMonth, unselectable,
  1483. tempDate = new Date(),
  1484. today = this._daylightSavingAdjust(
  1485. new Date(tempDate.getFullYear(), tempDate.getMonth(), tempDate.getDate())), // clear time
  1486. isRTL = this._get(inst, "isRTL"),
  1487. showButtonPanel = this._get(inst, "showButtonPanel"),
  1488. hideIfNoPrevNext = this._get(inst, "hideIfNoPrevNext"),
  1489. navigationAsDateFormat = this._get(inst, "navigationAsDateFormat"),
  1490. numMonths = this._getNumberOfMonths(inst),
  1491. showCurrentAtPos = this._get(inst, "showCurrentAtPos"),
  1492. stepMonths = this._get(inst, "stepMonths"),
  1493. isMultiMonth = (numMonths[0] !== 1 || numMonths[1] !== 1),
  1494. currentDate = this._daylightSavingAdjust((!inst.currentDay ? new Date(9999, 9, 9) :
  1495. new Date(inst.currentYear, inst.currentMonth, inst.currentDay))),
  1496. minDate = this._getMinMaxDate(inst, "min"),
  1497. maxDate = this._getMinMaxDate(inst, "max"),
  1498. drawMonth = inst.drawMonth - showCurrentAtPos,
  1499. drawYear = inst.drawYear;
  1500. if (drawMonth < 0) {
  1501. drawMonth += 12;
  1502. drawYear--;
  1503. }
  1504. if (maxDate) {
  1505. maxDraw = this._daylightSavingAdjust(new Date(maxDate.getFullYear(),
  1506. maxDate.getMonth() - (numMonths[0] * numMonths[1]) + 1, maxDate.getDate()));
  1507. maxDraw = (minDate && maxDraw < minDate ? minDate : maxDraw);
  1508. while (this._daylightSavingAdjust(new Date(drawYear, drawMonth, 1)) > maxDraw) {
  1509. drawMonth--;
  1510. if (drawMonth < 0) {
  1511. drawMonth = 11;
  1512. drawYear--;
  1513. }
  1514. }
  1515. }
  1516. inst.drawMonth = drawMonth;
  1517. inst.drawYear = drawYear;
  1518. prevText = this._get(inst, "prevText");
  1519. prevText = (!navigationAsDateFormat ? prevText : this.formatDate(prevText,
  1520. this._daylightSavingAdjust(new Date(drawYear, drawMonth - stepMonths, 1)),
  1521. this._getFormatConfig(inst)));
  1522. prev = (this._canAdjustMonth(inst, -1, drawYear, drawMonth) ?
  1523. "<a class='ui-datepicker-prev ui-corner-all' data-handler='prev' data-event='click'" +
  1524. " title='" + prevText + "'><span class='ui-icon ui-icon-circle-triangle-" + ( isRTL ? "e" : "w") + "'>" + prevText + "</span></a>" :
  1525. (hideIfNoPrevNext ? "" : "<a class='ui-datepicker-prev ui-corner-all ui-state-disabled' title='"+ prevText +"'><span class='ui-icon ui-icon-circle-triangle-" + ( isRTL ? "e" : "w") + "'>" + prevText + "</span></a>"));
  1526. nextText = this._get(inst, "nextText");
  1527. nextText = (!navigationAsDateFormat ? nextText : this.formatDate(nextText,
  1528. this._daylightSavingAdjust(new Date(drawYear, drawMonth + stepMonths, 1)),
  1529. this._getFormatConfig(inst)));
  1530. next = (this._canAdjustMonth(inst, +1, drawYear, drawMonth) ?
  1531. "<a class='ui-datepicker-next ui-corner-all' data-handler='next' data-event='click'" +
  1532. " title='" + nextText + "'><span class='ui-icon ui-icon-circle-triangle-" + ( isRTL ? "w" : "e") + "'>" + nextText + "</span></a>" :
  1533. (hideIfNoPrevNext ? "" : "<a class='ui-datepicker-next ui-corner-all ui-state-disabled' title='"+ nextText + "'><span class='ui-icon ui-icon-circle-triangle-" + ( isRTL ? "w" : "e") + "'>" + nextText + "</span></a>"));
  1534. currentText = this._get(inst, "currentText");
  1535. gotoDate = (this._get(inst, "gotoCurrent") && inst.currentDay ? currentDate : today);
  1536. currentText = (!navigationAsDateFormat ? currentText :
  1537. this.formatDate(currentText, gotoDate, this._getFormatConfig(inst)));
  1538. controls = (!inst.inline ? "<button type='button' class='ui-datepicker-close ui-state-default ui-priority-primary ui-corner-all' data-handler='hide' data-event='click'>" +
  1539. this._get(inst, "closeText") + "</button>" : "");
  1540. buttonPanel = (showButtonPanel) ? "<div class='ui-datepicker-buttonpane ui-widget-content'>" + (isRTL ? controls : "") +
  1541. (this._isInRange(inst, gotoDate) ? "<button type='button' class='ui-datepicker-current ui-state-default ui-priority-secondary ui-corner-all' data-handler='today' data-event='click'" +
  1542. ">" + currentText + "</button>" : "") + (isRTL ? "" : controls) + "</div>" : "";
  1543. firstDay = parseInt(this._get(inst, "firstDay"),10);
  1544. firstDay = (isNaN(firstDay) ? 0 : firstDay);
  1545. showWeek = this._get(inst, "showWeek");
  1546. dayNames = this._get(inst, "dayNames");
  1547. dayNamesMin = this._get(inst, "dayNamesMin");
  1548. monthNames = this._get(inst, "monthNames");
  1549. monthNamesShort = this._get(inst, "monthNamesShort");
  1550. beforeShowDay = this._get(inst, "beforeShowDay");
  1551. showOtherMonths = this._get(inst, "showOtherMonths");
  1552. selectOtherMonths = this._get(inst, "selectOtherMonths");
  1553. defaultDate = this._getDefaultDate(inst);
  1554. html = "";
  1555. dow;
  1556. for (row = 0; row < numMonths[0]; row++) {
  1557. group = "";
  1558. this.maxRows = 4;
  1559. for (col = 0; col < numMonths[1]; col++) {
  1560. selectedDate = this._daylightSavingAdjust(new Date(drawYear, drawMonth, inst.selectedDay));
  1561. cornerClass = " ui-corner-all";
  1562. calender = "";
  1563. if (isMultiMonth) {
  1564. calender += "<div class='ui-datepicker-group";
  1565. if (numMonths[1] > 1) {
  1566. switch (col) {
  1567. case 0: calender += " ui-datepicker-group-first";
  1568. cornerClass = " ui-corner-" + (isRTL ? "right" : "left"); break;
  1569. case numMonths[1]-1: calender += " ui-datepicker-group-last";
  1570. cornerClass = " ui-corner-" + (isRTL ? "left" : "right"); break;
  1571. default: calender += " ui-datepicker-group-middle"; cornerClass = ""; break;
  1572. }
  1573. }
  1574. calender += "'>";
  1575. }
  1576. calender += "<div class='ui-datepicker-header ui-widget-header ui-helper-clearfix" + cornerClass + "'>" +
  1577. (/all|left/.test(cornerClass) && row === 0 ? (isRTL ? next : prev) : "") +
  1578. (/all|right/.test(cornerClass) && row === 0 ? (isRTL ? prev : next) : "") +
  1579. this._generateMonthYearHeader(inst, drawMonth, drawYear, minDate, maxDate,
  1580. row > 0 || col > 0, monthNames, monthNamesShort) + // draw month headers
  1581. "</div><table class='ui-datepicker-calendar'><thead>" +
  1582. "<tr>";
  1583. thead = (showWeek ? "<th class='ui-datepicker-week-col'>" + this._get(inst, "weekHeader") + "</th>" : "");
  1584. for (dow = 0; dow < 7; dow++) { // days of the week
  1585. day = (dow + firstDay) % 7;
  1586. thead += "<th scope='col'" + ((dow + firstDay + 6) % 7 >= 5 ? " class='ui-datepicker-week-end'" : "") + ">" +
  1587. "<span title='" + dayNames[day] + "'>" + dayNamesMin[day] + "</span></th>";
  1588. }
  1589. calender += thead + "</tr></thead><tbody>";
  1590. daysInMonth = this._getDaysInMonth(drawYear, drawMonth);
  1591. if (drawYear === inst.selectedYear && drawMonth === inst.selectedMonth) {
  1592. inst.selectedDay = Math.min(inst.selectedDay, daysInMonth);
  1593. }
  1594. leadDays = (this._getFirstDayOfMonth(drawYear, drawMonth) - firstDay + 7) % 7;
  1595. curRows = Math.ceil((leadDays + daysInMonth) / 7); // calculate the number of rows to generate
  1596. numRows = (isMultiMonth ? this.maxRows > curRows ? this.maxRows : curRows : curRows); //If multiple months, use the higher number of rows (see #7043)
  1597. this.maxRows = numRows;
  1598. printDate = this._daylightSavingAdjust(new Date(drawYear, drawMonth, 1 - leadDays));
  1599. for (dRow = 0; dRow < numRows; dRow++) { // create date picker rows
  1600. calender += "<tr>";
  1601. tbody = (!showWeek ? "" : "<td class='ui-datepicker-week-col'>" +
  1602. this._get(inst, "calculateWeek")(printDate) + "</td>");
  1603. for (dow = 0; dow < 7; dow++) { // create date picker days
  1604. daySettings = (beforeShowDay ?
  1605. beforeShowDay.apply((inst.input ? inst.input[0] : null), [printDate]) : [true, ""]);
  1606. otherMonth = (printDate.getMonth() !== drawMonth);
  1607. unselectable = (otherMonth && !selectOtherMonths) || !daySettings[0] ||
  1608. (minDate && printDate < minDate) || (maxDate && printDate > maxDate);
  1609. tbody += "<td class='" +
  1610. ((dow + firstDay + 6) % 7 >= 5 ? " ui-datepicker-week-end" : "") + // highlight weekends
  1611. (otherMonth ? " ui-datepicker-other-month" : "") + // highlight days from other months
  1612. ((printDate.getTime() === selectedDate.getTime() && drawMonth === inst.selectedMonth && inst._keyEvent) || // user pressed key
  1613. (defaultDate.getTime() === printDate.getTime() && defaultDate.getTime() === selectedDate.getTime()) ?
  1614. // or defaultDate is current printedDate and defaultDate is selectedDate
  1615. " " + this._dayOverClass : "") + // highlight selected day
  1616. (unselectable ? " " + this._unselectableClass + " ui-state-disabled": "") + // highlight unselectable days
  1617. (otherMonth && !showOtherMonths ? "" : " " + daySettings[1] + // highlight custom dates
  1618. (printDate.getTime() === currentDate.getTime() ? " " + this._currentClass : "") + // highlight selected day
  1619. (printDate.getTime() === today.getTime() ? " ui-datepicker-today" : "")) + "'" + // highlight today (if different)
  1620. ((!otherMonth || showOtherMonths) && daySettings[2] ? " title='" + daySettings[2].replace(/'/g, "&#39;") + "'" : "") + // cell title
  1621. (unselectable ? "" : " data-handler='selectDay' data-event='click' data-month='" + printDate.getMonth() + "' data-year='" + printDate.getFullYear() + "'") + ">" + // actions
  1622. (otherMonth && !showOtherMonths ? "&#xa0;" : // display for other months
  1623. (unselectable ? "<span class='ui-state-default'>" + printDate.getDate() + "</span>" : "<a class='ui-state-default" +
  1624. (printDate.getTime() === today.getTime() ? " ui-state-highlight" : "") +
  1625. (printDate.getTime() === currentDate.getTime() ? " ui-state-active" : "") + // highlight selected day
  1626. (otherMonth ? " ui-priority-secondary" : "") + // distinguish dates from other months
  1627. "' href='#'>" + printDate.getDate() + "</a>")) + "</td>"; // display selectable date
  1628. printDate.setDate(printDate.getDate() + 1);
  1629. printDate = this._daylightSavingAdjust(printDate);
  1630. }
  1631. calender += tbody + "</tr>";
  1632. }
  1633. drawMonth++;
  1634. if (drawMonth > 11) {
  1635. drawMonth = 0;
  1636. drawYear++;
  1637. }
  1638. calender += "</tbody></table>" + (isMultiMonth ? "</div>" +
  1639. ((numMonths[0] > 0 && col === numMonths[1]-1) ? "<div class='ui-datepicker-row-break'></div>" : "") : "");
  1640. group += calender;
  1641. }
  1642. html += group;
  1643. }
  1644. html += buttonPanel;
  1645. inst._keyEvent = false;
  1646. return html;
  1647. },
  1648. /* Generate the month and year header. */
  1649. _generateMonthYearHeader: function(inst, drawMonth, drawYear, minDate, maxDate,
  1650. secondary, monthNames, monthNamesShort) {
  1651. var inMinYear, inMaxYear, month, years, thisYear, determineYear, year, endYear,
  1652. changeMonth = this._get(inst, "changeMonth"),
  1653. changeYear = this._get(inst, "changeYear"),
  1654. showMonthAfterYear = this._get(inst, "showMonthAfterYear"),
  1655. html = "<div class='ui-datepicker-title'>",
  1656. monthHtml = "";
  1657. // month selection
  1658. if (secondary || !changeMonth) {
  1659. monthHtml += "<span class='ui-datepicker-month'>" + monthNames[drawMonth] + "</span>";
  1660. } else {
  1661. inMinYear = (minDate && minDate.getFullYear() === drawYear);
  1662. inMaxYear = (maxDate && maxDate.getFullYear() === drawYear);
  1663. monthHtml += "<select class='ui-datepicker-month' data-handler='selectMonth' data-event='change'>";
  1664. for ( month = 0; month < 12; month++) {
  1665. if ((!inMinYear || month >= minDate.getMonth()) && (!inMaxYear || month <= maxDate.getMonth())) {
  1666. monthHtml += "<option value='" + month + "'" +
  1667. (month === drawMonth ? " selected='selected'" : "") +
  1668. ">" + monthNamesShort[month] + "</option>";
  1669. }
  1670. }
  1671. monthHtml += "</select>";
  1672. }
  1673. if (!showMonthAfterYear) {
  1674. html += monthHtml + (secondary || !(changeMonth && changeYear) ? "&#xa0;" : "");
  1675. }
  1676. // year selection
  1677. if ( !inst.yearshtml ) {
  1678. inst.yearshtml = "";
  1679. if (secondary || !changeYear) {
  1680. html += "<span class='ui-datepicker-year'>" + drawYear + "</span>";
  1681. } else {
  1682. // determine range of years to display
  1683. years = this._get(inst, "yearRange").split(":");
  1684. thisYear = new Date().getFullYear();
  1685. determineYear = function(value) {
  1686. var year = (value.match(/c[+\-].*/) ? drawYear + parseInt(value.substring(1), 10) :
  1687. (value.match(/[+\-].*/) ? thisYear + parseInt(value, 10) :
  1688. parseInt(value, 10)));
  1689. return (isNaN(year) ? thisYear : year);
  1690. };
  1691. year = determineYear(years[0]);
  1692. endYear = Math.max(year, determineYear(years[1] || ""));
  1693. year = (minDate ? Math.max(year, minDate.getFullYear()) : year);
  1694. endYear = (maxDate ? Math.min(endYear, maxDate.getFullYear()) : endYear);
  1695. inst.yearshtml += "<select class='ui-datepicker-year' data-handler='selectYear' data-event='change'>";
  1696. for (; year <= endYear; year++) {
  1697. inst.yearshtml += "<option value='" + year + "'" +
  1698. (year === drawYear ? " selected='selected'" : "") +
  1699. ">" + year + "</option>";
  1700. }
  1701. inst.yearshtml += "</select>";
  1702. html += inst.yearshtml;
  1703. inst.yearshtml = null;
  1704. }
  1705. }
  1706. html += this._get(inst, "yearSuffix");
  1707. if (showMonthAfterYear) {
  1708. html += (secondary || !(changeMonth && changeYear) ? "&#xa0;" : "") + monthHtml;
  1709. }
  1710. html += "</div>"; // Close datepicker_header
  1711. return html;
  1712. },
  1713. /* Adjust one of the date sub-fields. */
  1714. _adjustInstDate: function(inst, offset, period) {
  1715. var year = inst.drawYear + (period === "Y" ? offset : 0),
  1716. month = inst.drawMonth + (period === "M" ? offset : 0),
  1717. day = Math.min(inst.selectedDay, this._getDaysInMonth(year, month)) + (period === "D" ? offset : 0),
  1718. date = this._restrictMinMax(inst, this._daylightSavingAdjust(new Date(year, month, day)));
  1719. inst.selectedDay = date.getDate();
  1720. inst.drawMonth = inst.selectedMonth = date.getMonth();
  1721. inst.drawYear = inst.selectedYear = date.getFullYear();
  1722. if (period === "M" || period === "Y") {
  1723. this._notifyChange(inst);
  1724. }
  1725. },
  1726. /* Ensure a date is within any min/max bounds. */
  1727. _restrictMinMax: function(inst, date) {
  1728. var minDate = this._getMinMaxDate(inst, "min"),
  1729. maxDate = this._getMinMaxDate(inst, "max"),
  1730. newDate = (minDate && date < minDate ? minDate : date);
  1731. return (maxDate && newDate > maxDate ? maxDate : newDate);
  1732. },
  1733. /* Notify change of month/year. */
  1734. _notifyChange: function(inst) {
  1735. var onChange = this._get(inst, "onChangeMonthYear");
  1736. if (onChange) {
  1737. onChange.apply((inst.input ? inst.input[0] : null),
  1738. [inst.selectedYear, inst.selectedMonth + 1, inst]);
  1739. }
  1740. },
  1741. /* Determine the number of months to show. */
  1742. _getNumberOfMonths: function(inst) {
  1743. var numMonths = this._get(inst, "numberOfMonths");
  1744. return (numMonths == null ? [1, 1] : (typeof numMonths === "number" ? [1, numMonths] : numMonths));
  1745. },
  1746. /* Determine the current maximum date - ensure no time components are set. */
  1747. _getMinMaxDate: function(inst, minMax) {
  1748. return this._determineDate(inst, this._get(inst, minMax + "Date"), null);
  1749. },
  1750. /* Find the number of days in a given month. */
  1751. _getDaysInMonth: function(year, month) {
  1752. return 32 - this._daylightSavingAdjust(new Date(year, month, 32)).getDate();
  1753. },
  1754. /* Find the day of the week of the first of a month. */
  1755. _getFirstDayOfMonth: function(year, month) {
  1756. return new Date(year, month, 1).getDay();
  1757. },
  1758. /* Determines if we should allow a "next/prev" month display change. */
  1759. _canAdjustMonth: function(inst, offset, curYear, curMonth) {
  1760. var numMonths = this._getNumberOfMonths(inst),
  1761. date = this._daylightSavingAdjust(new Date(curYear,
  1762. curMonth + (offset < 0 ? offset : numMonths[0] * numMonths[1]), 1));
  1763. if (offset < 0) {
  1764. date.setDate(this._getDaysInMonth(date.getFullYear(), date.getMonth()));
  1765. }
  1766. return this._isInRange(inst, date);
  1767. },
  1768. /* Is the given date in the accepted range? */
  1769. _isInRange: function(inst, date) {
  1770. var yearSplit, currentYear,
  1771. minDate = this._getMinMaxDate(inst, "min"),
  1772. maxDate = this._getMinMaxDate(inst, "max"),
  1773. minYear = null,
  1774. maxYear = null,
  1775. years = this._get(inst, "yearRange");
  1776. if (years){
  1777. yearSplit = years.split(":");
  1778. currentYear = new Date().getFullYear();
  1779. minYear = parseInt(yearSplit[0], 10);
  1780. maxYear = parseInt(yearSplit[1], 10);
  1781. if ( yearSplit[0].match(/[+\-].*/) ) {
  1782. minYear += currentYear;
  1783. }
  1784. if ( yearSplit[1].match(/[+\-].*/) ) {
  1785. maxYear += currentYear;
  1786. }
  1787. }
  1788. return ((!minDate || date.getTime() >= minDate.getTime()) &&
  1789. (!maxDate || date.getTime() <= maxDate.getTime()) &&
  1790. (!minYear || date.getFullYear() >= minYear) &&
  1791. (!maxYear || date.getFullYear() <= maxYear));
  1792. },
  1793. /* Provide the configuration settings for formatting/parsing. */
  1794. _getFormatConfig: function(inst) {
  1795. var shortYearCutoff = this._get(inst, "shortYearCutoff");
  1796. shortYearCutoff = (typeof shortYearCutoff !== "string" ? shortYearCutoff :
  1797. new Date().getFullYear() % 100 + parseInt(shortYearCutoff, 10));
  1798. return {shortYearCutoff: shortYearCutoff,
  1799. dayNamesShort: this._get(inst, "dayNamesShort"), dayNames: this._get(inst, "dayNames"),
  1800. monthNamesShort: this._get(inst, "monthNamesShort"), monthNames: this._get(inst, "monthNames")};
  1801. },
  1802. /* Format the given date for display. */
  1803. _formatDate: function(inst, day, month, year) {
  1804. if (!day) {
  1805. inst.currentDay = inst.selectedDay;
  1806. inst.currentMonth = inst.selectedMonth;
  1807. inst.currentYear = inst.selectedYear;
  1808. }
  1809. var date = (day ? (typeof day === "object" ? day :
  1810. this._daylightSavingAdjust(new Date(year, month, day))) :
  1811. this._daylightSavingAdjust(new Date(inst.currentYear, inst.currentMonth, inst.currentDay)));
  1812. return this.formatDate(this._get(inst, "dateFormat"), date, this._getFormatConfig(inst));
  1813. }
  1814. });
  1815. /*
  1816. * Bind hover events for datepicker elements.
  1817. * Done via delegate so the binding only occurs once in the lifetime of the parent div.
  1818. * Global datepicker_instActive, set by _updateDatepicker allows the handlers to find their way back to the active picker.
  1819. */
  1820. function datepicker_bindHover(dpDiv) {
  1821. var selector = "button, .ui-datepicker-prev, .ui-datepicker-next, .ui-datepicker-calendar td a";
  1822. return dpDiv.delegate(selector, "mouseout", function() {
  1823. $(this).removeClass("ui-state-hover");
  1824. if (this.className.indexOf("ui-datepicker-prev") !== -1) {
  1825. $(this).removeClass("ui-datepicker-prev-hover");
  1826. }
  1827. if (this.className.indexOf("ui-datepicker-next") !== -1) {
  1828. $(this).removeClass("ui-datepicker-next-hover");
  1829. }
  1830. })
  1831. .delegate( selector, "mouseover", datepicker_handleMouseover );
  1832. }
  1833. function datepicker_handleMouseover() {
  1834. if (!$.datepicker._isDisabledDatepicker( datepicker_instActive.inline? datepicker_instActive.dpDiv.parent()[0] : datepicker_instActive.input[0])) {
  1835. $(this).parents(".ui-datepicker-calendar").find("a").removeClass("ui-state-hover");
  1836. $(this).addClass("ui-state-hover");
  1837. if (this.className.indexOf("ui-datepicker-prev") !== -1) {
  1838. $(this).addClass("ui-datepicker-prev-hover");
  1839. }
  1840. if (this.className.indexOf("ui-datepicker-next") !== -1) {
  1841. $(this).addClass("ui-datepicker-next-hover");
  1842. }
  1843. }
  1844. }
  1845. /* jQuery extend now ignores nulls! */
  1846. function datepicker_extendRemove(target, props) {
  1847. $.extend(target, props);
  1848. for (var name in props) {
  1849. if (props[name] == null) {
  1850. target[name] = props[name];
  1851. }
  1852. }
  1853. return target;
  1854. }
  1855. /* Invoke the datepicker functionality.
  1856. @param options string - a command, optionally followed by additional parameters or
  1857. Object - settings for attaching new datepicker functionality
  1858. @return jQuery object */
  1859. $.fn.datepicker = function(options){
  1860. /* Verify an empty collection wasn't passed - Fixes #6976 */
  1861. if ( !this.length ) {
  1862. return this;
  1863. }
  1864. /* Initialise the date picker. */
  1865. if (!$.datepicker.initialized) {
  1866. $(document).mousedown($.datepicker._checkExternalClick);
  1867. $.datepicker.initialized = true;
  1868. }
  1869. /* Append datepicker main container to body if not exist. */
  1870. if ($("#"+$.datepicker._mainDivId).length === 0) {
  1871. $("body").append($.datepicker.dpDiv);
  1872. }
  1873. var otherArgs = Array.prototype.slice.call(arguments, 1);
  1874. if (typeof options === "string" && (options === "isDisabled" || options === "getDate" || options === "widget")) {
  1875. return $.datepicker["_" + options + "Datepicker"].
  1876. apply($.datepicker, [this[0]].concat(otherArgs));
  1877. }
  1878. if (options === "option" && arguments.length === 2 && typeof arguments[1] === "string") {
  1879. return $.datepicker["_" + options + "Datepicker"].
  1880. apply($.datepicker, [this[0]].concat(otherArgs));
  1881. }
  1882. return this.each(function() {
  1883. typeof options === "string" ?
  1884. $.datepicker["_" + options + "Datepicker"].
  1885. apply($.datepicker, [this].concat(otherArgs)) :
  1886. $.datepicker._attachDatepicker(this, options);
  1887. });
  1888. };
  1889. $.datepicker = new Datepicker(); // singleton instance
  1890. $.datepicker.initialized = false;
  1891. $.datepicker.uuid = new Date().getTime();
  1892. $.datepicker.version = "1.11.4";
  1893. return $.datepicker;
  1894. }));