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