jquery.ui.datepicker.js 75KB

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