jquery-ui-timepicker-addon.js 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530
  1. /*
  2. * jQuery timepicker addon
  3. * By: Trent Richardson [http://trentrichardson.com]
  4. * Version 1.0.1
  5. * Last Modified: 07/01/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. * HERES THE CSS:
  13. * .ui-timepicker-div .ui-widget-header { margin-bottom: 8px; }
  14. * .ui-timepicker-div dl { text-align: left; }
  15. * .ui-timepicker-div dl dt { height: 25px; margin-bottom: -25px; }
  16. * .ui-timepicker-div dl dd { margin: 0 10px 10px 65px; }
  17. * .ui-timepicker-div td { font-size: 90%; }
  18. * .ui-tpicker-grid-label { background: none; border: none; margin: 0; padding: 0; }
  19. */
  20. /*jslint evil: true, maxlen: 300, white: false, undef: false, nomen: false, onevar: false */
  21. (function($) {
  22. // Prevent "Uncaught RangeError: Maximum call stack size exceeded"
  23. $.ui.timepicker = $.ui.timepicker || {};
  24. if ($.ui.timepicker.version) {
  25. return;
  26. }
  27. $.extend($.ui, { timepicker: { version: "1.0.1" } });
  28. /* Time picker manager.
  29. Use the singleton instance of this class, $.timepicker, to interact with the time picker.
  30. Settings for (groups of) time pickers are maintained in an instance object,
  31. allowing multiple different settings on the same page. */
  32. function Timepicker() {
  33. this.regional = []; // Available regional settings, indexed by language code
  34. this.regional[''] = { // Default regional settings
  35. currentText: 'Now',
  36. closeText: 'Done',
  37. ampm: false,
  38. amNames: ['AM', 'A'],
  39. pmNames: ['PM', 'P'],
  40. timeFormat: 'hh:mm tt',
  41. timeSuffix: '',
  42. timeOnlyTitle: 'Choose Time',
  43. timeText: 'Time',
  44. hourText: 'Hour',
  45. minuteText: 'Minute',
  46. secondText: 'Second',
  47. millisecText: 'Millisecond',
  48. timezoneText: 'Time Zone'
  49. };
  50. this._defaults = { // Global defaults for all the datetime picker instances
  51. showButtonPanel: true,
  52. timeOnly: false,
  53. showHour: true,
  54. showMinute: true,
  55. showSecond: false,
  56. showMillisec: false,
  57. showTimezone: false,
  58. showTime: true,
  59. stepHour: 1,
  60. stepMinute: 1,
  61. stepSecond: 1,
  62. stepMillisec: 1,
  63. hour: 0,
  64. minute: 0,
  65. second: 0,
  66. millisec: 0,
  67. timezone: null,
  68. useLocalTimezone: false,
  69. defaultTimezone: "+0000",
  70. hourMin: 0,
  71. minuteMin: 0,
  72. secondMin: 0,
  73. millisecMin: 0,
  74. hourMax: 23,
  75. minuteMax: 59,
  76. secondMax: 59,
  77. millisecMax: 999,
  78. minDateTime: null,
  79. maxDateTime: null,
  80. onSelect: null,
  81. hourGrid: 0,
  82. minuteGrid: 0,
  83. secondGrid: 0,
  84. millisecGrid: 0,
  85. alwaysSetTime: true,
  86. separator: ' ',
  87. altFieldTimeOnly: true,
  88. showTimepicker: true,
  89. timezoneIso8601: false,
  90. timezoneList: null,
  91. addSliderAccess: false,
  92. sliderAccessArgs: null
  93. };
  94. $.extend(this._defaults, this.regional['']);
  95. }
  96. $.extend(Timepicker.prototype, {
  97. $input: null,
  98. $altInput: null,
  99. $timeObj: null,
  100. inst: null,
  101. hour_slider: null,
  102. minute_slider: null,
  103. second_slider: null,
  104. millisec_slider: null,
  105. timezone_select: null,
  106. hour: 0,
  107. minute: 0,
  108. second: 0,
  109. millisec: 0,
  110. timezone: null,
  111. defaultTimezone: "+0000",
  112. hourMinOriginal: null,
  113. minuteMinOriginal: null,
  114. secondMinOriginal: null,
  115. millisecMinOriginal: null,
  116. hourMaxOriginal: null,
  117. minuteMaxOriginal: null,
  118. secondMaxOriginal: null,
  119. millisecMaxOriginal: null,
  120. ampm: '',
  121. formattedDate: '',
  122. formattedTime: '',
  123. formattedDateTime: '',
  124. timezoneList: null,
  125. /* Override the default settings for all instances of the time picker.
  126. @param settings object - the new settings to use as defaults (anonymous object)
  127. @return the manager object */
  128. setDefaults: function(settings) {
  129. extendRemove(this._defaults, settings || {});
  130. return this;
  131. },
  132. //########################################################################
  133. // Create a new Timepicker instance
  134. //########################################################################
  135. _newInst: function($input, o) {
  136. var tp_inst = new Timepicker(),
  137. inlineSettings = {};
  138. for (var attrName in this._defaults) {
  139. var attrValue = $input.attr('time:' + attrName);
  140. if (attrValue) {
  141. try {
  142. inlineSettings[attrName] = eval(attrValue);
  143. } catch (err) {
  144. inlineSettings[attrName] = attrValue;
  145. }
  146. }
  147. }
  148. tp_inst._defaults = $.extend({}, this._defaults, inlineSettings, o, {
  149. beforeShow: function(input, dp_inst) {
  150. if ($.isFunction(o.beforeShow)) {
  151. return o.beforeShow(input, dp_inst, tp_inst);
  152. }
  153. },
  154. onChangeMonthYear: function(year, month, dp_inst) {
  155. // Update the time as well : this prevents the time from disappearing from the $input field.
  156. tp_inst._updateDateTime(dp_inst);
  157. if ($.isFunction(o.onChangeMonthYear)) {
  158. o.onChangeMonthYear.call($input[0], year, month, dp_inst, tp_inst);
  159. }
  160. },
  161. onClose: function(dateText, dp_inst) {
  162. if (tp_inst.timeDefined === true && $input.val() !== '') {
  163. tp_inst._updateDateTime(dp_inst);
  164. }
  165. if ($.isFunction(o.onClose)) {
  166. o.onClose.call($input[0], dateText, dp_inst, tp_inst);
  167. }
  168. },
  169. timepicker: tp_inst // add timepicker as a property of datepicker: $.datepicker._get(dp_inst, 'timepicker');
  170. });
  171. tp_inst.amNames = $.map(tp_inst._defaults.amNames, function(val) { return val.toUpperCase(); });
  172. tp_inst.pmNames = $.map(tp_inst._defaults.pmNames, function(val) { return val.toUpperCase(); });
  173. if (tp_inst._defaults.timezoneList === null) {
  174. var timezoneList = [];
  175. for (var i = -11; i <= 12; i++) {
  176. timezoneList.push((i >= 0 ? '+' : '-') + ('0' + Math.abs(i).toString()).slice(-2) + '00');
  177. }
  178. if (tp_inst._defaults.timezoneIso8601) {
  179. timezoneList = $.map(timezoneList, function(val) {
  180. return val == '+0000' ? 'Z' : (val.substring(0, 3) + ':' + val.substring(3));
  181. });
  182. }
  183. tp_inst._defaults.timezoneList = timezoneList;
  184. }
  185. tp_inst.timezone = tp_inst._defaults.timezone;
  186. tp_inst.hour = tp_inst._defaults.hour;
  187. tp_inst.minute = tp_inst._defaults.minute;
  188. tp_inst.second = tp_inst._defaults.second;
  189. tp_inst.millisec = tp_inst._defaults.millisec;
  190. tp_inst.ampm = '';
  191. tp_inst.$input = $input;
  192. if (o.altField) {
  193. tp_inst.$altInput = $(o.altField)
  194. .css({ cursor: 'pointer' })
  195. .focus(function(){ $input.trigger("focus"); });
  196. }
  197. if(tp_inst._defaults.minDate===0 || tp_inst._defaults.minDateTime===0)
  198. {
  199. tp_inst._defaults.minDate=new Date();
  200. }
  201. if(tp_inst._defaults.maxDate===0 || tp_inst._defaults.maxDateTime===0)
  202. {
  203. tp_inst._defaults.maxDate=new Date();
  204. }
  205. // datepicker needs minDate/maxDate, timepicker needs minDateTime/maxDateTime..
  206. if(tp_inst._defaults.minDate !== undefined && tp_inst._defaults.minDate instanceof Date) {
  207. tp_inst._defaults.minDateTime = new Date(tp_inst._defaults.minDate.getTime());
  208. }
  209. if(tp_inst._defaults.minDateTime !== undefined && tp_inst._defaults.minDateTime instanceof Date) {
  210. tp_inst._defaults.minDate = new Date(tp_inst._defaults.minDateTime.getTime());
  211. }
  212. if(tp_inst._defaults.maxDate !== undefined && tp_inst._defaults.maxDate instanceof Date) {
  213. tp_inst._defaults.maxDateTime = new Date(tp_inst._defaults.maxDate.getTime());
  214. }
  215. if(tp_inst._defaults.maxDateTime !== undefined && tp_inst._defaults.maxDateTime instanceof Date) {
  216. tp_inst._defaults.maxDate = new Date(tp_inst._defaults.maxDateTime.getTime());
  217. }
  218. return tp_inst;
  219. },
  220. //########################################################################
  221. // add our sliders to the calendar
  222. //########################################################################
  223. _addTimePicker: function(dp_inst) {
  224. var currDT = (this.$altInput && this._defaults.altFieldTimeOnly) ?
  225. this.$input.val() + ' ' + this.$altInput.val() :
  226. this.$input.val();
  227. this.timeDefined = this._parseTime(currDT);
  228. this._limitMinMaxDateTime(dp_inst, false);
  229. this._injectTimePicker();
  230. },
  231. //########################################################################
  232. // parse the time string from input value or _setTime
  233. //########################################################################
  234. _parseTime: function(timeString, withDate) {
  235. if (!this.inst) {
  236. this.inst = $.datepicker._getInst(this.$input[0]);
  237. }
  238. if (withDate || !this._defaults.timeOnly)
  239. {
  240. var dp_dateFormat = $.datepicker._get(this.inst, 'dateFormat');
  241. try {
  242. var parseRes = parseDateTimeInternal(dp_dateFormat, this._defaults.timeFormat, timeString, $.datepicker._getFormatConfig(this.inst), this._defaults);
  243. if (!parseRes.timeObj) { return false; }
  244. $.extend(this, parseRes.timeObj);
  245. } catch (err)
  246. {
  247. return false;
  248. }
  249. return true;
  250. }
  251. else
  252. {
  253. var timeObj = $.datepicker.parseTime(this._defaults.timeFormat, timeString, this._defaults);
  254. if(!timeObj) { return false; }
  255. $.extend(this, timeObj);
  256. return true;
  257. }
  258. },
  259. //########################################################################
  260. // generate and inject html for timepicker into ui datepicker
  261. //########################################################################
  262. _injectTimePicker: function() {
  263. var $dp = this.inst.dpDiv,
  264. o = this._defaults,
  265. tp_inst = this,
  266. // Added by Peter Medeiros:
  267. // - Figure out what the hour/minute/second max should be based on the step values.
  268. // - Example: if stepMinute is 15, then minMax is 45.
  269. hourMax = parseInt((o.hourMax - ((o.hourMax - o.hourMin) % o.stepHour)) ,10),
  270. minMax = parseInt((o.minuteMax - ((o.minuteMax - o.minuteMin) % o.stepMinute)) ,10),
  271. secMax = parseInt((o.secondMax - ((o.secondMax - o.secondMin) % o.stepSecond)) ,10),
  272. millisecMax = parseInt((o.millisecMax - ((o.millisecMax - o.millisecMin) % o.stepMillisec)) ,10),
  273. dp_id = this.inst.id.toString().replace(/([^A-Za-z0-9_])/g, '');
  274. // Prevent displaying twice
  275. //if ($dp.find("div#ui-timepicker-div-"+ dp_id).length === 0) {
  276. if ($dp.find("div#ui-timepicker-div-"+ dp_id).length === 0 && o.showTimepicker) {
  277. var noDisplay = ' style="display:none;"',
  278. html = '<div class="ui-timepicker-div" id="ui-timepicker-div-' + dp_id + '"><dl>' +
  279. '<dt class="ui_tpicker_time_label" id="ui_tpicker_time_label_' + dp_id + '"' +
  280. ((o.showTime) ? '' : noDisplay) + '>' + o.timeText + '</dt>' +
  281. '<dd class="ui_tpicker_time" id="ui_tpicker_time_' + dp_id + '"' +
  282. ((o.showTime) ? '' : noDisplay) + '></dd>' +
  283. '<dt class="ui_tpicker_hour_label" id="ui_tpicker_hour_label_' + dp_id + '"' +
  284. ((o.showHour) ? '' : noDisplay) + '>' + o.hourText + '</dt>',
  285. hourGridSize = 0,
  286. minuteGridSize = 0,
  287. secondGridSize = 0,
  288. millisecGridSize = 0,
  289. size = null;
  290. // Hours
  291. html += '<dd class="ui_tpicker_hour"><div id="ui_tpicker_hour_' + dp_id + '"' +
  292. ((o.showHour) ? '' : noDisplay) + '></div>';
  293. if (o.showHour && o.hourGrid > 0) {
  294. html += '<div style="padding-left: 1px"><table class="ui-tpicker-grid-label"><tr>';
  295. for (var h = o.hourMin; h <= hourMax; h += parseInt(o.hourGrid,10)) {
  296. hourGridSize++;
  297. var tmph = (o.ampm && h > 12) ? h-12 : h;
  298. if (tmph < 10) { tmph = '0' + tmph; }
  299. if (o.ampm) {
  300. if (h === 0) {
  301. tmph = 12 +'a';
  302. } else {
  303. if (h < 12) { tmph += 'a'; }
  304. else { tmph += 'p'; }
  305. }
  306. }
  307. html += '<td>' + tmph + '</td>';
  308. }
  309. html += '</tr></table></div>';
  310. }
  311. html += '</dd>';
  312. // Minutes
  313. html += '<dt class="ui_tpicker_minute_label" id="ui_tpicker_minute_label_' + dp_id + '"' +
  314. ((o.showMinute) ? '' : noDisplay) + '>' + o.minuteText + '</dt>'+
  315. '<dd class="ui_tpicker_minute"><div id="ui_tpicker_minute_' + dp_id + '"' +
  316. ((o.showMinute) ? '' : noDisplay) + '></div>';
  317. if (o.showMinute && o.minuteGrid > 0) {
  318. html += '<div style="padding-left: 1px"><table class="ui-tpicker-grid-label"><tr>';
  319. for (var m = o.minuteMin; m <= minMax; m += parseInt(o.minuteGrid,10)) {
  320. minuteGridSize++;
  321. html += '<td>' + ((m < 10) ? '0' : '') + m + '</td>';
  322. }
  323. html += '</tr></table></div>';
  324. }
  325. html += '</dd>';
  326. // Seconds
  327. html += '<dt class="ui_tpicker_second_label" id="ui_tpicker_second_label_' + dp_id + '"' +
  328. ((o.showSecond) ? '' : noDisplay) + '>' + o.secondText + '</dt>'+
  329. '<dd class="ui_tpicker_second"><div id="ui_tpicker_second_' + dp_id + '"'+
  330. ((o.showSecond) ? '' : noDisplay) + '></div>';
  331. if (o.showSecond && o.secondGrid > 0) {
  332. html += '<div style="padding-left: 1px"><table><tr>';
  333. for (var s = o.secondMin; s <= secMax; s += parseInt(o.secondGrid,10)) {
  334. secondGridSize++;
  335. html += '<td>' + ((s < 10) ? '0' : '') + s + '</td>';
  336. }
  337. html += '</tr></table></div>';
  338. }
  339. html += '</dd>';
  340. // Milliseconds
  341. html += '<dt class="ui_tpicker_millisec_label" id="ui_tpicker_millisec_label_' + dp_id + '"' +
  342. ((o.showMillisec) ? '' : noDisplay) + '>' + o.millisecText + '</dt>'+
  343. '<dd class="ui_tpicker_millisec"><div id="ui_tpicker_millisec_' + dp_id + '"'+
  344. ((o.showMillisec) ? '' : noDisplay) + '></div>';
  345. if (o.showMillisec && o.millisecGrid > 0) {
  346. html += '<div style="padding-left: 1px"><table><tr>';
  347. for (var l = o.millisecMin; l <= millisecMax; l += parseInt(o.millisecGrid,10)) {
  348. millisecGridSize++;
  349. html += '<td>' + ((l < 10) ? '0' : '') + l + '</td>';
  350. }
  351. html += '</tr></table></div>';
  352. }
  353. html += '</dd>';
  354. // Timezone
  355. html += '<dt class="ui_tpicker_timezone_label" id="ui_tpicker_timezone_label_' + dp_id + '"' +
  356. ((o.showTimezone) ? '' : noDisplay) + '>' + o.timezoneText + '</dt>';
  357. html += '<dd class="ui_tpicker_timezone" id="ui_tpicker_timezone_' + dp_id + '"' +
  358. ((o.showTimezone) ? '' : noDisplay) + '></dd>';
  359. html += '</dl></div>';
  360. var $tp = $(html);
  361. // if we only want time picker...
  362. if (o.timeOnly === true) {
  363. $tp.prepend(
  364. '<div class="ui-widget-header ui-helper-clearfix ui-corner-all">' +
  365. '<div class="ui-datepicker-title">' + o.timeOnlyTitle + '</div>' +
  366. '</div>');
  367. $dp.find('.ui-datepicker-header, .ui-datepicker-calendar').hide();
  368. }
  369. this.hour_slider = $tp.find('#ui_tpicker_hour_'+ dp_id).slider({
  370. orientation: "horizontal",
  371. value: this.hour,
  372. min: o.hourMin,
  373. max: hourMax,
  374. step: o.stepHour,
  375. slide: function(event, ui) {
  376. tp_inst.hour_slider.slider( "option", "value", ui.value);
  377. tp_inst._onTimeChange();
  378. }
  379. });
  380. // Updated by Peter Medeiros:
  381. // - Pass in Event and UI instance into slide function
  382. this.minute_slider = $tp.find('#ui_tpicker_minute_'+ dp_id).slider({
  383. orientation: "horizontal",
  384. value: this.minute,
  385. min: o.minuteMin,
  386. max: minMax,
  387. step: o.stepMinute,
  388. slide: function(event, ui) {
  389. tp_inst.minute_slider.slider( "option", "value", ui.value);
  390. tp_inst._onTimeChange();
  391. }
  392. });
  393. this.second_slider = $tp.find('#ui_tpicker_second_'+ dp_id).slider({
  394. orientation: "horizontal",
  395. value: this.second,
  396. min: o.secondMin,
  397. max: secMax,
  398. step: o.stepSecond,
  399. slide: function(event, ui) {
  400. tp_inst.second_slider.slider( "option", "value", ui.value);
  401. tp_inst._onTimeChange();
  402. }
  403. });
  404. this.millisec_slider = $tp.find('#ui_tpicker_millisec_'+ dp_id).slider({
  405. orientation: "horizontal",
  406. value: this.millisec,
  407. min: o.millisecMin,
  408. max: millisecMax,
  409. step: o.stepMillisec,
  410. slide: function(event, ui) {
  411. tp_inst.millisec_slider.slider( "option", "value", ui.value);
  412. tp_inst._onTimeChange();
  413. }
  414. });
  415. this.timezone_select = $tp.find('#ui_tpicker_timezone_'+ dp_id).append('<select></select>').find("select");
  416. $.fn.append.apply(this.timezone_select,
  417. $.map(o.timezoneList, function(val, idx) {
  418. return $("<option />")
  419. .val(typeof val == "object" ? val.value : val)
  420. .text(typeof val == "object" ? val.label : val);
  421. })
  422. );
  423. if (typeof(this.timezone) != "undefined" && this.timezone !== null && this.timezone !== "") {
  424. var local_date = new Date(this.inst.selectedYear, this.inst.selectedMonth, this.inst.selectedDay, 12);
  425. var local_timezone = timeZoneString(local_date);
  426. if (local_timezone == this.timezone) {
  427. selectLocalTimeZone(tp_inst);
  428. } else {
  429. this.timezone_select.val(this.timezone);
  430. }
  431. } else {
  432. if (typeof(this.hour) != "undefined" && this.hour !== null && this.hour !== "") {
  433. this.timezone_select.val(o.defaultTimezone);
  434. } else {
  435. selectLocalTimeZone(tp_inst);
  436. }
  437. }
  438. this.timezone_select.change(function() {
  439. tp_inst._defaults.useLocalTimezone = false;
  440. tp_inst._onTimeChange();
  441. });
  442. // Add grid functionality
  443. if (o.showHour && o.hourGrid > 0) {
  444. size = 100 * hourGridSize * o.hourGrid / (hourMax - o.hourMin);
  445. $tp.find(".ui_tpicker_hour table").css({
  446. width: size + "%",
  447. marginLeft: (size / (-2 * hourGridSize)) + "%",
  448. borderCollapse: 'collapse'
  449. }).find("td").each( function(index) {
  450. $(this).click(function() {
  451. var h = $(this).html();
  452. if(o.ampm) {
  453. var ap = h.substring(2).toLowerCase(),
  454. aph = parseInt(h.substring(0,2), 10);
  455. if (ap == 'a') {
  456. if (aph == 12) { h = 0; }
  457. else { h = aph; }
  458. } else if (aph == 12) { h = 12; }
  459. else { h = aph + 12; }
  460. }
  461. tp_inst.hour_slider.slider("option", "value", h);
  462. tp_inst._onTimeChange();
  463. tp_inst._onSelectHandler();
  464. }).css({
  465. cursor: 'pointer',
  466. width: (100 / hourGridSize) + '%',
  467. textAlign: 'center',
  468. overflow: 'hidden'
  469. });
  470. });
  471. }
  472. if (o.showMinute && o.minuteGrid > 0) {
  473. size = 100 * minuteGridSize * o.minuteGrid / (minMax - o.minuteMin);
  474. $tp.find(".ui_tpicker_minute table").css({
  475. width: size + "%",
  476. marginLeft: (size / (-2 * minuteGridSize)) + "%",
  477. borderCollapse: 'collapse'
  478. }).find("td").each(function(index) {
  479. $(this).click(function() {
  480. tp_inst.minute_slider.slider("option", "value", $(this).html());
  481. tp_inst._onTimeChange();
  482. tp_inst._onSelectHandler();
  483. }).css({
  484. cursor: 'pointer',
  485. width: (100 / minuteGridSize) + '%',
  486. textAlign: 'center',
  487. overflow: 'hidden'
  488. });
  489. });
  490. }
  491. if (o.showSecond && o.secondGrid > 0) {
  492. $tp.find(".ui_tpicker_second table").css({
  493. width: size + "%",
  494. marginLeft: (size / (-2 * secondGridSize)) + "%",
  495. borderCollapse: 'collapse'
  496. }).find("td").each(function(index) {
  497. $(this).click(function() {
  498. tp_inst.second_slider.slider("option", "value", $(this).html());
  499. tp_inst._onTimeChange();
  500. tp_inst._onSelectHandler();
  501. }).css({
  502. cursor: 'pointer',
  503. width: (100 / secondGridSize) + '%',
  504. textAlign: 'center',
  505. overflow: 'hidden'
  506. });
  507. });
  508. }
  509. if (o.showMillisec && o.millisecGrid > 0) {
  510. $tp.find(".ui_tpicker_millisec table").css({
  511. width: size + "%",
  512. marginLeft: (size / (-2 * millisecGridSize)) + "%",
  513. borderCollapse: 'collapse'
  514. }).find("td").each(function(index) {
  515. $(this).click(function() {
  516. tp_inst.millisec_slider.slider("option", "value", $(this).html());
  517. tp_inst._onTimeChange();
  518. tp_inst._onSelectHandler();
  519. }).css({
  520. cursor: 'pointer',
  521. width: (100 / millisecGridSize) + '%',
  522. textAlign: 'center',
  523. overflow: 'hidden'
  524. });
  525. });
  526. }
  527. var $buttonPanel = $dp.find('.ui-datepicker-buttonpane');
  528. if ($buttonPanel.length) { $buttonPanel.before($tp); }
  529. else { $dp.append($tp); }
  530. this.$timeObj = $tp.find('#ui_tpicker_time_'+ dp_id);
  531. if (this.inst !== null) {
  532. var timeDefined = this.timeDefined;
  533. this._onTimeChange();
  534. this.timeDefined = timeDefined;
  535. }
  536. //Emulate datepicker onSelect behavior. Call on slidestop.
  537. var onSelectDelegate = function() {
  538. tp_inst._onSelectHandler();
  539. };
  540. this.hour_slider.bind('slidestop',onSelectDelegate);
  541. this.minute_slider.bind('slidestop',onSelectDelegate);
  542. this.second_slider.bind('slidestop',onSelectDelegate);
  543. this.millisec_slider.bind('slidestop',onSelectDelegate);
  544. // slideAccess integration: http://trentrichardson.com/2011/11/11/jquery-ui-sliders-and-touch-accessibility/
  545. if (this._defaults.addSliderAccess){
  546. var sliderAccessArgs = this._defaults.sliderAccessArgs;
  547. setTimeout(function(){ // fix for inline mode
  548. if($tp.find('.ui-slider-access').length === 0){
  549. $tp.find('.ui-slider:visible').sliderAccess(sliderAccessArgs);
  550. // fix any grids since sliders are shorter
  551. var sliderAccessWidth = $tp.find('.ui-slider-access:eq(0)').outerWidth(true);
  552. if(sliderAccessWidth){
  553. $tp.find('table:visible').each(function(){
  554. var $g = $(this),
  555. oldWidth = $g.outerWidth(),
  556. oldMarginLeft = $g.css('marginLeft').toString().replace('%',''),
  557. newWidth = oldWidth - sliderAccessWidth,
  558. newMarginLeft = ((oldMarginLeft * newWidth)/oldWidth) + '%';
  559. $g.css({ width: newWidth, marginLeft: newMarginLeft });
  560. });
  561. }
  562. }
  563. },0);
  564. }
  565. // end slideAccess integration
  566. }
  567. },
  568. //########################################################################
  569. // This function tries to limit the ability to go outside the
  570. // min/max date range
  571. //########################################################################
  572. _limitMinMaxDateTime: function(dp_inst, adjustSliders){
  573. var o = this._defaults,
  574. dp_date = new Date(dp_inst.selectedYear, dp_inst.selectedMonth, dp_inst.selectedDay);
  575. if(!this._defaults.showTimepicker) { return; } // No time so nothing to check here
  576. if($.datepicker._get(dp_inst, 'minDateTime') !== null && $.datepicker._get(dp_inst, 'minDateTime') !== undefined && dp_date){
  577. var minDateTime = $.datepicker._get(dp_inst, 'minDateTime'),
  578. minDateTimeDate = new Date(minDateTime.getFullYear(), minDateTime.getMonth(), minDateTime.getDate(), 0, 0, 0, 0);
  579. if(this.hourMinOriginal === null || this.minuteMinOriginal === null || this.secondMinOriginal === null || this.millisecMinOriginal === null){
  580. this.hourMinOriginal = o.hourMin;
  581. this.minuteMinOriginal = o.minuteMin;
  582. this.secondMinOriginal = o.secondMin;
  583. this.millisecMinOriginal = o.millisecMin;
  584. }
  585. if(dp_inst.settings.timeOnly || minDateTimeDate.getTime() == dp_date.getTime()) {
  586. this._defaults.hourMin = minDateTime.getHours();
  587. if (this.hour <= this._defaults.hourMin) {
  588. this.hour = this._defaults.hourMin;
  589. this._defaults.minuteMin = minDateTime.getMinutes();
  590. if (this.minute <= this._defaults.minuteMin) {
  591. this.minute = this._defaults.minuteMin;
  592. this._defaults.secondMin = minDateTime.getSeconds();
  593. } else if (this.second <= this._defaults.secondMin){
  594. this.second = this._defaults.secondMin;
  595. this._defaults.millisecMin = minDateTime.getMilliseconds();
  596. } else {
  597. if(this.millisec < this._defaults.millisecMin) {
  598. this.millisec = this._defaults.millisecMin;
  599. }
  600. this._defaults.millisecMin = this.millisecMinOriginal;
  601. }
  602. } else {
  603. this._defaults.minuteMin = this.minuteMinOriginal;
  604. this._defaults.secondMin = this.secondMinOriginal;
  605. this._defaults.millisecMin = this.millisecMinOriginal;
  606. }
  607. }else{
  608. this._defaults.hourMin = this.hourMinOriginal;
  609. this._defaults.minuteMin = this.minuteMinOriginal;
  610. this._defaults.secondMin = this.secondMinOriginal;
  611. this._defaults.millisecMin = this.millisecMinOriginal;
  612. }
  613. }
  614. if($.datepicker._get(dp_inst, 'maxDateTime') !== null && $.datepicker._get(dp_inst, 'maxDateTime') !== undefined && dp_date){
  615. var maxDateTime = $.datepicker._get(dp_inst, 'maxDateTime'),
  616. maxDateTimeDate = new Date(maxDateTime.getFullYear(), maxDateTime.getMonth(), maxDateTime.getDate(), 0, 0, 0, 0);
  617. if(this.hourMaxOriginal === null || this.minuteMaxOriginal === null || this.secondMaxOriginal === null){
  618. this.hourMaxOriginal = o.hourMax;
  619. this.minuteMaxOriginal = o.minuteMax;
  620. this.secondMaxOriginal = o.secondMax;
  621. this.millisecMaxOriginal = o.millisecMax;
  622. }
  623. if(dp_inst.settings.timeOnly || maxDateTimeDate.getTime() == dp_date.getTime()){
  624. this._defaults.hourMax = maxDateTime.getHours();
  625. if (this.hour >= this._defaults.hourMax) {
  626. this.hour = this._defaults.hourMax;
  627. this._defaults.minuteMax = maxDateTime.getMinutes();
  628. if (this.minute >= this._defaults.minuteMax) {
  629. this.minute = this._defaults.minuteMax;
  630. this._defaults.secondMax = maxDateTime.getSeconds();
  631. } else if (this.second >= this._defaults.secondMax) {
  632. this.second = this._defaults.secondMax;
  633. this._defaults.millisecMax = maxDateTime.getMilliseconds();
  634. } else {
  635. if(this.millisec > this._defaults.millisecMax) { this.millisec = this._defaults.millisecMax; }
  636. this._defaults.millisecMax = this.millisecMaxOriginal;
  637. }
  638. } else {
  639. this._defaults.minuteMax = this.minuteMaxOriginal;
  640. this._defaults.secondMax = this.secondMaxOriginal;
  641. this._defaults.millisecMax = this.millisecMaxOriginal;
  642. }
  643. }else{
  644. this._defaults.hourMax = this.hourMaxOriginal;
  645. this._defaults.minuteMax = this.minuteMaxOriginal;
  646. this._defaults.secondMax = this.secondMaxOriginal;
  647. this._defaults.millisecMax = this.millisecMaxOriginal;
  648. }
  649. }
  650. if(adjustSliders !== undefined && adjustSliders === true){
  651. var hourMax = parseInt((this._defaults.hourMax - ((this._defaults.hourMax - this._defaults.hourMin) % this._defaults.stepHour)) ,10),
  652. minMax = parseInt((this._defaults.minuteMax - ((this._defaults.minuteMax - this._defaults.minuteMin) % this._defaults.stepMinute)) ,10),
  653. secMax = parseInt((this._defaults.secondMax - ((this._defaults.secondMax - this._defaults.secondMin) % this._defaults.stepSecond)) ,10),
  654. millisecMax = parseInt((this._defaults.millisecMax - ((this._defaults.millisecMax - this._defaults.millisecMin) % this._defaults.stepMillisec)) ,10);
  655. if(this.hour_slider) {
  656. this.hour_slider.slider("option", { min: this._defaults.hourMin, max: hourMax }).slider('value', this.hour);
  657. }
  658. if(this.minute_slider) {
  659. this.minute_slider.slider("option", { min: this._defaults.minuteMin, max: minMax }).slider('value', this.minute);
  660. }
  661. if(this.second_slider){
  662. this.second_slider.slider("option", { min: this._defaults.secondMin, max: secMax }).slider('value', this.second);
  663. }
  664. if(this.millisec_slider) {
  665. this.millisec_slider.slider("option", { min: this._defaults.millisecMin, max: millisecMax }).slider('value', this.millisec);
  666. }
  667. }
  668. },
  669. //########################################################################
  670. // when a slider moves, set the internal time...
  671. // on time change is also called when the time is updated in the text field
  672. //########################################################################
  673. _onTimeChange: function() {
  674. var hour = (this.hour_slider) ? this.hour_slider.slider('value') : false,
  675. minute = (this.minute_slider) ? this.minute_slider.slider('value') : false,
  676. second = (this.second_slider) ? this.second_slider.slider('value') : false,
  677. millisec = (this.millisec_slider) ? this.millisec_slider.slider('value') : false,
  678. timezone = (this.timezone_select) ? this.timezone_select.val() : false,
  679. o = this._defaults;
  680. if (typeof(hour) == 'object') { hour = false; }
  681. if (typeof(minute) == 'object') { minute = false; }
  682. if (typeof(second) == 'object') { second = false; }
  683. if (typeof(millisec) == 'object') { millisec = false; }
  684. if (typeof(timezone) == 'object') { timezone = false; }
  685. if (hour !== false) { hour = parseInt(hour,10); }
  686. if (minute !== false) { minute = parseInt(minute,10); }
  687. if (second !== false) { second = parseInt(second,10); }
  688. if (millisec !== false) { millisec = parseInt(millisec,10); }
  689. var ampm = o[hour < 12 ? 'amNames' : 'pmNames'][0];
  690. // If the update was done in the input field, the input field should not be updated.
  691. // If the update was done using the sliders, update the input field.
  692. var hasChanged = (hour != this.hour || minute != this.minute ||
  693. second != this.second || millisec != this.millisec ||
  694. (this.ampm.length > 0 &&
  695. (hour < 12) != ($.inArray(this.ampm.toUpperCase(), this.amNames) !== -1)) ||
  696. timezone != this.timezone);
  697. if (hasChanged) {
  698. if (hour !== false) { this.hour = hour; }
  699. if (minute !== false) { this.minute = minute; }
  700. if (second !== false) { this.second = second; }
  701. if (millisec !== false) { this.millisec = millisec; }
  702. if (timezone !== false) { this.timezone = timezone; }
  703. if (!this.inst) { this.inst = $.datepicker._getInst(this.$input[0]); }
  704. this._limitMinMaxDateTime(this.inst, true);
  705. }
  706. if (o.ampm) { this.ampm = ampm; }
  707. //this._formatTime();
  708. this.formattedTime = $.datepicker.formatTime(this._defaults.timeFormat, this, this._defaults);
  709. if (this.$timeObj) { this.$timeObj.text(this.formattedTime + o.timeSuffix); }
  710. this.timeDefined = true;
  711. if (hasChanged) { this._updateDateTime(); }
  712. },
  713. //########################################################################
  714. // call custom onSelect.
  715. // bind to sliders slidestop, and grid click.
  716. //########################################################################
  717. _onSelectHandler: function() {
  718. var onSelect = this._defaults.onSelect;
  719. var inputEl = this.$input ? this.$input[0] : null;
  720. if (onSelect && inputEl) {
  721. onSelect.apply(inputEl, [this.formattedDateTime, this]);
  722. }
  723. },
  724. //########################################################################
  725. // left for any backwards compatibility
  726. //########################################################################
  727. _formatTime: function(time, format) {
  728. time = time || { hour: this.hour, minute: this.minute, second: this.second, millisec: this.millisec, ampm: this.ampm, timezone: this.timezone };
  729. var tmptime = (format || this._defaults.timeFormat).toString();
  730. tmptime = $.datepicker.formatTime(tmptime, time, this._defaults);
  731. if (arguments.length) { return tmptime; }
  732. else { this.formattedTime = tmptime; }
  733. },
  734. //########################################################################
  735. // update our input with the new date time..
  736. //########################################################################
  737. _updateDateTime: function(dp_inst) {
  738. dp_inst = this.inst || dp_inst;
  739. var dt = $.datepicker._daylightSavingAdjust(new Date(dp_inst.selectedYear, dp_inst.selectedMonth, dp_inst.selectedDay)),
  740. dateFmt = $.datepicker._get(dp_inst, 'dateFormat'),
  741. formatCfg = $.datepicker._getFormatConfig(dp_inst),
  742. timeAvailable = dt !== null && this.timeDefined;
  743. this.formattedDate = $.datepicker.formatDate(dateFmt, (dt === null ? new Date() : dt), formatCfg);
  744. var formattedDateTime = this.formattedDate;
  745. // remove following lines to force every changes in date picker to change the input value
  746. // Bug descriptions: when an input field has a default value, and click on the field to pop up the date picker.
  747. // If the user manually empty the value in the input field, the date picker will never change selected value.
  748. //if (dp_inst.lastVal !== undefined && (dp_inst.lastVal.length > 0 && this.$input.val().length === 0)) {
  749. // return;
  750. //}
  751. if (this._defaults.timeOnly === true) {
  752. formattedDateTime = this.formattedTime;
  753. } else if (this._defaults.timeOnly !== true && (this._defaults.alwaysSetTime || timeAvailable)) {
  754. formattedDateTime += this._defaults.separator + this.formattedTime + this._defaults.timeSuffix;
  755. }
  756. this.formattedDateTime = formattedDateTime;
  757. if(!this._defaults.showTimepicker) {
  758. this.$input.val(this.formattedDate);
  759. } else if (this.$altInput && this._defaults.altFieldTimeOnly === true) {
  760. this.$altInput.val(this.formattedTime);
  761. this.$input.val(this.formattedDate);
  762. } else if(this.$altInput) {
  763. this.$altInput.val(formattedDateTime);
  764. this.$input.val(formattedDateTime);
  765. } else {
  766. this.$input.val(formattedDateTime);
  767. }
  768. this.$input.trigger("change");
  769. }
  770. });
  771. $.fn.extend({
  772. //########################################################################
  773. // shorthand just to use timepicker..
  774. //########################################################################
  775. timepicker: function(o) {
  776. o = o || {};
  777. var tmp_args = arguments;
  778. if (typeof o == 'object') { tmp_args[0] = $.extend(o, { timeOnly: true }); }
  779. return $(this).each(function() {
  780. $.fn.datetimepicker.apply($(this), tmp_args);
  781. });
  782. },
  783. //########################################################################
  784. // extend timepicker to datepicker
  785. //########################################################################
  786. datetimepicker: function(o) {
  787. o = o || {};
  788. var tmp_args = arguments;
  789. if (typeof(o) == 'string'){
  790. if(o == 'getDate') {
  791. return $.fn.datepicker.apply($(this[0]), tmp_args);
  792. }
  793. else {
  794. return this.each(function() {
  795. var $t = $(this);
  796. $t.datepicker.apply($t, tmp_args);
  797. });
  798. }
  799. }
  800. else {
  801. return this.each(function() {
  802. var $t = $(this);
  803. $t.datepicker($.timepicker._newInst($t, o)._defaults);
  804. });
  805. }
  806. }
  807. });
  808. $.datepicker.parseDateTime = function(dateFormat, timeFormat, dateTimeString, dateSettings, timeSettings) {
  809. var parseRes = parseDateTimeInternal(dateFormat, timeFormat, dateTimeString, dateSettings, timeSettings);
  810. if (parseRes.timeObj)
  811. {
  812. var t = parseRes.timeObj;
  813. parseRes.date.setHours(t.hour, t.minute, t.second, t.millisec);
  814. }
  815. return parseRes.date;
  816. };
  817. $.datepicker.parseTime = function(timeFormat, timeString, options) {
  818. //########################################################################
  819. // pattern for standard and localized AM/PM markers
  820. //########################################################################
  821. var getPatternAmpm = function(amNames, pmNames) {
  822. var markers = [];
  823. if (amNames) {
  824. $.merge(markers, amNames);
  825. }
  826. if (pmNames) {
  827. $.merge(markers, pmNames);
  828. }
  829. markers = $.map(markers, function(val) { return val.replace(/[.*+?|()\[\]{}\\]/g, '\\$&'); });
  830. return '(' + markers.join('|') + ')?';
  831. };
  832. //########################################################################
  833. // figure out position of time elements.. cause js cant do named captures
  834. //########################################################################
  835. var getFormatPositions = function( timeFormat ) {
  836. var finds = timeFormat.toLowerCase().match(/(h{1,2}|m{1,2}|s{1,2}|l{1}|t{1,2}|z)/g),
  837. orders = { h: -1, m: -1, s: -1, l: -1, t: -1, z: -1 };
  838. if (finds) {
  839. for (var i = 0; i < finds.length; i++) {
  840. if (orders[finds[i].toString().charAt(0)] == -1) {
  841. orders[finds[i].toString().charAt(0)] = i + 1;
  842. }
  843. }
  844. }
  845. return orders;
  846. };
  847. var o = extendRemove(extendRemove({}, $.timepicker._defaults), options || {});
  848. var regstr = '^' + timeFormat.toString()
  849. .replace(/h{1,2}/ig, '(\\d?\\d)')
  850. .replace(/m{1,2}/ig, '(\\d?\\d)')
  851. .replace(/s{1,2}/ig, '(\\d?\\d)')
  852. .replace(/l{1}/ig, '(\\d?\\d?\\d)')
  853. .replace(/t{1,2}/ig, getPatternAmpm(o.amNames, o.pmNames))
  854. .replace(/z{1}/ig, '(z|[-+]\\d\\d:?\\d\\d)?')
  855. .replace(/\s/g, '\\s?') + o.timeSuffix + '$',
  856. order = getFormatPositions(timeFormat),
  857. ampm = '',
  858. treg;
  859. treg = timeString.match(new RegExp(regstr, 'i'));
  860. var resTime = {hour: 0, minute: 0, second: 0, millisec: 0};
  861. if (treg) {
  862. if (order.t !== -1) {
  863. if (treg[order.t] === undefined || treg[order.t].length === 0) {
  864. ampm = '';
  865. resTime.ampm = '';
  866. } else {
  867. ampm = $.inArray(treg[order.t], o.amNames) !== -1 ? 'AM' : 'PM';
  868. resTime.ampm = o[ampm == 'AM' ? 'amNames' : 'pmNames'][0];
  869. }
  870. }
  871. if (order.h !== -1) {
  872. if (ampm == 'AM' && treg[order.h] == '12') {
  873. resTime.hour = 0; // 12am = 0 hour
  874. } else {
  875. if (ampm == 'PM' && treg[order.h] != '12') {
  876. resTime.hour = parseInt(treg[order.h],10) + 12; // 12pm = 12 hour, any other pm = hour + 12
  877. }
  878. else { resTime.hour = Number(treg[order.h]); }
  879. }
  880. }
  881. if (order.m !== -1) { resTime.minute = Number(treg[order.m]); }
  882. if (order.s !== -1) { resTime.second = Number(treg[order.s]); }
  883. if (order.l !== -1) { resTime.millisec = Number(treg[order.l]); }
  884. if (order.z !== -1 && treg[order.z] !== undefined) {
  885. var tz = treg[order.z].toUpperCase();
  886. switch (tz.length) {
  887. case 1: // Z
  888. tz = o.timezoneIso8601 ? 'Z' : '+0000';
  889. break;
  890. case 5: // +hhmm
  891. if (o.timezoneIso8601) {
  892. tz = tz.substring(1) == '0000' ?
  893. 'Z' :
  894. tz.substring(0, 3) + ':' + tz.substring(3);
  895. }
  896. break;
  897. case 6: // +hh:mm
  898. if (!o.timezoneIso8601) {
  899. tz = tz == 'Z' || tz.substring(1) == '00:00' ?
  900. '+0000' :
  901. tz.replace(/:/, '');
  902. } else {
  903. if (tz.substring(1) == '00:00') {
  904. tz = 'Z';
  905. }
  906. }
  907. break;
  908. }
  909. resTime.timezone = tz;
  910. }
  911. return resTime;
  912. }
  913. return false;
  914. };
  915. //########################################################################
  916. // format the time all pretty...
  917. // format = string format of the time
  918. // time = a {}, not a Date() for timezones
  919. // options = essentially the regional[].. amNames, pmNames, ampm
  920. //########################################################################
  921. $.datepicker.formatTime = function(format, time, options) {
  922. options = options || {};
  923. options = $.extend($.timepicker._defaults, options);
  924. time = $.extend({hour:0, minute:0, second:0, millisec:0, timezone:'+0000'}, time);
  925. var tmptime = format;
  926. var ampmName = options.amNames[0];
  927. var hour = parseInt(time.hour, 10);
  928. if (options.ampm) {
  929. if (hour > 11){
  930. ampmName = options.pmNames[0];
  931. if(hour > 12) {
  932. hour = hour % 12;
  933. }
  934. }
  935. if (hour === 0) {
  936. hour = 12;
  937. }
  938. }
  939. tmptime = tmptime.replace(/(?:hh?|mm?|ss?|[tT]{1,2}|[lz])/g, function(match) {
  940. switch (match.toLowerCase()) {
  941. case 'hh': return ('0' + hour).slice(-2);
  942. case 'h': return hour;
  943. case 'mm': return ('0' + time.minute).slice(-2);
  944. case 'm': return time.minute;
  945. case 'ss': return ('0' + time.second).slice(-2);
  946. case 's': return time.second;
  947. case 'l': return ('00' + time.millisec).slice(-3);
  948. case 'z': return time.timezone;
  949. case 't': case 'tt':
  950. if (options.ampm) {
  951. if (match.length == 1) {
  952. ampmName = ampmName.charAt(0);
  953. }
  954. return match.charAt(0) == 'T' ? ampmName.toUpperCase() : ampmName.toLowerCase();
  955. }
  956. return '';
  957. }
  958. });
  959. tmptime = $.trim(tmptime);
  960. return tmptime;
  961. };
  962. //########################################################################
  963. // the bad hack :/ override datepicker so it doesnt close on select
  964. // inspired: http://stackoverflow.com/questions/1252512/jquery-datepicker-prevent-closing-picker-when-clicking-a-date/1762378#1762378
  965. //########################################################################
  966. $.datepicker._base_selectDate = $.datepicker._selectDate;
  967. $.datepicker._selectDate = function (id, dateStr) {
  968. var inst = this._getInst($(id)[0]),
  969. tp_inst = this._get(inst, 'timepicker');
  970. if (tp_inst) {
  971. tp_inst._limitMinMaxDateTime(inst, true);
  972. inst.inline = inst.stay_open = true;
  973. //This way the onSelect handler called from calendarpicker get the full dateTime
  974. this._base_selectDate(id, dateStr);
  975. inst.inline = inst.stay_open = false;
  976. this._notifyChange(inst);
  977. this._updateDatepicker(inst);
  978. }
  979. else { this._base_selectDate(id, dateStr); }
  980. };
  981. //#############################################################################################
  982. // second bad hack :/ override datepicker so it triggers an event when changing the input field
  983. // and does not redraw the datepicker on every selectDate event
  984. //#############################################################################################
  985. $.datepicker._base_updateDatepicker = $.datepicker._updateDatepicker;
  986. $.datepicker._updateDatepicker = function(inst) {
  987. // don't popup the datepicker if there is another instance already opened
  988. var input = inst.input[0];
  989. if($.datepicker._curInst &&
  990. $.datepicker._curInst != inst &&
  991. $.datepicker._datepickerShowing &&
  992. $.datepicker._lastInput != input) {
  993. return;
  994. }
  995. if (typeof(inst.stay_open) !== 'boolean' || inst.stay_open === false) {
  996. this._base_updateDatepicker(inst);
  997. // Reload the time control when changing something in the input text field.
  998. var tp_inst = this._get(inst, 'timepicker');
  999. if(tp_inst) {
  1000. tp_inst._addTimePicker(inst);
  1001. if (tp_inst._defaults.useLocalTimezone) { //checks daylight saving with the new date.
  1002. var date = new Date(inst.selectedYear, inst.selectedMonth, inst.selectedDay, 12);
  1003. selectLocalTimeZone(tp_inst, date);
  1004. tp_inst._onTimeChange();
  1005. }
  1006. }
  1007. }
  1008. };
  1009. //#######################################################################################
  1010. // third bad hack :/ override datepicker so it allows spaces and colon in the input field
  1011. //#######################################################################################
  1012. $.datepicker._base_doKeyPress = $.datepicker._doKeyPress;
  1013. $.datepicker._doKeyPress = function(event) {
  1014. var inst = $.datepicker._getInst(event.target),
  1015. tp_inst = $.datepicker._get(inst, 'timepicker');
  1016. if (tp_inst) {
  1017. if ($.datepicker._get(inst, 'constrainInput')) {
  1018. var ampm = tp_inst._defaults.ampm,
  1019. dateChars = $.datepicker._possibleChars($.datepicker._get(inst, 'dateFormat')),
  1020. datetimeChars = tp_inst._defaults.timeFormat.toString()
  1021. .replace(/[hms]/g, '')
  1022. .replace(/TT/g, ampm ? 'APM' : '')
  1023. .replace(/Tt/g, ampm ? 'AaPpMm' : '')
  1024. .replace(/tT/g, ampm ? 'AaPpMm' : '')
  1025. .replace(/T/g, ampm ? 'AP' : '')
  1026. .replace(/tt/g, ampm ? 'apm' : '')
  1027. .replace(/t/g, ampm ? 'ap' : '') +
  1028. " " +
  1029. tp_inst._defaults.separator +
  1030. tp_inst._defaults.timeSuffix +
  1031. (tp_inst._defaults.showTimezone ? tp_inst._defaults.timezoneList.join('') : '') +
  1032. (tp_inst._defaults.amNames.join('')) +
  1033. (tp_inst._defaults.pmNames.join('')) +
  1034. dateChars,
  1035. chr = String.fromCharCode(event.charCode === undefined ? event.keyCode : event.charCode);
  1036. return event.ctrlKey || (chr < ' ' || !dateChars || datetimeChars.indexOf(chr) > -1);
  1037. }
  1038. }
  1039. return $.datepicker._base_doKeyPress(event);
  1040. };
  1041. //#######################################################################################
  1042. // Override key up event to sync manual input changes.
  1043. //#######################################################################################
  1044. $.datepicker._base_doKeyUp = $.datepicker._doKeyUp;
  1045. $.datepicker._doKeyUp = function (event) {
  1046. var inst = $.datepicker._getInst(event.target),
  1047. tp_inst = $.datepicker._get(inst, 'timepicker');
  1048. if (tp_inst) {
  1049. if (tp_inst._defaults.timeOnly && (inst.input.val() != inst.lastVal)) {
  1050. try {
  1051. $.datepicker._updateDatepicker(inst);
  1052. }
  1053. catch (err) {
  1054. $.datepicker.log(err);
  1055. }
  1056. }
  1057. }
  1058. return $.datepicker._base_doKeyUp(event);
  1059. };
  1060. //#######################################################################################
  1061. // override "Today" button to also grab the time.
  1062. //#######################################################################################
  1063. $.datepicker._base_gotoToday = $.datepicker._gotoToday;
  1064. $.datepicker._gotoToday = function(id) {
  1065. var inst = this._getInst($(id)[0]),
  1066. $dp = inst.dpDiv;
  1067. this._base_gotoToday(id);
  1068. var tp_inst = this._get(inst, 'timepicker');
  1069. selectLocalTimeZone(tp_inst);
  1070. var now = new Date();
  1071. this._setTime(inst, now);
  1072. $( '.ui-datepicker-today', $dp).click();
  1073. };
  1074. //#######################################################################################
  1075. // Disable & enable the Time in the datetimepicker
  1076. //#######################################################################################
  1077. $.datepicker._disableTimepickerDatepicker = function(target) {
  1078. var inst = this._getInst(target);
  1079. if (!inst) { return; }
  1080. var tp_inst = this._get(inst, 'timepicker');
  1081. $(target).datepicker('getDate'); // Init selected[Year|Month|Day]
  1082. if (tp_inst) {
  1083. tp_inst._defaults.showTimepicker = false;
  1084. tp_inst._updateDateTime(inst);
  1085. }
  1086. };
  1087. $.datepicker._enableTimepickerDatepicker = function(target) {
  1088. var inst = this._getInst(target);
  1089. if (!inst) { return; }
  1090. var tp_inst = this._get(inst, 'timepicker');
  1091. $(target).datepicker('getDate'); // Init selected[Year|Month|Day]
  1092. if (tp_inst) {
  1093. tp_inst._defaults.showTimepicker = true;
  1094. tp_inst._addTimePicker(inst); // Could be disabled on page load
  1095. tp_inst._updateDateTime(inst);
  1096. }
  1097. };
  1098. //#######################################################################################
  1099. // Create our own set time function
  1100. //#######################################################################################
  1101. $.datepicker._setTime = function(inst, date) {
  1102. var tp_inst = this._get(inst, 'timepicker');
  1103. if (tp_inst) {
  1104. var defaults = tp_inst._defaults,
  1105. // calling _setTime with no date sets time to defaults
  1106. hour = date ? date.getHours() : defaults.hour,
  1107. minute = date ? date.getMinutes() : defaults.minute,
  1108. second = date ? date.getSeconds() : defaults.second,
  1109. millisec = date ? date.getMilliseconds() : defaults.millisec;
  1110. //check if within min/max times..
  1111. // correct check if within min/max times.
  1112. // Rewritten by Scott A. Woodward
  1113. var hourEq = hour === defaults.hourMin,
  1114. minuteEq = minute === defaults.minuteMin,
  1115. secondEq = second === defaults.secondMin;
  1116. var reset = false;
  1117. if(hour < defaults.hourMin || hour > defaults.hourMax)
  1118. reset = true;
  1119. else if( (minute < defaults.minuteMin || minute > defaults.minuteMax) && hourEq)
  1120. reset = true;
  1121. else if( (second < defaults.secondMin || second > defaults.secondMax ) && hourEq && minuteEq)
  1122. reset = true;
  1123. else if( (millisec < defaults.millisecMin || millisec > defaults.millisecMax) && hourEq && minuteEq && secondEq)
  1124. reset = true;
  1125. if(reset) {
  1126. hour = defaults.hourMin;
  1127. minute = defaults.minuteMin;
  1128. second = defaults.secondMin;
  1129. millisec = defaults.millisecMin;
  1130. }
  1131. tp_inst.hour = hour;
  1132. tp_inst.minute = minute;
  1133. tp_inst.second = second;
  1134. tp_inst.millisec = millisec;
  1135. if (tp_inst.hour_slider) tp_inst.hour_slider.slider('value', hour);
  1136. if (tp_inst.minute_slider) tp_inst.minute_slider.slider('value', minute);
  1137. if (tp_inst.second_slider) tp_inst.second_slider.slider('value', second);
  1138. if (tp_inst.millisec_slider) tp_inst.millisec_slider.slider('value', millisec);
  1139. tp_inst._onTimeChange();
  1140. tp_inst._updateDateTime(inst);
  1141. }
  1142. };
  1143. //#######################################################################################
  1144. // Create new public method to set only time, callable as $().datepicker('setTime', date)
  1145. //#######################################################################################
  1146. $.datepicker._setTimeDatepicker = function(target, date, withDate) {
  1147. var inst = this._getInst(target);
  1148. if (!inst) { return; }
  1149. var tp_inst = this._get(inst, 'timepicker');
  1150. if (tp_inst) {
  1151. this._setDateFromField(inst);
  1152. var tp_date;
  1153. if (date) {
  1154. if (typeof date == "string") {
  1155. tp_inst._parseTime(date, withDate);
  1156. tp_date = new Date();
  1157. tp_date.setHours(tp_inst.hour, tp_inst.minute, tp_inst.second, tp_inst.millisec);
  1158. }
  1159. else { tp_date = new Date(date.getTime()); }
  1160. if (tp_date.toString() == 'Invalid Date') { tp_date = undefined; }
  1161. this._setTime(inst, tp_date);
  1162. }
  1163. }
  1164. };
  1165. //#######################################################################################
  1166. // override setDate() to allow setting time too within Date object
  1167. //#######################################################################################
  1168. $.datepicker._base_setDateDatepicker = $.datepicker._setDateDatepicker;
  1169. $.datepicker._setDateDatepicker = function(target, date) {
  1170. var inst = this._getInst(target);
  1171. if (!inst) { return; }
  1172. var tp_date = (date instanceof Date) ? new Date(date.getTime()) : date;
  1173. this._updateDatepicker(inst);
  1174. this._base_setDateDatepicker.apply(this, arguments);
  1175. this._setTimeDatepicker(target, tp_date, true);
  1176. };
  1177. //#######################################################################################
  1178. // override getDate() to allow getting time too within Date object
  1179. //#######################################################################################
  1180. $.datepicker._base_getDateDatepicker = $.datepicker._getDateDatepicker;
  1181. $.datepicker._getDateDatepicker = function(target, noDefault) {
  1182. var inst = this._getInst(target);
  1183. if (!inst) { return; }
  1184. var tp_inst = this._get(inst, 'timepicker');
  1185. if (tp_inst) {
  1186. this._setDateFromField(inst, noDefault);
  1187. var date = this._getDate(inst);
  1188. if (date && tp_inst._parseTime($(target).val(), tp_inst.timeOnly)) { date.setHours(tp_inst.hour, tp_inst.minute, tp_inst.second, tp_inst.millisec); }
  1189. return date;
  1190. }
  1191. return this._base_getDateDatepicker(target, noDefault);
  1192. };
  1193. //#######################################################################################
  1194. // override parseDate() because UI 1.8.14 throws an error about "Extra characters"
  1195. // An option in datapicker to ignore extra format characters would be nicer.
  1196. //#######################################################################################
  1197. $.datepicker._base_parseDate = $.datepicker.parseDate;
  1198. $.datepicker.parseDate = function(format, value, settings) {
  1199. var splitRes = splitDateTime(format, value, settings);
  1200. return $.datepicker._base_parseDate(format, splitRes[0], settings);
  1201. };
  1202. //#######################################################################################
  1203. // override formatDate to set date with time to the input
  1204. //#######################################################################################
  1205. $.datepicker._base_formatDate = $.datepicker._formatDate;
  1206. $.datepicker._formatDate = function(inst, day, month, year){
  1207. var tp_inst = this._get(inst, 'timepicker');
  1208. if(tp_inst) {
  1209. tp_inst._updateDateTime(inst);
  1210. return tp_inst.$input.val();
  1211. }
  1212. return this._base_formatDate(inst);
  1213. };
  1214. //#######################################################################################
  1215. // override options setter to add time to maxDate(Time) and minDate(Time). MaxDate
  1216. //#######################################################################################
  1217. $.datepicker._base_optionDatepicker = $.datepicker._optionDatepicker;
  1218. $.datepicker._optionDatepicker = function(target, name, value) {
  1219. var inst = this._getInst(target);
  1220. if (!inst) { return null; }
  1221. var tp_inst = this._get(inst, 'timepicker');
  1222. if (tp_inst) {
  1223. var min = null, max = null, onselect = null;
  1224. if (typeof name == 'string') { // if min/max was set with the string
  1225. if (name === 'minDate' || name === 'minDateTime' ) {
  1226. min = value;
  1227. }
  1228. else {
  1229. if (name === 'maxDate' || name === 'maxDateTime') {
  1230. max = value;
  1231. }
  1232. else {
  1233. if (name === 'onSelect') {
  1234. onselect = value;
  1235. }
  1236. }
  1237. }
  1238. } else {
  1239. if (typeof name == 'object') { //if min/max was set with the JSON
  1240. if (name.minDate) {
  1241. min = name.minDate;
  1242. } else {
  1243. if (name.minDateTime) {
  1244. min = name.minDateTime;
  1245. } else {
  1246. if (name.maxDate) {
  1247. max = name.maxDate;
  1248. } else {
  1249. if (name.maxDateTime) {
  1250. max = name.maxDateTime;
  1251. }
  1252. }
  1253. }
  1254. }
  1255. }
  1256. }
  1257. if(min) { //if min was set
  1258. if (min === 0) {
  1259. min = new Date();
  1260. } else {
  1261. min = new Date(min);
  1262. }
  1263. tp_inst._defaults.minDate = min;
  1264. tp_inst._defaults.minDateTime = min;
  1265. } else if (max) { //if max was set
  1266. if(max===0) {
  1267. max=new Date();
  1268. } else {
  1269. max= new Date(max);
  1270. }
  1271. tp_inst._defaults.maxDate = max;
  1272. tp_inst._defaults.maxDateTime = max;
  1273. } else if (onselect) {
  1274. tp_inst._defaults.onSelect = onselect;
  1275. }
  1276. }
  1277. if (value === undefined) {
  1278. return this._base_optionDatepicker(target, name);
  1279. }
  1280. return this._base_optionDatepicker(target, name, value);
  1281. };
  1282. //#######################################################################################
  1283. // jQuery extend now ignores nulls!
  1284. //#######################################################################################
  1285. function extendRemove(target, props) {
  1286. $.extend(target, props);
  1287. for (var name in props) {
  1288. if (props[name] === null || props[name] === undefined) {
  1289. target[name] = props[name];
  1290. }
  1291. }
  1292. return target;
  1293. }
  1294. //#######################################################################################
  1295. // Splits datetime string into date ans time substrings.
  1296. // Throws exception when date can't be parsed
  1297. // If only date is present, time substring eill be ''
  1298. //#######################################################################################
  1299. var splitDateTime = function(dateFormat, dateTimeString, dateSettings)
  1300. {
  1301. try {
  1302. var date = $.datepicker._base_parseDate(dateFormat, dateTimeString, dateSettings);
  1303. } catch (err) {
  1304. if (err.indexOf(":") >= 0) {
  1305. // Hack! The error message ends with a colon, a space, and
  1306. // the "extra" characters. We rely on that instead of
  1307. // attempting to perfectly reproduce the parsing algorithm.
  1308. var dateStringLength = dateTimeString.length-(err.length-err.indexOf(':')-2);
  1309. var timeString = dateTimeString.substring(dateStringLength);
  1310. return [dateTimeString.substring(0, dateStringLength), dateTimeString.substring(dateStringLength)];
  1311. } else {
  1312. throw err;
  1313. }
  1314. }
  1315. return [dateTimeString, ''];
  1316. };
  1317. //#######################################################################################
  1318. // Internal function to parse datetime interval
  1319. // Returns: {date: Date, timeObj: Object}, where
  1320. // date - parsed date without time (type Date)
  1321. // timeObj = {hour: , minute: , second: , millisec: } - parsed time. Optional
  1322. //#######################################################################################
  1323. var parseDateTimeInternal = function(dateFormat, timeFormat, dateTimeString, dateSettings, timeSettings)
  1324. {
  1325. var date;
  1326. var splitRes = splitDateTime(dateFormat, dateTimeString, dateSettings);
  1327. date = $.datepicker._base_parseDate(dateFormat, splitRes[0], dateSettings);
  1328. if (splitRes[1] !== '')
  1329. {
  1330. var timeString = splitRes[1];
  1331. var separator = timeSettings && timeSettings.separator ? timeSettings.separator : $.timepicker._defaults.separator;
  1332. if ( timeString.indexOf(separator) !== 0) {
  1333. throw 'Missing time separator';
  1334. }
  1335. timeString = timeString.substring(separator.length);
  1336. var parsedTime = $.datepicker.parseTime(timeFormat, timeString, timeSettings);
  1337. if (parsedTime === null) {
  1338. throw 'Wrong time format';
  1339. }
  1340. return {date: date, timeObj: parsedTime};
  1341. } else {
  1342. return {date: date};
  1343. }
  1344. };
  1345. //#######################################################################################
  1346. // Internal function to set timezone_select to the local timezone
  1347. //#######################################################################################
  1348. var selectLocalTimeZone = function(tp_inst, date)
  1349. {
  1350. if (tp_inst && tp_inst.timezone_select) {
  1351. tp_inst._defaults.useLocalTimezone = true;
  1352. var now = typeof date !== 'undefined' ? date : new Date();
  1353. var tzoffset = timeZoneString(now);
  1354. if (tp_inst._defaults.timezoneIso8601) {
  1355. tzoffset = tzoffset.substring(0, 3) + ':' + tzoffset.substring(3);
  1356. }
  1357. tp_inst.timezone_select.val(tzoffset);
  1358. }
  1359. };
  1360. // Input: Date Object
  1361. // Output: String with timezone offset, e.g. '+0100'
  1362. var timeZoneString = function(date)
  1363. {
  1364. var off = date.getTimezoneOffset() * -10100 / 60;
  1365. var timezone = (off >= 0 ? '+' : '-') + Math.abs(off).toString().substr(1);
  1366. return timezone;
  1367. };
  1368. $.timepicker = new Timepicker(); // singleton instance
  1369. $.timepicker.version = "1.0.1";
  1370. })(jQuery);