jquery.timepicker.js 71 KB

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