bootstrap-timepicker.js 31 KB

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