jquery-ui-timepicker-addon.js 61 KB

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