minMaxTimePlugin.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. (function (global, factory) {
  2. typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
  3. typeof define === 'function' && define.amd ? define(factory) :
  4. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.minMaxTimePlugin = factory());
  5. }(this, (function () { 'use strict';
  6. var pad = function (number, length) {
  7. if (length === void 0) { length = 2; }
  8. return ("000" + number).slice(length * -1);
  9. };
  10. var int = function (bool) { return (bool === true ? 1 : 0); };
  11. var monthToStr = function (monthNumber, shorthand, locale) { return locale.months[shorthand ? "shorthand" : "longhand"][monthNumber]; };
  12. var formats = {
  13. // get the date in UTC
  14. Z: function (date) { return date.toISOString(); },
  15. // weekday name, short, e.g. Thu
  16. D: function (date, locale, options) {
  17. return locale.weekdays.shorthand[formats.w(date, locale, options)];
  18. },
  19. // full month name e.g. January
  20. F: function (date, locale, options) {
  21. return monthToStr(formats.n(date, locale, options) - 1, false, locale);
  22. },
  23. // padded hour 1-12
  24. G: function (date, locale, options) {
  25. return pad(formats.h(date, locale, options));
  26. },
  27. // hours with leading zero e.g. 03
  28. H: function (date) { return pad(date.getHours()); },
  29. // day (1-30) with ordinal suffix e.g. 1st, 2nd
  30. J: function (date, locale) {
  31. return locale.ordinal !== undefined
  32. ? date.getDate() + locale.ordinal(date.getDate())
  33. : date.getDate();
  34. },
  35. // AM/PM
  36. K: function (date, locale) { return locale.amPM[int(date.getHours() > 11)]; },
  37. // shorthand month e.g. Jan, Sep, Oct, etc
  38. M: function (date, locale) {
  39. return monthToStr(date.getMonth(), true, locale);
  40. },
  41. // seconds 00-59
  42. S: function (date) { return pad(date.getSeconds()); },
  43. // unix timestamp
  44. U: function (date) { return date.getTime() / 1000; },
  45. W: function (date, _, options) {
  46. return options.getWeek(date);
  47. },
  48. // full year e.g. 2016, padded (0001-9999)
  49. Y: function (date) { return pad(date.getFullYear(), 4); },
  50. // day in month, padded (01-30)
  51. d: function (date) { return pad(date.getDate()); },
  52. // hour from 1-12 (am/pm)
  53. h: function (date) { return (date.getHours() % 12 ? date.getHours() % 12 : 12); },
  54. // minutes, padded with leading zero e.g. 09
  55. i: function (date) { return pad(date.getMinutes()); },
  56. // day in month (1-30)
  57. j: function (date) { return date.getDate(); },
  58. // weekday name, full, e.g. Thursday
  59. l: function (date, locale) {
  60. return locale.weekdays.longhand[date.getDay()];
  61. },
  62. // padded month number (01-12)
  63. m: function (date) { return pad(date.getMonth() + 1); },
  64. // the month number (1-12)
  65. n: function (date) { return date.getMonth() + 1; },
  66. // seconds 0-59
  67. s: function (date) { return date.getSeconds(); },
  68. // Unix Milliseconds
  69. u: function (date) { return date.getTime(); },
  70. // number of the day of the week
  71. w: function (date) { return date.getDay(); },
  72. // last two digits of year e.g. 16 for 2016
  73. y: function (date) { return String(date.getFullYear()).substring(2); },
  74. };
  75. var defaults = {
  76. _disable: [],
  77. allowInput: false,
  78. allowInvalidPreload: false,
  79. altFormat: "F j, Y",
  80. altInput: false,
  81. altInputClass: "form-control input",
  82. animate: typeof window === "object" &&
  83. window.navigator.userAgent.indexOf("MSIE") === -1,
  84. ariaDateFormat: "F j, Y",
  85. autoFillDefaultTime: true,
  86. clickOpens: true,
  87. closeOnSelect: true,
  88. conjunction: ", ",
  89. dateFormat: "Y-m-d",
  90. defaultHour: 12,
  91. defaultMinute: 0,
  92. defaultSeconds: 0,
  93. disable: [],
  94. disableMobile: false,
  95. enableSeconds: false,
  96. enableTime: false,
  97. errorHandler: function (err) {
  98. return typeof console !== "undefined" && console.warn(err);
  99. },
  100. getWeek: function (givenDate) {
  101. var date = new Date(givenDate.getTime());
  102. date.setHours(0, 0, 0, 0);
  103. // Thursday in current week decides the year.
  104. date.setDate(date.getDate() + 3 - ((date.getDay() + 6) % 7));
  105. // January 4 is always in week 1.
  106. var week1 = new Date(date.getFullYear(), 0, 4);
  107. // Adjust to Thursday in week 1 and count number of weeks from date to week1.
  108. return (1 +
  109. Math.round(((date.getTime() - week1.getTime()) / 86400000 -
  110. 3 +
  111. ((week1.getDay() + 6) % 7)) /
  112. 7));
  113. },
  114. hourIncrement: 1,
  115. ignoredFocusElements: [],
  116. inline: false,
  117. locale: "default",
  118. minuteIncrement: 5,
  119. mode: "single",
  120. monthSelectorType: "dropdown",
  121. nextArrow: "<svg version='1.1' xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' viewBox='0 0 17 17'><g></g><path d='M13.207 8.472l-7.854 7.854-0.707-0.707 7.146-7.146-7.146-7.148 0.707-0.707 7.854 7.854z' /></svg>",
  122. noCalendar: false,
  123. now: new Date(),
  124. onChange: [],
  125. onClose: [],
  126. onDayCreate: [],
  127. onDestroy: [],
  128. onKeyDown: [],
  129. onMonthChange: [],
  130. onOpen: [],
  131. onParseConfig: [],
  132. onReady: [],
  133. onValueUpdate: [],
  134. onYearChange: [],
  135. onPreCalendarPosition: [],
  136. plugins: [],
  137. position: "auto",
  138. positionElement: undefined,
  139. prevArrow: "<svg version='1.1' xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' viewBox='0 0 17 17'><g></g><path d='M5.207 8.471l7.146 7.147-0.707 0.707-7.853-7.854 7.854-7.853 0.707 0.707-7.147 7.146z' /></svg>",
  140. shorthandCurrentMonth: false,
  141. showMonths: 1,
  142. static: false,
  143. time_24hr: false,
  144. weekNumbers: false,
  145. wrap: false,
  146. };
  147. var english = {
  148. weekdays: {
  149. shorthand: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
  150. longhand: [
  151. "Sunday",
  152. "Monday",
  153. "Tuesday",
  154. "Wednesday",
  155. "Thursday",
  156. "Friday",
  157. "Saturday",
  158. ],
  159. },
  160. months: {
  161. shorthand: [
  162. "Jan",
  163. "Feb",
  164. "Mar",
  165. "Apr",
  166. "May",
  167. "Jun",
  168. "Jul",
  169. "Aug",
  170. "Sep",
  171. "Oct",
  172. "Nov",
  173. "Dec",
  174. ],
  175. longhand: [
  176. "January",
  177. "February",
  178. "March",
  179. "April",
  180. "May",
  181. "June",
  182. "July",
  183. "August",
  184. "September",
  185. "October",
  186. "November",
  187. "December",
  188. ],
  189. },
  190. daysInMonth: [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
  191. firstDayOfWeek: 0,
  192. ordinal: function (nth) {
  193. var s = nth % 100;
  194. if (s > 3 && s < 21)
  195. return "th";
  196. switch (s % 10) {
  197. case 1:
  198. return "st";
  199. case 2:
  200. return "nd";
  201. case 3:
  202. return "rd";
  203. default:
  204. return "th";
  205. }
  206. },
  207. rangeSeparator: " to ",
  208. weekAbbreviation: "Wk",
  209. scrollTitle: "Scroll to increment",
  210. toggleTitle: "Click to toggle",
  211. amPM: ["AM", "PM"],
  212. yearAriaLabel: "Year",
  213. monthAriaLabel: "Month",
  214. hourAriaLabel: "Hour",
  215. minuteAriaLabel: "Minute",
  216. time_24hr: false,
  217. };
  218. var createDateFormatter = function (_a) {
  219. var _b = _a.config, config = _b === void 0 ? defaults : _b, _c = _a.l10n, l10n = _c === void 0 ? english : _c, _d = _a.isMobile, isMobile = _d === void 0 ? false : _d;
  220. return function (dateObj, frmt, overrideLocale) {
  221. var locale = overrideLocale || l10n;
  222. if (config.formatDate !== undefined && !isMobile) {
  223. return config.formatDate(dateObj, frmt, locale);
  224. }
  225. return frmt
  226. .split("")
  227. .map(function (c, i, arr) {
  228. return formats[c] && arr[i - 1] !== "\\"
  229. ? formats[c](dateObj, locale, config)
  230. : c !== "\\"
  231. ? c
  232. : "";
  233. })
  234. .join("");
  235. };
  236. };
  237. /**
  238. * Compute the difference in dates, measured in ms
  239. */
  240. function compareDates(date1, date2, timeless) {
  241. if (timeless === void 0) { timeless = true; }
  242. if (timeless !== false) {
  243. return (new Date(date1.getTime()).setHours(0, 0, 0, 0) -
  244. new Date(date2.getTime()).setHours(0, 0, 0, 0));
  245. }
  246. return date1.getTime() - date2.getTime();
  247. }
  248. /**
  249. * Compute the difference in times, measured in ms
  250. */
  251. function compareTimes(date1, date2) {
  252. return (3600 * (date1.getHours() - date2.getHours()) +
  253. 60 * (date1.getMinutes() - date2.getMinutes()) +
  254. date1.getSeconds() -
  255. date2.getSeconds());
  256. }
  257. function minMaxTimePlugin(config) {
  258. if (config === void 0) { config = {}; }
  259. var state = {
  260. formatDate: createDateFormatter({}),
  261. tableDateFormat: config.tableDateFormat || "Y-m-d",
  262. defaults: {
  263. minTime: undefined,
  264. maxTime: undefined,
  265. },
  266. };
  267. function findDateTimeLimit(date) {
  268. if (config.table !== undefined) {
  269. return config.table[state.formatDate(date, state.tableDateFormat)];
  270. }
  271. return config.getTimeLimits && config.getTimeLimits(date);
  272. }
  273. return function (fp) {
  274. return {
  275. onReady: function () {
  276. state.formatDate = this.formatDate;
  277. state.defaults = {
  278. minTime: this.config.minTime && state.formatDate(this.config.minTime, "H:i"),
  279. maxTime: this.config.maxTime && state.formatDate(this.config.maxTime, "H:i"),
  280. };
  281. fp.loadedPlugins.push("minMaxTime");
  282. },
  283. onChange: function () {
  284. var latest = this.latestSelectedDateObj;
  285. var matchingTimeLimit = latest && findDateTimeLimit(latest);
  286. if (latest && matchingTimeLimit !== undefined) {
  287. this.set(matchingTimeLimit);
  288. fp.config.minTime.setFullYear(latest.getFullYear());
  289. fp.config.maxTime.setFullYear(latest.getFullYear());
  290. fp.config.minTime.setMonth(latest.getMonth());
  291. fp.config.maxTime.setMonth(latest.getMonth());
  292. fp.config.minTime.setDate(latest.getDate());
  293. fp.config.maxTime.setDate(latest.getDate());
  294. if (compareDates(latest, fp.config.maxTime, false) > 0) {
  295. fp.setDate(new Date(latest.getTime()).setHours(fp.config.maxTime.getHours(), fp.config.maxTime.getMinutes(), fp.config.maxTime.getSeconds(), fp.config.maxTime.getMilliseconds()), false);
  296. }
  297. else if (compareDates(latest, fp.config.minTime, false) < 0)
  298. fp.setDate(new Date(latest.getTime()).setHours(fp.config.minTime.getHours(), fp.config.minTime.getMinutes(), fp.config.minTime.getSeconds(), fp.config.minTime.getMilliseconds()), false);
  299. }
  300. else {
  301. var newMinMax = state.defaults || {
  302. minTime: undefined,
  303. maxTime: undefined,
  304. };
  305. this.set(newMinMax);
  306. if (!latest)
  307. return;
  308. var _a = fp.config, minTime = _a.minTime, maxTime = _a.maxTime;
  309. if (minTime && compareTimes(latest, minTime) < 0) {
  310. fp.setDate(new Date(latest.getTime()).setHours(minTime.getHours(), minTime.getMinutes(), minTime.getSeconds(), minTime.getMilliseconds()), false);
  311. }
  312. else if (maxTime && compareTimes(latest, maxTime) > 0) {
  313. fp.setDate(new Date(latest.getTime()).setHours(maxTime.getHours(), maxTime.getMinutes(), maxTime.getSeconds(), maxTime.getMilliseconds()));
  314. }
  315. //
  316. }
  317. },
  318. };
  319. };
  320. }
  321. return minMaxTimePlugin;
  322. })));