bootstrap-timepicker.js 33 KB

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