jquery.timepicker.js 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416
  1. /*!
  2. * jquery-timepicker v1.11.14 - A jQuery timepicker plugin inspired by Google Calendar. It supports both mouse and keyboard navigation.
  3. * Copyright (c) 2015 Jon Thornton - http://jonthornton.github.com/jquery-timepicker/
  4. * License: MIT
  5. */
  6. (function(factory) {
  7. if (
  8. typeof exports === "object" &&
  9. exports &&
  10. typeof module === "object" &&
  11. module &&
  12. module.exports === exports
  13. ) {
  14. // Browserify. Attach to jQuery module.
  15. factory(require("jquery"));
  16. } else if (typeof define === "function" && define.amd) {
  17. // AMD. Register as an anonymous module.
  18. define(["jquery"], factory);
  19. } else {
  20. // Browser globals
  21. factory(jQuery);
  22. }
  23. })(function($) {
  24. var _ONE_DAY = 86400;
  25. var _lang = {
  26. am: "am",
  27. pm: "pm",
  28. AM: "AM",
  29. PM: "PM",
  30. decimal: ".",
  31. mins: "mins",
  32. hr: "hr",
  33. hrs: "hrs"
  34. };
  35. var _DEFAULTS = {
  36. appendTo: "body",
  37. className: null,
  38. closeOnWindowScroll: false,
  39. disableAlphaInput: false,
  40. disableTextInput: false,
  41. disableTimeRanges: [],
  42. disableTouchKeyboard: false,
  43. durationTime: null,
  44. forceRoundTime: false,
  45. maxTime: null,
  46. minTime: null,
  47. noneOption: false,
  48. orientation: "l",
  49. roundingFunction: function(seconds, settings) {
  50. if (seconds === null) {
  51. return null;
  52. } else if (typeof settings.step !== "number") {
  53. // TODO: nearest fit irregular steps
  54. return seconds;
  55. } else {
  56. var offset = seconds % (settings.step * 60); // step is in minutes
  57. var start = settings.minTime || 0;
  58. // adjust offset by start mod step so that the offset is aligned not to 00:00 but to the start
  59. offset -= start % (settings.step * 60);
  60. if (offset >= settings.step * 30) {
  61. // if offset is larger than a half step, round up
  62. seconds += settings.step * 60 - offset;
  63. } else {
  64. // round down
  65. seconds -= offset;
  66. }
  67. return _moduloSeconds(seconds, settings);
  68. }
  69. },
  70. scrollDefault: null,
  71. selectOnBlur: false,
  72. show2400: false,
  73. showDuration: false,
  74. showOn: ["click", "focus"],
  75. showOnFocus: true,
  76. step: 30,
  77. stopScrollPropagation: false,
  78. timeFormat: "g:ia",
  79. typeaheadHighlight: true,
  80. useSelect: false,
  81. wrapHours: true
  82. };
  83. var methods = {
  84. init: function(options) {
  85. return this.each(function() {
  86. var self = $(this);
  87. // pick up settings from data attributes
  88. var attributeOptions = [];
  89. for (var key in _DEFAULTS) {
  90. if (self.data(key)) {
  91. attributeOptions[key] = self.data(key);
  92. }
  93. }
  94. var settings = $.extend({}, _DEFAULTS, options, attributeOptions);
  95. if (settings.lang) {
  96. _lang = $.extend(_lang, settings.lang);
  97. }
  98. settings = _parseSettings(settings);
  99. self.data("timepicker-settings", settings);
  100. self.addClass("ui-timepicker-input");
  101. if (settings.useSelect) {
  102. _render(self);
  103. } else {
  104. self.prop("autocomplete", "off");
  105. if (settings.showOn) {
  106. for (var i in settings.showOn) {
  107. self.on(settings.showOn[i] + ".timepicker", methods.show);
  108. }
  109. }
  110. self.on("change.timepicker", _formatValue);
  111. self.on("keydown.timepicker", _keydownhandler);
  112. self.on("keyup.timepicker", _keyuphandler);
  113. if (settings.disableTextInput) {
  114. self.on("keydown.timepicker", _disableTextInputHandler);
  115. }
  116. if (settings.disableAlphaInput) {
  117. self.on("keydown.timepicker", _disableAlphaInputHandler);
  118. }
  119. self.on("cut.timepicker", _keyuphandler);
  120. self.on("paste.timepicker", _keyuphandler);
  121. _formatValue.call(self.get(0), null, "initial");
  122. }
  123. });
  124. },
  125. show: function(e) {
  126. var self = $(this);
  127. var settings = self.data("timepicker-settings");
  128. if (e) {
  129. e.preventDefault();
  130. }
  131. if (settings.useSelect) {
  132. self.data("timepicker-list").focus();
  133. return;
  134. }
  135. if (_hideKeyboard(self)) {
  136. // block the keyboard on mobile devices
  137. self.blur();
  138. }
  139. var list = self.data("timepicker-list");
  140. // check if input is readonly
  141. if (self.prop("readonly")) {
  142. return;
  143. }
  144. // check if list needs to be rendered
  145. if (
  146. !list ||
  147. list.length === 0 ||
  148. typeof settings.durationTime === "function"
  149. ) {
  150. _render(self);
  151. list = self.data("timepicker-list");
  152. }
  153. if (_isVisible(list)) {
  154. return;
  155. }
  156. self.data("ui-timepicker-value", self.val());
  157. _setSelected(self, list);
  158. // make sure other pickers are hidden
  159. methods.hide();
  160. // position the dropdown relative to the input
  161. list.show();
  162. var listOffset = {};
  163. if (settings.orientation.match(/r/)) {
  164. // right-align the dropdown
  165. listOffset.left =
  166. self.offset().left +
  167. self.outerWidth() -
  168. list.outerWidth() +
  169. parseInt(list.css("marginLeft").replace("px", ""), 10);
  170. } else {
  171. // left-align the dropdown
  172. listOffset.left =
  173. self.offset().left +
  174. parseInt(list.css("marginLeft").replace("px", ""), 10);
  175. }
  176. var verticalOrientation;
  177. if (settings.orientation.match(/t/)) {
  178. verticalOrientation = "t";
  179. } else if (settings.orientation.match(/b/)) {
  180. verticalOrientation = "b";
  181. } else if (
  182. self.offset().top + self.outerHeight(true) + list.outerHeight() >
  183. $(window).height() + $(window).scrollTop()
  184. ) {
  185. verticalOrientation = "t";
  186. } else {
  187. verticalOrientation = "b";
  188. }
  189. if (verticalOrientation == "t") {
  190. // position the dropdown on top
  191. list.addClass("ui-timepicker-positioned-top");
  192. listOffset.top =
  193. self.offset().top -
  194. list.outerHeight() +
  195. parseInt(list.css("marginTop").replace("px", ""), 10);
  196. } else {
  197. // put it under the input
  198. list.removeClass("ui-timepicker-positioned-top");
  199. listOffset.top =
  200. self.offset().top +
  201. self.outerHeight() +
  202. parseInt(list.css("marginTop").replace("px", ""), 10);
  203. }
  204. list.offset(listOffset);
  205. // position scrolling
  206. var selected = list.find(".ui-timepicker-selected");
  207. if (!selected.length) {
  208. var timeInt = _time2int(_getTimeValue(self));
  209. if (timeInt !== null) {
  210. selected = _findRow(self, list, timeInt);
  211. } else if (settings.scrollDefault) {
  212. selected = _findRow(self, list, settings.scrollDefault());
  213. }
  214. }
  215. // if not found or disabled, intelligently find first selectable element
  216. if (!selected.length || selected.hasClass("ui-timepicker-disabled")) {
  217. selected = list.find("li:not(.ui-timepicker-disabled):first");
  218. }
  219. if (selected && selected.length) {
  220. var topOffset =
  221. list.scrollTop() + selected.position().top - selected.outerHeight();
  222. list.scrollTop(topOffset);
  223. } else {
  224. list.scrollTop(0);
  225. }
  226. // prevent scroll propagation
  227. if (settings.stopScrollPropagation) {
  228. $(
  229. document
  230. ).on("wheel.ui-timepicker", ".ui-timepicker-wrapper", function(e) {
  231. e.preventDefault();
  232. var currentScroll = $(this).scrollTop();
  233. $(this).scrollTop(currentScroll + e.originalEvent.deltaY);
  234. });
  235. }
  236. // attach close handlers
  237. $(document).on(
  238. "touchstart.ui-timepicker mousedown.ui-timepicker",
  239. _closeHandler
  240. );
  241. $(window).on("resize.ui-timepicker", _closeHandler);
  242. if (settings.closeOnWindowScroll) {
  243. $(document).on("scroll.ui-timepicker", _closeHandler);
  244. }
  245. self.trigger("showTimepicker");
  246. return this;
  247. },
  248. hide: function(e) {
  249. var self = $(this);
  250. var settings = self.data("timepicker-settings");
  251. if (settings && settings.useSelect) {
  252. self.blur();
  253. }
  254. $(".ui-timepicker-wrapper").each(function() {
  255. var list = $(this);
  256. if (!_isVisible(list)) {
  257. return;
  258. }
  259. var self = list.data("timepicker-input");
  260. var settings = self.data("timepicker-settings");
  261. if (settings && settings.selectOnBlur) {
  262. _selectValue(self);
  263. }
  264. list.hide();
  265. self.trigger("hideTimepicker");
  266. });
  267. return this;
  268. },
  269. option: function(key, value) {
  270. if (typeof key == "string" && typeof value == "undefined") {
  271. return $(this).data("timepicker-settings")[key];
  272. }
  273. return this.each(function() {
  274. var self = $(this);
  275. var settings = self.data("timepicker-settings");
  276. var list = self.data("timepicker-list");
  277. if (typeof key == "object") {
  278. settings = $.extend(settings, key);
  279. } else if (typeof key == "string") {
  280. settings[key] = value;
  281. }
  282. settings = _parseSettings(settings);
  283. self.data("timepicker-settings", settings);
  284. _formatValue.call(self.get(0), { type: "change" }, "initial");
  285. if (list) {
  286. list.remove();
  287. self.data("timepicker-list", false);
  288. }
  289. if (settings.useSelect) {
  290. _render(self);
  291. }
  292. });
  293. },
  294. getSecondsFromMidnight: function() {
  295. return _time2int(_getTimeValue(this));
  296. },
  297. getTime: function(relative_date) {
  298. var self = this;
  299. var time_string = _getTimeValue(self);
  300. if (!time_string) {
  301. return null;
  302. }
  303. var offset = _time2int(time_string);
  304. if (offset === null) {
  305. return null;
  306. }
  307. if (!relative_date) {
  308. relative_date = new Date();
  309. }
  310. // construct a Date from relative date, and offset's time
  311. var time = new Date(relative_date);
  312. time.setHours(offset / 3600);
  313. time.setMinutes((offset % 3600) / 60);
  314. time.setSeconds(offset % 60);
  315. time.setMilliseconds(0);
  316. return time;
  317. },
  318. isVisible: function() {
  319. var self = this;
  320. var list = self.data("timepicker-list");
  321. return !!(list && _isVisible(list));
  322. },
  323. setTime: function(value) {
  324. var self = this;
  325. var settings = self.data("timepicker-settings");
  326. if (settings.forceRoundTime) {
  327. var prettyTime = _roundAndFormatTime(_time2int(value), settings);
  328. } else {
  329. var prettyTime = _int2time(_time2int(value), settings);
  330. }
  331. if (value && prettyTime === null && settings.noneOption) {
  332. prettyTime = value;
  333. }
  334. _setTimeValue(self, prettyTime, "initial");
  335. _formatValue.call(self.get(0), { type: "change" }, "initial");
  336. if (self.data("timepicker-list")) {
  337. _setSelected(self, self.data("timepicker-list"));
  338. }
  339. return this;
  340. },
  341. remove: function() {
  342. var self = this;
  343. // check if this element is a timepicker
  344. if (!self.hasClass("ui-timepicker-input")) {
  345. return;
  346. }
  347. var settings = self.data("timepicker-settings");
  348. self.removeAttr("autocomplete", "off");
  349. self.removeClass("ui-timepicker-input");
  350. self.removeData("timepicker-settings");
  351. self.off(".timepicker");
  352. // timepicker-list won't be present unless the user has interacted with this timepicker
  353. if (self.data("timepicker-list")) {
  354. self.data("timepicker-list").remove();
  355. }
  356. if (settings.useSelect) {
  357. self.show();
  358. }
  359. self.removeData("timepicker-list");
  360. return this;
  361. }
  362. };
  363. // private methods
  364. function _isVisible(elem) {
  365. var el = elem[0];
  366. return el.offsetWidth > 0 && el.offsetHeight > 0;
  367. }
  368. function _parseSettings(settings) {
  369. if (settings.minTime) {
  370. settings.minTime = _time2int(settings.minTime);
  371. }
  372. if (settings.maxTime) {
  373. settings.maxTime = _time2int(settings.maxTime);
  374. }
  375. if (settings.durationTime && typeof settings.durationTime !== "function") {
  376. settings.durationTime = _time2int(settings.durationTime);
  377. }
  378. if (settings.scrollDefault == "now") {
  379. settings.scrollDefault = function() {
  380. return settings.roundingFunction(_time2int(new Date()), settings);
  381. };
  382. } else if (
  383. settings.scrollDefault &&
  384. typeof settings.scrollDefault != "function"
  385. ) {
  386. var val = settings.scrollDefault;
  387. settings.scrollDefault = function() {
  388. return settings.roundingFunction(_time2int(val), settings);
  389. };
  390. } else if (settings.minTime) {
  391. settings.scrollDefault = function() {
  392. return settings.roundingFunction(settings.minTime, settings);
  393. };
  394. }
  395. if (
  396. $.type(settings.timeFormat) === "string" &&
  397. settings.timeFormat.match(/[gh]/)
  398. ) {
  399. settings._twelveHourTime = true;
  400. }
  401. if (
  402. settings.showOnFocus === false &&
  403. settings.showOn.indexOf("focus") != -1
  404. ) {
  405. settings.showOn.splice(settings.showOn.indexOf("focus"), 1);
  406. }
  407. if (settings.disableTimeRanges.length > 0) {
  408. // convert string times to integers
  409. for (var i in settings.disableTimeRanges) {
  410. settings.disableTimeRanges[i] = [
  411. _time2int(settings.disableTimeRanges[i][0]),
  412. _time2int(settings.disableTimeRanges[i][1])
  413. ];
  414. }
  415. // sort by starting time
  416. settings.disableTimeRanges = settings.disableTimeRanges.sort(function(
  417. a,
  418. b
  419. ) {
  420. return a[0] - b[0];
  421. });
  422. // merge any overlapping ranges
  423. for (var i = settings.disableTimeRanges.length - 1; i > 0; i--) {
  424. if (
  425. settings.disableTimeRanges[i][0] <=
  426. settings.disableTimeRanges[i - 1][1]
  427. ) {
  428. settings.disableTimeRanges[i - 1] = [
  429. Math.min(
  430. settings.disableTimeRanges[i][0],
  431. settings.disableTimeRanges[i - 1][0]
  432. ),
  433. Math.max(
  434. settings.disableTimeRanges[i][1],
  435. settings.disableTimeRanges[i - 1][1]
  436. )
  437. ];
  438. settings.disableTimeRanges.splice(i, 1);
  439. }
  440. }
  441. }
  442. return settings;
  443. }
  444. function _render(self) {
  445. var settings = self.data("timepicker-settings");
  446. var list = self.data("timepicker-list");
  447. if (list && list.length) {
  448. list.remove();
  449. self.data("timepicker-list", false);
  450. }
  451. if (settings.useSelect) {
  452. list = $("<select />", { class: "ui-timepicker-select" });
  453. if (self.attr('name')) {
  454. list.attr('name', 'ui-timepicker-' + self.attr('name'));
  455. }
  456. var wrapped_list = list;
  457. } else {
  458. list = $("<ul />", { class: "ui-timepicker-list" });
  459. var wrapped_list = $("<div />", {
  460. class: "ui-timepicker-wrapper",
  461. tabindex: -1
  462. });
  463. wrapped_list.css({ display: "none", position: "absolute" }).append(list);
  464. }
  465. if (settings.noneOption) {
  466. if (settings.noneOption === true) {
  467. settings.noneOption = settings.useSelect ? "Time..." : "None";
  468. }
  469. if ($.isArray(settings.noneOption)) {
  470. for (var i in settings.noneOption) {
  471. if (parseInt(i, 10) == i) {
  472. var noneElement = _generateNoneElement(
  473. settings.noneOption[i],
  474. settings.useSelect
  475. );
  476. list.append(noneElement);
  477. }
  478. }
  479. } else {
  480. var noneElement = _generateNoneElement(
  481. settings.noneOption,
  482. settings.useSelect
  483. );
  484. list.append(noneElement);
  485. }
  486. }
  487. if (settings.className) {
  488. wrapped_list.addClass(settings.className);
  489. }
  490. if (
  491. (settings.minTime !== null || settings.durationTime !== null) &&
  492. settings.showDuration
  493. ) {
  494. var stepval =
  495. typeof settings.step == "function" ? "function" : settings.step;
  496. wrapped_list.addClass("ui-timepicker-with-duration");
  497. wrapped_list.addClass("ui-timepicker-step-" + settings.step);
  498. }
  499. var durStart = settings.minTime;
  500. if (typeof settings.durationTime === "function") {
  501. durStart = _time2int(settings.durationTime());
  502. } else if (settings.durationTime !== null) {
  503. durStart = settings.durationTime;
  504. }
  505. var start = settings.minTime !== null ? settings.minTime : 0;
  506. var end =
  507. settings.maxTime !== null ? settings.maxTime : start + _ONE_DAY - 1;
  508. if (end < start) {
  509. // make sure the end time is greater than start time, otherwise there will be no list to show
  510. end += _ONE_DAY;
  511. }
  512. if (
  513. end === _ONE_DAY - 1 &&
  514. $.type(settings.timeFormat) === "string" &&
  515. settings.show2400
  516. ) {
  517. // show a 24:00 option when using military time
  518. end = _ONE_DAY;
  519. }
  520. var dr = settings.disableTimeRanges;
  521. var drCur = 0;
  522. var drLen = dr.length;
  523. var stepFunc = settings.step;
  524. if (typeof stepFunc != "function") {
  525. stepFunc = function() {
  526. return settings.step;
  527. };
  528. }
  529. for (var i = start, j = 0; i <= end; j++, i += stepFunc(j) * 60) {
  530. var timeInt = i;
  531. var timeString = _int2time(timeInt, settings);
  532. if (settings.useSelect) {
  533. var row = $("<option />", { value: timeString });
  534. row.text(timeString);
  535. } else {
  536. var row = $("<li />");
  537. row.addClass(
  538. timeInt % _ONE_DAY < _ONE_DAY / 2
  539. ? "ui-timepicker-am"
  540. : "ui-timepicker-pm"
  541. );
  542. row.data("time", _moduloSeconds(timeInt, settings));
  543. row.text(timeString);
  544. }
  545. if (
  546. (settings.minTime !== null || settings.durationTime !== null) &&
  547. settings.showDuration
  548. ) {
  549. var durationString = _int2duration(i - durStart, settings.step);
  550. if (settings.useSelect) {
  551. row.text(row.text() + " (" + durationString + ")");
  552. } else {
  553. var duration = $("<span />", { class: "ui-timepicker-duration" });
  554. duration.text(" (" + durationString + ")");
  555. row.append(duration);
  556. }
  557. }
  558. if (drCur < drLen) {
  559. if (timeInt >= dr[drCur][1]) {
  560. drCur += 1;
  561. }
  562. if (dr[drCur] && timeInt >= dr[drCur][0] && timeInt < dr[drCur][1]) {
  563. if (settings.useSelect) {
  564. row.prop("disabled", true);
  565. } else {
  566. row.addClass("ui-timepicker-disabled");
  567. }
  568. }
  569. }
  570. list.append(row);
  571. }
  572. wrapped_list.data("timepicker-input", self);
  573. self.data("timepicker-list", wrapped_list);
  574. if (settings.useSelect) {
  575. if (self.val()) {
  576. list.val(_roundAndFormatTime(_time2int(self.val()), settings));
  577. }
  578. list.on("focus", function() {
  579. $(this)
  580. .data("timepicker-input")
  581. .trigger("showTimepicker");
  582. });
  583. list.on("blur", function() {
  584. $(this)
  585. .data("timepicker-input")
  586. .trigger("hideTimepicker");
  587. });
  588. list.on("change", function() {
  589. _setTimeValue(self, $(this).val(), "select");
  590. });
  591. _setTimeValue(self, list.val(), "initial");
  592. self.hide().after(list);
  593. } else {
  594. var appendTo = settings.appendTo;
  595. if (typeof appendTo === "string") {
  596. appendTo = $(appendTo);
  597. } else if (typeof appendTo === "function") {
  598. appendTo = appendTo(self);
  599. }
  600. appendTo.append(wrapped_list);
  601. _setSelected(self, list);
  602. list.on("mousedown click", "li", function(e) {
  603. // hack: temporarily disable the focus handler
  604. // to deal with the fact that IE fires 'focus'
  605. // events asynchronously
  606. self.off("focus.timepicker");
  607. self.on("focus.timepicker-ie-hack", function() {
  608. self.off("focus.timepicker-ie-hack");
  609. self.on("focus.timepicker", methods.show);
  610. });
  611. if (!_hideKeyboard(self)) {
  612. self[0].focus();
  613. }
  614. // make sure only the clicked row is selected
  615. list.find("li").removeClass("ui-timepicker-selected");
  616. $(this).addClass("ui-timepicker-selected");
  617. if (_selectValue(self)) {
  618. self.trigger("hideTimepicker");
  619. list.on("mouseup.timepicker click.timepicker", "li", function(e) {
  620. list.off("mouseup.timepicker click.timepicker");
  621. wrapped_list.hide();
  622. });
  623. }
  624. });
  625. }
  626. }
  627. function _generateNoneElement(optionValue, useSelect) {
  628. var label, className, value;
  629. if (typeof optionValue == "object") {
  630. label = optionValue.label;
  631. className = optionValue.className;
  632. value = optionValue.value;
  633. } else if (typeof optionValue == "string") {
  634. label = optionValue;
  635. value = '';
  636. } else {
  637. $.error("Invalid noneOption value");
  638. }
  639. if (useSelect) {
  640. return $("<option />", {
  641. value: value,
  642. class: className,
  643. text: label
  644. });
  645. } else {
  646. return $("<li />", {
  647. class: className,
  648. text: label
  649. }).data("time", String(value));
  650. }
  651. }
  652. function _roundAndFormatTime(seconds, settings) {
  653. seconds = settings.roundingFunction(seconds, settings);
  654. if (seconds !== null) {
  655. return _int2time(seconds, settings);
  656. }
  657. }
  658. // event handler to decide whether to close timepicker
  659. function _closeHandler(e) {
  660. if (e.target == window) {
  661. // mobile Chrome fires focus events against window for some reason
  662. return;
  663. }
  664. var target = $(e.target);
  665. if (
  666. target.closest(".ui-timepicker-input").length ||
  667. target.closest(".ui-timepicker-wrapper").length
  668. ) {
  669. // active timepicker was focused. ignore
  670. return;
  671. }
  672. methods.hide();
  673. $(document).unbind(".ui-timepicker");
  674. $(window).unbind(".ui-timepicker");
  675. }
  676. function _hideKeyboard(self) {
  677. var settings = self.data("timepicker-settings");
  678. return (
  679. (window.navigator.msMaxTouchPoints || "ontouchstart" in document) &&
  680. settings.disableTouchKeyboard
  681. );
  682. }
  683. function _findRow(self, list, value) {
  684. if (!value && value !== 0) {
  685. return false;
  686. }
  687. var settings = self.data("timepicker-settings");
  688. var out = false;
  689. var value = settings.roundingFunction(value, settings);
  690. // loop through the menu items
  691. list.find("li").each(function(i, obj) {
  692. var jObj = $(obj);
  693. if (typeof jObj.data("time") != "number") {
  694. return;
  695. }
  696. if (jObj.data("time") == value) {
  697. out = jObj;
  698. return false;
  699. }
  700. });
  701. return out;
  702. }
  703. function _setSelected(self, list) {
  704. list.find("li").removeClass("ui-timepicker-selected");
  705. var settings = self.data("timepicker-settings");
  706. var timeValue = _time2int(_getTimeValue(self), settings);
  707. if (timeValue === null) {
  708. return;
  709. }
  710. var selected = _findRow(self, list, timeValue);
  711. if (selected) {
  712. var topDelta = selected.offset().top - list.offset().top;
  713. if (
  714. topDelta + selected.outerHeight() > list.outerHeight() ||
  715. topDelta < 0
  716. ) {
  717. list.scrollTop(
  718. list.scrollTop() + selected.position().top - selected.outerHeight()
  719. );
  720. }
  721. if (settings.forceRoundTime || selected.data("time") === timeValue) {
  722. selected.addClass("ui-timepicker-selected");
  723. }
  724. }
  725. }
  726. function _formatValue(e, origin) {
  727. if (origin == "timepicker") {
  728. return;
  729. }
  730. var self = $(this);
  731. if (this.value === "") {
  732. _setTimeValue(self, null, origin);
  733. return;
  734. }
  735. if (self.is(":focus") && (!e || e.type != "change")) {
  736. return;
  737. }
  738. var settings = self.data("timepicker-settings");
  739. var seconds = _time2int(this.value, settings);
  740. if (seconds === null) {
  741. self.trigger("timeFormatError");
  742. return;
  743. }
  744. var rangeError = false;
  745. // check that the time in within bounds
  746. if (
  747. settings.minTime !== null &&
  748. settings.maxTime !== null &&
  749. (seconds < settings.minTime || seconds > settings.maxTime)
  750. ) {
  751. rangeError = true;
  752. }
  753. // check that time isn't within disabled time ranges
  754. $.each(settings.disableTimeRanges, function() {
  755. if (seconds >= this[0] && seconds < this[1]) {
  756. rangeError = true;
  757. return false;
  758. }
  759. });
  760. if (settings.forceRoundTime) {
  761. var roundSeconds = settings.roundingFunction(seconds, settings);
  762. if (roundSeconds != seconds) {
  763. seconds = roundSeconds;
  764. origin = null;
  765. }
  766. }
  767. var prettyTime = _int2time(seconds, settings);
  768. if (rangeError) {
  769. if (
  770. _setTimeValue(self, prettyTime, "error") ||
  771. (e && e.type == "change")
  772. ) {
  773. self.trigger("timeRangeError");
  774. }
  775. } else {
  776. _setTimeValue(self, prettyTime, origin);
  777. }
  778. }
  779. function _getTimeValue(self) {
  780. if (self.is("input")) {
  781. return self.val();
  782. } else {
  783. // use the element's data attributes to store values
  784. return self.data("ui-timepicker-value");
  785. }
  786. }
  787. function _setTimeValue(self, value, source) {
  788. if (self.is("input")) {
  789. self.val(value);
  790. var settings = self.data("timepicker-settings");
  791. if (settings.useSelect && source != "select" && self.data("timepicker-list")) {
  792. self
  793. .data("timepicker-list")
  794. .val(_roundAndFormatTime(_time2int(value), settings));
  795. }
  796. }
  797. if (self.data("ui-timepicker-value") != value) {
  798. self.data("ui-timepicker-value", value);
  799. if (source == "select") {
  800. self
  801. .trigger("selectTime")
  802. .trigger("changeTime")
  803. .trigger("change", "timepicker");
  804. } else if (["error", "initial"].indexOf(source) == -1) {
  805. self.trigger("changeTime");
  806. }
  807. return true;
  808. } else {
  809. if (["error", "initial"].indexOf(source) == -1) {
  810. self.trigger("selectTime");
  811. }
  812. return false;
  813. }
  814. }
  815. /*
  816. * Filter freeform input
  817. */
  818. function _disableTextInputHandler(e) {
  819. switch (e.keyCode) {
  820. case 13: // return
  821. case 9: //tab
  822. return;
  823. default:
  824. e.preventDefault();
  825. }
  826. }
  827. /*
  828. * Filter validate format hh:mm input
  829. */
  830. function _disableAlphaInputHandler(e) {
  831. switch (e.keyCode) {
  832. case 13: // return
  833. case 8: //back
  834. case 9: //tab
  835. case 96: // 0
  836. case 97: // 1
  837. case 98: // 2
  838. case 99: // 3
  839. case 100: // 4
  840. case 101: // 5
  841. case 102: // 6
  842. case 103: // 7
  843. case 104: // 8
  844. case 105: // 9
  845. case 58: // :
  846. return;
  847. default:
  848. e.preventDefault();
  849. }
  850. }
  851. /*
  852. * Keyboard navigation via arrow keys
  853. */
  854. function _keydownhandler(e) {
  855. var self = $(this);
  856. var list = self.data("timepicker-list");
  857. if (!list || !_isVisible(list)) {
  858. if (e.keyCode == 40) {
  859. // show the list!
  860. methods.show.call(self.get(0));
  861. list = self.data("timepicker-list");
  862. if (!_hideKeyboard(self)) {
  863. self.focus();
  864. }
  865. } else {
  866. return true;
  867. }
  868. }
  869. switch (e.keyCode) {
  870. case 13: // return
  871. if (_selectValue(self)) {
  872. _formatValue.call(self.get(0), { type: "change" });
  873. methods.hide.apply(this);
  874. }
  875. e.preventDefault();
  876. return false;
  877. case 38: // up
  878. var selected = list.find(".ui-timepicker-selected");
  879. if (!selected.length) {
  880. list.find("li").each(function(i, obj) {
  881. if ($(obj).position().top > 0) {
  882. selected = $(obj);
  883. return false;
  884. }
  885. });
  886. selected.addClass("ui-timepicker-selected");
  887. } else if (!selected.is(":first-child")) {
  888. selected.removeClass("ui-timepicker-selected");
  889. selected.prev().addClass("ui-timepicker-selected");
  890. if (selected.prev().position().top < selected.outerHeight()) {
  891. list.scrollTop(list.scrollTop() - selected.outerHeight());
  892. }
  893. }
  894. return false;
  895. case 40: // down
  896. selected = list.find(".ui-timepicker-selected");
  897. if (selected.length === 0) {
  898. list.find("li").each(function(i, obj) {
  899. if ($(obj).position().top > 0) {
  900. selected = $(obj);
  901. return false;
  902. }
  903. });
  904. selected.addClass("ui-timepicker-selected");
  905. } else if (!selected.is(":last-child")) {
  906. selected.removeClass("ui-timepicker-selected");
  907. selected.next().addClass("ui-timepicker-selected");
  908. if (
  909. selected.next().position().top + 2 * selected.outerHeight() >
  910. list.outerHeight()
  911. ) {
  912. list.scrollTop(list.scrollTop() + selected.outerHeight());
  913. }
  914. }
  915. return false;
  916. case 27: // escape
  917. list.find("li").removeClass("ui-timepicker-selected");
  918. methods.hide();
  919. break;
  920. case 9: //tab
  921. methods.hide();
  922. break;
  923. default:
  924. return true;
  925. }
  926. }
  927. /*
  928. * Time typeahead
  929. */
  930. function _keyuphandler(e) {
  931. var self = $(this);
  932. var list = self.data("timepicker-list");
  933. var settings = self.data("timepicker-settings");
  934. if (!list || !_isVisible(list) || settings.disableTextInput) {
  935. return true;
  936. }
  937. if (e.type === "paste" || e.type === "cut") {
  938. setTimeout(function() {
  939. if (settings.typeaheadHighlight) {
  940. _setSelected(self, list);
  941. } else {
  942. list.hide();
  943. }
  944. }, 0);
  945. return;
  946. }
  947. switch (e.keyCode) {
  948. case 96: // numpad numerals
  949. case 97:
  950. case 98:
  951. case 99:
  952. case 100:
  953. case 101:
  954. case 102:
  955. case 103:
  956. case 104:
  957. case 105:
  958. case 48: // numerals
  959. case 49:
  960. case 50:
  961. case 51:
  962. case 52:
  963. case 53:
  964. case 54:
  965. case 55:
  966. case 56:
  967. case 57:
  968. case 65: // a
  969. case 77: // m
  970. case 80: // p
  971. case 186: // colon
  972. case 8: // backspace
  973. case 46: // delete
  974. if (settings.typeaheadHighlight) {
  975. _setSelected(self, list);
  976. } else {
  977. list.hide();
  978. }
  979. break;
  980. }
  981. }
  982. function _selectValue(self) {
  983. var settings = self.data("timepicker-settings");
  984. var list = self.data("timepicker-list");
  985. var timeValue = null;
  986. var cursor = list.find(".ui-timepicker-selected");
  987. if (cursor.hasClass("ui-timepicker-disabled")) {
  988. return false;
  989. }
  990. if (cursor.length) {
  991. // selected value found
  992. timeValue = cursor.data("time");
  993. }
  994. if (timeValue !== null) {
  995. if (typeof timeValue != "string") {
  996. timeValue = _int2time(timeValue, settings);
  997. }
  998. _setTimeValue(self, timeValue, "select");
  999. }
  1000. return true;
  1001. }
  1002. function _int2duration(seconds, step) {
  1003. seconds = Math.abs(seconds);
  1004. var minutes = Math.round(seconds / 60),
  1005. duration = [],
  1006. hours,
  1007. mins;
  1008. if (minutes < 60) {
  1009. // Only show (x mins) under 1 hour
  1010. duration = [minutes, _lang.mins];
  1011. } else {
  1012. hours = Math.floor(minutes / 60);
  1013. mins = minutes % 60;
  1014. // Show decimal notation (eg: 1.5 hrs) for 30 minute steps
  1015. if (step == 30 && mins == 30) {
  1016. hours += _lang.decimal + 5;
  1017. }
  1018. duration.push(hours);
  1019. duration.push(hours == 1 ? _lang.hr : _lang.hrs);
  1020. // Show remainder minutes notation (eg: 1 hr 15 mins) for non-30 minute steps
  1021. // and only if there are remainder minutes to show
  1022. if (step != 30 && mins) {
  1023. duration.push(mins);
  1024. duration.push(_lang.mins);
  1025. }
  1026. }
  1027. return duration.join(" ");
  1028. }
  1029. function _int2time(timeInt, settings) {
  1030. if (typeof timeInt != "number") {
  1031. return null;
  1032. }
  1033. var seconds = parseInt(timeInt % 60),
  1034. minutes = parseInt((timeInt / 60) % 60),
  1035. hours = parseInt((timeInt / (60 * 60)) % 24);
  1036. var time = new Date(1970, 0, 2, hours, minutes, seconds, 0);
  1037. if (isNaN(time.getTime())) {
  1038. return null;
  1039. }
  1040. if ($.type(settings.timeFormat) === "function") {
  1041. return settings.timeFormat(time);
  1042. }
  1043. var output = "";
  1044. var hour, code;
  1045. for (var i = 0; i < settings.timeFormat.length; i++) {
  1046. code = settings.timeFormat.charAt(i);
  1047. switch (code) {
  1048. case "a":
  1049. output += time.getHours() > 11 ? _lang.pm : _lang.am;
  1050. break;
  1051. case "A":
  1052. output += time.getHours() > 11 ? _lang.PM : _lang.AM;
  1053. break;
  1054. case "g":
  1055. hour = time.getHours() % 12;
  1056. output += hour === 0 ? "12" : hour;
  1057. break;
  1058. case "G":
  1059. hour = time.getHours();
  1060. if (timeInt === _ONE_DAY) hour = settings.show2400 ? 24 : 0;
  1061. output += hour;
  1062. break;
  1063. case "h":
  1064. hour = time.getHours() % 12;
  1065. if (hour !== 0 && hour < 10) {
  1066. hour = "0" + hour;
  1067. }
  1068. output += hour === 0 ? "12" : hour;
  1069. break;
  1070. case "H":
  1071. hour = time.getHours();
  1072. if (timeInt === _ONE_DAY) hour = settings.show2400 ? 24 : 0;
  1073. output += hour > 9 ? hour : "0" + hour;
  1074. break;
  1075. case "i":
  1076. var minutes = time.getMinutes();
  1077. output += minutes > 9 ? minutes : "0" + minutes;
  1078. break;
  1079. case "s":
  1080. seconds = time.getSeconds();
  1081. output += seconds > 9 ? seconds : "0" + seconds;
  1082. break;
  1083. case "\\":
  1084. // escape character; add the next character and skip ahead
  1085. i++;
  1086. output += settings.timeFormat.charAt(i);
  1087. break;
  1088. default:
  1089. output += code;
  1090. }
  1091. }
  1092. return output;
  1093. }
  1094. function _time2int(timeString, settings) {
  1095. if (timeString === "" || timeString === null) return null;
  1096. if (typeof timeString == "object") {
  1097. return (
  1098. timeString.getHours() * 3600 +
  1099. timeString.getMinutes() * 60 +
  1100. timeString.getSeconds()
  1101. );
  1102. }
  1103. if (typeof timeString != "string") {
  1104. return timeString;
  1105. }
  1106. timeString = timeString.toLowerCase().replace(/[\s\.]/g, "");
  1107. // if the last character is an "a" or "p", add the "m"
  1108. if (timeString.slice(-1) == "a" || timeString.slice(-1) == "p") {
  1109. timeString += "m";
  1110. }
  1111. var ampmRegex =
  1112. "(" +
  1113. _lang.am.replace(".", "") +
  1114. "|" +
  1115. _lang.pm.replace(".", "") +
  1116. "|" +
  1117. _lang.AM.replace(".", "") +
  1118. "|" +
  1119. _lang.PM.replace(".", "") +
  1120. ")?";
  1121. // try to parse time input
  1122. var pattern = new RegExp(
  1123. "^" +
  1124. ampmRegex +
  1125. "([0-9]?[0-9])\\W?([0-5][0-9])?\\W?([0-5][0-9])?" +
  1126. ampmRegex +
  1127. "$"
  1128. );
  1129. var time = timeString.match(pattern);
  1130. if (!time) {
  1131. return null;
  1132. }
  1133. var hour = parseInt(time[2] * 1, 10);
  1134. var ampm = time[1] || time[5];
  1135. var hours = hour;
  1136. var minutes = time[3] * 1 || 0;
  1137. var seconds = time[4] * 1 || 0;
  1138. if (hour <= 12 && ampm) {
  1139. var isPm = ampm == _lang.pm || ampm == _lang.PM;
  1140. if (hour == 12) {
  1141. hours = isPm ? 12 : 0;
  1142. } else {
  1143. hours = hour + (isPm ? 12 : 0);
  1144. }
  1145. } else if (settings) {
  1146. var t = hour * 3600 + minutes * 60 + seconds;
  1147. if (t >= _ONE_DAY + (settings.show2400 ? 1 : 0)) {
  1148. if (settings.wrapHours === false) {
  1149. return null;
  1150. }
  1151. hours = hour % 24;
  1152. }
  1153. }
  1154. var timeInt = hours * 3600 + minutes * 60 + seconds;
  1155. // if no am/pm provided, intelligently guess based on the scrollDefault
  1156. if (
  1157. hour < 12 &&
  1158. !ampm &&
  1159. settings &&
  1160. settings._twelveHourTime &&
  1161. settings.scrollDefault
  1162. ) {
  1163. var delta = timeInt - settings.scrollDefault();
  1164. if (delta < 0 && delta >= _ONE_DAY / -2) {
  1165. timeInt = (timeInt + _ONE_DAY / 2) % _ONE_DAY;
  1166. }
  1167. }
  1168. return timeInt;
  1169. }
  1170. function _pad2(n) {
  1171. return ("0" + n).slice(-2);
  1172. }
  1173. function _moduloSeconds(seconds, settings) {
  1174. if (seconds == _ONE_DAY && settings.show2400) {
  1175. return seconds;
  1176. }
  1177. return seconds % _ONE_DAY;
  1178. }
  1179. // Plugin entry
  1180. $.fn.timepicker = function(method) {
  1181. if (!this.length) return this;
  1182. if (methods[method]) {
  1183. // check if this element is a timepicker
  1184. if (!this.hasClass("ui-timepicker-input")) {
  1185. return this;
  1186. }
  1187. return methods[method].apply(
  1188. this,
  1189. Array.prototype.slice.call(arguments, 1)
  1190. );
  1191. } else if (typeof method === "object" || !method) {
  1192. return methods.init.apply(this, arguments);
  1193. } else {
  1194. $.error("Method " + method + " does not exist on jQuery.timepicker");
  1195. }
  1196. };
  1197. });