jquery-ui-timepicker-addon.js 70 KB

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