jquery-ui-timepicker-addon.js 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154
  1. /*! jQuery Timepicker Addon - v1.4.3 - 2013-11-30
  2. * http://trentrichardson.com/examples/timepicker
  3. * Copyright (c) 2013 Trent Richardson; Licensed MIT */
  4. (function (factory) {
  5. if (typeof define === 'function' && define.amd) {
  6. define([
  7. "jquery",
  8. "jquery/ui"
  9. ], factory);
  10. } else {
  11. factory(jQuery);
  12. }
  13. }(function ($) {
  14. /*
  15. * Lets not redefine timepicker, Prevent "Uncaught RangeError: Maximum call stack size exceeded"
  16. */
  17. $.ui.timepicker = $.ui.timepicker || {};
  18. if ($.ui.timepicker.version) {
  19. return;
  20. }
  21. /*
  22. * Extend jQueryUI, get it started with our version number
  23. */
  24. $.extend($.ui, {
  25. timepicker: {
  26. version: "1.4.3"
  27. }
  28. });
  29. /*
  30. * Timepicker manager.
  31. * Use the singleton instance of this class, $.timepicker, to interact with the time picker.
  32. * Settings for (groups of) time pickers are maintained in an instance object,
  33. * allowing multiple different settings on the same page.
  34. */
  35. var Timepicker = function () {
  36. this.regional = []; // Available regional settings, indexed by language code
  37. this.regional[''] = { // Default regional settings
  38. currentText: 'Now',
  39. closeText: 'Done',
  40. amNames: ['AM', 'A'],
  41. pmNames: ['PM', 'P'],
  42. timeFormat: 'HH:mm',
  43. timeSuffix: '',
  44. timeOnlyTitle: 'Choose Time',
  45. timeText: 'Time',
  46. hourText: 'Hour',
  47. minuteText: 'Minute',
  48. secondText: 'Second',
  49. millisecText: 'Millisecond',
  50. microsecText: 'Microsecond',
  51. timezoneText: 'Time Zone',
  52. isRTL: false
  53. };
  54. this._defaults = { // Global defaults for all the datetime picker instances
  55. showButtonPanel: true,
  56. timeOnly: false,
  57. showHour: null,
  58. showMinute: null,
  59. showSecond: null,
  60. showMillisec: null,
  61. showMicrosec: null,
  62. showTimezone: null,
  63. showTime: true,
  64. stepHour: 1,
  65. stepMinute: 1,
  66. stepSecond: 1,
  67. stepMillisec: 1,
  68. stepMicrosec: 1,
  69. hour: 0,
  70. minute: 0,
  71. second: 0,
  72. millisec: 0,
  73. microsec: 0,
  74. timezone: null,
  75. hourMin: 0,
  76. minuteMin: 0,
  77. secondMin: 0,
  78. millisecMin: 0,
  79. microsecMin: 0,
  80. hourMax: 23,
  81. minuteMax: 59,
  82. secondMax: 59,
  83. millisecMax: 999,
  84. microsecMax: 999,
  85. minDateTime: null,
  86. maxDateTime: null,
  87. onSelect: null,
  88. hourGrid: 0,
  89. minuteGrid: 0,
  90. secondGrid: 0,
  91. millisecGrid: 0,
  92. microsecGrid: 0,
  93. alwaysSetTime: true,
  94. separator: ' ',
  95. altFieldTimeOnly: true,
  96. altTimeFormat: null,
  97. altSeparator: null,
  98. altTimeSuffix: null,
  99. pickerTimeFormat: null,
  100. pickerTimeSuffix: null,
  101. showTimepicker: true,
  102. timezoneList: null,
  103. addSliderAccess: false,
  104. sliderAccessArgs: null,
  105. controlType: 'slider',
  106. defaultValue: null,
  107. parse: 'strict'
  108. };
  109. $.extend(this._defaults, this.regional['']);
  110. };
  111. $.extend(Timepicker.prototype, {
  112. $input: null,
  113. $altInput: null,
  114. $timeObj: null,
  115. inst: null,
  116. hour_slider: null,
  117. minute_slider: null,
  118. second_slider: null,
  119. millisec_slider: null,
  120. microsec_slider: null,
  121. timezone_select: null,
  122. hour: 0,
  123. minute: 0,
  124. second: 0,
  125. millisec: 0,
  126. microsec: 0,
  127. timezone: null,
  128. hourMinOriginal: null,
  129. minuteMinOriginal: null,
  130. secondMinOriginal: null,
  131. millisecMinOriginal: null,
  132. microsecMinOriginal: null,
  133. hourMaxOriginal: null,
  134. minuteMaxOriginal: null,
  135. secondMaxOriginal: null,
  136. millisecMaxOriginal: null,
  137. microsecMaxOriginal: null,
  138. ampm: '',
  139. formattedDate: '',
  140. formattedTime: '',
  141. formattedDateTime: '',
  142. timezoneList: null,
  143. units: ['hour', 'minute', 'second', 'millisec', 'microsec'],
  144. support: {},
  145. control: null,
  146. /*
  147. * Override the default settings for all instances of the time picker.
  148. * @param {Object} settings object - the new settings to use as defaults (anonymous object)
  149. * @return {Object} the manager object
  150. */
  151. setDefaults: function (settings) {
  152. extendRemove(this._defaults, settings || {});
  153. return this;
  154. },
  155. /*
  156. * Create a new Timepicker instance
  157. */
  158. _newInst: function ($input, opts) {
  159. var tp_inst = new Timepicker(),
  160. inlineSettings = {},
  161. fns = {},
  162. overrides, i;
  163. for (var attrName in this._defaults) {
  164. if (this._defaults.hasOwnProperty(attrName)) {
  165. var attrValue = $input.attr('time:' + attrName);
  166. if (attrValue) {
  167. try {
  168. inlineSettings[attrName] = eval(attrValue);
  169. } catch (err) {
  170. inlineSettings[attrName] = attrValue;
  171. }
  172. }
  173. }
  174. }
  175. overrides = {
  176. beforeShow: function (input, dp_inst) {
  177. if ($.isFunction(tp_inst._defaults.evnts.beforeShow)) {
  178. return tp_inst._defaults.evnts.beforeShow.call($input[0], input, dp_inst, tp_inst);
  179. }
  180. },
  181. onChangeMonthYear: function (year, month, dp_inst) {
  182. // Update the time as well : this prevents the time from disappearing from the $input field.
  183. tp_inst._updateDateTime(dp_inst);
  184. if ($.isFunction(tp_inst._defaults.evnts.onChangeMonthYear)) {
  185. tp_inst._defaults.evnts.onChangeMonthYear.call($input[0], year, month, dp_inst, tp_inst);
  186. }
  187. },
  188. onClose: function (dateText, dp_inst) {
  189. if (tp_inst.timeDefined === true && $input.val() !== '') {
  190. tp_inst._updateDateTime(dp_inst);
  191. }
  192. if ($.isFunction(tp_inst._defaults.evnts.onClose)) {
  193. tp_inst._defaults.evnts.onClose.call($input[0], dateText, dp_inst, tp_inst);
  194. }
  195. }
  196. };
  197. for (i in overrides) {
  198. if (overrides.hasOwnProperty(i)) {
  199. fns[i] = opts[i] || null;
  200. }
  201. }
  202. tp_inst._defaults = $.extend({}, this._defaults, inlineSettings, opts, overrides, {
  203. evnts: fns,
  204. timepicker: tp_inst // add timepicker as a property of datepicker: $.datepicker._get(dp_inst, 'timepicker');
  205. });
  206. tp_inst.amNames = $.map(tp_inst._defaults.amNames, function (val) {
  207. return val.toUpperCase();
  208. });
  209. tp_inst.pmNames = $.map(tp_inst._defaults.pmNames, function (val) {
  210. return val.toUpperCase();
  211. });
  212. // detect which units are supported
  213. tp_inst.support = detectSupport(
  214. tp_inst._defaults.timeFormat +
  215. (tp_inst._defaults.pickerTimeFormat ? tp_inst._defaults.pickerTimeFormat : '') +
  216. (tp_inst._defaults.altTimeFormat ? tp_inst._defaults.altTimeFormat : ''));
  217. // controlType is string - key to our this._controls
  218. if (typeof(tp_inst._defaults.controlType) === 'string') {
  219. if (tp_inst._defaults.controlType === 'slider' && typeof($.ui.slider) === 'undefined') {
  220. tp_inst._defaults.controlType = 'select';
  221. }
  222. tp_inst.control = tp_inst._controls[tp_inst._defaults.controlType];
  223. }
  224. // controlType is an object and must implement create, options, value methods
  225. else {
  226. tp_inst.control = tp_inst._defaults.controlType;
  227. }
  228. // prep the timezone options
  229. var timezoneList = [-720, -660, -600, -570, -540, -480, -420, -360, -300, -270, -240, -210, -180, -120, -60,
  230. 0, 60, 120, 180, 210, 240, 270, 300, 330, 345, 360, 390, 420, 480, 525, 540, 570, 600, 630, 660, 690, 720, 765, 780, 840];
  231. if (tp_inst._defaults.timezoneList !== null) {
  232. timezoneList = tp_inst._defaults.timezoneList;
  233. }
  234. var tzl = timezoneList.length, tzi = 0, tzv = null;
  235. if (tzl > 0 && typeof timezoneList[0] !== 'object') {
  236. for (; tzi < tzl; tzi++) {
  237. tzv = timezoneList[tzi];
  238. timezoneList[tzi] = { value: tzv, label: $.timepicker.timezoneOffsetString(tzv, tp_inst.support.iso8601) };
  239. }
  240. }
  241. tp_inst._defaults.timezoneList = timezoneList;
  242. // set the default units
  243. tp_inst.timezone = tp_inst._defaults.timezone !== null ? $.timepicker.timezoneOffsetNumber(tp_inst._defaults.timezone) :
  244. ((new Date()).getTimezoneOffset() * -1);
  245. tp_inst.hour = tp_inst._defaults.hour < tp_inst._defaults.hourMin ? tp_inst._defaults.hourMin :
  246. tp_inst._defaults.hour > tp_inst._defaults.hourMax ? tp_inst._defaults.hourMax : tp_inst._defaults.hour;
  247. tp_inst.minute = tp_inst._defaults.minute < tp_inst._defaults.minuteMin ? tp_inst._defaults.minuteMin :
  248. tp_inst._defaults.minute > tp_inst._defaults.minuteMax ? tp_inst._defaults.minuteMax : tp_inst._defaults.minute;
  249. tp_inst.second = tp_inst._defaults.second < tp_inst._defaults.secondMin ? tp_inst._defaults.secondMin :
  250. tp_inst._defaults.second > tp_inst._defaults.secondMax ? tp_inst._defaults.secondMax : tp_inst._defaults.second;
  251. tp_inst.millisec = tp_inst._defaults.millisec < tp_inst._defaults.millisecMin ? tp_inst._defaults.millisecMin :
  252. tp_inst._defaults.millisec > tp_inst._defaults.millisecMax ? tp_inst._defaults.millisecMax : tp_inst._defaults.millisec;
  253. tp_inst.microsec = tp_inst._defaults.microsec < tp_inst._defaults.microsecMin ? tp_inst._defaults.microsecMin :
  254. tp_inst._defaults.microsec > tp_inst._defaults.microsecMax ? tp_inst._defaults.microsecMax : tp_inst._defaults.microsec;
  255. tp_inst.ampm = '';
  256. tp_inst.$input = $input;
  257. if (tp_inst._defaults.altField) {
  258. tp_inst.$altInput = $(tp_inst._defaults.altField).css({
  259. cursor: 'pointer'
  260. }).focus(function () {
  261. $input.trigger("focus");
  262. });
  263. }
  264. if (tp_inst._defaults.minDate === 0 || tp_inst._defaults.minDateTime === 0) {
  265. tp_inst._defaults.minDate = new Date();
  266. }
  267. if (tp_inst._defaults.maxDate === 0 || tp_inst._defaults.maxDateTime === 0) {
  268. tp_inst._defaults.maxDate = new Date();
  269. }
  270. // datepicker needs minDate/maxDate, timepicker needs minDateTime/maxDateTime..
  271. if (tp_inst._defaults.minDate !== undefined && tp_inst._defaults.minDate instanceof Date) {
  272. tp_inst._defaults.minDateTime = new Date(tp_inst._defaults.minDate.getTime());
  273. }
  274. if (tp_inst._defaults.minDateTime !== undefined && tp_inst._defaults.minDateTime instanceof Date) {
  275. tp_inst._defaults.minDate = new Date(tp_inst._defaults.minDateTime.getTime());
  276. }
  277. if (tp_inst._defaults.maxDate !== undefined && tp_inst._defaults.maxDate instanceof Date) {
  278. tp_inst._defaults.maxDateTime = new Date(tp_inst._defaults.maxDate.getTime());
  279. }
  280. if (tp_inst._defaults.maxDateTime !== undefined && tp_inst._defaults.maxDateTime instanceof Date) {
  281. tp_inst._defaults.maxDate = new Date(tp_inst._defaults.maxDateTime.getTime());
  282. }
  283. tp_inst.$input.bind('focus', function () {
  284. tp_inst._onFocus();
  285. });
  286. return tp_inst;
  287. },
  288. /*
  289. * add our sliders to the calendar
  290. */
  291. _addTimePicker: function (dp_inst) {
  292. var currDT = (this.$altInput && this._defaults.altFieldTimeOnly) ? this.$input.val() + ' ' + this.$altInput.val() : this.$input.val();
  293. this.timeDefined = this._parseTime(currDT);
  294. this._limitMinMaxDateTime(dp_inst, false);
  295. this._injectTimePicker();
  296. },
  297. /*
  298. * parse the time string from input value or _setTime
  299. */
  300. _parseTime: function (timeString, withDate) {
  301. if (!this.inst) {
  302. this.inst = $.datepicker._getInst(this.$input[0]);
  303. }
  304. if (withDate || !this._defaults.timeOnly) {
  305. var dp_dateFormat = $.datepicker._get(this.inst, 'dateFormat');
  306. try {
  307. var parseRes = parseDateTimeInternal(dp_dateFormat, this._defaults.timeFormat, timeString, $.datepicker._getFormatConfig(this.inst), this._defaults);
  308. if (!parseRes.timeObj) {
  309. return false;
  310. }
  311. $.extend(this, parseRes.timeObj);
  312. } catch (err) {
  313. $.timepicker.log("Error parsing the date/time string: " + err +
  314. "\ndate/time string = " + timeString +
  315. "\ntimeFormat = " + this._defaults.timeFormat +
  316. "\ndateFormat = " + dp_dateFormat);
  317. return false;
  318. }
  319. return true;
  320. } else {
  321. var timeObj = $.datepicker.parseTime(this._defaults.timeFormat, timeString, this._defaults);
  322. if (!timeObj) {
  323. return false;
  324. }
  325. $.extend(this, timeObj);
  326. return true;
  327. }
  328. },
  329. /*
  330. * generate and inject html for timepicker into ui datepicker
  331. */
  332. _injectTimePicker: function () {
  333. var $dp = this.inst.dpDiv,
  334. o = this.inst.settings,
  335. tp_inst = this,
  336. litem = '',
  337. uitem = '',
  338. show = null,
  339. max = {},
  340. gridSize = {},
  341. size = null,
  342. i = 0,
  343. l = 0;
  344. // Prevent displaying twice
  345. if ($dp.find("div.ui-timepicker-div").length === 0 && o.showTimepicker) {
  346. var noDisplay = ' style="display:none;"',
  347. html = '<div class="ui-timepicker-div' + (o.isRTL ? ' ui-timepicker-rtl' : '') + '"><dl>' + '<dt class="ui_tpicker_time_label"' + ((o.showTime) ? '' : noDisplay) + '>' + o.timeText + '</dt>' +
  348. '<dd class="ui_tpicker_time"' + ((o.showTime) ? '' : noDisplay) + '></dd>';
  349. // Create the markup
  350. for (i = 0, l = this.units.length; i < l; i++) {
  351. litem = this.units[i];
  352. uitem = litem.substr(0, 1).toUpperCase() + litem.substr(1);
  353. show = o['show' + uitem] !== null ? o['show' + uitem] : this.support[litem];
  354. // Added by Peter Medeiros:
  355. // - Figure out what the hour/minute/second max should be based on the step values.
  356. // - Example: if stepMinute is 15, then minMax is 45.
  357. max[litem] = parseInt((o[litem + 'Max'] - ((o[litem + 'Max'] - o[litem + 'Min']) % o['step' + uitem])), 10);
  358. gridSize[litem] = 0;
  359. html += '<dt class="ui_tpicker_' + litem + '_label"' + (show ? '' : noDisplay) + '>' + o[litem + 'Text'] + '</dt>' +
  360. '<dd class="ui_tpicker_' + litem + '"><div class="ui_tpicker_' + litem + '_slider"' + (show ? '' : noDisplay) + '></div>';
  361. if (show && o[litem + 'Grid'] > 0) {
  362. html += '<div style="padding-left: 1px"><table class="ui-tpicker-grid-label"><tr>';
  363. if (litem === 'hour') {
  364. for (var h = o[litem + 'Min']; h <= max[litem]; h += parseInt(o[litem + 'Grid'], 10)) {
  365. gridSize[litem]++;
  366. var tmph = $.datepicker.formatTime(this.support.ampm ? 'hht' : 'HH', {hour: h}, o);
  367. html += '<td data-for="' + litem + '">' + tmph + '</td>';
  368. }
  369. }
  370. else {
  371. for (var m = o[litem + 'Min']; m <= max[litem]; m += parseInt(o[litem + 'Grid'], 10)) {
  372. gridSize[litem]++;
  373. html += '<td data-for="' + litem + '">' + ((m < 10) ? '0' : '') + m + '</td>';
  374. }
  375. }
  376. html += '</tr></table></div>';
  377. }
  378. html += '</dd>';
  379. }
  380. // Timezone
  381. var showTz = o.showTimezone !== null ? o.showTimezone : this.support.timezone;
  382. html += '<dt class="ui_tpicker_timezone_label"' + (showTz ? '' : noDisplay) + '>' + o.timezoneText + '</dt>';
  383. html += '<dd class="ui_tpicker_timezone" ' + (showTz ? '' : noDisplay) + '></dd>';
  384. // Create the elements from string
  385. html += '</dl></div>';
  386. var $tp = $(html);
  387. // if we only want time picker...
  388. if (o.timeOnly === true) {
  389. $tp.prepend('<div class="ui-widget-header ui-helper-clearfix ui-corner-all">' + '<div class="ui-datepicker-title">' + o.timeOnlyTitle + '</div>' + '</div>');
  390. $dp.find('.ui-datepicker-header, .ui-datepicker-calendar').hide();
  391. }
  392. // add sliders, adjust grids, add events
  393. for (i = 0, l = tp_inst.units.length; i < l; i++) {
  394. litem = tp_inst.units[i];
  395. uitem = litem.substr(0, 1).toUpperCase() + litem.substr(1);
  396. show = o['show' + uitem] !== null ? o['show' + uitem] : this.support[litem];
  397. // add the slider
  398. tp_inst[litem + '_slider'] = tp_inst.control.create(tp_inst, $tp.find('.ui_tpicker_' + litem + '_slider'), litem, tp_inst[litem], o[litem + 'Min'], max[litem], o['step' + uitem]);
  399. // adjust the grid and add click event
  400. if (show && o[litem + 'Grid'] > 0) {
  401. size = 100 * gridSize[litem] * o[litem + 'Grid'] / (max[litem] - o[litem + 'Min']);
  402. $tp.find('.ui_tpicker_' + litem + ' table').css({
  403. width: size + "%",
  404. marginLeft: o.isRTL ? '0' : ((size / (-2 * gridSize[litem])) + "%"),
  405. marginRight: o.isRTL ? ((size / (-2 * gridSize[litem])) + "%") : '0',
  406. borderCollapse: 'collapse'
  407. }).find("td").click(function (e) {
  408. var $t = $(this),
  409. h = $t.html(),
  410. n = parseInt(h.replace(/[^0-9]/g), 10),
  411. ap = h.replace(/[^apm]/ig),
  412. f = $t.data('for'); // loses scope, so we use data-for
  413. if (f === 'hour') {
  414. if (ap.indexOf('p') !== -1 && n < 12) {
  415. n += 12;
  416. }
  417. else {
  418. if (ap.indexOf('a') !== -1 && n === 12) {
  419. n = 0;
  420. }
  421. }
  422. }
  423. tp_inst.control.value(tp_inst, tp_inst[f + '_slider'], litem, n);
  424. tp_inst._onTimeChange();
  425. tp_inst._onSelectHandler();
  426. }).css({
  427. cursor: 'pointer',
  428. width: (100 / gridSize[litem]) + '%',
  429. textAlign: 'center',
  430. overflow: 'hidden'
  431. });
  432. } // end if grid > 0
  433. } // end for loop
  434. // Add timezone options
  435. this.timezone_select = $tp.find('.ui_tpicker_timezone').append('<select></select>').find("select");
  436. $.fn.append.apply(this.timezone_select,
  437. $.map(o.timezoneList, function (val, idx) {
  438. return $("<option />").val(typeof val === "object" ? val.value : val).text(typeof val === "object" ? val.label : val);
  439. }));
  440. if (typeof(this.timezone) !== "undefined" && this.timezone !== null && this.timezone !== "") {
  441. var local_timezone = (new Date(this.inst.selectedYear, this.inst.selectedMonth, this.inst.selectedDay, 12)).getTimezoneOffset() * -1;
  442. if (local_timezone === this.timezone) {
  443. selectLocalTimezone(tp_inst);
  444. } else {
  445. this.timezone_select.val(this.timezone);
  446. }
  447. } else {
  448. if (typeof(this.hour) !== "undefined" && this.hour !== null && this.hour !== "") {
  449. this.timezone_select.val(o.timezone);
  450. } else {
  451. selectLocalTimezone(tp_inst);
  452. }
  453. }
  454. this.timezone_select.change(function () {
  455. tp_inst._onTimeChange();
  456. tp_inst._onSelectHandler();
  457. });
  458. // End timezone options
  459. // inject timepicker into datepicker
  460. var $buttonPanel = $dp.find('.ui-datepicker-buttonpane');
  461. if ($buttonPanel.length) {
  462. $buttonPanel.before($tp);
  463. } else {
  464. $dp.append($tp);
  465. }
  466. this.$timeObj = $tp.find('.ui_tpicker_time');
  467. if (this.inst !== null) {
  468. var timeDefined = this.timeDefined;
  469. this._onTimeChange();
  470. this.timeDefined = timeDefined;
  471. }
  472. // slideAccess integration: http://trentrichardson.com/2011/11/11/jquery-ui-sliders-and-touch-accessibility/
  473. if (this._defaults.addSliderAccess) {
  474. var sliderAccessArgs = this._defaults.sliderAccessArgs,
  475. rtl = this._defaults.isRTL;
  476. sliderAccessArgs.isRTL = rtl;
  477. setTimeout(function () { // fix for inline mode
  478. if ($tp.find('.ui-slider-access').length === 0) {
  479. $tp.find('.ui-slider:visible').sliderAccess(sliderAccessArgs);
  480. // fix any grids since sliders are shorter
  481. var sliderAccessWidth = $tp.find('.ui-slider-access:eq(0)').outerWidth(true);
  482. if (sliderAccessWidth) {
  483. $tp.find('table:visible').each(function () {
  484. var $g = $(this),
  485. oldWidth = $g.outerWidth(),
  486. oldMarginLeft = $g.css(rtl ? 'marginRight' : 'marginLeft').toString().replace('%', ''),
  487. newWidth = oldWidth - sliderAccessWidth,
  488. newMarginLeft = ((oldMarginLeft * newWidth) / oldWidth) + '%',
  489. css = { width: newWidth, marginRight: 0, marginLeft: 0 };
  490. css[rtl ? 'marginRight' : 'marginLeft'] = newMarginLeft;
  491. $g.css(css);
  492. });
  493. }
  494. }
  495. }, 10);
  496. }
  497. // end slideAccess integration
  498. tp_inst._limitMinMaxDateTime(this.inst, true);
  499. }
  500. },
  501. /*
  502. * This function tries to limit the ability to go outside the
  503. * min/max date range
  504. */
  505. _limitMinMaxDateTime: function (dp_inst, adjustSliders) {
  506. var o = this._defaults,
  507. dp_date = new Date(dp_inst.selectedYear, dp_inst.selectedMonth, dp_inst.selectedDay);
  508. if (!this._defaults.showTimepicker) {
  509. return;
  510. } // No time so nothing to check here
  511. if ($.datepicker._get(dp_inst, 'minDateTime') !== null && $.datepicker._get(dp_inst, 'minDateTime') !== undefined && dp_date) {
  512. var minDateTime = $.datepicker._get(dp_inst, 'minDateTime'),
  513. minDateTimeDate = new Date(minDateTime.getFullYear(), minDateTime.getMonth(), minDateTime.getDate(), 0, 0, 0, 0);
  514. if (this.hourMinOriginal === null || this.minuteMinOriginal === null || this.secondMinOriginal === null || this.millisecMinOriginal === null || this.microsecMinOriginal === null) {
  515. this.hourMinOriginal = o.hourMin;
  516. this.minuteMinOriginal = o.minuteMin;
  517. this.secondMinOriginal = o.secondMin;
  518. this.millisecMinOriginal = o.millisecMin;
  519. this.microsecMinOriginal = o.microsecMin;
  520. }
  521. if (dp_inst.settings.timeOnly || minDateTimeDate.getTime() === dp_date.getTime()) {
  522. this._defaults.hourMin = minDateTime.getHours();
  523. if (this.hour <= this._defaults.hourMin) {
  524. this.hour = this._defaults.hourMin;
  525. this._defaults.minuteMin = minDateTime.getMinutes();
  526. if (this.minute <= this._defaults.minuteMin) {
  527. this.minute = this._defaults.minuteMin;
  528. this._defaults.secondMin = minDateTime.getSeconds();
  529. if (this.second <= this._defaults.secondMin) {
  530. this.second = this._defaults.secondMin;
  531. this._defaults.millisecMin = minDateTime.getMilliseconds();
  532. if (this.millisec <= this._defaults.millisecMin) {
  533. this.millisec = this._defaults.millisecMin;
  534. this._defaults.microsecMin = minDateTime.getMicroseconds();
  535. } else {
  536. if (this.microsec < this._defaults.microsecMin) {
  537. this.microsec = this._defaults.microsecMin;
  538. }
  539. this._defaults.microsecMin = this.microsecMinOriginal;
  540. }
  541. } else {
  542. this._defaults.millisecMin = this.millisecMinOriginal;
  543. this._defaults.microsecMin = this.microsecMinOriginal;
  544. }
  545. } else {
  546. this._defaults.secondMin = this.secondMinOriginal;
  547. this._defaults.millisecMin = this.millisecMinOriginal;
  548. this._defaults.microsecMin = this.microsecMinOriginal;
  549. }
  550. } else {
  551. this._defaults.minuteMin = this.minuteMinOriginal;
  552. this._defaults.secondMin = this.secondMinOriginal;
  553. this._defaults.millisecMin = this.millisecMinOriginal;
  554. this._defaults.microsecMin = this.microsecMinOriginal;
  555. }
  556. } else {
  557. this._defaults.hourMin = this.hourMinOriginal;
  558. this._defaults.minuteMin = this.minuteMinOriginal;
  559. this._defaults.secondMin = this.secondMinOriginal;
  560. this._defaults.millisecMin = this.millisecMinOriginal;
  561. this._defaults.microsecMin = this.microsecMinOriginal;
  562. }
  563. }
  564. if ($.datepicker._get(dp_inst, 'maxDateTime') !== null && $.datepicker._get(dp_inst, 'maxDateTime') !== undefined && dp_date) {
  565. var maxDateTime = $.datepicker._get(dp_inst, 'maxDateTime'),
  566. maxDateTimeDate = new Date(maxDateTime.getFullYear(), maxDateTime.getMonth(), maxDateTime.getDate(), 0, 0, 0, 0);
  567. if (this.hourMaxOriginal === null || this.minuteMaxOriginal === null || this.secondMaxOriginal === null || this.millisecMaxOriginal === null) {
  568. this.hourMaxOriginal = o.hourMax;
  569. this.minuteMaxOriginal = o.minuteMax;
  570. this.secondMaxOriginal = o.secondMax;
  571. this.millisecMaxOriginal = o.millisecMax;
  572. this.microsecMaxOriginal = o.microsecMax;
  573. }
  574. if (dp_inst.settings.timeOnly || maxDateTimeDate.getTime() === dp_date.getTime()) {
  575. this._defaults.hourMax = maxDateTime.getHours();
  576. if (this.hour >= this._defaults.hourMax) {
  577. this.hour = this._defaults.hourMax;
  578. this._defaults.minuteMax = maxDateTime.getMinutes();
  579. if (this.minute >= this._defaults.minuteMax) {
  580. this.minute = this._defaults.minuteMax;
  581. this._defaults.secondMax = maxDateTime.getSeconds();
  582. if (this.second >= this._defaults.secondMax) {
  583. this.second = this._defaults.secondMax;
  584. this._defaults.millisecMax = maxDateTime.getMilliseconds();
  585. if (this.millisec >= this._defaults.millisecMax) {
  586. this.millisec = this._defaults.millisecMax;
  587. this._defaults.microsecMax = maxDateTime.getMicroseconds();
  588. } else {
  589. if (this.microsec > this._defaults.microsecMax) {
  590. this.microsec = this._defaults.microsecMax;
  591. }
  592. this._defaults.microsecMax = this.microsecMaxOriginal;
  593. }
  594. } else {
  595. this._defaults.millisecMax = this.millisecMaxOriginal;
  596. this._defaults.microsecMax = this.microsecMaxOriginal;
  597. }
  598. } else {
  599. this._defaults.secondMax = this.secondMaxOriginal;
  600. this._defaults.millisecMax = this.millisecMaxOriginal;
  601. this._defaults.microsecMax = this.microsecMaxOriginal;
  602. }
  603. } else {
  604. this._defaults.minuteMax = this.minuteMaxOriginal;
  605. this._defaults.secondMax = this.secondMaxOriginal;
  606. this._defaults.millisecMax = this.millisecMaxOriginal;
  607. this._defaults.microsecMax = this.microsecMaxOriginal;
  608. }
  609. } else {
  610. this._defaults.hourMax = this.hourMaxOriginal;
  611. this._defaults.minuteMax = this.minuteMaxOriginal;
  612. this._defaults.secondMax = this.secondMaxOriginal;
  613. this._defaults.millisecMax = this.millisecMaxOriginal;
  614. this._defaults.microsecMax = this.microsecMaxOriginal;
  615. }
  616. }
  617. if (adjustSliders !== undefined && adjustSliders === true) {
  618. var hourMax = parseInt((this._defaults.hourMax - ((this._defaults.hourMax - this._defaults.hourMin) % this._defaults.stepHour)), 10),
  619. minMax = parseInt((this._defaults.minuteMax - ((this._defaults.minuteMax - this._defaults.minuteMin) % this._defaults.stepMinute)), 10),
  620. secMax = parseInt((this._defaults.secondMax - ((this._defaults.secondMax - this._defaults.secondMin) % this._defaults.stepSecond)), 10),
  621. millisecMax = parseInt((this._defaults.millisecMax - ((this._defaults.millisecMax - this._defaults.millisecMin) % this._defaults.stepMillisec)), 10),
  622. microsecMax = parseInt((this._defaults.microsecMax - ((this._defaults.microsecMax - this._defaults.microsecMin) % this._defaults.stepMicrosec)), 10);
  623. if (this.hour_slider) {
  624. this.control.options(this, this.hour_slider, 'hour', { min: this._defaults.hourMin, max: hourMax });
  625. this.control.value(this, this.hour_slider, 'hour', this.hour - (this.hour % this._defaults.stepHour));
  626. }
  627. if (this.minute_slider) {
  628. this.control.options(this, this.minute_slider, 'minute', { min: this._defaults.minuteMin, max: minMax });
  629. this.control.value(this, this.minute_slider, 'minute', this.minute - (this.minute % this._defaults.stepMinute));
  630. }
  631. if (this.second_slider) {
  632. this.control.options(this, this.second_slider, 'second', { min: this._defaults.secondMin, max: secMax });
  633. this.control.value(this, this.second_slider, 'second', this.second - (this.second % this._defaults.stepSecond));
  634. }
  635. if (this.millisec_slider) {
  636. this.control.options(this, this.millisec_slider, 'millisec', { min: this._defaults.millisecMin, max: millisecMax });
  637. this.control.value(this, this.millisec_slider, 'millisec', this.millisec - (this.millisec % this._defaults.stepMillisec));
  638. }
  639. if (this.microsec_slider) {
  640. this.control.options(this, this.microsec_slider, 'microsec', { min: this._defaults.microsecMin, max: microsecMax });
  641. this.control.value(this, this.microsec_slider, 'microsec', this.microsec - (this.microsec % this._defaults.stepMicrosec));
  642. }
  643. }
  644. },
  645. /*
  646. * when a slider moves, set the internal time...
  647. * on time change is also called when the time is updated in the text field
  648. */
  649. _onTimeChange: function () {
  650. if (!this._defaults.showTimepicker) {
  651. return;
  652. }
  653. var hour = (this.hour_slider) ? this.control.value(this, this.hour_slider, 'hour') : false,
  654. minute = (this.minute_slider) ? this.control.value(this, this.minute_slider, 'minute') : false,
  655. second = (this.second_slider) ? this.control.value(this, this.second_slider, 'second') : false,
  656. millisec = (this.millisec_slider) ? this.control.value(this, this.millisec_slider, 'millisec') : false,
  657. microsec = (this.microsec_slider) ? this.control.value(this, this.microsec_slider, 'microsec') : false,
  658. timezone = (this.timezone_select) ? this.timezone_select.val() : false,
  659. o = this._defaults,
  660. pickerTimeFormat = o.pickerTimeFormat || o.timeFormat,
  661. pickerTimeSuffix = o.pickerTimeSuffix || o.timeSuffix;
  662. if (typeof(hour) === 'object') {
  663. hour = false;
  664. }
  665. if (typeof(minute) === 'object') {
  666. minute = false;
  667. }
  668. if (typeof(second) === 'object') {
  669. second = false;
  670. }
  671. if (typeof(millisec) === 'object') {
  672. millisec = false;
  673. }
  674. if (typeof(microsec) === 'object') {
  675. microsec = false;
  676. }
  677. if (typeof(timezone) === 'object') {
  678. timezone = false;
  679. }
  680. if (hour !== false) {
  681. hour = parseInt(hour, 10);
  682. }
  683. if (minute !== false) {
  684. minute = parseInt(minute, 10);
  685. }
  686. if (second !== false) {
  687. second = parseInt(second, 10);
  688. }
  689. if (millisec !== false) {
  690. millisec = parseInt(millisec, 10);
  691. }
  692. if (microsec !== false) {
  693. microsec = parseInt(microsec, 10);
  694. }
  695. if (timezone !== false) {
  696. timezone = timezone.toString();
  697. }
  698. var ampm = o[hour < 12 ? 'amNames' : 'pmNames'][0];
  699. // If the update was done in the input field, the input field should not be updated.
  700. // If the update was done using the sliders, update the input field.
  701. var hasChanged = (
  702. hour !== parseInt(this.hour,10) || // sliders should all be numeric
  703. minute !== parseInt(this.minute,10) ||
  704. second !== parseInt(this.second,10) ||
  705. millisec !== parseInt(this.millisec,10) ||
  706. microsec !== parseInt(this.microsec,10) ||
  707. (this.ampm.length > 0 && (hour < 12) !== ($.inArray(this.ampm.toUpperCase(), this.amNames) !== -1)) ||
  708. (this.timezone !== null && timezone !== this.timezone.toString()) // could be numeric or "EST" format, so use toString()
  709. );
  710. if (hasChanged) {
  711. if (hour !== false) {
  712. this.hour = hour;
  713. }
  714. if (minute !== false) {
  715. this.minute = minute;
  716. }
  717. if (second !== false) {
  718. this.second = second;
  719. }
  720. if (millisec !== false) {
  721. this.millisec = millisec;
  722. }
  723. if (microsec !== false) {
  724. this.microsec = microsec;
  725. }
  726. if (timezone !== false) {
  727. this.timezone = timezone;
  728. }
  729. if (!this.inst) {
  730. this.inst = $.datepicker._getInst(this.$input[0]);
  731. }
  732. this._limitMinMaxDateTime(this.inst, true);
  733. }
  734. if (this.support.ampm) {
  735. this.ampm = ampm;
  736. }
  737. // Updates the time within the timepicker
  738. this.formattedTime = $.datepicker.formatTime(o.timeFormat, this, o);
  739. if (this.$timeObj) {
  740. if (pickerTimeFormat === o.timeFormat) {
  741. this.$timeObj.text(this.formattedTime + pickerTimeSuffix);
  742. }
  743. else {
  744. this.$timeObj.text($.datepicker.formatTime(pickerTimeFormat, this, o) + pickerTimeSuffix);
  745. }
  746. }
  747. this.timeDefined = true;
  748. if (hasChanged) {
  749. this._updateDateTime();
  750. this.$input.focus();
  751. }
  752. },
  753. /*
  754. * call custom onSelect.
  755. * bind to sliders slidestop, and grid click.
  756. */
  757. _onSelectHandler: function () {
  758. var onSelect = this._defaults.onSelect || this.inst.settings.onSelect;
  759. var inputEl = this.$input ? this.$input[0] : null;
  760. if (onSelect && inputEl) {
  761. onSelect.apply(inputEl, [this.formattedDateTime, this]);
  762. }
  763. },
  764. /*
  765. * update our input with the new date time..
  766. */
  767. _updateDateTime: function (dp_inst) {
  768. dp_inst = this.inst || dp_inst;
  769. var dtTmp = (dp_inst.currentYear > 0?
  770. new Date(dp_inst.currentYear, dp_inst.currentMonth, dp_inst.currentDay) :
  771. new Date(dp_inst.selectedYear, dp_inst.selectedMonth, dp_inst.selectedDay)),
  772. dt = $.datepicker._daylightSavingAdjust(dtTmp),
  773. //dt = $.datepicker._daylightSavingAdjust(new Date(dp_inst.selectedYear, dp_inst.selectedMonth, dp_inst.selectedDay)),
  774. //dt = $.datepicker._daylightSavingAdjust(new Date(dp_inst.currentYear, dp_inst.currentMonth, dp_inst.currentDay)),
  775. dateFmt = $.datepicker._get(dp_inst, 'dateFormat'),
  776. formatCfg = $.datepicker._getFormatConfig(dp_inst),
  777. timeAvailable = dt !== null && this.timeDefined;
  778. this.formattedDate = $.datepicker.formatDate(dateFmt, (dt === null ? new Date() : dt), formatCfg);
  779. var formattedDateTime = this.formattedDate;
  780. // if a slider was changed but datepicker doesn't have a value yet, set it
  781. if (dp_inst.lastVal === "") {
  782. dp_inst.currentYear = dp_inst.selectedYear;
  783. dp_inst.currentMonth = dp_inst.selectedMonth;
  784. dp_inst.currentDay = dp_inst.selectedDay;
  785. }
  786. /*
  787. * remove following lines to force every changes in date picker to change the input value
  788. * Bug descriptions: when an input field has a default value, and click on the field to pop up the date picker.
  789. * If the user manually empty the value in the input field, the date picker will never change selected value.
  790. */
  791. //if (dp_inst.lastVal !== undefined && (dp_inst.lastVal.length > 0 && this.$input.val().length === 0)) {
  792. // return;
  793. //}
  794. if (this._defaults.timeOnly === true) {
  795. formattedDateTime = this.formattedTime;
  796. } else if (this._defaults.timeOnly !== true && (this._defaults.alwaysSetTime || timeAvailable)) {
  797. formattedDateTime += this._defaults.separator + this.formattedTime + this._defaults.timeSuffix;
  798. }
  799. this.formattedDateTime = formattedDateTime;
  800. if (!this._defaults.showTimepicker) {
  801. this.$input.val(this.formattedDate);
  802. } else if (this.$altInput && this._defaults.timeOnly === false && this._defaults.altFieldTimeOnly === true) {
  803. this.$altInput.val(this.formattedTime);
  804. this.$input.val(this.formattedDate);
  805. } else if (this.$altInput) {
  806. this.$input.val(formattedDateTime);
  807. var altFormattedDateTime = '',
  808. altSeparator = this._defaults.altSeparator ? this._defaults.altSeparator : this._defaults.separator,
  809. altTimeSuffix = this._defaults.altTimeSuffix ? this._defaults.altTimeSuffix : this._defaults.timeSuffix;
  810. if (!this._defaults.timeOnly) {
  811. if (this._defaults.altFormat) {
  812. altFormattedDateTime = $.datepicker.formatDate(this._defaults.altFormat, (dt === null ? new Date() : dt), formatCfg);
  813. }
  814. else {
  815. altFormattedDateTime = this.formattedDate;
  816. }
  817. if (altFormattedDateTime) {
  818. altFormattedDateTime += altSeparator;
  819. }
  820. }
  821. if (this._defaults.altTimeFormat) {
  822. altFormattedDateTime += $.datepicker.formatTime(this._defaults.altTimeFormat, this, this._defaults) + altTimeSuffix;
  823. }
  824. else {
  825. altFormattedDateTime += this.formattedTime + altTimeSuffix;
  826. }
  827. this.$altInput.val(altFormattedDateTime);
  828. } else {
  829. this.$input.val(formattedDateTime);
  830. }
  831. this.$input.trigger("change");
  832. },
  833. _onFocus: function () {
  834. if (!this.$input.val() && this._defaults.defaultValue) {
  835. this.$input.val(this._defaults.defaultValue);
  836. var inst = $.datepicker._getInst(this.$input.get(0)),
  837. tp_inst = $.datepicker._get(inst, 'timepicker');
  838. if (tp_inst) {
  839. if (tp_inst._defaults.timeOnly && (inst.input.val() !== inst.lastVal)) {
  840. try {
  841. $.datepicker._updateDatepicker(inst);
  842. } catch (err) {
  843. $.timepicker.log(err);
  844. }
  845. }
  846. }
  847. }
  848. },
  849. /*
  850. * Small abstraction to control types
  851. * We can add more, just be sure to follow the pattern: create, options, value
  852. */
  853. _controls: {
  854. // slider methods
  855. slider: {
  856. create: function (tp_inst, obj, unit, val, min, max, step) {
  857. var rtl = tp_inst._defaults.isRTL; // if rtl go -60->0 instead of 0->60
  858. return obj.prop('slide', null).slider({
  859. orientation: "horizontal",
  860. value: rtl ? val * -1 : val,
  861. min: rtl ? max * -1 : min,
  862. max: rtl ? min * -1 : max,
  863. step: step,
  864. slide: function (event, ui) {
  865. tp_inst.control.value(tp_inst, $(this), unit, rtl ? ui.value * -1 : ui.value);
  866. tp_inst._onTimeChange();
  867. },
  868. stop: function (event, ui) {
  869. tp_inst._onSelectHandler();
  870. }
  871. });
  872. },
  873. options: function (tp_inst, obj, unit, opts, val) {
  874. if (tp_inst._defaults.isRTL) {
  875. if (typeof(opts) === 'string') {
  876. if (opts === 'min' || opts === 'max') {
  877. if (val !== undefined) {
  878. return obj.slider(opts, val * -1);
  879. }
  880. return Math.abs(obj.slider(opts));
  881. }
  882. return obj.slider(opts);
  883. }
  884. var min = opts.min,
  885. max = opts.max;
  886. opts.min = opts.max = null;
  887. if (min !== undefined) {
  888. opts.max = min * -1;
  889. }
  890. if (max !== undefined) {
  891. opts.min = max * -1;
  892. }
  893. return obj.slider(opts);
  894. }
  895. if (typeof(opts) === 'string' && val !== undefined) {
  896. return obj.slider(opts, val);
  897. }
  898. return obj.slider(opts);
  899. },
  900. value: function (tp_inst, obj, unit, val) {
  901. if (tp_inst._defaults.isRTL) {
  902. if (val !== undefined) {
  903. return obj.slider('value', val * -1);
  904. }
  905. return Math.abs(obj.slider('value'));
  906. }
  907. if (val !== undefined) {
  908. return obj.slider('value', val);
  909. }
  910. return obj.slider('value');
  911. }
  912. },
  913. // select methods
  914. select: {
  915. create: function (tp_inst, obj, unit, val, min, max, step) {
  916. var sel = '<select class="ui-timepicker-select" data-unit="' + unit + '" data-min="' + min + '" data-max="' + max + '" data-step="' + step + '">',
  917. format = tp_inst._defaults.pickerTimeFormat || tp_inst._defaults.timeFormat;
  918. for (var i = min; i <= max; i += step) {
  919. sel += '<option value="' + i + '"' + (i === val ? ' selected' : '') + '>';
  920. if (unit === 'hour') {
  921. sel += $.datepicker.formatTime($.trim(format.replace(/[^ht ]/ig, '')), {hour: i}, tp_inst._defaults);
  922. }
  923. else if (unit === 'millisec' || unit === 'microsec' || i >= 10) { sel += i; }
  924. else {sel += '0' + i.toString(); }
  925. sel += '</option>';
  926. }
  927. sel += '</select>';
  928. obj.children('select').remove();
  929. $(sel).appendTo(obj).change(function (e) {
  930. tp_inst._onTimeChange();
  931. tp_inst._onSelectHandler();
  932. });
  933. return obj;
  934. },
  935. options: function (tp_inst, obj, unit, opts, val) {
  936. var o = {},
  937. $t = obj.children('select');
  938. if (typeof(opts) === 'string') {
  939. if (val === undefined) {
  940. return $t.data(opts);
  941. }
  942. o[opts] = val;
  943. }
  944. else { o = opts; }
  945. return tp_inst.control.create(tp_inst, obj, $t.data('unit'), $t.val(), o.min || $t.data('min'), o.max || $t.data('max'), o.step || $t.data('step'));
  946. },
  947. value: function (tp_inst, obj, unit, val) {
  948. var $t = obj.children('select');
  949. if (val !== undefined) {
  950. return $t.val(val);
  951. }
  952. return $t.val();
  953. }
  954. }
  955. } // end _controls
  956. });
  957. $.fn.extend({
  958. /*
  959. * shorthand just to use timepicker.
  960. */
  961. timepicker: function (o) {
  962. o = o || {};
  963. var tmp_args = Array.prototype.slice.call(arguments);
  964. if (typeof o === 'object') {
  965. tmp_args[0] = $.extend(o, {
  966. timeOnly: true
  967. });
  968. }
  969. return $(this).each(function () {
  970. $.fn.datetimepicker.apply($(this), tmp_args);
  971. });
  972. },
  973. /*
  974. * extend timepicker to datepicker
  975. */
  976. datetimepicker: function (o) {
  977. o = o || {};
  978. var tmp_args = arguments;
  979. if (typeof(o) === 'string') {
  980. if (o === 'getDate') {
  981. return $.fn.datepicker.apply($(this[0]), tmp_args);
  982. } else {
  983. return this.each(function () {
  984. var $t = $(this);
  985. $t.datepicker.apply($t, tmp_args);
  986. });
  987. }
  988. } else {
  989. return this.each(function () {
  990. var $t = $(this);
  991. $t.datepicker($.timepicker._newInst($t, o)._defaults);
  992. });
  993. }
  994. }
  995. });
  996. /*
  997. * Public Utility to parse date and time
  998. */
  999. $.datepicker.parseDateTime = function (dateFormat, timeFormat, dateTimeString, dateSettings, timeSettings) {
  1000. var parseRes = parseDateTimeInternal(dateFormat, timeFormat, dateTimeString, dateSettings, timeSettings);
  1001. if (parseRes.timeObj) {
  1002. var t = parseRes.timeObj;
  1003. parseRes.date.setHours(t.hour, t.minute, t.second, t.millisec);
  1004. parseRes.date.setMicroseconds(t.microsec);
  1005. }
  1006. return parseRes.date;
  1007. };
  1008. /*
  1009. * Public utility to parse time
  1010. */
  1011. $.datepicker.parseTime = function (timeFormat, timeString, options) {
  1012. var o = extendRemove(extendRemove({}, $.timepicker._defaults), options || {}),
  1013. iso8601 = (timeFormat.replace(/\'.*?\'/g, '').indexOf('Z') !== -1);
  1014. // Strict parse requires the timeString to match the timeFormat exactly
  1015. var strictParse = function (f, s, o) {
  1016. // pattern for standard and localized AM/PM markers
  1017. var getPatternAmpm = function (amNames, pmNames) {
  1018. var markers = [];
  1019. if (amNames) {
  1020. $.merge(markers, amNames);
  1021. }
  1022. if (pmNames) {
  1023. $.merge(markers, pmNames);
  1024. }
  1025. markers = $.map(markers, function (val) {
  1026. return val.replace(/[.*+?|()\[\]{}\\]/g, '\\$&');
  1027. });
  1028. return '(' + markers.join('|') + ')?';
  1029. };
  1030. // figure out position of time elements.. cause js cant do named captures
  1031. var getFormatPositions = function (timeFormat) {
  1032. var finds = timeFormat.toLowerCase().match(/(h{1,2}|m{1,2}|s{1,2}|l{1}|c{1}|t{1,2}|z|'.*?')/g),
  1033. orders = {
  1034. h: -1,
  1035. m: -1,
  1036. s: -1,
  1037. l: -1,
  1038. c: -1,
  1039. t: -1,
  1040. z: -1
  1041. };
  1042. if (finds) {
  1043. for (var i = 0; i < finds.length; i++) {
  1044. if (orders[finds[i].toString().charAt(0)] === -1) {
  1045. orders[finds[i].toString().charAt(0)] = i + 1;
  1046. }
  1047. }
  1048. }
  1049. return orders;
  1050. };
  1051. var regstr = '^' + f.toString()
  1052. .replace(/([hH]{1,2}|mm?|ss?|[tT]{1,2}|[zZ]|[lc]|'.*?')/g, function (match) {
  1053. var ml = match.length;
  1054. switch (match.charAt(0).toLowerCase()) {
  1055. case 'h':
  1056. return ml === 1 ? '(\\d?\\d)' : '(\\d{' + ml + '})';
  1057. case 'm':
  1058. return ml === 1 ? '(\\d?\\d)' : '(\\d{' + ml + '})';
  1059. case 's':
  1060. return ml === 1 ? '(\\d?\\d)' : '(\\d{' + ml + '})';
  1061. case 'l':
  1062. return '(\\d?\\d?\\d)';
  1063. case 'c':
  1064. return '(\\d?\\d?\\d)';
  1065. case 'z':
  1066. return '(z|[-+]\\d\\d:?\\d\\d|\\S+)?';
  1067. case 't':
  1068. return getPatternAmpm(o.amNames, o.pmNames);
  1069. default: // literal escaped in quotes
  1070. return '(' + match.replace(/\'/g, "").replace(/(\.|\$|\^|\\|\/|\(|\)|\[|\]|\?|\+|\*)/g, function (m) { return "\\" + m; }) + ')?';
  1071. }
  1072. })
  1073. .replace(/\s/g, '\\s?') +
  1074. o.timeSuffix + '$',
  1075. order = getFormatPositions(f),
  1076. ampm = '',
  1077. treg;
  1078. treg = s.match(new RegExp(regstr, 'i'));
  1079. var resTime = {
  1080. hour: 0,
  1081. minute: 0,
  1082. second: 0,
  1083. millisec: 0,
  1084. microsec: 0
  1085. };
  1086. if (treg) {
  1087. if (order.t !== -1) {
  1088. if (treg[order.t] === undefined || treg[order.t].length === 0) {
  1089. ampm = '';
  1090. resTime.ampm = '';
  1091. } else {
  1092. ampm = $.inArray(treg[order.t].toUpperCase(), o.amNames) !== -1 ? 'AM' : 'PM';
  1093. resTime.ampm = o[ampm === 'AM' ? 'amNames' : 'pmNames'][0];
  1094. }
  1095. }
  1096. if (order.h !== -1) {
  1097. if (ampm === 'AM' && treg[order.h] === '12') {
  1098. resTime.hour = 0; // 12am = 0 hour
  1099. } else {
  1100. if (ampm === 'PM' && treg[order.h] !== '12') {
  1101. resTime.hour = parseInt(treg[order.h], 10) + 12; // 12pm = 12 hour, any other pm = hour + 12
  1102. } else {
  1103. resTime.hour = Number(treg[order.h]);
  1104. }
  1105. }
  1106. }
  1107. if (order.m !== -1) {
  1108. resTime.minute = Number(treg[order.m]);
  1109. }
  1110. if (order.s !== -1) {
  1111. resTime.second = Number(treg[order.s]);
  1112. }
  1113. if (order.l !== -1) {
  1114. resTime.millisec = Number(treg[order.l]);
  1115. }
  1116. if (order.c !== -1) {
  1117. resTime.microsec = Number(treg[order.c]);
  1118. }
  1119. if (order.z !== -1 && treg[order.z] !== undefined) {
  1120. resTime.timezone = $.timepicker.timezoneOffsetNumber(treg[order.z]);
  1121. }
  1122. return resTime;
  1123. }
  1124. return false;
  1125. };// end strictParse
  1126. // First try JS Date, if that fails, use strictParse
  1127. var looseParse = function (f, s, o) {
  1128. try {
  1129. var d = new Date('2012-01-01 ' + s);
  1130. if (isNaN(d.getTime())) {
  1131. d = new Date('2012-01-01T' + s);
  1132. if (isNaN(d.getTime())) {
  1133. d = new Date('01/01/2012 ' + s);
  1134. if (isNaN(d.getTime())) {
  1135. throw "Unable to parse time with native Date: " + s;
  1136. }
  1137. }
  1138. }
  1139. return {
  1140. hour: d.getHours(),
  1141. minute: d.getMinutes(),
  1142. second: d.getSeconds(),
  1143. millisec: d.getMilliseconds(),
  1144. microsec: d.getMicroseconds(),
  1145. timezone: d.getTimezoneOffset() * -1
  1146. };
  1147. }
  1148. catch (err) {
  1149. try {
  1150. return strictParse(f, s, o);
  1151. }
  1152. catch (err2) {
  1153. $.timepicker.log("Unable to parse \ntimeString: " + s + "\ntimeFormat: " + f);
  1154. }
  1155. }
  1156. return false;
  1157. }; // end looseParse
  1158. if (typeof o.parse === "function") {
  1159. return o.parse(timeFormat, timeString, o);
  1160. }
  1161. if (o.parse === 'loose') {
  1162. return looseParse(timeFormat, timeString, o);
  1163. }
  1164. return strictParse(timeFormat, timeString, o);
  1165. };
  1166. /**
  1167. * Public utility to format the time
  1168. * @param {string} format format of the time
  1169. * @param {Object} time Object not a Date for timezones
  1170. * @param {Object} [options] essentially the regional[].. amNames, pmNames, ampm
  1171. * @returns {string} the formatted time
  1172. */
  1173. $.datepicker.formatTime = function (format, time, options) {
  1174. options = options || {};
  1175. options = $.extend({}, $.timepicker._defaults, options);
  1176. time = $.extend({
  1177. hour: 0,
  1178. minute: 0,
  1179. second: 0,
  1180. millisec: 0,
  1181. microsec: 0,
  1182. timezone: null
  1183. }, time);
  1184. var tmptime = format,
  1185. ampmName = options.amNames[0],
  1186. hour = parseInt(time.hour, 10);
  1187. if (hour > 11) {
  1188. ampmName = options.pmNames[0];
  1189. }
  1190. tmptime = tmptime.replace(/(?:HH?|hh?|mm?|ss?|[tT]{1,2}|[zZ]|[lc]|'.*?')/g, function (match) {
  1191. switch (match) {
  1192. case 'HH':
  1193. return ('0' + hour).slice(-2);
  1194. case 'H':
  1195. return hour;
  1196. case 'hh':
  1197. return ('0' + convert24to12(hour)).slice(-2);
  1198. case 'h':
  1199. return convert24to12(hour);
  1200. case 'mm':
  1201. return ('0' + time.minute).slice(-2);
  1202. case 'm':
  1203. return time.minute;
  1204. case 'ss':
  1205. return ('0' + time.second).slice(-2);
  1206. case 's':
  1207. return time.second;
  1208. case 'l':
  1209. return ('00' + time.millisec).slice(-3);
  1210. case 'c':
  1211. return ('00' + time.microsec).slice(-3);
  1212. case 'z':
  1213. return $.timepicker.timezoneOffsetString(time.timezone === null ? options.timezone : time.timezone, false);
  1214. case 'Z':
  1215. return $.timepicker.timezoneOffsetString(time.timezone === null ? options.timezone : time.timezone, true);
  1216. case 'T':
  1217. return ampmName.charAt(0).toUpperCase();
  1218. case 'TT':
  1219. return ampmName.toUpperCase();
  1220. case 't':
  1221. return ampmName.charAt(0).toLowerCase();
  1222. case 'tt':
  1223. return ampmName.toLowerCase();
  1224. default:
  1225. return match.replace(/'/g, "");
  1226. }
  1227. });
  1228. return tmptime;
  1229. };
  1230. /*
  1231. * the bad hack :/ override datepicker so it doesn't close on select
  1232. // inspired: http://stackoverflow.com/questions/1252512/jquery-datepicker-prevent-closing-picker-when-clicking-a-date/1762378#1762378
  1233. */
  1234. $.datepicker._base_selectDate = $.datepicker._selectDate;
  1235. $.datepicker._selectDate = function (id, dateStr) {
  1236. var inst = this._getInst($(id)[0]),
  1237. tp_inst = this._get(inst, 'timepicker');
  1238. if (tp_inst) {
  1239. tp_inst._limitMinMaxDateTime(inst, true);
  1240. inst.inline = inst.stay_open = true;
  1241. //This way the onSelect handler called from calendarpicker get the full dateTime
  1242. this._base_selectDate(id, dateStr);
  1243. inst.inline = inst.stay_open = false;
  1244. this._notifyChange(inst);
  1245. this._updateDatepicker(inst);
  1246. } else {
  1247. this._base_selectDate(id, dateStr);
  1248. }
  1249. };
  1250. /*
  1251. * second bad hack :/ override datepicker so it triggers an event when changing the input field
  1252. * and does not redraw the datepicker on every selectDate event
  1253. */
  1254. $.datepicker._base_updateDatepicker = $.datepicker._updateDatepicker;
  1255. $.datepicker._updateDatepicker = function (inst) {
  1256. // don't popup the datepicker if there is another instance already opened
  1257. var input = inst.input[0];
  1258. if ($.datepicker._curInst && $.datepicker._curInst !== inst && $.datepicker._datepickerShowing && $.datepicker._lastInput !== input) {
  1259. return;
  1260. }
  1261. if (typeof(inst.stay_open) !== 'boolean' || inst.stay_open === false) {
  1262. this._base_updateDatepicker(inst);
  1263. // Reload the time control when changing something in the input text field.
  1264. var tp_inst = this._get(inst, 'timepicker');
  1265. if (tp_inst) {
  1266. tp_inst._addTimePicker(inst);
  1267. }
  1268. }
  1269. };
  1270. /*
  1271. * third bad hack :/ override datepicker so it allows spaces and colon in the input field
  1272. */
  1273. $.datepicker._base_doKeyPress = $.datepicker._doKeyPress;
  1274. $.datepicker._doKeyPress = function (event) {
  1275. var inst = $.datepicker._getInst(event.target),
  1276. tp_inst = $.datepicker._get(inst, 'timepicker');
  1277. if (tp_inst) {
  1278. if ($.datepicker._get(inst, 'constrainInput')) {
  1279. var ampm = tp_inst.support.ampm,
  1280. tz = tp_inst._defaults.showTimezone !== null ? tp_inst._defaults.showTimezone : tp_inst.support.timezone,
  1281. dateChars = $.datepicker._possibleChars($.datepicker._get(inst, 'dateFormat')),
  1282. datetimeChars = tp_inst._defaults.timeFormat.toString()
  1283. .replace(/[hms]/g, '')
  1284. .replace(/TT/g, ampm ? 'APM' : '')
  1285. .replace(/Tt/g, ampm ? 'AaPpMm' : '')
  1286. .replace(/tT/g, ampm ? 'AaPpMm' : '')
  1287. .replace(/T/g, ampm ? 'AP' : '')
  1288. .replace(/tt/g, ampm ? 'apm' : '')
  1289. .replace(/t/g, ampm ? 'ap' : '') +
  1290. " " + tp_inst._defaults.separator +
  1291. tp_inst._defaults.timeSuffix +
  1292. (tz ? tp_inst._defaults.timezoneList.join('') : '') +
  1293. (tp_inst._defaults.amNames.join('')) + (tp_inst._defaults.pmNames.join('')) +
  1294. dateChars,
  1295. chr = String.fromCharCode(event.charCode === undefined ? event.keyCode : event.charCode);
  1296. return event.ctrlKey || (chr < ' ' || !dateChars || datetimeChars.indexOf(chr) > -1);
  1297. }
  1298. }
  1299. return $.datepicker._base_doKeyPress(event);
  1300. };
  1301. /*
  1302. * Fourth bad hack :/ override _updateAlternate function used in inline mode to init altField
  1303. * Update any alternate field to synchronise with the main field.
  1304. */
  1305. $.datepicker._base_updateAlternate = $.datepicker._updateAlternate;
  1306. $.datepicker._updateAlternate = function (inst) {
  1307. var tp_inst = this._get(inst, 'timepicker');
  1308. if (tp_inst) {
  1309. var altField = tp_inst._defaults.altField;
  1310. if (altField) { // update alternate field too
  1311. var altFormat = tp_inst._defaults.altFormat || tp_inst._defaults.dateFormat,
  1312. date = this._getDate(inst),
  1313. formatCfg = $.datepicker._getFormatConfig(inst),
  1314. altFormattedDateTime = '',
  1315. altSeparator = tp_inst._defaults.altSeparator ? tp_inst._defaults.altSeparator : tp_inst._defaults.separator,
  1316. altTimeSuffix = tp_inst._defaults.altTimeSuffix ? tp_inst._defaults.altTimeSuffix : tp_inst._defaults.timeSuffix,
  1317. altTimeFormat = tp_inst._defaults.altTimeFormat !== null ? tp_inst._defaults.altTimeFormat : tp_inst._defaults.timeFormat;
  1318. altFormattedDateTime += $.datepicker.formatTime(altTimeFormat, tp_inst, tp_inst._defaults) + altTimeSuffix;
  1319. if (!tp_inst._defaults.timeOnly && !tp_inst._defaults.altFieldTimeOnly && date !== null) {
  1320. if (tp_inst._defaults.altFormat) {
  1321. altFormattedDateTime = $.datepicker.formatDate(tp_inst._defaults.altFormat, date, formatCfg) + altSeparator + altFormattedDateTime;
  1322. }
  1323. else {
  1324. altFormattedDateTime = tp_inst.formattedDate + altSeparator + altFormattedDateTime;
  1325. }
  1326. }
  1327. $(altField).val(altFormattedDateTime);
  1328. }
  1329. }
  1330. else {
  1331. $.datepicker._base_updateAlternate(inst);
  1332. }
  1333. };
  1334. /*
  1335. * Override key up event to sync manual input changes.
  1336. */
  1337. $.datepicker._base_doKeyUp = $.datepicker._doKeyUp;
  1338. $.datepicker._doKeyUp = function (event) {
  1339. var inst = $.datepicker._getInst(event.target),
  1340. tp_inst = $.datepicker._get(inst, 'timepicker');
  1341. if (tp_inst) {
  1342. if (tp_inst._defaults.timeOnly && (inst.input.val() !== inst.lastVal)) {
  1343. try {
  1344. $.datepicker._updateDatepicker(inst);
  1345. } catch (err) {
  1346. $.timepicker.log(err);
  1347. }
  1348. }
  1349. }
  1350. return $.datepicker._base_doKeyUp(event);
  1351. };
  1352. /*
  1353. * override "Today" button to also grab the time.
  1354. */
  1355. $.datepicker._base_gotoToday = $.datepicker._gotoToday;
  1356. $.datepicker._gotoToday = function (id) {
  1357. var inst = this._getInst($(id)[0]),
  1358. $dp = inst.dpDiv;
  1359. this._base_gotoToday(id);
  1360. var tp_inst = this._get(inst, 'timepicker');
  1361. selectLocalTimezone(tp_inst);
  1362. var now = new Date();
  1363. this._setTime(inst, now);
  1364. $('.ui-datepicker-today', $dp).click();
  1365. };
  1366. /*
  1367. * Disable & enable the Time in the datetimepicker
  1368. */
  1369. $.datepicker._disableTimepickerDatepicker = function (target) {
  1370. var inst = this._getInst(target);
  1371. if (!inst) {
  1372. return;
  1373. }
  1374. var tp_inst = this._get(inst, 'timepicker');
  1375. $(target).datepicker('getDate'); // Init selected[Year|Month|Day]
  1376. if (tp_inst) {
  1377. inst.settings.showTimepicker = false;
  1378. tp_inst._defaults.showTimepicker = false;
  1379. tp_inst._updateDateTime(inst);
  1380. }
  1381. };
  1382. $.datepicker._enableTimepickerDatepicker = function (target) {
  1383. var inst = this._getInst(target);
  1384. if (!inst) {
  1385. return;
  1386. }
  1387. var tp_inst = this._get(inst, 'timepicker');
  1388. $(target).datepicker('getDate'); // Init selected[Year|Month|Day]
  1389. if (tp_inst) {
  1390. inst.settings.showTimepicker = true;
  1391. tp_inst._defaults.showTimepicker = true;
  1392. tp_inst._addTimePicker(inst); // Could be disabled on page load
  1393. tp_inst._updateDateTime(inst);
  1394. }
  1395. };
  1396. /*
  1397. * Create our own set time function
  1398. */
  1399. $.datepicker._setTime = function (inst, date) {
  1400. var tp_inst = this._get(inst, 'timepicker');
  1401. if (tp_inst) {
  1402. var defaults = tp_inst._defaults;
  1403. // calling _setTime with no date sets time to defaults
  1404. tp_inst.hour = date ? date.getHours() : defaults.hour;
  1405. tp_inst.minute = date ? date.getMinutes() : defaults.minute;
  1406. tp_inst.second = date ? date.getSeconds() : defaults.second;
  1407. tp_inst.millisec = date ? date.getMilliseconds() : defaults.millisec;
  1408. tp_inst.microsec = date ? date.getMicroseconds() : defaults.microsec;
  1409. //check if within min/max times..
  1410. tp_inst._limitMinMaxDateTime(inst, true);
  1411. tp_inst._onTimeChange();
  1412. tp_inst._updateDateTime(inst);
  1413. }
  1414. };
  1415. /*
  1416. * Create new public method to set only time, callable as $().datepicker('setTime', date)
  1417. */
  1418. $.datepicker._setTimeDatepicker = function (target, date, withDate) {
  1419. var inst = this._getInst(target);
  1420. if (!inst) {
  1421. return;
  1422. }
  1423. var tp_inst = this._get(inst, 'timepicker');
  1424. if (tp_inst) {
  1425. this._setDateFromField(inst);
  1426. var tp_date;
  1427. if (date) {
  1428. if (typeof date === "string") {
  1429. tp_inst._parseTime(date, withDate);
  1430. tp_date = new Date();
  1431. tp_date.setHours(tp_inst.hour, tp_inst.minute, tp_inst.second, tp_inst.millisec);
  1432. tp_date.setMicroseconds(tp_inst.microsec);
  1433. } else {
  1434. tp_date = new Date(date.getTime());
  1435. tp_date.setMicroseconds(date.getMicroseconds());
  1436. }
  1437. if (tp_date.toString() === 'Invalid Date') {
  1438. tp_date = undefined;
  1439. }
  1440. this._setTime(inst, tp_date);
  1441. }
  1442. }
  1443. };
  1444. /*
  1445. * override setDate() to allow setting time too within Date object
  1446. */
  1447. $.datepicker._base_setDateDatepicker = $.datepicker._setDateDatepicker;
  1448. $.datepicker._setDateDatepicker = function (target, date) {
  1449. var inst = this._getInst(target);
  1450. if (!inst) {
  1451. return;
  1452. }
  1453. if (typeof(date) === 'string') {
  1454. date = new Date(date);
  1455. if (!date.getTime()) {
  1456. $.timepicker.log("Error creating Date object from string.");
  1457. }
  1458. }
  1459. var tp_inst = this._get(inst, 'timepicker');
  1460. var tp_date;
  1461. if (date instanceof Date) {
  1462. tp_date = new Date(date.getTime());
  1463. tp_date.setMicroseconds(date.getMicroseconds());
  1464. } else {
  1465. tp_date = date;
  1466. }
  1467. // This is important if you are using the timezone option, javascript's Date
  1468. // object will only return the timezone offset for the current locale, so we
  1469. // adjust it accordingly. If not using timezone option this won't matter..
  1470. // If a timezone is different in tp, keep the timezone as is
  1471. if (tp_inst && tp_date) {
  1472. // look out for DST if tz wasn't specified
  1473. if (!tp_inst.support.timezone && tp_inst._defaults.timezone === null) {
  1474. tp_inst.timezone = tp_date.getTimezoneOffset() * -1;
  1475. }
  1476. date = $.timepicker.timezoneAdjust(date, tp_inst.timezone);
  1477. tp_date = $.timepicker.timezoneAdjust(tp_date, tp_inst.timezone);
  1478. }
  1479. this._updateDatepicker(inst);
  1480. this._base_setDateDatepicker.apply(this, arguments);
  1481. this._setTimeDatepicker(target, tp_date, true);
  1482. };
  1483. /*
  1484. * override getDate() to allow getting time too within Date object
  1485. */
  1486. $.datepicker._base_getDateDatepicker = $.datepicker._getDateDatepicker;
  1487. $.datepicker._getDateDatepicker = function (target, noDefault) {
  1488. var inst = this._getInst(target);
  1489. if (!inst) {
  1490. return;
  1491. }
  1492. var tp_inst = this._get(inst, 'timepicker');
  1493. if (tp_inst) {
  1494. // if it hasn't yet been defined, grab from field
  1495. if (inst.lastVal === undefined) {
  1496. this._setDateFromField(inst, noDefault);
  1497. }
  1498. var date = this._getDate(inst);
  1499. if (date && tp_inst._parseTime($(target).val(), tp_inst.timeOnly)) {
  1500. date.setHours(tp_inst.hour, tp_inst.minute, tp_inst.second, tp_inst.millisec);
  1501. date.setMicroseconds(tp_inst.microsec);
  1502. // This is important if you are using the timezone option, javascript's Date
  1503. // object will only return the timezone offset for the current locale, so we
  1504. // adjust it accordingly. If not using timezone option this won't matter..
  1505. if (tp_inst.timezone != null) {
  1506. // look out for DST if tz wasn't specified
  1507. if (!tp_inst.support.timezone && tp_inst._defaults.timezone === null) {
  1508. tp_inst.timezone = date.getTimezoneOffset() * -1;
  1509. }
  1510. date = $.timepicker.timezoneAdjust(date, tp_inst.timezone);
  1511. }
  1512. }
  1513. return date;
  1514. }
  1515. return this._base_getDateDatepicker(target, noDefault);
  1516. };
  1517. /*
  1518. * override parseDate() because UI 1.8.14 throws an error about "Extra characters"
  1519. * An option in datapicker to ignore extra format characters would be nicer.
  1520. */
  1521. $.datepicker._base_parseDate = $.datepicker.parseDate;
  1522. $.datepicker.parseDate = function (format, value, settings) {
  1523. var date;
  1524. try {
  1525. date = this._base_parseDate(format, value, settings);
  1526. } catch (err) {
  1527. // Hack! The error message ends with a colon, a space, and
  1528. // the "extra" characters. We rely on that instead of
  1529. // attempting to perfectly reproduce the parsing algorithm.
  1530. if (err.indexOf(":") >= 0) {
  1531. date = this._base_parseDate(format, value.substring(0, value.length - (err.length - err.indexOf(':') - 2)), settings);
  1532. $.timepicker.log("Error parsing the date string: " + err + "\ndate string = " + value + "\ndate format = " + format);
  1533. } else {
  1534. throw err;
  1535. }
  1536. }
  1537. return date;
  1538. };
  1539. /*
  1540. * override formatDate to set date with time to the input
  1541. */
  1542. $.datepicker._base_formatDate = $.datepicker._formatDate;
  1543. $.datepicker._formatDate = function (inst, day, month, year) {
  1544. var tp_inst = this._get(inst, 'timepicker');
  1545. if (tp_inst) {
  1546. tp_inst._updateDateTime(inst);
  1547. return tp_inst.$input.val();
  1548. }
  1549. return this._base_formatDate(inst);
  1550. };
  1551. /*
  1552. * override options setter to add time to maxDate(Time) and minDate(Time). MaxDate
  1553. */
  1554. $.datepicker._base_optionDatepicker = $.datepicker._optionDatepicker;
  1555. $.datepicker._optionDatepicker = function (target, name, value) {
  1556. var inst = this._getInst(target),
  1557. name_clone;
  1558. if (!inst) {
  1559. return null;
  1560. }
  1561. var tp_inst = this._get(inst, 'timepicker');
  1562. if (tp_inst) {
  1563. var min = null,
  1564. max = null,
  1565. onselect = null,
  1566. overrides = tp_inst._defaults.evnts,
  1567. fns = {},
  1568. prop;
  1569. if (typeof name === 'string') { // if min/max was set with the string
  1570. if (name === 'minDate' || name === 'minDateTime') {
  1571. min = value;
  1572. } else if (name === 'maxDate' || name === 'maxDateTime') {
  1573. max = value;
  1574. } else if (name === 'onSelect') {
  1575. onselect = value;
  1576. } else if (overrides.hasOwnProperty(name)) {
  1577. if (typeof (value) === 'undefined') {
  1578. return overrides[name];
  1579. }
  1580. fns[name] = value;
  1581. name_clone = {}; //empty results in exiting function after overrides updated
  1582. }
  1583. } else if (typeof name === 'object') { //if min/max was set with the JSON
  1584. if (name.minDate) {
  1585. min = name.minDate;
  1586. } else if (name.minDateTime) {
  1587. min = name.minDateTime;
  1588. } else if (name.maxDate) {
  1589. max = name.maxDate;
  1590. } else if (name.maxDateTime) {
  1591. max = name.maxDateTime;
  1592. }
  1593. for (prop in overrides) {
  1594. if (overrides.hasOwnProperty(prop) && name[prop]) {
  1595. fns[prop] = name[prop];
  1596. }
  1597. }
  1598. }
  1599. for (prop in fns) {
  1600. if (fns.hasOwnProperty(prop)) {
  1601. overrides[prop] = fns[prop];
  1602. if (!name_clone) { name_clone = $.extend({}, name); }
  1603. delete name_clone[prop];
  1604. }
  1605. }
  1606. if (name_clone && isEmptyObject(name_clone)) { return; }
  1607. if (min) { //if min was set
  1608. if (min === 0) {
  1609. min = new Date();
  1610. } else {
  1611. min = new Date(min);
  1612. }
  1613. tp_inst._defaults.minDate = min;
  1614. tp_inst._defaults.minDateTime = min;
  1615. } else if (max) { //if max was set
  1616. if (max === 0) {
  1617. max = new Date();
  1618. } else {
  1619. max = new Date(max);
  1620. }
  1621. tp_inst._defaults.maxDate = max;
  1622. tp_inst._defaults.maxDateTime = max;
  1623. } else if (onselect) {
  1624. tp_inst._defaults.onSelect = onselect;
  1625. }
  1626. }
  1627. if (value === undefined) {
  1628. return this._base_optionDatepicker.call($.datepicker, target, name);
  1629. }
  1630. return this._base_optionDatepicker.call($.datepicker, target, name_clone || name, value);
  1631. };
  1632. /*
  1633. * jQuery isEmptyObject does not check hasOwnProperty - if someone has added to the object prototype,
  1634. * it will return false for all objects
  1635. */
  1636. var isEmptyObject = function (obj) {
  1637. var prop;
  1638. for (prop in obj) {
  1639. if (obj.hasOwnProperty(prop)) {
  1640. return false;
  1641. }
  1642. }
  1643. return true;
  1644. };
  1645. /*
  1646. * jQuery extend now ignores nulls!
  1647. */
  1648. var extendRemove = function (target, props) {
  1649. $.extend(target, props);
  1650. for (var name in props) {
  1651. if (props[name] === null || props[name] === undefined) {
  1652. target[name] = props[name];
  1653. }
  1654. }
  1655. return target;
  1656. };
  1657. /*
  1658. * Determine by the time format which units are supported
  1659. * Returns an object of booleans for each unit
  1660. */
  1661. var detectSupport = function (timeFormat) {
  1662. var tf = timeFormat.replace(/'.*?'/g, '').toLowerCase(), // removes literals
  1663. isIn = function (f, t) { // does the format contain the token?
  1664. return f.indexOf(t) !== -1 ? true : false;
  1665. };
  1666. return {
  1667. hour: isIn(tf, 'h'),
  1668. minute: isIn(tf, 'm'),
  1669. second: isIn(tf, 's'),
  1670. millisec: isIn(tf, 'l'),
  1671. microsec: isIn(tf, 'c'),
  1672. timezone: isIn(tf, 'z'),
  1673. ampm: isIn(tf, 't') && isIn(timeFormat, 'h'),
  1674. iso8601: isIn(timeFormat, 'Z')
  1675. };
  1676. };
  1677. /*
  1678. * Converts 24 hour format into 12 hour
  1679. * Returns 12 hour without leading 0
  1680. */
  1681. var convert24to12 = function (hour) {
  1682. hour %= 12;
  1683. if (hour === 0) {
  1684. hour = 12;
  1685. }
  1686. return String(hour);
  1687. };
  1688. var computeEffectiveSetting = function (settings, property) {
  1689. return settings && settings[property] ? settings[property] : $.timepicker._defaults[property];
  1690. };
  1691. /*
  1692. * Splits datetime string into date and time substrings.
  1693. * Throws exception when date can't be parsed
  1694. * Returns {dateString: dateString, timeString: timeString}
  1695. */
  1696. var splitDateTime = function (dateTimeString, timeSettings) {
  1697. // The idea is to get the number separator occurrences in datetime and the time format requested (since time has
  1698. // fewer unknowns, mostly numbers and am/pm). We will use the time pattern to split.
  1699. var separator = computeEffectiveSetting(timeSettings, 'separator'),
  1700. format = computeEffectiveSetting(timeSettings, 'timeFormat'),
  1701. timeParts = format.split(separator), // how many occurrences of separator may be in our format?
  1702. timePartsLen = timeParts.length,
  1703. allParts = dateTimeString.split(separator),
  1704. allPartsLen = allParts.length;
  1705. if (allPartsLen > 1) {
  1706. return {
  1707. dateString: allParts.splice(0, allPartsLen - timePartsLen).join(separator),
  1708. timeString: allParts.splice(0, timePartsLen).join(separator)
  1709. };
  1710. }
  1711. return {
  1712. dateString: dateTimeString,
  1713. timeString: ''
  1714. };
  1715. };
  1716. /*
  1717. * Internal function to parse datetime interval
  1718. * Returns: {date: Date, timeObj: Object}, where
  1719. * date - parsed date without time (type Date)
  1720. * timeObj = {hour: , minute: , second: , millisec: , microsec: } - parsed time. Optional
  1721. */
  1722. var parseDateTimeInternal = function (dateFormat, timeFormat, dateTimeString, dateSettings, timeSettings) {
  1723. var date,
  1724. parts,
  1725. parsedTime;
  1726. parts = splitDateTime(dateTimeString, timeSettings);
  1727. date = $.datepicker._base_parseDate(dateFormat, parts.dateString, dateSettings);
  1728. if (parts.timeString === '') {
  1729. return {
  1730. date: date
  1731. };
  1732. }
  1733. parsedTime = $.datepicker.parseTime(timeFormat, parts.timeString, timeSettings);
  1734. if (!parsedTime) {
  1735. throw 'Wrong time format';
  1736. }
  1737. return {
  1738. date: date,
  1739. timeObj: parsedTime
  1740. };
  1741. };
  1742. /*
  1743. * Internal function to set timezone_select to the local timezone
  1744. */
  1745. var selectLocalTimezone = function (tp_inst, date) {
  1746. if (tp_inst && tp_inst.timezone_select) {
  1747. var now = date || new Date();
  1748. tp_inst.timezone_select.val(-now.getTimezoneOffset());
  1749. }
  1750. };
  1751. /*
  1752. * Create a Singleton Instance
  1753. */
  1754. $.timepicker = new Timepicker();
  1755. /**
  1756. * Get the timezone offset as string from a date object (eg '+0530' for UTC+5.5)
  1757. * @param {number} tzMinutes if not a number, less than -720 (-1200), or greater than 840 (+1400) this value is returned
  1758. * @param {boolean} iso8601 if true formats in accordance to iso8601 "+12:45"
  1759. * @return {string}
  1760. */
  1761. $.timepicker.timezoneOffsetString = function (tzMinutes, iso8601) {
  1762. if (isNaN(tzMinutes) || tzMinutes > 840 || tzMinutes < -720) {
  1763. return tzMinutes;
  1764. }
  1765. var off = tzMinutes,
  1766. minutes = off % 60,
  1767. hours = (off - minutes) / 60,
  1768. iso = iso8601 ? ':' : '',
  1769. tz = (off >= 0 ? '+' : '-') + ('0' + Math.abs(hours)).slice(-2) + iso + ('0' + Math.abs(minutes)).slice(-2);
  1770. if (tz === '+00:00') {
  1771. return 'Z';
  1772. }
  1773. return tz;
  1774. };
  1775. /**
  1776. * Get the number in minutes that represents a timezone string
  1777. * @param {string} tzString formatted like "+0500", "-1245", "Z"
  1778. * @return {number} the offset minutes or the original string if it doesn't match expectations
  1779. */
  1780. $.timepicker.timezoneOffsetNumber = function (tzString) {
  1781. var normalized = tzString.toString().replace(':', ''); // excuse any iso8601, end up with "+1245"
  1782. if (normalized.toUpperCase() === 'Z') { // if iso8601 with Z, its 0 minute offset
  1783. return 0;
  1784. }
  1785. if (!/^(\-|\+)\d{4}$/.test(normalized)) { // possibly a user defined tz, so just give it back
  1786. return tzString;
  1787. }
  1788. return ((normalized.substr(0, 1) === '-' ? -1 : 1) * // plus or minus
  1789. ((parseInt(normalized.substr(1, 2), 10) * 60) + // hours (converted to minutes)
  1790. parseInt(normalized.substr(3, 2), 10))); // minutes
  1791. };
  1792. /**
  1793. * No way to set timezone in js Date, so we must adjust the minutes to compensate. (think setDate, getDate)
  1794. * @param {Date} date
  1795. * @param {string} toTimezone formatted like "+0500", "-1245"
  1796. * @return {Date}
  1797. */
  1798. $.timepicker.timezoneAdjust = function (date, toTimezone) {
  1799. var toTz = $.timepicker.timezoneOffsetNumber(toTimezone);
  1800. if (!isNaN(toTz)) {
  1801. date.setMinutes(date.getMinutes() + -date.getTimezoneOffset() - toTz);
  1802. }
  1803. return date;
  1804. };
  1805. /**
  1806. * Calls `timepicker()` on the `startTime` and `endTime` elements, and configures them to
  1807. * enforce date range limits.
  1808. * n.b. The input value must be correctly formatted (reformatting is not supported)
  1809. * @param {Element} startTime
  1810. * @param {Element} endTime
  1811. * @param {Object} options Options for the timepicker() call
  1812. * @return {jQuery}
  1813. */
  1814. $.timepicker.timeRange = function (startTime, endTime, options) {
  1815. return $.timepicker.handleRange('timepicker', startTime, endTime, options);
  1816. };
  1817. /**
  1818. * Calls `datetimepicker` on the `startTime` and `endTime` elements, and configures them to
  1819. * enforce date range limits.
  1820. * @param {Element} startTime
  1821. * @param {Element} endTime
  1822. * @param {Object} options Options for the `timepicker()` call. Also supports `reformat`,
  1823. * a boolean value that can be used to reformat the input values to the `dateFormat`.
  1824. * @param {string} method Can be used to specify the type of picker to be added
  1825. * @return {jQuery}
  1826. */
  1827. $.timepicker.datetimeRange = function (startTime, endTime, options) {
  1828. $.timepicker.handleRange('datetimepicker', startTime, endTime, options);
  1829. };
  1830. /**
  1831. * Calls `datepicker` on the `startTime` and `endTime` elements, and configures them to
  1832. * enforce date range limits.
  1833. * @param {Element} startTime
  1834. * @param {Element} endTime
  1835. * @param {Object} options Options for the `timepicker()` call. Also supports `reformat`,
  1836. * a boolean value that can be used to reformat the input values to the `dateFormat`.
  1837. * @return {jQuery}
  1838. */
  1839. $.timepicker.dateRange = function (startTime, endTime, options) {
  1840. $.timepicker.handleRange('datepicker', startTime, endTime, options);
  1841. };
  1842. /**
  1843. * Calls `method` on the `startTime` and `endTime` elements, and configures them to
  1844. * enforce date range limits.
  1845. * @param {string} method Can be used to specify the type of picker to be added
  1846. * @param {Element} startTime
  1847. * @param {Element} endTime
  1848. * @param {Object} options Options for the `timepicker()` call. Also supports `reformat`,
  1849. * a boolean value that can be used to reformat the input values to the `dateFormat`.
  1850. * @return {jQuery}
  1851. */
  1852. $.timepicker.handleRange = function (method, startTime, endTime, options) {
  1853. options = $.extend({}, {
  1854. minInterval: 0, // min allowed interval in milliseconds
  1855. maxInterval: 0, // max allowed interval in milliseconds
  1856. start: {}, // options for start picker
  1857. end: {} // options for end picker
  1858. }, options);
  1859. function checkDates(changed, other) {
  1860. var startdt = startTime[method]('getDate'),
  1861. enddt = endTime[method]('getDate'),
  1862. changeddt = changed[method]('getDate');
  1863. if (startdt !== null) {
  1864. var minDate = new Date(startdt.getTime()),
  1865. maxDate = new Date(startdt.getTime());
  1866. minDate.setMilliseconds(minDate.getMilliseconds() + options.minInterval);
  1867. maxDate.setMilliseconds(maxDate.getMilliseconds() + options.maxInterval);
  1868. if (options.minInterval > 0 && minDate > enddt) { // minInterval check
  1869. endTime[method]('setDate', minDate);
  1870. }
  1871. else if (options.maxInterval > 0 && maxDate < enddt) { // max interval check
  1872. endTime[method]('setDate', maxDate);
  1873. }
  1874. else if (startdt > enddt) {
  1875. other[method]('setDate', changeddt);
  1876. }
  1877. }
  1878. }
  1879. function selected(changed, other, option) {
  1880. if (!changed.val()) {
  1881. return;
  1882. }
  1883. var date = changed[method].call(changed, 'getDate');
  1884. if (date !== null && options.minInterval > 0) {
  1885. if (option === 'minDate') {
  1886. date.setMilliseconds(date.getMilliseconds() + options.minInterval);
  1887. }
  1888. if (option === 'maxDate') {
  1889. date.setMilliseconds(date.getMilliseconds() - options.minInterval);
  1890. }
  1891. }
  1892. if (date.getTime) {
  1893. other[method].call(other, 'option', option, date);
  1894. }
  1895. }
  1896. $.fn[method].call(startTime, $.extend({
  1897. onClose: function (dateText, inst) {
  1898. checkDates($(this), endTime);
  1899. },
  1900. onSelect: function (selectedDateTime) {
  1901. selected($(this), endTime, 'minDate');
  1902. }
  1903. }, options, options.start));
  1904. $.fn[method].call(endTime, $.extend({
  1905. onClose: function (dateText, inst) {
  1906. checkDates($(this), startTime);
  1907. },
  1908. onSelect: function (selectedDateTime) {
  1909. selected($(this), startTime, 'maxDate');
  1910. }
  1911. }, options, options.end));
  1912. checkDates(startTime, endTime);
  1913. selected(startTime, endTime, 'minDate');
  1914. selected(endTime, startTime, 'maxDate');
  1915. return $([startTime.get(0), endTime.get(0)]);
  1916. };
  1917. /**
  1918. * Log error or data to the console during error or debugging
  1919. * @param {Object} err pass any type object to log to the console during error or debugging
  1920. * @return {void}
  1921. */
  1922. $.timepicker.log = function (err) {
  1923. if (window.console) {
  1924. window.console.log(err);
  1925. }
  1926. };
  1927. /*
  1928. * Add util object to allow access to private methods for testability.
  1929. */
  1930. $.timepicker._util = {
  1931. _extendRemove: extendRemove,
  1932. _isEmptyObject: isEmptyObject,
  1933. _convert24to12: convert24to12,
  1934. _detectSupport: detectSupport,
  1935. _selectLocalTimezone: selectLocalTimezone,
  1936. _computeEffectiveSetting: computeEffectiveSetting,
  1937. _splitDateTime: splitDateTime,
  1938. _parseDateTimeInternal: parseDateTimeInternal
  1939. };
  1940. /*
  1941. * Microsecond support
  1942. */
  1943. if (!Date.prototype.getMicroseconds) {
  1944. Date.prototype.microseconds = 0;
  1945. Date.prototype.getMicroseconds = function () { return this.microseconds; };
  1946. Date.prototype.setMicroseconds = function (m) {
  1947. this.setMilliseconds(this.getMilliseconds() + Math.floor(m / 1000));
  1948. this.microseconds = m % 1000;
  1949. return this;
  1950. };
  1951. }
  1952. /*
  1953. * Keep up with the version
  1954. */
  1955. $.timepicker.version = "1.4.3";
  1956. }));