jquery-ui-timepicker-addon.js 59 KB

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