You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1172 lines
42 KiB

4 years ago
  1. /*!
  2. * Timepicker Component for Twitter Bootstrap
  3. *
  4. * Copyright 2013 Joris de Wit and bootstrap-timepicker contributors
  5. *
  6. * Contributors https://github.com/jdewit/bootstrap-timepicker/graphs/contributors
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. (function ($, window, document) {
  12. 'use strict';
  13. // TIMEPICKER PUBLIC CLASS DEFINITION
  14. var Timepicker = function (element, options) {
  15. this.widget = '';
  16. this.$element = $(element);
  17. this.defaultTime = options.defaultTime;
  18. this.disableFocus = options.disableFocus;
  19. this.disableMousewheel = options.disableMousewheel;
  20. this.isOpen = options.isOpen;
  21. this.minuteStep = options.minuteStep;
  22. this.modalBackdrop = options.modalBackdrop;
  23. this.orientation = options.orientation;
  24. this.secondStep = options.secondStep;
  25. this.snapToStep = options.snapToStep;
  26. this.showInputs = options.showInputs;
  27. this.showMeridian = options.showMeridian;
  28. this.showSeconds = options.showSeconds;
  29. this.template = options.template;
  30. this.appendWidgetTo = options.appendWidgetTo;
  31. this.showWidgetOnAddonClick = options.showWidgetOnAddonClick;
  32. this.icons = options.icons;
  33. this.maxHours = options.maxHours;
  34. this.explicitMode = options.explicitMode; // If true 123 = 1:23, 12345 = 1:23:45, else invalid.
  35. this.handleDocumentClick = function (e) {
  36. var self = e.data.scope;
  37. // This condition was inspired by bootstrap-datepicker.
  38. // The element the timepicker is invoked on is the input but it has a sibling for addon/button.
  39. if (!(self.$element.parent().find(e.target).length ||
  40. self.$widget.is(e.target) ||
  41. self.$widget.find(e.target).length)) {
  42. self.hideWidget();
  43. }
  44. };
  45. this._init();
  46. };
  47. Timepicker.prototype = {
  48. constructor: Timepicker,
  49. _init: function () {
  50. var self = this;
  51. if (this.showWidgetOnAddonClick && (this.$element.parent().hasClass('input-group') && this.$element.parent().hasClass('bootstrap-timepicker'))) {
  52. this.$element.parent('.input-group.bootstrap-timepicker').find('.input-group-addon').on({
  53. 'click.timepicker': $.proxy(this.showWidget, this)
  54. });
  55. this.$element.on({
  56. 'focus.timepicker': $.proxy(this.highlightUnit, this),
  57. 'click.timepicker': $.proxy(this.highlightUnit, this),
  58. 'keydown.timepicker': $.proxy(this.elementKeydown, this),
  59. 'blur.timepicker': $.proxy(this.blurElement, this),
  60. 'mousewheel.timepicker DOMMouseScroll.timepicker': $.proxy(this.mousewheel, this)
  61. });
  62. } else {
  63. if (this.template) {
  64. this.$element.on({
  65. 'focus.timepicker': $.proxy(this.showWidget, this),
  66. 'click.timepicker': $.proxy(this.showWidget, this),
  67. 'blur.timepicker': $.proxy(this.blurElement, this),
  68. 'mousewheel.timepicker DOMMouseScroll.timepicker': $.proxy(this.mousewheel, this)
  69. });
  70. } else {
  71. this.$element.on({
  72. 'focus.timepicker': $.proxy(this.highlightUnit, this),
  73. 'click.timepicker': $.proxy(this.highlightUnit, this),
  74. 'keydown.timepicker': $.proxy(this.elementKeydown, this),
  75. 'blur.timepicker': $.proxy(this.blurElement, this),
  76. 'mousewheel.timepicker DOMMouseScroll.timepicker': $.proxy(this.mousewheel, this)
  77. });
  78. }
  79. }
  80. if (this.template !== false) {
  81. this.$widget = $(this.getTemplate()).on('click', $.proxy(this.widgetClick, this));
  82. } else {
  83. this.$widget = false;
  84. }
  85. if (this.showInputs && this.$widget !== false) {
  86. this.$widget.find('input').each(function () {
  87. $(this).on({
  88. 'click.timepicker': function () { $(this).select(); },
  89. 'keydown.timepicker': $.proxy(self.widgetKeydown, self),
  90. 'keyup.timepicker': $.proxy(self.widgetKeyup, self)
  91. });
  92. });
  93. }
  94. this.setDefaultTime(this.defaultTime);
  95. },
  96. blurElement: function () {
  97. this.highlightedUnit = null;
  98. this.updateFromElementVal();
  99. },
  100. clear: function () {
  101. this.hour = '';
  102. this.minute = '';
  103. this.second = '';
  104. this.meridian = '';
  105. this.$element.val('');
  106. },
  107. decrementHour: function () {
  108. if (this.showMeridian) {
  109. if (this.hour === 1) {
  110. this.hour = 12;
  111. } else if (this.hour === 12) {
  112. this.hour--;
  113. return this.toggleMeridian();
  114. } else if (this.hour === 0) {
  115. this.hour = 11;
  116. return this.toggleMeridian();
  117. } else {
  118. this.hour--;
  119. }
  120. } else {
  121. if (this.hour <= 0) {
  122. this.hour = this.maxHours - 1;
  123. } else {
  124. this.hour--;
  125. }
  126. }
  127. },
  128. decrementMinute: function (step) {
  129. var newVal;
  130. if (step) {
  131. newVal = this.minute - step;
  132. } else {
  133. newVal = this.minute - this.minuteStep;
  134. }
  135. if (newVal < 0) {
  136. this.decrementHour();
  137. this.minute = newVal + 60;
  138. } else {
  139. this.minute = newVal;
  140. }
  141. },
  142. decrementSecond: function () {
  143. var newVal = this.second - this.secondStep;
  144. if (newVal < 0) {
  145. this.decrementMinute(true);
  146. this.second = newVal + 60;
  147. } else {
  148. this.second = newVal;
  149. }
  150. },
  151. elementKeydown: function (e) {
  152. switch (e.which) {
  153. case 9: //tab
  154. if (e.shiftKey) {
  155. if (this.highlightedUnit === 'hour') {
  156. this.hideWidget();
  157. break;
  158. }
  159. this.highlightPrevUnit();
  160. } else if ((this.showMeridian && this.highlightedUnit === 'meridian') || (this.showSeconds && this.highlightedUnit === 'second') || (!this.showMeridian && !this.showSeconds && this.highlightedUnit === 'minute')) {
  161. this.hideWidget();
  162. break;
  163. } else {
  164. this.highlightNextUnit();
  165. }
  166. e.preventDefault();
  167. this.updateFromElementVal();
  168. break;
  169. case 27: // escape
  170. this.updateFromElementVal();
  171. break;
  172. case 37: // left arrow
  173. e.preventDefault();
  174. this.highlightPrevUnit();
  175. this.updateFromElementVal();
  176. break;
  177. case 38: // up arrow
  178. e.preventDefault();
  179. switch (this.highlightedUnit) {
  180. case 'hour':
  181. this.incrementHour();
  182. this.highlightHour();
  183. break;
  184. case 'minute':
  185. this.incrementMinute();
  186. this.highlightMinute();
  187. break;
  188. case 'second':
  189. this.incrementSecond();
  190. this.highlightSecond();
  191. break;
  192. case 'meridian':
  193. this.toggleMeridian();
  194. this.highlightMeridian();
  195. break;
  196. }
  197. this.update();
  198. break;
  199. case 39: // right arrow
  200. e.preventDefault();
  201. this.highlightNextUnit();
  202. this.updateFromElementVal();
  203. break;
  204. case 40: // down arrow
  205. e.preventDefault();
  206. switch (this.highlightedUnit) {
  207. case 'hour':
  208. this.decrementHour();
  209. this.highlightHour();
  210. break;
  211. case 'minute':
  212. this.decrementMinute();
  213. this.highlightMinute();
  214. break;
  215. case 'second':
  216. this.decrementSecond();
  217. this.highlightSecond();
  218. break;
  219. case 'meridian':
  220. this.toggleMeridian();
  221. this.highlightMeridian();
  222. break;
  223. }
  224. this.update();
  225. break;
  226. }
  227. },
  228. getCursorPosition: function () {
  229. var input = this.$element.get(0);
  230. if ('selectionStart' in input) {// Standard-compliant browsers
  231. return input.selectionStart;
  232. } else if (document.selection) {// IE fix
  233. input.focus();
  234. var sel = document.selection.createRange(),
  235. selLen = document.selection.createRange().text.length;
  236. sel.moveStart('character', - input.value.length);
  237. return sel.text.length - selLen;
  238. }
  239. },
  240. getTemplate: function () {
  241. var template,
  242. hourTemplate,
  243. minuteTemplate,
  244. secondTemplate,
  245. meridianTemplate,
  246. templateContent;
  247. if (this.showInputs) {
  248. hourTemplate = '<input type="text" class="bootstrap-timepicker-hour" maxlength="2"/>';
  249. minuteTemplate = '<input type="text" class="bootstrap-timepicker-minute" maxlength="2"/>';
  250. secondTemplate = '<input type="text" class="bootstrap-timepicker-second" maxlength="2"/>';
  251. meridianTemplate = '<input type="text" class="bootstrap-timepicker-meridian" maxlength="2"/>';
  252. } else {
  253. hourTemplate = '<span class="bootstrap-timepicker-hour"></span>';
  254. minuteTemplate = '<span class="bootstrap-timepicker-minute"></span>';
  255. secondTemplate = '<span class="bootstrap-timepicker-second"></span>';
  256. meridianTemplate = '<span class="bootstrap-timepicker-meridian"></span>';
  257. }
  258. templateContent = '<table>' +
  259. '<tr>' +
  260. '<td><a href="#" data-action="incrementHour"><span class="' + this.icons.up + '"></span></a></td>' +
  261. '<td class="separator">&nbsp;</td>' +
  262. '<td><a href="#" data-action="incrementMinute"><span class="' + this.icons.up + '"></span></a></td>' +
  263. (this.showSeconds ?
  264. '<td class="separator">&nbsp;</td>' +
  265. '<td><a href="#" data-action="incrementSecond"><span class="' + this.icons.up + '"></span></a></td>'
  266. : '') +
  267. (this.showMeridian ?
  268. '<td class="separator">&nbsp;</td>' +
  269. '<td class="meridian-column"><a href="#" data-action="toggleMeridian"><span class="' + this.icons.up + '"></span></a></td>'
  270. : '') +
  271. '</tr>' +
  272. '<tr>' +
  273. '<td>' + hourTemplate + '</td> ' +
  274. '<td class="separator">:</td>' +
  275. '<td>' + minuteTemplate + '</td> ' +
  276. (this.showSeconds ?
  277. '<td class="separator">:</td>' +
  278. '<td>' + secondTemplate + '</td>'
  279. : '') +
  280. (this.showMeridian ?
  281. '<td class="separator">&nbsp;</td>' +
  282. '<td>' + meridianTemplate + '</td>'
  283. : '') +
  284. '</tr>' +
  285. '<tr>' +
  286. '<td><a href="#" data-action="decrementHour"><span class="' + this.icons.down + '"></span></a></td>' +
  287. '<td class="separator"></td>' +
  288. '<td><a href="#" data-action="decrementMinute"><span class="' + this.icons.down + '"></span></a></td>' +
  289. (this.showSeconds ?
  290. '<td class="separator">&nbsp;</td>' +
  291. '<td><a href="#" data-action="decrementSecond"><span class="' + this.icons.down + '"></span></a></td>'
  292. : '') +
  293. (this.showMeridian ?
  294. '<td class="separator">&nbsp;</td>' +
  295. '<td><a href="#" data-action="toggleMeridian"><span class="' + this.icons.down + '"></span></a></td>'
  296. : '') +
  297. '</tr>' +
  298. '</table>';
  299. switch (this.template) {
  300. case 'modal':
  301. template = '<div class="bootstrap-timepicker-widget modal hide fade in" data-backdrop="' + (this.modalBackdrop ? 'true' : 'false') + '">' +
  302. '<div class="modal-header">' +
  303. '<a href="#" class="close" data-dismiss="modal">&times;</a>' +
  304. '<h3>Pick a Time</h3>' +
  305. '</div>' +
  306. '<div class="modal-content">' +
  307. templateContent +
  308. '</div>' +
  309. '<div class="modal-footer">' +
  310. '<a href="#" class="btn btn-primary" data-dismiss="modal">OK</a>' +
  311. '</div>' +
  312. '</div>';
  313. break;
  314. case 'dropdown':
  315. template = '<div class="bootstrap-timepicker-widget dropdown-menu">' + templateContent + '</div>';
  316. break;
  317. }
  318. return template;
  319. },
  320. getTime: function () {
  321. if (this.hour === '') {
  322. return '';
  323. }
  324. return this.hour + ':' + (this.minute.toString().length === 1 ? '0' + this.minute : this.minute) + (this.showSeconds ? ':' + (this.second.toString().length === 1 ? '0' + this.second : this.second) : '') + (this.showMeridian ? ' ' + this.meridian : '');
  325. },
  326. hideWidget: function () {
  327. if (this.isOpen === false) {
  328. return;
  329. }
  330. this.$element.trigger({
  331. 'type': 'hide.timepicker',
  332. 'time': {
  333. 'value': this.getTime(),
  334. 'hours': this.hour,
  335. 'minutes': this.minute,
  336. 'seconds': this.second,
  337. 'meridian': this.meridian
  338. }
  339. });
  340. if (this.template === 'modal' && this.$widget.modal) {
  341. this.$widget.modal('hide');
  342. } else {
  343. this.$widget.removeClass('open');
  344. }
  345. $(document).off('mousedown.timepicker, touchend.timepicker', this.handleDocumentClick);
  346. this.isOpen = false;
  347. // show/hide approach taken by datepicker
  348. this.$widget.detach();
  349. },
  350. highlightUnit: function () {
  351. this.position = this.getCursorPosition();
  352. if (this.position >= 0 && this.position <= 2) {
  353. this.highlightHour();
  354. } else if (this.position >= 3 && this.position <= 5) {
  355. this.highlightMinute();
  356. } else if (this.position >= 6 && this.position <= 8) {
  357. if (this.showSeconds) {
  358. this.highlightSecond();
  359. } else {
  360. this.highlightMeridian();
  361. }
  362. } else if (this.position >= 9 && this.position <= 11) {
  363. this.highlightMeridian();
  364. }
  365. },
  366. highlightNextUnit: function () {
  367. switch (this.highlightedUnit) {
  368. case 'hour':
  369. this.highlightMinute();
  370. break;
  371. case 'minute':
  372. if (this.showSeconds) {
  373. this.highlightSecond();
  374. } else if (this.showMeridian) {
  375. this.highlightMeridian();
  376. } else {
  377. this.highlightHour();
  378. }
  379. break;
  380. case 'second':
  381. if (this.showMeridian) {
  382. this.highlightMeridian();
  383. } else {
  384. this.highlightHour();
  385. }
  386. break;
  387. case 'meridian':
  388. this.highlightHour();
  389. break;
  390. }
  391. },
  392. highlightPrevUnit: function () {
  393. switch (this.highlightedUnit) {
  394. case 'hour':
  395. if (this.showMeridian) {
  396. this.highlightMeridian();
  397. } else if (this.showSeconds) {
  398. this.highlightSecond();
  399. } else {
  400. this.highlightMinute();
  401. }
  402. break;
  403. case 'minute':
  404. this.highlightHour();
  405. break;
  406. case 'second':
  407. this.highlightMinute();
  408. break;
  409. case 'meridian':
  410. if (this.showSeconds) {
  411. this.highlightSecond();
  412. } else {
  413. this.highlightMinute();
  414. }
  415. break;
  416. }
  417. },
  418. highlightHour: function () {
  419. var $element = this.$element.get(0),
  420. self = this;
  421. this.highlightedUnit = 'hour';
  422. if ($element.setSelectionRange) {
  423. setTimeout(function () {
  424. if (self.hour < 10) {
  425. $element.setSelectionRange(0, 1);
  426. } else {
  427. $element.setSelectionRange(0, 2);
  428. }
  429. }, 0);
  430. }
  431. },
  432. highlightMinute: function () {
  433. var $element = this.$element.get(0),
  434. self = this;
  435. this.highlightedUnit = 'minute';
  436. if ($element.setSelectionRange) {
  437. setTimeout(function () {
  438. if (self.hour < 10) {
  439. $element.setSelectionRange(2, 4);
  440. } else {
  441. $element.setSelectionRange(3, 5);
  442. }
  443. }, 0);
  444. }
  445. },
  446. highlightSecond: function () {
  447. var $element = this.$element.get(0),
  448. self = this;
  449. this.highlightedUnit = 'second';
  450. if ($element.setSelectionRange) {
  451. setTimeout(function () {
  452. if (self.hour < 10) {
  453. $element.setSelectionRange(5, 7);
  454. } else {
  455. $element.setSelectionRange(6, 8);
  456. }
  457. }, 0);
  458. }
  459. },
  460. highlightMeridian: function () {
  461. var $element = this.$element.get(0),
  462. self = this;
  463. this.highlightedUnit = 'meridian';
  464. if ($element.setSelectionRange) {
  465. if (this.showSeconds) {
  466. setTimeout(function () {
  467. if (self.hour < 10) {
  468. $element.setSelectionRange(8, 10);
  469. } else {
  470. $element.setSelectionRange(9, 11);
  471. }
  472. }, 0);
  473. } else {
  474. setTimeout(function () {
  475. if (self.hour < 10) {
  476. $element.setSelectionRange(5, 7);
  477. } else {
  478. $element.setSelectionRange(6, 8);
  479. }
  480. }, 0);
  481. }
  482. }
  483. },
  484. incrementHour: function () {
  485. if (this.showMeridian) {
  486. if (this.hour === 11) {
  487. this.hour++;
  488. return this.toggleMeridian();
  489. } else if (this.hour === 12) {
  490. this.hour = 0;
  491. }
  492. }
  493. if (this.hour === this.maxHours - 1) {
  494. this.hour = 0;
  495. return;
  496. }
  497. this.hour++;
  498. },
  499. incrementMinute: function (step) {
  500. var newVal;
  501. if (step) {
  502. newVal = this.minute + step;
  503. } else {
  504. newVal = this.minute + this.minuteStep - (this.minute % this.minuteStep);
  505. }
  506. if (newVal > 59) {
  507. this.incrementHour();
  508. this.minute = newVal - 60;
  509. } else {
  510. this.minute = newVal;
  511. }
  512. },
  513. incrementSecond: function () {
  514. var newVal = this.second + this.secondStep - (this.second % this.secondStep);
  515. if (newVal > 59) {
  516. this.incrementMinute(true);
  517. this.second = newVal - 60;
  518. } else {
  519. this.second = newVal;
  520. }
  521. },
  522. mousewheel: function (e) {
  523. if (this.disableMousewheel) {
  524. return;
  525. }
  526. e.preventDefault();
  527. e.stopPropagation();
  528. var delta = e.originalEvent.wheelDelta || -e.originalEvent.detail,
  529. scrollTo = null;
  530. if (e.type === 'mousewheel') {
  531. scrollTo = (e.originalEvent.wheelDelta * -1);
  532. }
  533. else if (e.type === 'DOMMouseScroll') {
  534. scrollTo = 40 * e.originalEvent.detail;
  535. }
  536. if (scrollTo) {
  537. e.preventDefault();
  538. $(this).scrollTop(scrollTo + $(this).scrollTop());
  539. }
  540. switch (this.highlightedUnit) {
  541. case 'minute':
  542. if (delta > 0) {
  543. this.incrementMinute();
  544. } else {
  545. this.decrementMinute();
  546. }
  547. this.highlightMinute();
  548. break;
  549. case 'second':
  550. if (delta > 0) {
  551. this.incrementSecond();
  552. } else {
  553. this.decrementSecond();
  554. }
  555. this.highlightSecond();
  556. break;
  557. case 'meridian':
  558. this.toggleMeridian();
  559. this.highlightMeridian();
  560. break;
  561. default:
  562. if (delta > 0) {
  563. this.incrementHour();
  564. } else {
  565. this.decrementHour();
  566. }
  567. this.highlightHour();
  568. break;
  569. }
  570. return false;
  571. },
  572. /**
  573. * Given a segment value like 43, will round and snap the segment
  574. * to the nearest "step", like 45 if step is 15. Segment will
  575. * "overflow" to 0 if it's larger than 59 or would otherwise
  576. * round up to 60.
  577. */
  578. changeToNearestStep: function (segment, step) {
  579. if (segment % step === 0) {
  580. return segment;
  581. }
  582. if (Math.round((segment % step) / step)) {
  583. return (segment + (step - segment % step)) % 60;
  584. } else {
  585. return segment - segment % step;
  586. }
  587. },
  588. // This method was adapted from bootstrap-datepicker.
  589. place: function () {
  590. if (this.isInline) {
  591. return;
  592. }
  593. var widgetWidth = this.$widget.outerWidth(), widgetHeight = this.$widget.outerHeight(), visualPadding = 10, windowWidth =
  594. $(window).width(), windowHeight = $(window).height(), scrollTop = $(window).scrollTop();
  595. var zIndex = parseInt(this.$element.parents().filter(function () { return $(this).css('z-index') !== 'auto'; }).first().css('z-index'), 10) + 10;
  596. var offset = this.component ? this.component.parent().offset() : this.$element.offset();
  597. var height = this.component ? this.component.outerHeight(true) : this.$element.outerHeight(false);
  598. var width = this.component ? this.component.outerWidth(true) : this.$element.outerWidth(false);
  599. var left = offset.left, top = offset.top;
  600. this.$widget.removeClass('timepicker-orient-top timepicker-orient-bottom timepicker-orient-right timepicker-orient-left');
  601. if (this.orientation.x !== 'auto') {
  602. this.$widget.addClass('timepicker-orient-' + this.orientation.x);
  603. if (this.orientation.x === 'right') {
  604. left -= widgetWidth - width;
  605. }
  606. } else {
  607. // auto x orientation is best-placement: if it crosses a window edge, fudge it sideways
  608. // Default to left
  609. this.$widget.addClass('timepicker-orient-left');
  610. if (offset.left < 0) {
  611. left -= offset.left - visualPadding;
  612. } else if (offset.left + widgetWidth > windowWidth) {
  613. left = windowWidth - widgetWidth - visualPadding;
  614. }
  615. }
  616. // auto y orientation is best-situation: top or bottom, no fudging, decision based on which shows more of the widget
  617. var yorient = this.orientation.y, topOverflow, bottomOverflow;
  618. if (yorient === 'auto') {
  619. topOverflow = -scrollTop + offset.top - widgetHeight;
  620. bottomOverflow = scrollTop + windowHeight - (offset.top + height + widgetHeight);
  621. if (Math.max(topOverflow, bottomOverflow) === bottomOverflow) {
  622. yorient = 'top';
  623. } else {
  624. yorient = 'bottom';
  625. }
  626. }
  627. this.$widget.addClass('timepicker-orient-' + yorient);
  628. if (yorient === 'top') {
  629. top += height;
  630. } else {
  631. top -= widgetHeight + parseInt(this.$widget.css('padding-top'), 10);
  632. }
  633. this.$widget.css({
  634. top: top,
  635. left: left,
  636. zIndex: zIndex
  637. });
  638. },
  639. remove: function () {
  640. $('document').off('.timepicker');
  641. if (this.$widget) {
  642. this.$widget.remove();
  643. }
  644. delete this.$element.data().timepicker;
  645. },
  646. setDefaultTime: function (defaultTime) {
  647. if (!this.$element.val()) {
  648. if (defaultTime === 'current') {
  649. var dTime = new Date(),
  650. hours = dTime.getHours(),
  651. minutes = dTime.getMinutes(),
  652. seconds = dTime.getSeconds(),
  653. meridian = 'AM';
  654. if (seconds !== 0) {
  655. seconds = Math.ceil(dTime.getSeconds() / this.secondStep) * this.secondStep;
  656. if (seconds === 60) {
  657. minutes += 1;
  658. seconds = 0;
  659. }
  660. }
  661. if (minutes !== 0) {
  662. minutes = Math.ceil(dTime.getMinutes() / this.minuteStep) * this.minuteStep;
  663. if (minutes === 60) {
  664. hours += 1;
  665. minutes = 0;
  666. }
  667. }
  668. if (this.showMeridian) {
  669. if (hours === 0) {
  670. hours = 12;
  671. } else if (hours >= 12) {
  672. if (hours > 12) {
  673. hours = hours - 12;
  674. }
  675. meridian = 'PM';
  676. } else {
  677. meridian = 'AM';
  678. }
  679. }
  680. this.hour = hours;
  681. this.minute = minutes;
  682. this.second = seconds;
  683. this.meridian = meridian;
  684. this.update();
  685. } else if (defaultTime === false) {
  686. this.hour = 0;
  687. this.minute = 0;
  688. this.second = 0;
  689. this.meridian = 'AM';
  690. } else {
  691. this.setTime(defaultTime);
  692. }
  693. } else {
  694. this.updateFromElementVal();
  695. }
  696. },
  697. setTime: function (time, ignoreWidget) {
  698. if (!time) {
  699. this.clear();
  700. return;
  701. }
  702. var timeMode,
  703. timeArray,
  704. hour,
  705. minute,
  706. second,
  707. meridian;
  708. if (typeof time === 'object' && time.getMonth) {
  709. // this is a date object
  710. hour = time.getHours();
  711. minute = time.getMinutes();
  712. second = time.getSeconds();
  713. if (this.showMeridian) {
  714. meridian = 'AM';
  715. if (hour > 12) {
  716. meridian = 'PM';
  717. hour = hour % 12;
  718. }
  719. if (hour === 12) {
  720. meridian = 'PM';
  721. }
  722. }
  723. } else {
  724. timeMode = ((/a/i).test(time) ? 1 : 0) + ((/p/i).test(time) ? 2 : 0); // 0 = none, 1 = AM, 2 = PM, 3 = BOTH.
  725. if (timeMode > 2) { // If both are present, fail.
  726. this.clear();
  727. return;
  728. }
  729. timeArray = time.replace(/[^0-9\:]/g, '').split(':');
  730. hour = timeArray[0] ? timeArray[0].toString() : timeArray.toString();
  731. if (this.explicitMode && hour.length > 2 && (hour.length % 2) !== 0) {
  732. this.clear();
  733. return;
  734. }
  735. minute = timeArray[1] ? timeArray[1].toString() : '';
  736. second = timeArray[2] ? timeArray[2].toString() : '';
  737. // adaptive time parsing
  738. if (hour.length > 4) {
  739. second = hour.slice(-2);
  740. hour = hour.slice(0, -2);
  741. }
  742. if (hour.length > 2) {
  743. minute = hour.slice(-2);
  744. hour = hour.slice(0, -2);
  745. }
  746. if (minute.length > 2) {
  747. second = minute.slice(-2);
  748. minute = minute.slice(0, -2);
  749. }
  750. hour = parseInt(hour, 10);
  751. minute = parseInt(minute, 10);
  752. second = parseInt(second, 10);
  753. if (isNaN(hour)) {
  754. hour = 0;
  755. }
  756. if (isNaN(minute)) {
  757. minute = 0;
  758. }
  759. if (isNaN(second)) {
  760. second = 0;
  761. }
  762. // Adjust the time based upon unit boundary.
  763. // NOTE: Negatives will never occur due to time.replace() above.
  764. if (second > 59) {
  765. second = 59;
  766. }
  767. if (minute > 59) {
  768. minute = 59;
  769. }
  770. if (hour >= this.maxHours) {
  771. // No day/date handling.
  772. hour = this.maxHours - 1;
  773. }
  774. if (this.showMeridian) {
  775. if (hour > 12) {
  776. // Force PM.
  777. timeMode = 2;
  778. hour -= 12;
  779. }
  780. if (!timeMode) {
  781. timeMode = 1;
  782. }
  783. if (hour === 0) {
  784. hour = 12; // AM or PM, reset to 12. 0 AM = 12 AM. 0 PM = 12 PM, etc.
  785. }
  786. meridian = timeMode === 1 ? 'AM' : 'PM';
  787. } else if (hour < 12 && timeMode === 2) {
  788. hour += 12;
  789. } else {
  790. if (hour >= this.maxHours) {
  791. hour = this.maxHours - 1;
  792. } else if ((hour < 0) || (hour === 12 && timeMode === 1)) {
  793. hour = 0;
  794. }
  795. }
  796. }
  797. this.hour = hour;
  798. if (this.snapToStep) {
  799. this.minute = this.changeToNearestStep(minute, this.minuteStep);
  800. this.second = this.changeToNearestStep(second, this.secondStep);
  801. } else {
  802. this.minute = minute;
  803. this.second = second;
  804. }
  805. this.meridian = meridian;
  806. this.update(ignoreWidget);
  807. },
  808. showWidget: function () {
  809. if (this.isOpen) {
  810. return;
  811. }
  812. if (this.$element.is(':disabled')) {
  813. return;
  814. }
  815. // show/hide approach taken by datepicker
  816. this.$widget.appendTo(this.appendWidgetTo);
  817. $(document).on('mousedown.timepicker, touchend.timepicker', { scope: this }, this.handleDocumentClick);
  818. this.$element.trigger({
  819. 'type': 'show.timepicker',
  820. 'time': {
  821. 'value': this.getTime(),
  822. 'hours': this.hour,
  823. 'minutes': this.minute,
  824. 'seconds': this.second,
  825. 'meridian': this.meridian
  826. }
  827. });
  828. this.place();
  829. if (this.disableFocus) {
  830. this.$element.blur();
  831. }
  832. // widget shouldn't be empty on open
  833. if (this.hour === '') {
  834. if (this.defaultTime) {
  835. this.setDefaultTime(this.defaultTime);
  836. } else {
  837. this.setTime('0:0:0');
  838. }
  839. }
  840. if (this.template === 'modal' && this.$widget.modal) {
  841. this.$widget.modal('show').on('hidden', $.proxy(this.hideWidget, this));
  842. } else {
  843. if (this.isOpen === false) {
  844. this.$widget.addClass('open');
  845. }
  846. }
  847. this.isOpen = true;
  848. },
  849. toggleMeridian: function () {
  850. this.meridian = this.meridian === 'AM' ? 'PM' : 'AM';
  851. },
  852. update: function (ignoreWidget) {
  853. this.updateElement();
  854. if (!ignoreWidget) {
  855. this.updateWidget();
  856. }
  857. this.$element.trigger({
  858. 'type': 'changeTime.timepicker',
  859. 'time': {
  860. 'value': this.getTime(),
  861. 'hours': this.hour,
  862. 'minutes': this.minute,
  863. 'seconds': this.second,
  864. 'meridian': this.meridian
  865. }
  866. });
  867. },
  868. updateElement: function () {
  869. this.$element.val(this.getTime()).change();
  870. },
  871. updateFromElementVal: function () {
  872. this.setTime(this.$element.val());
  873. },
  874. updateWidget: function () {
  875. if (this.$widget === false) {
  876. return;
  877. }
  878. var hour = this.hour,
  879. minute = this.minute.toString().length === 1 ? '0' + this.minute : this.minute,
  880. second = this.second.toString().length === 1 ? '0' + this.second : this.second;
  881. if (this.showInputs) {
  882. this.$widget.find('input.bootstrap-timepicker-hour').val(hour);
  883. this.$widget.find('input.bootstrap-timepicker-minute').val(minute);
  884. if (this.showSeconds) {
  885. this.$widget.find('input.bootstrap-timepicker-second').val(second);
  886. }
  887. if (this.showMeridian) {
  888. this.$widget.find('input.bootstrap-timepicker-meridian').val(this.meridian);
  889. }
  890. } else {
  891. this.$widget.find('span.bootstrap-timepicker-hour').text(hour);
  892. this.$widget.find('span.bootstrap-timepicker-minute').text(minute);
  893. if (this.showSeconds) {
  894. this.$widget.find('span.bootstrap-timepicker-second').text(second);
  895. }
  896. if (this.showMeridian) {
  897. this.$widget.find('span.bootstrap-timepicker-meridian').text(this.meridian);
  898. }
  899. }
  900. },
  901. updateFromWidgetInputs: function () {
  902. if (this.$widget === false) {
  903. return;
  904. }
  905. var t = this.$widget.find('input.bootstrap-timepicker-hour').val() + ':' +
  906. this.$widget.find('input.bootstrap-timepicker-minute').val() +
  907. (this.showSeconds ? ':' + this.$widget.find('input.bootstrap-timepicker-second').val() : '') +
  908. (this.showMeridian ? this.$widget.find('input.bootstrap-timepicker-meridian').val() : '')
  909. ;
  910. this.setTime(t, true);
  911. },
  912. widgetClick: function (e) {
  913. e.stopPropagation();
  914. e.preventDefault();
  915. var $input = $(e.target),
  916. action = $input.closest('a').data('action');
  917. if (action) {
  918. this[action]();
  919. }
  920. this.update();
  921. if ($input.is('input')) {
  922. $input.get(0).setSelectionRange(0, 2);
  923. }
  924. },
  925. widgetKeydown: function (e) {
  926. var $input = $(e.target),
  927. name = $input.attr('class').replace('bootstrap-timepicker-', '');
  928. switch (e.which) {
  929. case 9: //tab
  930. if (e.shiftKey) {
  931. if (name === 'hour') {
  932. return this.hideWidget();
  933. }
  934. } else if ((this.showMeridian && name === 'meridian') || (this.showSeconds && name === 'second') || (!this.showMeridian && !this.showSeconds && name === 'minute')) {
  935. return this.hideWidget();
  936. }
  937. break;
  938. case 27: // escape
  939. this.hideWidget();
  940. break;
  941. case 38: // up arrow
  942. e.preventDefault();
  943. switch (name) {
  944. case 'hour':
  945. this.incrementHour();
  946. break;
  947. case 'minute':
  948. this.incrementMinute();
  949. break;
  950. case 'second':
  951. this.incrementSecond();
  952. break;
  953. case 'meridian':
  954. this.toggleMeridian();
  955. break;
  956. }
  957. this.setTime(this.getTime());
  958. $input.get(0).setSelectionRange(0, 2);
  959. break;
  960. case 40: // down arrow
  961. e.preventDefault();
  962. switch (name) {
  963. case 'hour':
  964. this.decrementHour();
  965. break;
  966. case 'minute':
  967. this.decrementMinute();
  968. break;
  969. case 'second':
  970. this.decrementSecond();
  971. break;
  972. case 'meridian':
  973. this.toggleMeridian();
  974. break;
  975. }
  976. this.setTime(this.getTime());
  977. $input.get(0).setSelectionRange(0, 2);
  978. break;
  979. }
  980. },
  981. widgetKeyup: function (e) {
  982. if ((e.which === 65) || (e.which === 77) || (e.which === 80) || (e.which === 46) || (e.which === 8) || (e.which >= 48 && e.which <= 57) || (e.which >= 96 && e.which <= 105)) {
  983. this.updateFromWidgetInputs();
  984. }
  985. }
  986. };
  987. //TIMEPICKER PLUGIN DEFINITION
  988. $.fn.timepicker = function (option) {
  989. var args = Array.apply(null, arguments);
  990. args.shift();
  991. return this.each(function () {
  992. var $this = $(this),
  993. data = $this.data('timepicker'),
  994. options = typeof option === 'object' && option;
  995. if (!data) {
  996. $this.data('timepicker', (data = new Timepicker(this, $.extend({}, $.fn.timepicker.defaults, options, $(this).data()))));
  997. }
  998. if (typeof option === 'string') {
  999. data[option].apply(data, args);
  1000. }
  1001. });
  1002. };
  1003. $.fn.timepicker.defaults = {
  1004. defaultTime: 'current',
  1005. disableFocus: false,
  1006. disableMousewheel: false,
  1007. isOpen: false,
  1008. minuteStep: 15,
  1009. modalBackdrop: false,
  1010. orientation: { x: 'auto', y: 'auto' },
  1011. secondStep: 15,
  1012. snapToStep: false,
  1013. showSeconds: false,
  1014. showInputs: true,
  1015. showMeridian: true,
  1016. template: 'dropdown',
  1017. appendWidgetTo: 'body',
  1018. showWidgetOnAddonClick: true,
  1019. icons: {
  1020. up: 'glyphicon glyphicon-chevron-up',
  1021. down: 'glyphicon glyphicon-chevron-down'
  1022. },
  1023. maxHours: 24,
  1024. explicitMode: false
  1025. };
  1026. $.fn.timepicker.Constructor = Timepicker;
  1027. $(document).on(
  1028. 'focus.timepicker.data-api click.timepicker.data-api',
  1029. '[data-provide="timepicker"]',
  1030. function (e) {
  1031. var $this = $(this);
  1032. if ($this.data('timepicker')) {
  1033. return;
  1034. }
  1035. e.preventDefault();
  1036. // component click requires us to explicitly show it
  1037. $this.timepicker();
  1038. }
  1039. );
  1040. })(jQuery, window, document);