jquery-ui-timepicker-addon.js 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134
  1. /*! jQuery Timepicker Addon - v1.4 - 2013-08-11
  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"
  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. var ampm = o[hour < 12 ? 'amNames' : 'pmNames'][0];
  687. // If the update was done in the input field, the input field should not be updated.
  688. // If the update was done using the sliders, update the input field.
  689. var hasChanged = (hour !== this.hour || minute !== this.minute || second !== this.second || millisec !== this.millisec || microsec !== this.microsec ||
  690. (this.ampm.length > 0 && (hour < 12) !== ($.inArray(this.ampm.toUpperCase(), this.amNames) !== -1)) || (this.timezone !== null && timezone !== this.timezone));
  691. if (hasChanged) {
  692. if (hour !== false) {
  693. this.hour = hour;
  694. }
  695. if (minute !== false) {
  696. this.minute = minute;
  697. }
  698. if (second !== false) {
  699. this.second = second;
  700. }
  701. if (millisec !== false) {
  702. this.millisec = millisec;
  703. }
  704. if (microsec !== false) {
  705. this.microsec = microsec;
  706. }
  707. if (timezone !== false) {
  708. this.timezone = timezone;
  709. }
  710. if (!this.inst) {
  711. this.inst = $.datepicker._getInst(this.$input[0]);
  712. }
  713. this._limitMinMaxDateTime(this.inst, true);
  714. }
  715. if (this.support.ampm) {
  716. this.ampm = ampm;
  717. }
  718. // Updates the time within the timepicker
  719. this.formattedTime = $.datepicker.formatTime(o.timeFormat, this, o);
  720. if (this.$timeObj) {
  721. if (pickerTimeFormat === o.timeFormat) {
  722. this.$timeObj.text(this.formattedTime + pickerTimeSuffix);
  723. }
  724. else {
  725. this.$timeObj.text($.datepicker.formatTime(pickerTimeFormat, this, o) + pickerTimeSuffix);
  726. }
  727. }
  728. this.timeDefined = true;
  729. if (hasChanged) {
  730. this._updateDateTime();
  731. }
  732. },
  733. /*
  734. * call custom onSelect.
  735. * bind to sliders slidestop, and grid click.
  736. */
  737. _onSelectHandler: function () {
  738. var onSelect = this._defaults.onSelect || this.inst.settings.onSelect;
  739. var inputEl = this.$input ? this.$input[0] : null;
  740. if (onSelect && inputEl) {
  741. onSelect.apply(inputEl, [this.formattedDateTime, this]);
  742. }
  743. },
  744. /*
  745. * update our input with the new date time..
  746. */
  747. _updateDateTime: function (dp_inst) {
  748. dp_inst = this.inst || dp_inst;
  749. var dtTmp = (dp_inst.currentYear > 0?
  750. new Date(dp_inst.currentYear, dp_inst.currentMonth, dp_inst.currentDay) :
  751. new Date(dp_inst.selectedYear, dp_inst.selectedMonth, dp_inst.selectedDay)),
  752. dt = $.datepicker._daylightSavingAdjust(dtTmp),
  753. //dt = $.datepicker._daylightSavingAdjust(new Date(dp_inst.selectedYear, dp_inst.selectedMonth, dp_inst.selectedDay)),
  754. //dt = $.datepicker._daylightSavingAdjust(new Date(dp_inst.currentYear, dp_inst.currentMonth, dp_inst.currentDay)),
  755. dateFmt = $.datepicker._get(dp_inst, 'dateFormat'),
  756. formatCfg = $.datepicker._getFormatConfig(dp_inst),
  757. timeAvailable = dt !== null && this.timeDefined;
  758. this.formattedDate = $.datepicker.formatDate(dateFmt, (dt === null ? new Date() : dt), formatCfg);
  759. var formattedDateTime = this.formattedDate;
  760. // if a slider was changed but datepicker doesn't have a value yet, set it
  761. if (dp_inst.lastVa === "") {
  762. dp_inst.currentYear = dp_inst.selectedYear;
  763. dp_inst.currentMonth = dp_inst.selectedMonth;
  764. dp_inst.currentDay = dp_inst.selectedDay;
  765. }
  766. /*
  767. * remove following lines to force every changes in date picker to change the input value
  768. * Bug descriptions: when an input field has a default value, and click on the field to pop up the date picker.
  769. * If the user manually empty the value in the input field, the date picker will never change selected value.
  770. */
  771. //if (dp_inst.lastVal !== undefined && (dp_inst.lastVal.length > 0 && this.$input.val().length === 0)) {
  772. // return;
  773. //}
  774. if (this._defaults.timeOnly === true) {
  775. formattedDateTime = this.formattedTime;
  776. } else if (this._defaults.timeOnly !== true && (this._defaults.alwaysSetTime || timeAvailable)) {
  777. formattedDateTime += this._defaults.separator + this.formattedTime + this._defaults.timeSuffix;
  778. }
  779. this.formattedDateTime = formattedDateTime;
  780. if (!this._defaults.showTimepicker) {
  781. this.$input.val(this.formattedDate);
  782. } else if (this.$altInput && this._defaults.timeOnly === false && this._defaults.altFieldTimeOnly === true) {
  783. this.$altInput.val(this.formattedTime);
  784. this.$input.val(this.formattedDate);
  785. } else if (this.$altInput) {
  786. this.$input.val(formattedDateTime);
  787. var altFormattedDateTime = '',
  788. altSeparator = this._defaults.altSeparator ? this._defaults.altSeparator : this._defaults.separator,
  789. altTimeSuffix = this._defaults.altTimeSuffix ? this._defaults.altTimeSuffix : this._defaults.timeSuffix;
  790. if (!this._defaults.timeOnly) {
  791. if (this._defaults.altFormat) {
  792. altFormattedDateTime = $.datepicker.formatDate(this._defaults.altFormat, (dt === null ? new Date() : dt), formatCfg);
  793. }
  794. else {
  795. altFormattedDateTime = this.formattedDate;
  796. }
  797. if (altFormattedDateTime) {
  798. altFormattedDateTime += altSeparator;
  799. }
  800. }
  801. if (this._defaults.altTimeFormat) {
  802. altFormattedDateTime += $.datepicker.formatTime(this._defaults.altTimeFormat, this, this._defaults) + altTimeSuffix;
  803. }
  804. else {
  805. altFormattedDateTime += this.formattedTime + altTimeSuffix;
  806. }
  807. this.$altInput.val(altFormattedDateTime);
  808. } else {
  809. this.$input.val(formattedDateTime);
  810. }
  811. this.$input.trigger("change");
  812. },
  813. _onFocus: function () {
  814. if (!this.$input.val() && this._defaults.defaultValue) {
  815. this.$input.val(this._defaults.defaultValue);
  816. var inst = $.datepicker._getInst(this.$input.get(0)),
  817. tp_inst = $.datepicker._get(inst, 'timepicker');
  818. if (tp_inst) {
  819. if (tp_inst._defaults.timeOnly && (inst.input.val() !== inst.lastVal)) {
  820. try {
  821. $.datepicker._updateDatepicker(inst);
  822. } catch (err) {
  823. $.timepicker.log(err);
  824. }
  825. }
  826. }
  827. }
  828. },
  829. /*
  830. * Small abstraction to control types
  831. * We can add more, just be sure to follow the pattern: create, options, value
  832. */
  833. _controls: {
  834. // slider methods
  835. slider: {
  836. create: function (tp_inst, obj, unit, val, min, max, step) {
  837. var rtl = tp_inst._defaults.isRTL; // if rtl go -60->0 instead of 0->60
  838. return obj.prop('slide', null).slider({
  839. orientation: "horizontal",
  840. value: rtl ? val * -1 : val,
  841. min: rtl ? max * -1 : min,
  842. max: rtl ? min * -1 : max,
  843. step: step,
  844. slide: function (event, ui) {
  845. tp_inst.control.value(tp_inst, $(this), unit, rtl ? ui.value * -1 : ui.value);
  846. tp_inst._onTimeChange();
  847. },
  848. stop: function (event, ui) {
  849. tp_inst._onSelectHandler();
  850. }
  851. });
  852. },
  853. options: function (tp_inst, obj, unit, opts, val) {
  854. if (tp_inst._defaults.isRTL) {
  855. if (typeof(opts) === 'string') {
  856. if (opts === 'min' || opts === 'max') {
  857. if (val !== undefined) {
  858. return obj.slider(opts, val * -1);
  859. }
  860. return Math.abs(obj.slider(opts));
  861. }
  862. return obj.slider(opts);
  863. }
  864. var min = opts.min,
  865. max = opts.max;
  866. opts.min = opts.max = null;
  867. if (min !== undefined) {
  868. opts.max = min * -1;
  869. }
  870. if (max !== undefined) {
  871. opts.min = max * -1;
  872. }
  873. return obj.slider(opts);
  874. }
  875. if (typeof(opts) === 'string' && val !== undefined) {
  876. return obj.slider(opts, val);
  877. }
  878. return obj.slider(opts);
  879. },
  880. value: function (tp_inst, obj, unit, val) {
  881. if (tp_inst._defaults.isRTL) {
  882. if (val !== undefined) {
  883. return obj.slider('value', val * -1);
  884. }
  885. return Math.abs(obj.slider('value'));
  886. }
  887. if (val !== undefined) {
  888. return obj.slider('value', val);
  889. }
  890. return obj.slider('value');
  891. }
  892. },
  893. // select methods
  894. select: {
  895. create: function (tp_inst, obj, unit, val, min, max, step) {
  896. var sel = '<select class="ui-timepicker-select" data-unit="' + unit + '" data-min="' + min + '" data-max="' + max + '" data-step="' + step + '">',
  897. format = tp_inst._defaults.pickerTimeFormat || tp_inst._defaults.timeFormat;
  898. for (var i = min; i <= max; i += step) {
  899. sel += '<option value="' + i + '"' + (i === val ? ' selected' : '') + '>';
  900. if (unit === 'hour') {
  901. sel += $.datepicker.formatTime($.trim(format.replace(/[^ht ]/ig, '')), {hour: i}, tp_inst._defaults);
  902. }
  903. else if (unit === 'millisec' || unit === 'microsec' || i >= 10) { sel += i; }
  904. else {sel += '0' + i.toString(); }
  905. sel += '</option>';
  906. }
  907. sel += '</select>';
  908. obj.children('select').remove();
  909. $(sel).appendTo(obj).change(function (e) {
  910. tp_inst._onTimeChange();
  911. tp_inst._onSelectHandler();
  912. });
  913. return obj;
  914. },
  915. options: function (tp_inst, obj, unit, opts, val) {
  916. var o = {},
  917. $t = obj.children('select');
  918. if (typeof(opts) === 'string') {
  919. if (val === undefined) {
  920. return $t.data(opts);
  921. }
  922. o[opts] = val;
  923. }
  924. else { o = opts; }
  925. 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'));
  926. },
  927. value: function (tp_inst, obj, unit, val) {
  928. var $t = obj.children('select');
  929. if (val !== undefined) {
  930. return $t.val(val);
  931. }
  932. return $t.val();
  933. }
  934. }
  935. } // end _controls
  936. });
  937. $.fn.extend({
  938. /*
  939. * shorthand just to use timepicker.
  940. */
  941. timepicker: function (o) {
  942. o = o || {};
  943. var tmp_args = Array.prototype.slice.call(arguments);
  944. if (typeof o === 'object') {
  945. tmp_args[0] = $.extend(o, {
  946. timeOnly: true
  947. });
  948. }
  949. return $(this).each(function () {
  950. $.fn.datetimepicker.apply($(this), tmp_args);
  951. });
  952. },
  953. /*
  954. * extend timepicker to datepicker
  955. */
  956. datetimepicker: function (o) {
  957. o = o || {};
  958. var tmp_args = arguments;
  959. if (typeof(o) === 'string') {
  960. if (o === 'getDate') {
  961. return $.fn.datepicker.apply($(this[0]), tmp_args);
  962. } else {
  963. return this.each(function () {
  964. var $t = $(this);
  965. $t.datepicker.apply($t, tmp_args);
  966. });
  967. }
  968. } else {
  969. return this.each(function () {
  970. var $t = $(this);
  971. $t.datepicker($.timepicker._newInst($t, o)._defaults);
  972. });
  973. }
  974. }
  975. });
  976. /*
  977. * Public Utility to parse date and time
  978. */
  979. $.datepicker.parseDateTime = function (dateFormat, timeFormat, dateTimeString, dateSettings, timeSettings) {
  980. var parseRes = parseDateTimeInternal(dateFormat, timeFormat, dateTimeString, dateSettings, timeSettings);
  981. if (parseRes.timeObj) {
  982. var t = parseRes.timeObj;
  983. parseRes.date.setHours(t.hour, t.minute, t.second, t.millisec);
  984. parseRes.date.setMicroseconds(t.microsec);
  985. }
  986. return parseRes.date;
  987. };
  988. /*
  989. * Public utility to parse time
  990. */
  991. $.datepicker.parseTime = function (timeFormat, timeString, options) {
  992. var o = extendRemove(extendRemove({}, $.timepicker._defaults), options || {}),
  993. iso8601 = (timeFormat.replace(/\'.*?\'/g, '').indexOf('Z') !== -1);
  994. // Strict parse requires the timeString to match the timeFormat exactly
  995. var strictParse = function (f, s, o) {
  996. // pattern for standard and localized AM/PM markers
  997. var getPatternAmpm = function (amNames, pmNames) {
  998. var markers = [];
  999. if (amNames) {
  1000. $.merge(markers, amNames);
  1001. }
  1002. if (pmNames) {
  1003. $.merge(markers, pmNames);
  1004. }
  1005. markers = $.map(markers, function (val) {
  1006. return val.replace(/[.*+?|()\[\]{}\\]/g, '\\$&');
  1007. });
  1008. return '(' + markers.join('|') + ')?';
  1009. };
  1010. // figure out position of time elements.. cause js cant do named captures
  1011. var getFormatPositions = function (timeFormat) {
  1012. var finds = timeFormat.toLowerCase().match(/(h{1,2}|m{1,2}|s{1,2}|l{1}|c{1}|t{1,2}|z|'.*?')/g),
  1013. orders = {
  1014. h: -1,
  1015. m: -1,
  1016. s: -1,
  1017. l: -1,
  1018. c: -1,
  1019. t: -1,
  1020. z: -1
  1021. };
  1022. if (finds) {
  1023. for (var i = 0; i < finds.length; i++) {
  1024. if (orders[finds[i].toString().charAt(0)] === -1) {
  1025. orders[finds[i].toString().charAt(0)] = i + 1;
  1026. }
  1027. }
  1028. }
  1029. return orders;
  1030. };
  1031. var regstr = '^' + f.toString()
  1032. .replace(/([hH]{1,2}|mm?|ss?|[tT]{1,2}|[zZ]|[lc]|'.*?')/g, function (match) {
  1033. var ml = match.length;
  1034. switch (match.charAt(0).toLowerCase()) {
  1035. case 'h':
  1036. return ml === 1 ? '(\\d?\\d)' : '(\\d{' + ml + '})';
  1037. case 'm':
  1038. return ml === 1 ? '(\\d?\\d)' : '(\\d{' + ml + '})';
  1039. case 's':
  1040. return ml === 1 ? '(\\d?\\d)' : '(\\d{' + ml + '})';
  1041. case 'l':
  1042. return '(\\d?\\d?\\d)';
  1043. case 'c':
  1044. return '(\\d?\\d?\\d)';
  1045. case 'z':
  1046. return '(z|[-+]\\d\\d:?\\d\\d|\\S+)?';
  1047. case 't':
  1048. return getPatternAmpm(o.amNames, o.pmNames);
  1049. default: // literal escaped in quotes
  1050. return '(' + match.replace(/\'/g, "").replace(/(\.|\$|\^|\\|\/|\(|\)|\[|\]|\?|\+|\*)/g, function (m) { return "\\" + m; }) + ')?';
  1051. }
  1052. })
  1053. .replace(/\s/g, '\\s?') +
  1054. o.timeSuffix + '$',
  1055. order = getFormatPositions(f),
  1056. ampm = '',
  1057. treg;
  1058. treg = s.match(new RegExp(regstr, 'i'));
  1059. var resTime = {
  1060. hour: 0,
  1061. minute: 0,
  1062. second: 0,
  1063. millisec: 0,
  1064. microsec: 0
  1065. };
  1066. if (treg) {
  1067. if (order.t !== -1) {
  1068. if (treg[order.t] === undefined || treg[order.t].length === 0) {
  1069. ampm = '';
  1070. resTime.ampm = '';
  1071. } else {
  1072. ampm = $.inArray(treg[order.t].toUpperCase(), o.amNames) !== -1 ? 'AM' : 'PM';
  1073. resTime.ampm = o[ampm === 'AM' ? 'amNames' : 'pmNames'][0];
  1074. }
  1075. }
  1076. if (order.h !== -1) {
  1077. if (ampm === 'AM' && treg[order.h] === '12') {
  1078. resTime.hour = 0; // 12am = 0 hour
  1079. } else {
  1080. if (ampm === 'PM' && treg[order.h] !== '12') {
  1081. resTime.hour = parseInt(treg[order.h], 10) + 12; // 12pm = 12 hour, any other pm = hour + 12
  1082. } else {
  1083. resTime.hour = Number(treg[order.h]);
  1084. }
  1085. }
  1086. }
  1087. if (order.m !== -1) {
  1088. resTime.minute = Number(treg[order.m]);
  1089. }
  1090. if (order.s !== -1) {
  1091. resTime.second = Number(treg[order.s]);
  1092. }
  1093. if (order.l !== -1) {
  1094. resTime.millisec = Number(treg[order.l]);
  1095. }
  1096. if (order.c !== -1) {
  1097. resTime.microsec = Number(treg[order.c]);
  1098. }
  1099. if (order.z !== -1 && treg[order.z] !== undefined) {
  1100. resTime.timezone = $.timepicker.timezoneOffsetNumber(treg[order.z]);
  1101. }
  1102. return resTime;
  1103. }
  1104. return false;
  1105. };// end strictParse
  1106. // First try JS Date, if that fails, use strictParse
  1107. var looseParse = function (f, s, o) {
  1108. try {
  1109. var d = new Date('2012-01-01 ' + s);
  1110. if (isNaN(d.getTime())) {
  1111. d = new Date('2012-01-01T' + s);
  1112. if (isNaN(d.getTime())) {
  1113. d = new Date('01/01/2012 ' + s);
  1114. if (isNaN(d.getTime())) {
  1115. throw "Unable to parse time with native Date: " + s;
  1116. }
  1117. }
  1118. }
  1119. return {
  1120. hour: d.getHours(),
  1121. minute: d.getMinutes(),
  1122. second: d.getSeconds(),
  1123. millisec: d.getMilliseconds(),
  1124. microsec: d.getMicroseconds(),
  1125. timezone: d.getTimezoneOffset() * -1
  1126. };
  1127. }
  1128. catch (err) {
  1129. try {
  1130. return strictParse(f, s, o);
  1131. }
  1132. catch (err2) {
  1133. $.timepicker.log("Unable to parse \ntimeString: " + s + "\ntimeFormat: " + f);
  1134. }
  1135. }
  1136. return false;
  1137. }; // end looseParse
  1138. if (typeof o.parse === "function") {
  1139. return o.parse(timeFormat, timeString, o);
  1140. }
  1141. if (o.parse === 'loose') {
  1142. return looseParse(timeFormat, timeString, o);
  1143. }
  1144. return strictParse(timeFormat, timeString, o);
  1145. };
  1146. /**
  1147. * Public utility to format the time
  1148. * @param {string} format format of the time
  1149. * @param {Object} time Object not a Date for timezones
  1150. * @param {Object} [options] essentially the regional[].. amNames, pmNames, ampm
  1151. * @returns {string} the formatted time
  1152. */
  1153. $.datepicker.formatTime = function (format, time, options) {
  1154. options = options || {};
  1155. options = $.extend({}, $.timepicker._defaults, options);
  1156. time = $.extend({
  1157. hour: 0,
  1158. minute: 0,
  1159. second: 0,
  1160. millisec: 0,
  1161. microsec: 0,
  1162. timezone: null
  1163. }, time);
  1164. var tmptime = format,
  1165. ampmName = options.amNames[0],
  1166. hour = parseInt(time.hour, 10);
  1167. if (hour > 11) {
  1168. ampmName = options.pmNames[0];
  1169. }
  1170. tmptime = tmptime.replace(/(?:HH?|hh?|mm?|ss?|[tT]{1,2}|[zZ]|[lc]|'.*?')/g, function (match) {
  1171. switch (match) {
  1172. case 'HH':
  1173. return ('0' + hour).slice(-2);
  1174. case 'H':
  1175. return hour;
  1176. case 'hh':
  1177. return ('0' + convert24to12(hour)).slice(-2);
  1178. case 'h':
  1179. return convert24to12(hour);
  1180. case 'mm':
  1181. return ('0' + time.minute).slice(-2);
  1182. case 'm':
  1183. return time.minute;
  1184. case 'ss':
  1185. return ('0' + time.second).slice(-2);
  1186. case 's':
  1187. return time.second;
  1188. case 'l':
  1189. return ('00' + time.millisec).slice(-3);
  1190. case 'c':
  1191. return ('00' + time.microsec).slice(-3);
  1192. case 'z':
  1193. return $.timepicker.timezoneOffsetString(time.timezone === null ? options.timezone : time.timezone, false);
  1194. case 'Z':
  1195. return $.timepicker.timezoneOffsetString(time.timezone === null ? options.timezone : time.timezone, true);
  1196. case 'T':
  1197. return ampmName.charAt(0).toUpperCase();
  1198. case 'TT':
  1199. return ampmName.toUpperCase();
  1200. case 't':
  1201. return ampmName.charAt(0).toLowerCase();
  1202. case 'tt':
  1203. return ampmName.toLowerCase();
  1204. default:
  1205. return match.replace(/'/g, "");
  1206. }
  1207. });
  1208. return tmptime;
  1209. };
  1210. /*
  1211. * the bad hack :/ override datepicker so it doesn't close on select
  1212. // inspired: http://stackoverflow.com/questions/1252512/jquery-datepicker-prevent-closing-picker-when-clicking-a-date/1762378#1762378
  1213. */
  1214. $.datepicker._base_selectDate = $.datepicker._selectDate;
  1215. $.datepicker._selectDate = function (id, dateStr) {
  1216. var inst = this._getInst($(id)[0]),
  1217. tp_inst = this._get(inst, 'timepicker');
  1218. if (tp_inst) {
  1219. tp_inst._limitMinMaxDateTime(inst, true);
  1220. inst.inline = inst.stay_open = true;
  1221. //This way the onSelect handler called from calendarpicker get the full dateTime
  1222. this._base_selectDate(id, dateStr);
  1223. inst.inline = inst.stay_open = false;
  1224. this._notifyChange(inst);
  1225. this._updateDatepicker(inst);
  1226. } else {
  1227. this._base_selectDate(id, dateStr);
  1228. }
  1229. };
  1230. /*
  1231. * second bad hack :/ override datepicker so it triggers an event when changing the input field
  1232. * and does not redraw the datepicker on every selectDate event
  1233. */
  1234. $.datepicker._base_updateDatepicker = $.datepicker._updateDatepicker;
  1235. $.datepicker._updateDatepicker = function (inst) {
  1236. // don't popup the datepicker if there is another instance already opened
  1237. var input = inst.input[0];
  1238. if ($.datepicker._curInst && $.datepicker._curInst !== inst && $.datepicker._datepickerShowing && $.datepicker._lastInput !== input) {
  1239. return;
  1240. }
  1241. if (typeof(inst.stay_open) !== 'boolean' || inst.stay_open === false) {
  1242. this._base_updateDatepicker(inst);
  1243. // Reload the time control when changing something in the input text field.
  1244. var tp_inst = this._get(inst, 'timepicker');
  1245. if (tp_inst) {
  1246. tp_inst._addTimePicker(inst);
  1247. }
  1248. }
  1249. };
  1250. /*
  1251. * third bad hack :/ override datepicker so it allows spaces and colon in the input field
  1252. */
  1253. $.datepicker._base_doKeyPress = $.datepicker._doKeyPress;
  1254. $.datepicker._doKeyPress = function (event) {
  1255. var inst = $.datepicker._getInst(event.target),
  1256. tp_inst = $.datepicker._get(inst, 'timepicker');
  1257. if (tp_inst) {
  1258. if ($.datepicker._get(inst, 'constrainInput')) {
  1259. var ampm = tp_inst.support.ampm,
  1260. tz = tp_inst._defaults.showTimezone !== null ? tp_inst._defaults.showTimezone : tp_inst.support.timezone,
  1261. dateChars = $.datepicker._possibleChars($.datepicker._get(inst, 'dateFormat')),
  1262. datetimeChars = tp_inst._defaults.timeFormat.toString()
  1263. .replace(/[hms]/g, '')
  1264. .replace(/TT/g, ampm ? 'APM' : '')
  1265. .replace(/Tt/g, ampm ? 'AaPpMm' : '')
  1266. .replace(/tT/g, ampm ? 'AaPpMm' : '')
  1267. .replace(/T/g, ampm ? 'AP' : '')
  1268. .replace(/tt/g, ampm ? 'apm' : '')
  1269. .replace(/t/g, ampm ? 'ap' : '') +
  1270. " " + tp_inst._defaults.separator +
  1271. tp_inst._defaults.timeSuffix +
  1272. (tz ? tp_inst._defaults.timezoneList.join('') : '') +
  1273. (tp_inst._defaults.amNames.join('')) + (tp_inst._defaults.pmNames.join('')) +
  1274. dateChars,
  1275. chr = String.fromCharCode(event.charCode === undefined ? event.keyCode : event.charCode);
  1276. return event.ctrlKey || (chr < ' ' || !dateChars || datetimeChars.indexOf(chr) > -1);
  1277. }
  1278. }
  1279. return $.datepicker._base_doKeyPress(event);
  1280. };
  1281. /*
  1282. * Fourth bad hack :/ override _updateAlternate function used in inline mode to init altField
  1283. * Update any alternate field to synchronise with the main field.
  1284. */
  1285. $.datepicker._base_updateAlternate = $.datepicker._updateAlternate;
  1286. $.datepicker._updateAlternate = function (inst) {
  1287. var tp_inst = this._get(inst, 'timepicker');
  1288. if (tp_inst) {
  1289. var altField = tp_inst._defaults.altField;
  1290. if (altField) { // update alternate field too
  1291. var altFormat = tp_inst._defaults.altFormat || tp_inst._defaults.dateFormat,
  1292. date = this._getDate(inst),
  1293. formatCfg = $.datepicker._getFormatConfig(inst),
  1294. altFormattedDateTime = '',
  1295. altSeparator = tp_inst._defaults.altSeparator ? tp_inst._defaults.altSeparator : tp_inst._defaults.separator,
  1296. altTimeSuffix = tp_inst._defaults.altTimeSuffix ? tp_inst._defaults.altTimeSuffix : tp_inst._defaults.timeSuffix,
  1297. altTimeFormat = tp_inst._defaults.altTimeFormat !== null ? tp_inst._defaults.altTimeFormat : tp_inst._defaults.timeFormat;
  1298. altFormattedDateTime += $.datepicker.formatTime(altTimeFormat, tp_inst, tp_inst._defaults) + altTimeSuffix;
  1299. if (!tp_inst._defaults.timeOnly && !tp_inst._defaults.altFieldTimeOnly && date !== null) {
  1300. if (tp_inst._defaults.altFormat) {
  1301. altFormattedDateTime = $.datepicker.formatDate(tp_inst._defaults.altFormat, date, formatCfg) + altSeparator + altFormattedDateTime;
  1302. }
  1303. else {
  1304. altFormattedDateTime = tp_inst.formattedDate + altSeparator + altFormattedDateTime;
  1305. }
  1306. }
  1307. $(altField).val(altFormattedDateTime);
  1308. }
  1309. }
  1310. else {
  1311. $.datepicker._base_updateAlternate(inst);
  1312. }
  1313. };
  1314. /*
  1315. * Override key up event to sync manual input changes.
  1316. */
  1317. $.datepicker._base_doKeyUp = $.datepicker._doKeyUp;
  1318. $.datepicker._doKeyUp = function (event) {
  1319. var inst = $.datepicker._getInst(event.target),
  1320. tp_inst = $.datepicker._get(inst, 'timepicker');
  1321. if (tp_inst) {
  1322. if (tp_inst._defaults.timeOnly && (inst.input.val() !== inst.lastVal)) {
  1323. try {
  1324. $.datepicker._updateDatepicker(inst);
  1325. } catch (err) {
  1326. $.timepicker.log(err);
  1327. }
  1328. }
  1329. }
  1330. return $.datepicker._base_doKeyUp(event);
  1331. };
  1332. /*
  1333. * override "Today" button to also grab the time.
  1334. */
  1335. $.datepicker._base_gotoToday = $.datepicker._gotoToday;
  1336. $.datepicker._gotoToday = function (id) {
  1337. var inst = this._getInst($(id)[0]),
  1338. $dp = inst.dpDiv;
  1339. this._base_gotoToday(id);
  1340. var tp_inst = this._get(inst, 'timepicker');
  1341. selectLocalTimezone(tp_inst);
  1342. var now = new Date();
  1343. this._setTime(inst, now);
  1344. $('.ui-datepicker-today', $dp).click();
  1345. };
  1346. /*
  1347. * Disable & enable the Time in the datetimepicker
  1348. */
  1349. $.datepicker._disableTimepickerDatepicker = function (target) {
  1350. var inst = this._getInst(target);
  1351. if (!inst) {
  1352. return;
  1353. }
  1354. var tp_inst = this._get(inst, 'timepicker');
  1355. $(target).datepicker('getDate'); // Init selected[Year|Month|Day]
  1356. if (tp_inst) {
  1357. inst.settings.showTimepicker = false;
  1358. tp_inst._defaults.showTimepicker = false;
  1359. tp_inst._updateDateTime(inst);
  1360. }
  1361. };
  1362. $.datepicker._enableTimepickerDatepicker = function (target) {
  1363. var inst = this._getInst(target);
  1364. if (!inst) {
  1365. return;
  1366. }
  1367. var tp_inst = this._get(inst, 'timepicker');
  1368. $(target).datepicker('getDate'); // Init selected[Year|Month|Day]
  1369. if (tp_inst) {
  1370. inst.settings.showTimepicker = true;
  1371. tp_inst._defaults.showTimepicker = true;
  1372. tp_inst._addTimePicker(inst); // Could be disabled on page load
  1373. tp_inst._updateDateTime(inst);
  1374. }
  1375. };
  1376. /*
  1377. * Create our own set time function
  1378. */
  1379. $.datepicker._setTime = function (inst, date) {
  1380. var tp_inst = this._get(inst, 'timepicker');
  1381. if (tp_inst) {
  1382. var defaults = tp_inst._defaults;
  1383. // calling _setTime with no date sets time to defaults
  1384. tp_inst.hour = date ? date.getHours() : defaults.hour;
  1385. tp_inst.minute = date ? date.getMinutes() : defaults.minute;
  1386. tp_inst.second = date ? date.getSeconds() : defaults.second;
  1387. tp_inst.millisec = date ? date.getMilliseconds() : defaults.millisec;
  1388. tp_inst.microsec = date ? date.getMicroseconds() : defaults.microsec;
  1389. //check if within min/max times..
  1390. tp_inst._limitMinMaxDateTime(inst, true);
  1391. tp_inst._onTimeChange();
  1392. tp_inst._updateDateTime(inst);
  1393. }
  1394. };
  1395. /*
  1396. * Create new public method to set only time, callable as $().datepicker('setTime', date)
  1397. */
  1398. $.datepicker._setTimeDatepicker = function (target, date, withDate) {
  1399. var inst = this._getInst(target);
  1400. if (!inst) {
  1401. return;
  1402. }
  1403. var tp_inst = this._get(inst, 'timepicker');
  1404. if (tp_inst) {
  1405. this._setDateFromField(inst);
  1406. var tp_date;
  1407. if (date) {
  1408. if (typeof date === "string") {
  1409. tp_inst._parseTime(date, withDate);
  1410. tp_date = new Date();
  1411. tp_date.setHours(tp_inst.hour, tp_inst.minute, tp_inst.second, tp_inst.millisec);
  1412. tp_date.setMicroseconds(tp_inst.microsec);
  1413. } else {
  1414. tp_date = new Date(date.getTime());
  1415. tp_date.setMicroseconds(date.getMicroseconds());
  1416. }
  1417. if (tp_date.toString() === 'Invalid Date') {
  1418. tp_date = undefined;
  1419. }
  1420. this._setTime(inst, tp_date);
  1421. }
  1422. }
  1423. };
  1424. /*
  1425. * override setDate() to allow setting time too within Date object
  1426. */
  1427. $.datepicker._base_setDateDatepicker = $.datepicker._setDateDatepicker;
  1428. $.datepicker._setDateDatepicker = function (target, date) {
  1429. var inst = this._getInst(target);
  1430. if (!inst) {
  1431. return;
  1432. }
  1433. if (typeof(date) === 'string') {
  1434. date = new Date(date);
  1435. if (!date.getTime()) {
  1436. $.timepicker.log("Error creating Date object from string.");
  1437. }
  1438. }
  1439. var tp_inst = this._get(inst, 'timepicker');
  1440. var tp_date;
  1441. if (date instanceof Date) {
  1442. tp_date = new Date(date.getTime());
  1443. tp_date.setMicroseconds(date.getMicroseconds());
  1444. } else {
  1445. tp_date = date;
  1446. }
  1447. // This is important if you are using the timezone option, javascript's Date
  1448. // object will only return the timezone offset for the current locale, so we
  1449. // adjust it accordingly. If not using timezone option this won't matter..
  1450. // If a timezone is different in tp, keep the timezone as is
  1451. if (tp_inst) {
  1452. // look out for DST if tz wasn't specified
  1453. if (!tp_inst.support.timezone && tp_inst._defaults.timezone === null) {
  1454. tp_inst.timezone = tp_date.getTimezoneOffset() * -1;
  1455. }
  1456. date = $.timepicker.timezoneAdjust(date, tp_inst.timezone);
  1457. tp_date = $.timepicker.timezoneAdjust(tp_date, tp_inst.timezone);
  1458. }
  1459. this._updateDatepicker(inst);
  1460. this._base_setDateDatepicker.apply(this, arguments);
  1461. this._setTimeDatepicker(target, tp_date, true);
  1462. };
  1463. /*
  1464. * override getDate() to allow getting time too within Date object
  1465. */
  1466. $.datepicker._base_getDateDatepicker = $.datepicker._getDateDatepicker;
  1467. $.datepicker._getDateDatepicker = function (target, noDefault) {
  1468. var inst = this._getInst(target);
  1469. if (!inst) {
  1470. return;
  1471. }
  1472. var tp_inst = this._get(inst, 'timepicker');
  1473. if (tp_inst) {
  1474. // if it hasn't yet been defined, grab from field
  1475. if (inst.lastVal === undefined) {
  1476. this._setDateFromField(inst, noDefault);
  1477. }
  1478. var date = this._getDate(inst);
  1479. if (date && tp_inst._parseTime($(target).val(), tp_inst.timeOnly)) {
  1480. date.setHours(tp_inst.hour, tp_inst.minute, tp_inst.second, tp_inst.millisec);
  1481. date.setMicroseconds(tp_inst.microsec);
  1482. // This is important if you are using the timezone option, javascript's Date
  1483. // object will only return the timezone offset for the current locale, so we
  1484. // adjust it accordingly. If not using timezone option this won't matter..
  1485. if (tp_inst.timezone != null) {
  1486. // look out for DST if tz wasn't specified
  1487. if (!tp_inst.support.timezone && tp_inst._defaults.timezone === null) {
  1488. tp_inst.timezone = date.getTimezoneOffset() * -1;
  1489. }
  1490. date = $.timepicker.timezoneAdjust(date, tp_inst.timezone);
  1491. }
  1492. }
  1493. return date;
  1494. }
  1495. return this._base_getDateDatepicker(target, noDefault);
  1496. };
  1497. /*
  1498. * override parseDate() because UI 1.8.14 throws an error about "Extra characters"
  1499. * An option in datapicker to ignore extra format characters would be nicer.
  1500. */
  1501. $.datepicker._base_parseDate = $.datepicker.parseDate;
  1502. $.datepicker.parseDate = function (format, value, settings) {
  1503. var date;
  1504. try {
  1505. date = this._base_parseDate(format, value, settings);
  1506. } catch (err) {
  1507. // Hack! The error message ends with a colon, a space, and
  1508. // the "extra" characters. We rely on that instead of
  1509. // attempting to perfectly reproduce the parsing algorithm.
  1510. if (err.indexOf(":") >= 0) {
  1511. date = this._base_parseDate(format, value.substring(0, value.length - (err.length - err.indexOf(':') - 2)), settings);
  1512. $.timepicker.log("Error parsing the date string: " + err + "\ndate string = " + value + "\ndate format = " + format);
  1513. } else {
  1514. throw err;
  1515. }
  1516. }
  1517. return date;
  1518. };
  1519. /*
  1520. * override formatDate to set date with time to the input
  1521. */
  1522. $.datepicker._base_formatDate = $.datepicker._formatDate;
  1523. $.datepicker._formatDate = function (inst, day, month, year) {
  1524. var tp_inst = this._get(inst, 'timepicker');
  1525. if (tp_inst) {
  1526. tp_inst._updateDateTime(inst);
  1527. return tp_inst.$input.val();
  1528. }
  1529. return this._base_formatDate(inst);
  1530. };
  1531. /*
  1532. * override options setter to add time to maxDate(Time) and minDate(Time). MaxDate
  1533. */
  1534. $.datepicker._base_optionDatepicker = $.datepicker._optionDatepicker;
  1535. $.datepicker._optionDatepicker = function (target, name, value) {
  1536. var inst = this._getInst(target),
  1537. name_clone;
  1538. if (!inst) {
  1539. return null;
  1540. }
  1541. var tp_inst = this._get(inst, 'timepicker');
  1542. if (tp_inst) {
  1543. var min = null,
  1544. max = null,
  1545. onselect = null,
  1546. overrides = tp_inst._defaults.evnts,
  1547. fns = {},
  1548. prop;
  1549. if (typeof name === 'string') { // if min/max was set with the string
  1550. if (name === 'minDate' || name === 'minDateTime') {
  1551. min = value;
  1552. } else if (name === 'maxDate' || name === 'maxDateTime') {
  1553. max = value;
  1554. } else if (name === 'onSelect') {
  1555. onselect = value;
  1556. } else if (overrides.hasOwnProperty(name)) {
  1557. if (typeof (value) === 'undefined') {
  1558. return overrides[name];
  1559. }
  1560. fns[name] = value;
  1561. name_clone = {}; //empty results in exiting function after overrides updated
  1562. }
  1563. } else if (typeof name === 'object') { //if min/max was set with the JSON
  1564. if (name.minDate) {
  1565. min = name.minDate;
  1566. } else if (name.minDateTime) {
  1567. min = name.minDateTime;
  1568. } else if (name.maxDate) {
  1569. max = name.maxDate;
  1570. } else if (name.maxDateTime) {
  1571. max = name.maxDateTime;
  1572. }
  1573. for (prop in overrides) {
  1574. if (overrides.hasOwnProperty(prop) && name[prop]) {
  1575. fns[prop] = name[prop];
  1576. }
  1577. }
  1578. }
  1579. for (prop in fns) {
  1580. if (fns.hasOwnProperty(prop)) {
  1581. overrides[prop] = fns[prop];
  1582. if (!name_clone) { name_clone = $.extend({}, name); }
  1583. delete name_clone[prop];
  1584. }
  1585. }
  1586. if (name_clone && isEmptyObject(name_clone)) { return; }
  1587. if (min) { //if min was set
  1588. if (min === 0) {
  1589. min = new Date();
  1590. } else {
  1591. min = new Date(min);
  1592. }
  1593. tp_inst._defaults.minDate = min;
  1594. tp_inst._defaults.minDateTime = min;
  1595. } else if (max) { //if max was set
  1596. if (max === 0) {
  1597. max = new Date();
  1598. } else {
  1599. max = new Date(max);
  1600. }
  1601. tp_inst._defaults.maxDate = max;
  1602. tp_inst._defaults.maxDateTime = max;
  1603. } else if (onselect) {
  1604. tp_inst._defaults.onSelect = onselect;
  1605. }
  1606. }
  1607. if (value === undefined) {
  1608. return this._base_optionDatepicker.call($.datepicker, target, name);
  1609. }
  1610. return this._base_optionDatepicker.call($.datepicker, target, name_clone || name, value);
  1611. };
  1612. /*
  1613. * jQuery isEmptyObject does not check hasOwnProperty - if someone has added to the object prototype,
  1614. * it will return false for all objects
  1615. */
  1616. var isEmptyObject = function (obj) {
  1617. var prop;
  1618. for (prop in obj) {
  1619. if (obj.hasOwnProperty(prop)) {
  1620. return false;
  1621. }
  1622. }
  1623. return true;
  1624. };
  1625. /*
  1626. * jQuery extend now ignores nulls!
  1627. */
  1628. var extendRemove = function (target, props) {
  1629. $.extend(target, props);
  1630. for (var name in props) {
  1631. if (props[name] === null || props[name] === undefined) {
  1632. target[name] = props[name];
  1633. }
  1634. }
  1635. return target;
  1636. };
  1637. /*
  1638. * Determine by the time format which units are supported
  1639. * Returns an object of booleans for each unit
  1640. */
  1641. var detectSupport = function (timeFormat) {
  1642. var tf = timeFormat.replace(/'.*?'/g, '').toLowerCase(), // removes literals
  1643. isIn = function (f, t) { // does the format contain the token?
  1644. return f.indexOf(t) !== -1 ? true : false;
  1645. };
  1646. return {
  1647. hour: isIn(tf, 'h'),
  1648. minute: isIn(tf, 'm'),
  1649. second: isIn(tf, 's'),
  1650. millisec: isIn(tf, 'l'),
  1651. microsec: isIn(tf, 'c'),
  1652. timezone: isIn(tf, 'z'),
  1653. ampm: isIn(tf, 't') && isIn(timeFormat, 'h'),
  1654. iso8601: isIn(timeFormat, 'Z')
  1655. };
  1656. };
  1657. /*
  1658. * Converts 24 hour format into 12 hour
  1659. * Returns 12 hour without leading 0
  1660. */
  1661. var convert24to12 = function (hour) {
  1662. hour %= 12;
  1663. if (hour === 0) {
  1664. hour = 12;
  1665. }
  1666. return String(hour);
  1667. };
  1668. var computeEffectiveSetting = function (settings, property) {
  1669. return settings && settings[property] ? settings[property] : $.timepicker._defaults[property];
  1670. };
  1671. /*
  1672. * Splits datetime string into date and time substrings.
  1673. * Throws exception when date can't be parsed
  1674. * Returns {dateString: dateString, timeString: timeString}
  1675. */
  1676. var splitDateTime = function (dateTimeString, timeSettings) {
  1677. // The idea is to get the number separator occurrences in datetime and the time format requested (since time has
  1678. // fewer unknowns, mostly numbers and am/pm). We will use the time pattern to split.
  1679. var separator = computeEffectiveSetting(timeSettings, 'separator'),
  1680. format = computeEffectiveSetting(timeSettings, 'timeFormat'),
  1681. timeParts = format.split(separator), // how many occurrences of separator may be in our format?
  1682. timePartsLen = timeParts.length,
  1683. allParts = dateTimeString.split(separator),
  1684. allPartsLen = allParts.length;
  1685. if (allPartsLen > 1) {
  1686. return {
  1687. dateString: allParts.splice(0, allPartsLen - timePartsLen).join(separator),
  1688. timeString: allParts.splice(0, timePartsLen).join(separator)
  1689. };
  1690. }
  1691. return {
  1692. dateString: dateTimeString,
  1693. timeString: ''
  1694. };
  1695. };
  1696. /*
  1697. * Internal function to parse datetime interval
  1698. * Returns: {date: Date, timeObj: Object}, where
  1699. * date - parsed date without time (type Date)
  1700. * timeObj = {hour: , minute: , second: , millisec: , microsec: } - parsed time. Optional
  1701. */
  1702. var parseDateTimeInternal = function (dateFormat, timeFormat, dateTimeString, dateSettings, timeSettings) {
  1703. var date,
  1704. parts,
  1705. parsedTime;
  1706. parts = splitDateTime(dateTimeString, timeSettings);
  1707. date = $.datepicker._base_parseDate(dateFormat, parts.dateString, dateSettings);
  1708. if (parts.timeString === '') {
  1709. return {
  1710. date: date
  1711. };
  1712. }
  1713. parsedTime = $.datepicker.parseTime(timeFormat, parts.timeString, timeSettings);
  1714. if (!parsedTime) {
  1715. throw 'Wrong time format';
  1716. }
  1717. return {
  1718. date: date,
  1719. timeObj: parsedTime
  1720. };
  1721. };
  1722. /*
  1723. * Internal function to set timezone_select to the local timezone
  1724. */
  1725. var selectLocalTimezone = function (tp_inst, date) {
  1726. if (tp_inst && tp_inst.timezone_select) {
  1727. var now = date || new Date();
  1728. tp_inst.timezone_select.val(-now.getTimezoneOffset());
  1729. }
  1730. };
  1731. /*
  1732. * Create a Singleton Instance
  1733. */
  1734. $.timepicker = new Timepicker();
  1735. /**
  1736. * Get the timezone offset as string from a date object (eg '+0530' for UTC+5.5)
  1737. * @param {number} tzMinutes if not a number, less than -720 (-1200), or greater than 840 (+1400) this value is returned
  1738. * @param {boolean} iso8601 if true formats in accordance to iso8601 "+12:45"
  1739. * @return {string}
  1740. */
  1741. $.timepicker.timezoneOffsetString = function (tzMinutes, iso8601) {
  1742. if (isNaN(tzMinutes) || tzMinutes > 840 || tzMinutes < -720) {
  1743. return tzMinutes;
  1744. }
  1745. var off = tzMinutes,
  1746. minutes = off % 60,
  1747. hours = (off - minutes) / 60,
  1748. iso = iso8601 ? ':' : '',
  1749. tz = (off >= 0 ? '+' : '-') + ('0' + Math.abs(hours)).slice(-2) + iso + ('0' + Math.abs(minutes)).slice(-2);
  1750. if (tz === '+00:00') {
  1751. return 'Z';
  1752. }
  1753. return tz;
  1754. };
  1755. /**
  1756. * Get the number in minutes that represents a timezone string
  1757. * @param {string} tzString formatted like "+0500", "-1245", "Z"
  1758. * @return {number} the offset minutes or the original string if it doesn't match expectations
  1759. */
  1760. $.timepicker.timezoneOffsetNumber = function (tzString) {
  1761. var normalized = tzString.toString().replace(':', ''); // excuse any iso8601, end up with "+1245"
  1762. if (normalized.toUpperCase() === 'Z') { // if iso8601 with Z, its 0 minute offset
  1763. return 0;
  1764. }
  1765. if (!/^(\-|\+)\d{4}$/.test(normalized)) { // possibly a user defined tz, so just give it back
  1766. return tzString;
  1767. }
  1768. return ((normalized.substr(0, 1) === '-' ? -1 : 1) * // plus or minus
  1769. ((parseInt(normalized.substr(1, 2), 10) * 60) + // hours (converted to minutes)
  1770. parseInt(normalized.substr(3, 2), 10))); // minutes
  1771. };
  1772. /**
  1773. * No way to set timezone in js Date, so we must adjust the minutes to compensate. (think setDate, getDate)
  1774. * @param {Date} date
  1775. * @param {string} toTimezone formatted like "+0500", "-1245"
  1776. * @return {Date}
  1777. */
  1778. $.timepicker.timezoneAdjust = function (date, toTimezone) {
  1779. var toTz = $.timepicker.timezoneOffsetNumber(toTimezone);
  1780. if (!isNaN(toTz)) {
  1781. date.setMinutes(date.getMinutes() + -date.getTimezoneOffset() - toTz);
  1782. }
  1783. return date;
  1784. };
  1785. /**
  1786. * Calls `timepicker()` on the `startTime` and `endTime` elements, and configures them to
  1787. * enforce date range limits.
  1788. * n.b. The input value must be correctly formatted (reformatting is not supported)
  1789. * @param {Element} startTime
  1790. * @param {Element} endTime
  1791. * @param {Object} options Options for the timepicker() call
  1792. * @return {jQuery}
  1793. */
  1794. $.timepicker.timeRange = function (startTime, endTime, options) {
  1795. return $.timepicker.handleRange('timepicker', startTime, endTime, options);
  1796. };
  1797. /**
  1798. * Calls `datetimepicker` on the `startTime` and `endTime` elements, and configures them to
  1799. * enforce date range limits.
  1800. * @param {Element} startTime
  1801. * @param {Element} endTime
  1802. * @param {Object} options Options for the `timepicker()` call. Also supports `reformat`,
  1803. * a boolean value that can be used to reformat the input values to the `dateFormat`.
  1804. * @param {string} method Can be used to specify the type of picker to be added
  1805. * @return {jQuery}
  1806. */
  1807. $.timepicker.datetimeRange = function (startTime, endTime, options) {
  1808. $.timepicker.handleRange('datetimepicker', startTime, endTime, options);
  1809. };
  1810. /**
  1811. * Calls `datepicker` on the `startTime` and `endTime` elements, and configures them to
  1812. * enforce date range limits.
  1813. * @param {Element} startTime
  1814. * @param {Element} endTime
  1815. * @param {Object} options Options for the `timepicker()` call. Also supports `reformat`,
  1816. * a boolean value that can be used to reformat the input values to the `dateFormat`.
  1817. * @return {jQuery}
  1818. */
  1819. $.timepicker.dateRange = function (startTime, endTime, options) {
  1820. $.timepicker.handleRange('datepicker', startTime, endTime, options);
  1821. };
  1822. /**
  1823. * Calls `method` on the `startTime` and `endTime` elements, and configures them to
  1824. * enforce date range limits.
  1825. * @param {string} method Can be used to specify the type of picker to be added
  1826. * @param {Element} startTime
  1827. * @param {Element} endTime
  1828. * @param {Object} options Options for the `timepicker()` call. Also supports `reformat`,
  1829. * a boolean value that can be used to reformat the input values to the `dateFormat`.
  1830. * @return {jQuery}
  1831. */
  1832. $.timepicker.handleRange = function (method, startTime, endTime, options) {
  1833. options = $.extend({}, {
  1834. minInterval: 0, // min allowed interval in milliseconds
  1835. maxInterval: 0, // max allowed interval in milliseconds
  1836. start: {}, // options for start picker
  1837. end: {} // options for end picker
  1838. }, options);
  1839. function checkDates(changed, other) {
  1840. var startdt = startTime[method]('getDate'),
  1841. enddt = endTime[method]('getDate'),
  1842. changeddt = changed[method]('getDate');
  1843. if (startdt !== null) {
  1844. var minDate = new Date(startdt.getTime()),
  1845. maxDate = new Date(startdt.getTime());
  1846. minDate.setMilliseconds(minDate.getMilliseconds() + options.minInterval);
  1847. maxDate.setMilliseconds(maxDate.getMilliseconds() + options.maxInterval);
  1848. if (options.minInterval > 0 && minDate > enddt) { // minInterval check
  1849. endTime[method]('setDate', minDate);
  1850. }
  1851. else if (options.maxInterval > 0 && maxDate < enddt) { // max interval check
  1852. endTime[method]('setDate', maxDate);
  1853. }
  1854. else if (startdt > enddt) {
  1855. other[method]('setDate', changeddt);
  1856. }
  1857. }
  1858. }
  1859. function selected(changed, other, option) {
  1860. if (!changed.val()) {
  1861. return;
  1862. }
  1863. var date = changed[method].call(changed, 'getDate');
  1864. if (date !== null && options.minInterval > 0) {
  1865. if (option === 'minDate') {
  1866. date.setMilliseconds(date.getMilliseconds() + options.minInterval);
  1867. }
  1868. if (option === 'maxDate') {
  1869. date.setMilliseconds(date.getMilliseconds() - options.minInterval);
  1870. }
  1871. }
  1872. if (date.getTime) {
  1873. other[method].call(other, 'option', option, date);
  1874. }
  1875. }
  1876. $.fn[method].call(startTime, $.extend({
  1877. onClose: function (dateText, inst) {
  1878. checkDates($(this), endTime);
  1879. },
  1880. onSelect: function (selectedDateTime) {
  1881. selected($(this), endTime, 'minDate');
  1882. }
  1883. }, options, options.start));
  1884. $.fn[method].call(endTime, $.extend({
  1885. onClose: function (dateText, inst) {
  1886. checkDates($(this), startTime);
  1887. },
  1888. onSelect: function (selectedDateTime) {
  1889. selected($(this), startTime, 'maxDate');
  1890. }
  1891. }, options, options.end));
  1892. checkDates(startTime, endTime);
  1893. selected(startTime, endTime, 'minDate');
  1894. selected(endTime, startTime, 'maxDate');
  1895. return $([startTime.get(0), endTime.get(0)]);
  1896. };
  1897. /**
  1898. * Log error or data to the console during error or debugging
  1899. * @param {Object} err pass any type object to log to the console during error or debugging
  1900. * @return {void}
  1901. */
  1902. $.timepicker.log = function (err) {
  1903. if (window.console) {
  1904. window.console.log(err);
  1905. }
  1906. };
  1907. /*
  1908. * Add util object to allow access to private methods for testability.
  1909. */
  1910. $.timepicker._util = {
  1911. _extendRemove: extendRemove,
  1912. _isEmptyObject: isEmptyObject,
  1913. _convert24to12: convert24to12,
  1914. _detectSupport: detectSupport,
  1915. _selectLocalTimezone: selectLocalTimezone,
  1916. _computeEffectiveSetting: computeEffectiveSetting,
  1917. _splitDateTime: splitDateTime,
  1918. _parseDateTimeInternal: parseDateTimeInternal
  1919. };
  1920. /*
  1921. * Microsecond support
  1922. */
  1923. if (!Date.prototype.getMicroseconds) {
  1924. Date.prototype.microseconds = 0;
  1925. Date.prototype.getMicroseconds = function () { return this.microseconds; };
  1926. Date.prototype.setMicroseconds = function (m) {
  1927. this.setMilliseconds(this.getMilliseconds() + Math.floor(m / 1000));
  1928. this.microseconds = m % 1000;
  1929. return this;
  1930. };
  1931. }
  1932. /*
  1933. * Keep up with the version
  1934. */
  1935. $.timepicker.version = "1.4";
  1936. })(jQuery);