bootstrap-timepicker.js 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804
  1. /* =========================================================
  2. * bootstrap-timepicker.js
  3. * http://www.github.com/jdewit/bootstrap-timepicker
  4. * =========================================================
  5. * Copyright 2012
  6. *
  7. * Created By:
  8. * Joris de Wit @joris_dewit
  9. *
  10. * Contributions By:
  11. * Gilbert @mindeavor
  12. * Koen Punt info@koenpunt.nl
  13. * Nek
  14. * Chris Martin
  15. * Dominic Barnes contact@dominicbarnes.us
  16. *
  17. * Licensed under the Apache License, Version 2.0 (the "License");
  18. * you may not use this file except in compliance with the License.
  19. * You may obtain a copy of the License at
  20. *
  21. * http://www.apache.org/licenses/LICENSE-2.0
  22. *
  23. * Unless required by applicable law or agreed to in writing, software
  24. * distributed under the License is distributed on an "AS IS" BASIS,
  25. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  26. * See the License for the specific language governing permissions and
  27. * limitations under the License.
  28. * ========================================================= */
  29. !function($) {
  30. "use strict"; // jshint ;_;
  31. /* TIMEPICKER PUBLIC CLASS DEFINITION
  32. * ================================== */
  33. var Timepicker = function(element, options) {
  34. this.$element = $(element);
  35. this.options = $.extend({}, $.fn.timepicker.defaults, options, this.$element.data());
  36. this.minuteStep = this.options.minuteStep || this.minuteStep;
  37. this.secondStep = this.options.secondStep || this.secondStep;
  38. this.showMeridian = this.options.showMeridian || this.showMeridian;
  39. this.showSeconds = this.options.showSeconds || this.showSeconds;
  40. this.showInputs = this.options.showInputs || this.showInputs;
  41. this.disableFocus = this.options.disableFocus || this.disableFocus;
  42. this.template = this.options.template || this.template;
  43. this.modalBackdrop = this.options.modalBackdrop || this.modalBackdrop;
  44. this.defaultTime = this.options.defaultTime || this.defaultTime;
  45. this.open = false;
  46. this.init();
  47. };
  48. Timepicker.prototype = {
  49. constructor: Timepicker
  50. , init: function () {
  51. if (this.$element.parent().hasClass('input-append')) {
  52. this.$element.parent('.input-append').find('.add-on').on('click', $.proxy(this.showWidget, this));
  53. this.$element.on({
  54. focus: $.proxy(this.highlightUnit, this),
  55. click: $.proxy(this.highlightUnit, this),
  56. keypress: $.proxy(this.elementKeypress, this),
  57. blur: $.proxy(this.blurElement, this)
  58. });
  59. } else {
  60. if (this.template) {
  61. this.$element.on({
  62. focus: $.proxy(this.showWidget, this),
  63. click: $.proxy(this.showWidget, this),
  64. blur: $.proxy(this.blurElement, this)
  65. });
  66. } else {
  67. this.$element.on({
  68. focus: $.proxy(this.highlightUnit, this),
  69. click: $.proxy(this.highlightUnit, this),
  70. keypress: $.proxy(this.elementKeypress, this),
  71. blur: $.proxy(this.blurElement, this)
  72. });
  73. }
  74. }
  75. this.$widget = $(this.getTemplate()).appendTo('body');
  76. this.$widget.on('click', $.proxy(this.widgetClick, this));
  77. if (this.showInputs) {
  78. this.$widget.find('input').on({
  79. click: function() { this.select(); },
  80. keypress: $.proxy(this.widgetKeypress, this),
  81. change: $.proxy(this.updateFromWidgetInputs, this)
  82. });
  83. }
  84. this.setDefaultTime(this.defaultTime);
  85. }
  86. , showWidget: function(e) {
  87. e.stopPropagation();
  88. e.preventDefault();
  89. if (this.open) {
  90. return;
  91. }
  92. this.$element.trigger('show');
  93. if (this.disableFocus) {
  94. this.$element.blur();
  95. }
  96. var pos = $.extend({}, this.$element.offset(), {
  97. height: this.$element[0].offsetHeight
  98. });
  99. this.updateFromElementVal();
  100. $('html')
  101. .trigger('click.timepicker.data-api')
  102. .one('click.timepicker.data-api', $.proxy(this.hideWidget, this));
  103. if (this.template === 'modal') {
  104. this.$widget.modal('show').on('hidden', $.proxy(this.hideWidget, this));
  105. } else {
  106. this.$widget.css({
  107. top: pos.top + pos.height
  108. , left: pos.left
  109. })
  110. if (!this.open) {
  111. this.$widget.addClass('open');
  112. }
  113. }
  114. this.open = true;
  115. this.$element.trigger('shown');
  116. }
  117. , hideWidget: function(){
  118. this.$element.trigger('hide');
  119. if (this.template === 'modal') {
  120. this.$widget.modal('hide');
  121. } else {
  122. this.$widget.removeClass('open');
  123. }
  124. this.open = false;
  125. this.$element.trigger('hidden');
  126. }
  127. , widgetClick: function(e) {
  128. e.stopPropagation();
  129. e.preventDefault();
  130. var action = $(e.target).closest('a').data('action');
  131. if (action) {
  132. this[action]();
  133. this.update();
  134. }
  135. }
  136. , widgetKeypress: function(e) {
  137. var input = $(e.target).closest('input').attr('name');
  138. switch (e.keyCode) {
  139. case 9: //tab
  140. if (this.showMeridian) {
  141. if (input == 'meridian') {
  142. this.hideWidget();
  143. }
  144. } else {
  145. if (this.showSeconds) {
  146. if (input == 'second') {
  147. this.hideWidget();
  148. }
  149. } else {
  150. if (input == 'minute') {
  151. this.hideWidget();
  152. }
  153. }
  154. }
  155. break;
  156. case 27: // escape
  157. this.hideWidget();
  158. break;
  159. case 38: // up arrow
  160. switch (input) {
  161. case 'hour':
  162. this.incrementHour();
  163. break;
  164. case 'minute':
  165. this.incrementMinute();
  166. break;
  167. case 'second':
  168. this.incrementSecond();
  169. break;
  170. case 'meridian':
  171. this.toggleMeridian();
  172. break;
  173. }
  174. this.update();
  175. break;
  176. case 40: // down arrow
  177. switch (input) {
  178. case 'hour':
  179. this.decrementHour();
  180. break;
  181. case 'minute':
  182. this.decrementMinute();
  183. break;
  184. case 'second':
  185. this.decrementSecond();
  186. break;
  187. case 'meridian':
  188. this.toggleMeridian();
  189. break;
  190. }
  191. this.update();
  192. break;
  193. }
  194. }
  195. , elementKeypress: function(e) {
  196. var input = this.$element.get(0);
  197. switch (e.keyCode) {
  198. case 0: //input
  199. break;
  200. case 9: //tab
  201. this.updateFromElementVal();
  202. if (this.showMeridian) {
  203. if (this.highlightedUnit != 'meridian') {
  204. e.preventDefault();
  205. this.highlightNextUnit();
  206. }
  207. } else {
  208. if (this.showSeconds) {
  209. if (this.highlightedUnit != 'second') {
  210. e.preventDefault();
  211. this.highlightNextUnit();
  212. }
  213. } else {
  214. if (this.highlightedUnit != 'minute') {
  215. e.preventDefault();
  216. this.highlightNextUnit();
  217. }
  218. }
  219. }
  220. break;
  221. case 27: // escape
  222. this.updateFromElementVal();
  223. break;
  224. case 37: // left arrow
  225. this.updateFromElementVal();
  226. this.highlightPrevUnit();
  227. break;
  228. case 38: // up arrow
  229. switch (this.highlightedUnit) {
  230. case 'hour':
  231. this.incrementHour();
  232. break;
  233. case 'minute':
  234. this.incrementMinute();
  235. break;
  236. case 'second':
  237. this.incrementSecond();
  238. break;
  239. case 'meridian':
  240. this.toggleMeridian();
  241. break;
  242. }
  243. this.updateElement();
  244. break;
  245. case 39: // right arrow
  246. this.updateFromElementVal();
  247. this.highlightNextUnit();
  248. break;
  249. case 40: // down arrow
  250. switch (this.highlightedUnit) {
  251. case 'hour':
  252. this.decrementHour();
  253. break;
  254. case 'minute':
  255. this.decrementMinute();
  256. break;
  257. case 'second':
  258. this.decrementSecond();
  259. break;
  260. case 'meridian':
  261. this.toggleMeridian();
  262. break;
  263. }
  264. this.updateElement();
  265. break;
  266. }
  267. if (e.keyCode !== 0 && e.keyCode !== 8 && e.keyCode !== 9 && e.keyCode !== 46) {
  268. e.preventDefault();
  269. }
  270. }
  271. , setValues: function(time) {
  272. if (this.showMeridian) {
  273. var arr = time.split(' ');
  274. var timeArray = arr[0].split(':');
  275. this.meridian = arr[1];
  276. } else {
  277. var timeArray = time.split(':');
  278. }
  279. this.hour = parseInt(timeArray[0], 10);
  280. this.minute = parseInt(timeArray[1], 10);
  281. this.second = parseInt(timeArray[2], 10);
  282. if (isNaN(this.hour)) {
  283. this.hour = 0;
  284. }
  285. if (isNaN(this.minute)) {
  286. this.minute = 0;
  287. }
  288. if (this.showMeridian) {
  289. if (this.hour > 12) {
  290. this.hour = 12;
  291. } else if (this.hour < 1) {
  292. this.hour = 1;
  293. }
  294. if (this.meridian == 'am' || this.meridian == 'a') {
  295. this.meridian = 'AM';
  296. } else if (this.meridian == 'pm' || this.meridian == 'p') {
  297. this.meridian = 'PM';
  298. }
  299. if (this.meridian != 'AM' && this.meridian != 'PM') {
  300. this.meridian = 'AM';
  301. }
  302. } else {
  303. if (this.hour >= 24) {
  304. this.hour = 23;
  305. } else if (this.hour < 0) {
  306. this.hour = 0;
  307. }
  308. }
  309. if (this.minute < 0) {
  310. this.minute = 0;
  311. } else if (this.minute >= 60) {
  312. this.minute = 59;
  313. }
  314. if (this.showSeconds) {
  315. if (isNaN(this.second)) {
  316. this.second = 0;
  317. } else if (this.second < 0) {
  318. this.second = 0;
  319. } else if (this.second >= 60) {
  320. this.second = 59;
  321. }
  322. }
  323. if ( this.$element.val() != '' )
  324. this.updateElement();
  325. this.updateWidget();
  326. }
  327. , setMeridian: function(meridian) {
  328. if (meridian == 'a' || meridian == 'am' || meridian == 'AM' ) {
  329. this.meridian = 'AM';
  330. } else if (meridian == 'p' || meridian == 'pm' || meridian == 'PM' ) {
  331. this.meridian = 'PM';
  332. } else {
  333. this.updateWidget();
  334. }
  335. this.updateElement();
  336. }
  337. , setDefaultTime: function(defaultTime){
  338. if (defaultTime) {
  339. if (defaultTime === 'current') {
  340. var dTime = new Date();
  341. var hours = dTime.getHours();
  342. var minutes = Math.floor(dTime.getMinutes() / this.minuteStep) * this.minuteStep;
  343. var seconds = Math.floor(dTime.getSeconds() / this.secondStep) * this.secondStep;
  344. var meridian = "AM";
  345. if (this.showMeridian) {
  346. if (hours === 0) {
  347. hours = 12;
  348. } else if (hours >= 12) {
  349. if (hours > 12) {
  350. hours = hours - 12;
  351. }
  352. meridian = "PM";
  353. } else {
  354. meridian = "AM";
  355. }
  356. }
  357. this.hour = hours;
  358. this.minute = minutes;
  359. this.second = seconds;
  360. this.meridian = meridian;
  361. } else if (defaultTime === 'value') {
  362. this.setValues(this.$element.val());
  363. } else {
  364. this.setValues(defaultTime);
  365. }
  366. if ( this.$element.val() != '' )
  367. this.updateElement();
  368. this.updateWidget();
  369. } else {
  370. this.hour = 0;
  371. this.minute = 0;
  372. this.second = 0;
  373. }
  374. }
  375. , formatTime: function(hour, minute, second, meridian) {
  376. hour = hour < 10 ? '0' + hour : hour;
  377. minute = minute < 10 ? '0' + minute : minute;
  378. second = second < 10 ? '0' + second : second;
  379. return hour + ':' + minute + (this.showSeconds ? ':' + second : '') + (this.showMeridian ? ' ' + meridian : '');
  380. }
  381. , getTime: function() {
  382. return this.formatTime(this.hour, this.minute, this.second, this.meridian);
  383. }
  384. , setTime: function(time) {
  385. this.setValues(time);
  386. this.update();
  387. }
  388. , update: function() {
  389. this.updateElement();
  390. this.updateWidget();
  391. }
  392. , blurElement: function() {
  393. this.highlightedUnit = undefined;
  394. this.updateFromElementVal();
  395. }
  396. , updateElement: function() {
  397. var time = this.getTime();
  398. this.$element.val(time).change();
  399. switch (this.highlightedUnit) {
  400. case 'hour':
  401. this.highlightHour();
  402. break;
  403. case 'minute':
  404. this.highlightMinute();
  405. break;
  406. case 'second':
  407. this.highlightSecond();
  408. break;
  409. case 'meridian':
  410. this.highlightMeridian();
  411. break;
  412. }
  413. }
  414. , updateWidget: function() {
  415. if (this.showInputs) {
  416. this.$widget.find('input.bootstrap-timepicker-hour').val(this.hour < 10 ? '0' + this.hour : this.hour);
  417. this.$widget.find('input.bootstrap-timepicker-minute').val(this.minute < 10 ? '0' + this.minute : this.minute);
  418. if (this.showSeconds) {
  419. this.$widget.find('input.bootstrap-timepicker-second').val(this.second < 10 ? '0' + this.second : this.second);
  420. }
  421. if (this.showMeridian) {
  422. this.$widget.find('input.bootstrap-timepicker-meridian').val(this.meridian);
  423. }
  424. } else {
  425. this.$widget.find('span.bootstrap-timepicker-hour').text(this.hour);
  426. this.$widget.find('span.bootstrap-timepicker-minute').text(this.minute < 10 ? '0' + this.minute : this.minute);
  427. if (this.showSeconds) {
  428. this.$widget.find('span.bootstrap-timepicker-second').text(this.second < 10 ? '0' + this.second : this.second);
  429. }
  430. if (this.showMeridian) {
  431. this.$widget.find('span.bootstrap-timepicker-meridian').text(this.meridian);
  432. }
  433. }
  434. }
  435. , updateFromElementVal: function (e) {
  436. var time = this.$element.val();
  437. if (time) {
  438. this.setValues(time);
  439. this.updateWidget();
  440. }
  441. }
  442. , updateFromWidgetInputs: function () {
  443. var time = $('input.bootstrap-timepicker-hour', this.$widget).val() + ':' +
  444. $('input.bootstrap-timepicker-minute', this.$widget).val() +
  445. (this.showSeconds ?
  446. ':' + $('input.bootstrap-timepicker-second', this.$widget).val()
  447. : '') +
  448. (this.showMeridian ?
  449. ' ' + $('input.bootstrap-timepicker-meridian', this.$widget).val()
  450. : '');
  451. this.setValues(time);
  452. }
  453. , getCursorPosition: function() {
  454. var input = this.$element.get(0);
  455. if ('selectionStart' in input) {
  456. // Standard-compliant browsers
  457. return input.selectionStart;
  458. } else if (document.selection) {
  459. // IE fix
  460. input.focus();
  461. var sel = document.selection.createRange();
  462. var selLen = document.selection.createRange().text.length;
  463. sel.moveStart('character', - input.value.length);
  464. return sel.text.length - selLen;
  465. }
  466. }
  467. , highlightUnit: function () {
  468. var input = this.$element.get(0);
  469. this.position = this.getCursorPosition();
  470. if (this.position >= 0 && this.position <= 2) {
  471. this.highlightHour();
  472. } else if (this.position >= 3 && this.position <= 5) {
  473. this.highlightMinute();
  474. } else if (this.position >= 6 && this.position <= 8) {
  475. if (this.showSeconds) {
  476. this.highlightSecond();
  477. } else {
  478. this.highlightMeridian();
  479. }
  480. } else if (this.position >= 9 && this.position <= 11) {
  481. this.highlightMeridian();
  482. }
  483. }
  484. , highlightNextUnit: function() {
  485. switch (this.highlightedUnit) {
  486. case 'hour':
  487. this.highlightMinute();
  488. break;
  489. case 'minute':
  490. if (this.showSeconds) {
  491. this.highlightSecond();
  492. } else {
  493. this.highlightMeridian();
  494. }
  495. break;
  496. case 'second':
  497. this.highlightMeridian();
  498. break;
  499. case 'meridian':
  500. this.highlightHour();
  501. break;
  502. }
  503. }
  504. , highlightPrevUnit: function() {
  505. switch (this.highlightedUnit) {
  506. case 'hour':
  507. this.highlightMeridian();
  508. break;
  509. case 'minute':
  510. this.highlightHour();
  511. break;
  512. case 'second':
  513. this.highlightMinute();
  514. break;
  515. case 'meridian':
  516. if (this.showSeconds) {
  517. this.highlightSecond();
  518. } else {
  519. this.highlightMinute();
  520. }
  521. break;
  522. }
  523. }
  524. , highlightHour: function() {
  525. this.highlightedUnit = 'hour';
  526. this.$element.get(0).setSelectionRange(0,2);
  527. }
  528. , highlightMinute: function() {
  529. this.highlightedUnit = 'minute';
  530. this.$element.get(0).setSelectionRange(3,5);
  531. }
  532. , highlightSecond: function() {
  533. this.highlightedUnit = 'second';
  534. this.$element.get(0).setSelectionRange(6,8);
  535. }
  536. , highlightMeridian: function() {
  537. this.highlightedUnit = 'meridian';
  538. if (this.showSeconds) {
  539. this.$element.get(0).setSelectionRange(9,11);
  540. } else {
  541. this.$element.get(0).setSelectionRange(6,8);
  542. }
  543. }
  544. , incrementHour: function() {
  545. if (this.showMeridian) {
  546. if (this.hour === 11) {
  547. this.toggleMeridian();
  548. } else if (this.hour === 12) {
  549. return this.hour = 1;
  550. }
  551. }
  552. if (this.hour === 23) {
  553. return this.hour = 0;
  554. }
  555. this.hour = this.hour + 1;
  556. }
  557. , decrementHour: function() {
  558. if (this.showMeridian) {
  559. if (this.hour === 1) {
  560. return this.hour = 12;
  561. }
  562. else if (this.hour === 12) {
  563. this.toggleMeridian();
  564. }
  565. }
  566. if (this.hour === 0) {
  567. return this.hour = 23;
  568. }
  569. this.hour = this.hour - 1;
  570. }
  571. , incrementMinute: function() {
  572. var newVal = this.minute + this.minuteStep - (this.minute % this.minuteStep);
  573. if (newVal > 59) {
  574. this.incrementHour();
  575. this.minute = newVal - 60;
  576. } else {
  577. this.minute = newVal;
  578. }
  579. }
  580. , decrementMinute: function() {
  581. var newVal = this.minute - this.minuteStep;
  582. if (newVal < 0) {
  583. this.decrementHour();
  584. this.minute = newVal + 60;
  585. } else {
  586. this.minute = newVal;
  587. }
  588. }
  589. , incrementSecond: function() {
  590. var newVal = this.second + this.secondStep - (this.second % this.secondStep);
  591. if (newVal > 59) {
  592. this.incrementMinute();
  593. this.second = newVal - 60;
  594. } else {
  595. this.second = newVal;
  596. }
  597. }
  598. , decrementSecond: function() {
  599. var newVal = this.second - this.secondStep;
  600. if (newVal < 0) {
  601. this.decrementMinute();
  602. this.second = newVal + 60;
  603. } else {
  604. this.second = newVal;
  605. }
  606. }
  607. , toggleMeridian: function() {
  608. this.meridian = this.meridian === 'AM' ? 'PM' : 'AM';
  609. this.update();
  610. }
  611. , getTemplate: function() {
  612. if (this.options.templates[this.options.template]) {
  613. return this.options.templates[this.options.template];
  614. }
  615. if (this.showInputs) {
  616. var hourTemplate = '<input type="text" name="hour" class="bootstrap-timepicker-hour" maxlength="2"/>';
  617. var minuteTemplate = '<input type="text" name="minute" class="bootstrap-timepicker-minute" maxlength="2"/>';
  618. var secondTemplate = '<input type="text" name="second" class="bootstrap-timepicker-second" maxlength="2"/>';
  619. var meridianTemplate = '<input type="text" name="meridian" class="bootstrap-timepicker-meridian" maxlength="2"/>';
  620. } else {
  621. var hourTemplate = '<span class="bootstrap-timepicker-hour"></span>';
  622. var minuteTemplate = '<span class="bootstrap-timepicker-minute"></span>';
  623. var secondTemplate = '<span class="bootstrap-timepicker-second"></span>';
  624. var meridianTemplate = '<span class="bootstrap-timepicker-meridian"></span>';
  625. }
  626. var templateContent = '<table class="'+ (this.showSeconds ? 'show-seconds' : '') +' '+ (this.showMeridian ? 'show-meridian' : '') +'">'+
  627. '<tr>'+
  628. '<td><a href="#" data-action="incrementHour"><i class="icon-chevron-up"></i></a></td>'+
  629. '<td class="separator">&nbsp;</td>'+
  630. '<td><a href="#" data-action="incrementMinute"><i class="icon-chevron-up"></i></a></td>'+
  631. (this.showSeconds ?
  632. '<td class="separator">&nbsp;</td>'+
  633. '<td><a href="#" data-action="incrementSecond"><i class="icon-chevron-up"></i></a></td>'
  634. : '') +
  635. (this.showMeridian ?
  636. '<td class="separator">&nbsp;</td>'+
  637. '<td class="meridian-column"><a href="#" data-action="toggleMeridian"><i class="icon-chevron-up"></i></a></td>'
  638. : '') +
  639. '</tr>'+
  640. '<tr>'+
  641. '<td>'+ hourTemplate +'</td> '+
  642. '<td class="separator">:</td>'+
  643. '<td>'+ minuteTemplate +'</td> '+
  644. (this.showSeconds ?
  645. '<td class="separator">:</td>'+
  646. '<td>'+ secondTemplate +'</td>'
  647. : '') +
  648. (this.showMeridian ?
  649. '<td class="separator">&nbsp;</td>'+
  650. '<td>'+ meridianTemplate +'</td>'
  651. : '') +
  652. '</tr>'+
  653. '<tr>'+
  654. '<td><a href="#" data-action="decrementHour"><i class="icon-chevron-down"></i></a></td>'+
  655. '<td class="separator"></td>'+
  656. '<td><a href="#" data-action="decrementMinute"><i class="icon-chevron-down"></i></a></td>'+
  657. (this.showSeconds ?
  658. '<td class="separator">&nbsp;</td>'+
  659. '<td><a href="#" data-action="decrementSecond"><i class="icon-chevron-down"></i></a></td>'
  660. : '') +
  661. (this.showMeridian ?
  662. '<td class="separator">&nbsp;</td>'+
  663. '<td><a href="#" data-action="toggleMeridian"><i class="icon-chevron-down"></i></a></td>'
  664. : '') +
  665. '</tr>'+
  666. '</table>';
  667. var template;
  668. switch(this.options.template) {
  669. case 'modal':
  670. template = '<div class="bootstrap-timepicker modal hide fade in" style="top: 30%; margin-top: 0; width: 200px; margin-left: -100px;" data-backdrop="'+ (this.modalBackdrop ? 'true' : 'false') +'">'+
  671. '<div class="modal-header">'+
  672. '<a href="#" class="close" data-dismiss="modal">×</a>'+
  673. '<h3>Pick a Time</h3>'+
  674. '</div>'+
  675. '<div class="modal-content">'+
  676. templateContent +
  677. '</div>'+
  678. '<div class="modal-footer">'+
  679. '<a href="#" class="btn btn-primary" data-dismiss="modal">Ok</a>'+
  680. '</div>'+
  681. '</div>';
  682. break;
  683. case 'dropdown':
  684. template = '<div class="bootstrap-timepicker dropdown-menu">'+
  685. templateContent +
  686. '</div>';
  687. break;
  688. }
  689. return template;
  690. }
  691. };
  692. /* TIMEPICKER PLUGIN DEFINITION
  693. * =========================== */
  694. $.fn.timepicker = function (option) {
  695. return this.each(function () {
  696. var $this = $(this)
  697. , data = $this.data('timepicker')
  698. , options = typeof option == 'object' && option;
  699. if (!data) {
  700. $this.data('timepicker', (data = new Timepicker(this, options)));
  701. }
  702. if (typeof option == 'string') {
  703. data[option]();
  704. }
  705. })
  706. }
  707. $.fn.timepicker.defaults = {
  708. minuteStep: 15
  709. , secondStep: 15
  710. , disableFocus: false
  711. , defaultTime: 'current'
  712. , showSeconds: false
  713. , showInputs: true
  714. , showMeridian: true
  715. , template: 'dropdown'
  716. , modalBackdrop: false
  717. , templates: {} // set custom templates
  718. }
  719. $.fn.timepicker.Constructor = Timepicker
  720. }(window.jQuery);