bootstrap-timepicker.js 31 KB

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