datepicker-full.js 87 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758
  1. (function () {
  2. 'use strict';
  3. function hasProperty(obj, prop) {
  4. return Object.prototype.hasOwnProperty.call(obj, prop);
  5. }
  6. function lastItemOf(arr) {
  7. return arr[arr.length - 1];
  8. }
  9. // push only the items not included in the array
  10. function pushUnique(arr, ...items) {
  11. items.forEach((item) => {
  12. if (arr.includes(item)) {
  13. return;
  14. }
  15. arr.push(item);
  16. });
  17. return arr;
  18. }
  19. function stringToArray(str, separator) {
  20. // convert empty string to an empty array
  21. return str ? str.split(separator) : [];
  22. }
  23. function isInRange(testVal, min, max) {
  24. const minOK = min === undefined || testVal >= min;
  25. const maxOK = max === undefined || testVal <= max;
  26. return minOK && maxOK;
  27. }
  28. function limitToRange(val, min, max) {
  29. if (val < min) {
  30. return min;
  31. }
  32. if (val > max) {
  33. return max;
  34. }
  35. return val;
  36. }
  37. function createTagRepeat(tagName, repeat, attributes = {}, index = 0, html = '') {
  38. const openTagSrc = Object.keys(attributes).reduce((src, attr) => {
  39. let val = attributes[attr];
  40. if (typeof val === 'function') {
  41. val = val(index);
  42. }
  43. return `${src} ${attr}="${val}"`;
  44. }, tagName);
  45. html += `<${openTagSrc}></${tagName}>`;
  46. const next = index + 1;
  47. return next < repeat
  48. ? createTagRepeat(tagName, repeat, attributes, next, html)
  49. : html;
  50. }
  51. // Remove the spacing surrounding tags for HTML parser not to create text nodes
  52. // before/after elements
  53. function optimizeTemplateHTML(html) {
  54. return html.replace(/>\s+/g, '>').replace(/\s+</, '<');
  55. }
  56. function stripTime(timeValue) {
  57. return new Date(timeValue).setHours(0, 0, 0, 0);
  58. }
  59. function today() {
  60. return new Date().setHours(0, 0, 0, 0);
  61. }
  62. // Get the time value of the start of given date or year, month and day
  63. function dateValue(...args) {
  64. switch (args.length) {
  65. case 0:
  66. return today();
  67. case 1:
  68. return stripTime(args[0]);
  69. }
  70. // use setFullYear() to keep 2-digit year from being mapped to 1900-1999
  71. const newDate = new Date(0);
  72. newDate.setFullYear(...args);
  73. return newDate.setHours(0, 0, 0, 0);
  74. }
  75. function addDays(date, amount) {
  76. const newDate = new Date(date);
  77. return newDate.setDate(newDate.getDate() + amount);
  78. }
  79. function addWeeks(date, amount) {
  80. return addDays(date, amount * 7);
  81. }
  82. function addMonths(date, amount) {
  83. // If the day of the date is not in the new month, the last day of the new
  84. // month will be returned. e.g. Jan 31 + 1 month → Feb 28 (not Mar 03)
  85. const newDate = new Date(date);
  86. const monthsToSet = newDate.getMonth() + amount;
  87. let expectedMonth = monthsToSet % 12;
  88. if (expectedMonth < 0) {
  89. expectedMonth += 12;
  90. }
  91. const time = newDate.setMonth(monthsToSet);
  92. return newDate.getMonth() !== expectedMonth ? newDate.setDate(0) : time;
  93. }
  94. function addYears(date, amount) {
  95. // If the date is Feb 29 and the new year is not a leap year, Feb 28 of the
  96. // new year will be returned.
  97. const newDate = new Date(date);
  98. const expectedMonth = newDate.getMonth();
  99. const time = newDate.setFullYear(newDate.getFullYear() + amount);
  100. return expectedMonth === 1 && newDate.getMonth() === 2 ? newDate.setDate(0) : time;
  101. }
  102. // Calculate the distance bettwen 2 days of the week
  103. function dayDiff(day, from) {
  104. return (day - from + 7) % 7;
  105. }
  106. // Get the date of the specified day of the week of given base date
  107. function dayOfTheWeekOf(baseDate, dayOfWeek, weekStart = 0) {
  108. const baseDay = new Date(baseDate).getDay();
  109. return addDays(baseDate, dayDiff(dayOfWeek, weekStart) - dayDiff(baseDay, weekStart));
  110. }
  111. // Get the ISO week of a date
  112. function getWeek(date) {
  113. // start of ISO week is Monday
  114. const thuOfTheWeek = dayOfTheWeekOf(date, 4, 1);
  115. // 1st week == the week where the 4th of January is in
  116. const firstThu = dayOfTheWeekOf(new Date(thuOfTheWeek).setMonth(0, 4), 4, 1);
  117. return Math.round((thuOfTheWeek - firstThu) / 604800000) + 1;
  118. }
  119. // Get the start year of the period of years that includes given date
  120. // years: length of the year period
  121. function startOfYearPeriod(date, years) {
  122. /* @see https://en.wikipedia.org/wiki/Year_zero#ISO_8601 */
  123. const year = new Date(date).getFullYear();
  124. return Math.floor(year / years) * years;
  125. }
  126. // pattern for format parts
  127. const reFormatTokens = /dd?|DD?|mm?|MM?|yy?(?:yy)?/;
  128. // pattern for non date parts
  129. const reNonDateParts = /[\s!-/:-@[-`{-~年月日]+/;
  130. // cache for persed formats
  131. let knownFormats = {};
  132. // parse funtions for date parts
  133. const parseFns = {
  134. y(date, year) {
  135. return new Date(date).setFullYear(parseInt(year, 10));
  136. },
  137. m(date, month, locale) {
  138. const newDate = new Date(date);
  139. let monthIndex = parseInt(month, 10) - 1;
  140. if (isNaN(monthIndex)) {
  141. if (!month) {
  142. return NaN;
  143. }
  144. const monthName = month.toLowerCase();
  145. const compareNames = name => name.toLowerCase().startsWith(monthName);
  146. // compare with both short and full names because some locales have periods
  147. // in the short names (not equal to the first X letters of the full names)
  148. monthIndex = locale.monthsShort.findIndex(compareNames);
  149. if (monthIndex < 0) {
  150. monthIndex = locale.months.findIndex(compareNames);
  151. }
  152. if (monthIndex < 0) {
  153. return NaN;
  154. }
  155. }
  156. newDate.setMonth(monthIndex);
  157. return newDate.getMonth() !== normalizeMonth(monthIndex)
  158. ? newDate.setDate(0)
  159. : newDate.getTime();
  160. },
  161. d(date, day) {
  162. return new Date(date).setDate(parseInt(day, 10));
  163. },
  164. };
  165. // format functions for date parts
  166. const formatFns = {
  167. d(date) {
  168. return date.getDate();
  169. },
  170. dd(date) {
  171. return padZero(date.getDate(), 2);
  172. },
  173. D(date, locale) {
  174. return locale.daysShort[date.getDay()];
  175. },
  176. DD(date, locale) {
  177. return locale.days[date.getDay()];
  178. },
  179. m(date) {
  180. return date.getMonth() + 1;
  181. },
  182. mm(date) {
  183. return padZero(date.getMonth() + 1, 2);
  184. },
  185. M(date, locale) {
  186. return locale.monthsShort[date.getMonth()];
  187. },
  188. MM(date, locale) {
  189. return locale.months[date.getMonth()];
  190. },
  191. y(date) {
  192. return date.getFullYear();
  193. },
  194. yy(date) {
  195. return padZero(date.getFullYear(), 2).slice(-2);
  196. },
  197. yyyy(date) {
  198. return padZero(date.getFullYear(), 4);
  199. },
  200. };
  201. // get month index in normal range (0 - 11) from any number
  202. function normalizeMonth(monthIndex) {
  203. return monthIndex > -1 ? monthIndex % 12 : normalizeMonth(monthIndex + 12);
  204. }
  205. function padZero(num, length) {
  206. return num.toString().padStart(length, '0');
  207. }
  208. function parseFormatString(format) {
  209. if (typeof format !== 'string') {
  210. throw new Error("Invalid date format.");
  211. }
  212. if (format in knownFormats) {
  213. return knownFormats[format];
  214. }
  215. // sprit the format string into parts and seprators
  216. const separators = format.split(reFormatTokens);
  217. const parts = format.match(new RegExp(reFormatTokens, 'g'));
  218. if (separators.length === 0 || !parts) {
  219. throw new Error("Invalid date format.");
  220. }
  221. // collect format functions used in the format
  222. const partFormatters = parts.map(token => formatFns[token]);
  223. // collect parse function keys used in the format
  224. // iterate over parseFns' keys in order to keep the order of the keys.
  225. const partParserKeys = Object.keys(parseFns).reduce((keys, key) => {
  226. const token = parts.find(part => part[0] !== 'D' && part[0].toLowerCase() === key);
  227. if (token) {
  228. keys.push(key);
  229. }
  230. return keys;
  231. }, []);
  232. return knownFormats[format] = {
  233. parser(dateStr, locale) {
  234. const dateParts = dateStr.split(reNonDateParts).reduce((dtParts, part, index) => {
  235. if (part.length > 0 && parts[index]) {
  236. const token = parts[index][0];
  237. if (token === 'M') {
  238. dtParts.m = part;
  239. } else if (token !== 'D') {
  240. dtParts[token] = part;
  241. }
  242. }
  243. return dtParts;
  244. }, {});
  245. // iterate over partParserkeys so that the parsing is made in the oder
  246. // of year, month and day to prevent the day parser from correcting last
  247. // day of month wrongly
  248. return partParserKeys.reduce((origDate, key) => {
  249. const newDate = parseFns[key](origDate, dateParts[key], locale);
  250. // ingnore the part failed to parse
  251. return isNaN(newDate) ? origDate : newDate;
  252. }, today());
  253. },
  254. formatter(date, locale) {
  255. let dateStr = partFormatters.reduce((str, fn, index) => {
  256. return str += `${separators[index]}${fn(date, locale)}`;
  257. }, '');
  258. // separators' length is always parts' length + 1,
  259. return dateStr += lastItemOf(separators);
  260. },
  261. };
  262. }
  263. function parseDate(dateStr, format, locale) {
  264. if (dateStr instanceof Date || typeof dateStr === 'number') {
  265. const date = stripTime(dateStr);
  266. return isNaN(date) ? undefined : date;
  267. }
  268. if (!dateStr) {
  269. return undefined;
  270. }
  271. if (dateStr === 'today') {
  272. return today();
  273. }
  274. if (format && format.toValue) {
  275. const date = format.toValue(dateStr, format, locale);
  276. return isNaN(date) ? undefined : stripTime(date);
  277. }
  278. return parseFormatString(format).parser(dateStr, locale);
  279. }
  280. function formatDate(date, format, locale) {
  281. if (isNaN(date) || (!date && date !== 0)) {
  282. return '';
  283. }
  284. const dateObj = typeof date === 'number' ? new Date(date) : date;
  285. if (format.toDisplay) {
  286. return format.toDisplay(dateObj, format, locale);
  287. }
  288. return parseFormatString(format).formatter(dateObj, locale);
  289. }
  290. const listenerRegistry = new WeakMap();
  291. const {addEventListener, removeEventListener} = EventTarget.prototype;
  292. // Register event listeners to a key object
  293. // listeners: array of listener definitions;
  294. // - each definition must be a flat array of event target and the arguments
  295. // used to call addEventListener() on the target
  296. function registerListeners(keyObj, listeners) {
  297. let registered = listenerRegistry.get(keyObj);
  298. if (!registered) {
  299. registered = [];
  300. listenerRegistry.set(keyObj, registered);
  301. }
  302. listeners.forEach((listener) => {
  303. addEventListener.call(...listener);
  304. registered.push(listener);
  305. });
  306. }
  307. function unregisterListeners(keyObj) {
  308. let listeners = listenerRegistry.get(keyObj);
  309. if (!listeners) {
  310. return;
  311. }
  312. listeners.forEach((listener) => {
  313. removeEventListener.call(...listener);
  314. });
  315. listenerRegistry.delete(keyObj);
  316. }
  317. // Event.composedPath() polyfill for Edge
  318. // based on https://gist.github.com/kleinfreund/e9787d73776c0e3750dcfcdc89f100ec
  319. if (!Event.prototype.composedPath) {
  320. const getComposedPath = (node, path = []) => {
  321. path.push(node);
  322. let parent;
  323. if (node.parentNode) {
  324. parent = node.parentNode;
  325. } else if (node.host) { // ShadowRoot
  326. parent = node.host;
  327. } else if (node.defaultView) { // Document
  328. parent = node.defaultView;
  329. }
  330. return parent ? getComposedPath(parent, path) : path;
  331. };
  332. Event.prototype.composedPath = function () {
  333. return getComposedPath(this.target);
  334. };
  335. }
  336. function findFromPath(path, criteria, currentTarget, index = 0) {
  337. const el = path[index];
  338. if (criteria(el)) {
  339. return el;
  340. } else if (el === currentTarget || !el.parentElement) {
  341. // stop when reaching currentTarget or <html>
  342. return;
  343. }
  344. return findFromPath(path, criteria, currentTarget, index + 1);
  345. }
  346. // Search for the actual target of a delegated event
  347. function findElementInEventPath(ev, selector) {
  348. const criteria = typeof selector === 'function' ? selector : el => el.matches(selector);
  349. return findFromPath(ev.composedPath(), criteria, ev.currentTarget);
  350. }
  351. // default locales
  352. const locales = {
  353. en: {
  354. days: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],
  355. daysShort: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
  356. daysMin: ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"],
  357. months: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
  358. monthsShort: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
  359. today: "Today",
  360. clear: "Clear",
  361. titleFormat: "MM y"
  362. }
  363. };
  364. // config options updatable by setOptions() and their default values
  365. const defaultOptions = {
  366. autohide: false,
  367. beforeShowDay: null,
  368. beforeShowDecade: null,
  369. beforeShowMonth: null,
  370. beforeShowYear: null,
  371. calendarWeeks: false,
  372. clearBtn: false,
  373. dateDelimiter: ',',
  374. datesDisabled: [],
  375. daysOfWeekDisabled: [],
  376. daysOfWeekHighlighted: [],
  377. defaultViewDate: undefined, // placeholder, defaults to today() by the program
  378. disableTouchKeyboard: false,
  379. format: 'mm/dd/yyyy',
  380. language: 'en',
  381. maxDate: null,
  382. maxNumberOfDates: 1,
  383. maxView: 3,
  384. minDate: null,
  385. nextArrow: '»',
  386. orientation: 'auto',
  387. pickLevel: 0,
  388. prevArrow: '«',
  389. showDaysOfWeek: true,
  390. showOnClick: true,
  391. showOnFocus: true,
  392. startView: 0,
  393. title: '',
  394. todayBtn: false,
  395. todayBtnMode: 0,
  396. todayHighlight: false,
  397. updateOnBlur: true,
  398. weekStart: 0,
  399. };
  400. const range = document.createRange();
  401. function parseHTML(html) {
  402. return range.createContextualFragment(html);
  403. }
  404. function hideElement(el) {
  405. if (el.style.display === 'none') {
  406. return;
  407. }
  408. // back up the existing display setting in data-style-display
  409. if (el.style.display) {
  410. el.dataset.styleDisplay = el.style.display;
  411. }
  412. el.style.display = 'none';
  413. }
  414. function showElement(el) {
  415. if (el.style.display !== 'none') {
  416. return;
  417. }
  418. if (el.dataset.styleDisplay) {
  419. // restore backed-up dispay property
  420. el.style.display = el.dataset.styleDisplay;
  421. delete el.dataset.styleDisplay;
  422. } else {
  423. el.style.display = '';
  424. }
  425. }
  426. function emptyChildNodes(el) {
  427. if (el.firstChild) {
  428. el.removeChild(el.firstChild);
  429. emptyChildNodes(el);
  430. }
  431. }
  432. function replaceChildNodes(el, newChildNodes) {
  433. emptyChildNodes(el);
  434. if (newChildNodes instanceof DocumentFragment) {
  435. el.appendChild(newChildNodes);
  436. } else if (typeof newChildNodes === 'string') {
  437. el.appendChild(parseHTML(newChildNodes));
  438. } else if (typeof newChildNodes.forEach === 'function') {
  439. newChildNodes.forEach((node) => {
  440. el.appendChild(node);
  441. });
  442. }
  443. }
  444. const {
  445. language: defaultLang,
  446. format: defaultFormat,
  447. weekStart: defaultWeekStart,
  448. } = defaultOptions;
  449. // Reducer function to filter out invalid day-of-week from the input
  450. function sanitizeDOW(dow, day) {
  451. return dow.length < 6 && day >= 0 && day < 7
  452. ? pushUnique(dow, day)
  453. : dow;
  454. }
  455. function calcEndOfWeek(startOfWeek) {
  456. return (startOfWeek + 6) % 7;
  457. }
  458. // validate input date. if invalid, fallback to the original value
  459. function validateDate(value, format, locale, origValue) {
  460. const date = parseDate(value, format, locale);
  461. return date !== undefined ? date : origValue;
  462. }
  463. // Validate viewId. if invalid, fallback to the original value
  464. function validateViewId(value, origValue, max = 3) {
  465. const viewId = parseInt(value, 10);
  466. return viewId >= 0 && viewId <= max ? viewId : origValue;
  467. }
  468. // Create Datepicker configuration to set
  469. function processOptions(options, datepicker) {
  470. const inOpts = Object.assign({}, options);
  471. const config = {};
  472. const locales = datepicker.constructor.locales;
  473. let {
  474. format,
  475. language,
  476. locale,
  477. maxDate,
  478. maxView,
  479. minDate,
  480. pickLevel,
  481. startView,
  482. weekStart,
  483. } = datepicker.config || {};
  484. if (inOpts.language) {
  485. let lang;
  486. if (inOpts.language !== language) {
  487. if (locales[inOpts.language]) {
  488. lang = inOpts.language;
  489. } else {
  490. // Check if langauge + region tag can fallback to the one without
  491. // region (e.g. fr-CA → fr)
  492. lang = inOpts.language.split('-')[0];
  493. if (locales[lang] === undefined) {
  494. lang = false;
  495. }
  496. }
  497. }
  498. delete inOpts.language;
  499. if (lang) {
  500. language = config.language = lang;
  501. // update locale as well when updating language
  502. const origLocale = locale || locales[defaultLang];
  503. // use default language's properties for the fallback
  504. locale = Object.assign({
  505. format: defaultFormat,
  506. weekStart: defaultWeekStart
  507. }, locales[defaultLang]);
  508. if (language !== defaultLang) {
  509. Object.assign(locale, locales[language]);
  510. }
  511. config.locale = locale;
  512. // if format and/or weekStart are the same as old locale's defaults,
  513. // update them to new locale's defaults
  514. if (format === origLocale.format) {
  515. format = config.format = locale.format;
  516. }
  517. if (weekStart === origLocale.weekStart) {
  518. weekStart = config.weekStart = locale.weekStart;
  519. config.weekEnd = calcEndOfWeek(locale.weekStart);
  520. }
  521. }
  522. }
  523. if (inOpts.format) {
  524. const hasToDisplay = typeof inOpts.format.toDisplay === 'function';
  525. const hasToValue = typeof inOpts.format.toValue === 'function';
  526. const validFormatString = reFormatTokens.test(inOpts.format);
  527. if ((hasToDisplay && hasToValue) || validFormatString) {
  528. format = config.format = inOpts.format;
  529. }
  530. delete inOpts.format;
  531. }
  532. //*** dates ***//
  533. // while min and maxDate for "no limit" in the options are better to be null
  534. // (especially when updating), the ones in the config have to be undefined
  535. // because null is treated as 0 (= unix epoch) when comparing with time value
  536. let minDt = minDate;
  537. let maxDt = maxDate;
  538. if (inOpts.minDate !== undefined) {
  539. minDt = inOpts.minDate === null
  540. ? dateValue(0, 0, 1) // set 0000-01-01 to prevent negative values for year
  541. : validateDate(inOpts.minDate, format, locale, minDt);
  542. delete inOpts.minDate;
  543. }
  544. if (inOpts.maxDate !== undefined) {
  545. maxDt = inOpts.maxDate === null
  546. ? undefined
  547. : validateDate(inOpts.maxDate, format, locale, maxDt);
  548. delete inOpts.maxDate;
  549. }
  550. if (maxDt < minDt) {
  551. minDate = config.minDate = maxDt;
  552. maxDate = config.maxDate = minDt;
  553. } else {
  554. if (minDate !== minDt) {
  555. minDate = config.minDate = minDt;
  556. }
  557. if (maxDate !== maxDt) {
  558. maxDate = config.maxDate = maxDt;
  559. }
  560. }
  561. if (inOpts.datesDisabled) {
  562. config.datesDisabled = inOpts.datesDisabled.reduce((dates, dt) => {
  563. const date = parseDate(dt, format, locale);
  564. return date !== undefined ? pushUnique(dates, date) : dates;
  565. }, []);
  566. delete inOpts.datesDisabled;
  567. }
  568. if (inOpts.defaultViewDate !== undefined) {
  569. const viewDate = parseDate(inOpts.defaultViewDate, format, locale);
  570. if (viewDate !== undefined) {
  571. config.defaultViewDate = viewDate;
  572. }
  573. delete inOpts.defaultViewDate;
  574. }
  575. //*** days of week ***//
  576. if (inOpts.weekStart !== undefined) {
  577. const wkStart = Number(inOpts.weekStart) % 7;
  578. if (!isNaN(wkStart)) {
  579. weekStart = config.weekStart = wkStart;
  580. config.weekEnd = calcEndOfWeek(wkStart);
  581. }
  582. delete inOpts.weekStart;
  583. }
  584. if (inOpts.daysOfWeekDisabled) {
  585. config.daysOfWeekDisabled = inOpts.daysOfWeekDisabled.reduce(sanitizeDOW, []);
  586. delete inOpts.daysOfWeekDisabled;
  587. }
  588. if (inOpts.daysOfWeekHighlighted) {
  589. config.daysOfWeekHighlighted = inOpts.daysOfWeekHighlighted.reduce(sanitizeDOW, []);
  590. delete inOpts.daysOfWeekHighlighted;
  591. }
  592. //*** multi date ***//
  593. if (inOpts.maxNumberOfDates !== undefined) {
  594. const maxNumberOfDates = parseInt(inOpts.maxNumberOfDates, 10);
  595. if (maxNumberOfDates >= 0) {
  596. config.maxNumberOfDates = maxNumberOfDates;
  597. config.multidate = maxNumberOfDates !== 1;
  598. }
  599. delete inOpts.maxNumberOfDates;
  600. }
  601. if (inOpts.dateDelimiter) {
  602. config.dateDelimiter = String(inOpts.dateDelimiter);
  603. delete inOpts.dateDelimiter;
  604. }
  605. //*** pick level & view ***//
  606. let newPickLevel = pickLevel;
  607. if (inOpts.pickLevel !== undefined) {
  608. newPickLevel = validateViewId(inOpts.pickLevel, 2);
  609. delete inOpts.pickLevel;
  610. }
  611. if (newPickLevel !== pickLevel) {
  612. pickLevel = config.pickLevel = newPickLevel;
  613. }
  614. let newMaxView = maxView;
  615. if (inOpts.maxView !== undefined) {
  616. newMaxView = validateViewId(inOpts.maxView, maxView);
  617. delete inOpts.maxView;
  618. }
  619. // ensure max view >= pick level
  620. newMaxView = pickLevel > newMaxView ? pickLevel : newMaxView;
  621. if (newMaxView !== maxView) {
  622. maxView = config.maxView = newMaxView;
  623. }
  624. let newStartView = startView;
  625. if (inOpts.startView !== undefined) {
  626. newStartView = validateViewId(inOpts.startView, newStartView);
  627. delete inOpts.startView;
  628. }
  629. // ensure pick level <= start view <= max view
  630. if (newStartView < pickLevel) {
  631. newStartView = pickLevel;
  632. } else if (newStartView > maxView) {
  633. newStartView = maxView;
  634. }
  635. if (newStartView !== startView) {
  636. config.startView = newStartView;
  637. }
  638. //*** template ***//
  639. if (inOpts.prevArrow) {
  640. const prevArrow = parseHTML(inOpts.prevArrow);
  641. if (prevArrow.childNodes.length > 0) {
  642. config.prevArrow = prevArrow.childNodes;
  643. }
  644. delete inOpts.prevArrow;
  645. }
  646. if (inOpts.nextArrow) {
  647. const nextArrow = parseHTML(inOpts.nextArrow);
  648. if (nextArrow.childNodes.length > 0) {
  649. config.nextArrow = nextArrow.childNodes;
  650. }
  651. delete inOpts.nextArrow;
  652. }
  653. //*** misc ***//
  654. if (inOpts.disableTouchKeyboard !== undefined) {
  655. config.disableTouchKeyboard = 'ontouchstart' in document && !!inOpts.disableTouchKeyboard;
  656. delete inOpts.disableTouchKeyboard;
  657. }
  658. if (inOpts.orientation) {
  659. const orientation = inOpts.orientation.toLowerCase().split(/\s+/g);
  660. config.orientation = {
  661. x: orientation.find(x => (x === 'left' || x === 'right')) || 'auto',
  662. y: orientation.find(y => (y === 'top' || y === 'bottom')) || 'auto',
  663. };
  664. delete inOpts.orientation;
  665. }
  666. if (inOpts.todayBtnMode !== undefined) {
  667. switch(inOpts.todayBtnMode) {
  668. case 0:
  669. case 1:
  670. config.todayBtnMode = inOpts.todayBtnMode;
  671. }
  672. delete inOpts.todayBtnMode;
  673. }
  674. //*** copy the rest ***//
  675. Object.keys(inOpts).forEach((key) => {
  676. if (inOpts[key] !== undefined && hasProperty(defaultOptions, key)) {
  677. config[key] = inOpts[key];
  678. }
  679. });
  680. return config;
  681. }
  682. const pickerTemplate = optimizeTemplateHTML(`<div class="datepicker">
  683. <div class="datepicker-picker">
  684. <div class="datepicker-header">
  685. <div class="datepicker-title"></div>
  686. <div class="datepicker-controls">
  687. <button type="button" class="%buttonClass% prev-btn"></button>
  688. <button type="button" class="%buttonClass% view-switch"></button>
  689. <button type="button" class="%buttonClass% next-btn"></button>
  690. </div>
  691. </div>
  692. <div class="datepicker-main"></div>
  693. <div class="datepicker-footer">
  694. <div class="datepicker-controls">
  695. <button type="button" class="%buttonClass% today-btn"></button>
  696. <button type="button" class="%buttonClass% clear-btn"></button>
  697. </div>
  698. </div>
  699. </div>
  700. </div>`);
  701. const daysTemplate = optimizeTemplateHTML(`<div class="days">
  702. <div class="days-of-week">${createTagRepeat('span', 7, {class: 'dow'})}</div>
  703. <div class="datepicker-grid">${createTagRepeat('span', 42)}</div>
  704. </div>`);
  705. const calendarWeeksTemplate = optimizeTemplateHTML(`<div class="calendar-weeks">
  706. <div class="days-of-week"><span class="dow"></span></div>
  707. <div class="weeks">${createTagRepeat('span', 6, {class: 'week'})}</div>
  708. </div>`);
  709. // Base class of the view classes
  710. class View {
  711. constructor(picker, config) {
  712. Object.assign(this, config, {
  713. picker,
  714. element: parseHTML(`<div class="datepicker-view"></div>`).firstChild,
  715. selected: [],
  716. });
  717. this.init(this.picker.datepicker.config);
  718. }
  719. init(options) {
  720. if (options.pickLevel !== undefined) {
  721. this.isMinView = this.id === options.pickLevel;
  722. }
  723. this.setOptions(options);
  724. this.updateFocus();
  725. this.updateSelection();
  726. }
  727. // Execute beforeShow() callback and apply the result to the element
  728. // args:
  729. // - current - current value on the iteration on view rendering
  730. // - timeValue - time value of the date to pass to beforeShow()
  731. performBeforeHook(el, current, timeValue) {
  732. let result = this.beforeShow(new Date(timeValue));
  733. switch (typeof result) {
  734. case 'boolean':
  735. result = {enabled: result};
  736. break;
  737. case 'string':
  738. result = {classes: result};
  739. }
  740. if (result) {
  741. if (result.enabled === false) {
  742. el.classList.add('disabled');
  743. pushUnique(this.disabled, current);
  744. }
  745. if (result.classes) {
  746. const extraClasses = result.classes.split(/\s+/);
  747. el.classList.add(...extraClasses);
  748. if (extraClasses.includes('disabled')) {
  749. pushUnique(this.disabled, current);
  750. }
  751. }
  752. if (result.content) {
  753. replaceChildNodes(el, result.content);
  754. }
  755. }
  756. }
  757. }
  758. class DaysView extends View {
  759. constructor(picker) {
  760. super(picker, {
  761. id: 0,
  762. name: 'days',
  763. cellClass: 'day',
  764. });
  765. }
  766. init(options, onConstruction = true) {
  767. if (onConstruction) {
  768. const inner = parseHTML(daysTemplate).firstChild;
  769. this.dow = inner.firstChild;
  770. this.grid = inner.lastChild;
  771. this.element.appendChild(inner);
  772. }
  773. super.init(options);
  774. }
  775. setOptions(options) {
  776. let updateDOW;
  777. if (hasProperty(options, 'minDate')) {
  778. this.minDate = options.minDate;
  779. }
  780. if (hasProperty(options, 'maxDate')) {
  781. this.maxDate = options.maxDate;
  782. }
  783. if (options.datesDisabled) {
  784. this.datesDisabled = options.datesDisabled;
  785. }
  786. if (options.daysOfWeekDisabled) {
  787. this.daysOfWeekDisabled = options.daysOfWeekDisabled;
  788. updateDOW = true;
  789. }
  790. if (options.daysOfWeekHighlighted) {
  791. this.daysOfWeekHighlighted = options.daysOfWeekHighlighted;
  792. }
  793. if (options.todayHighlight !== undefined) {
  794. this.todayHighlight = options.todayHighlight;
  795. }
  796. if (options.weekStart !== undefined) {
  797. this.weekStart = options.weekStart;
  798. this.weekEnd = options.weekEnd;
  799. updateDOW = true;
  800. }
  801. if (options.locale) {
  802. const locale = this.locale = options.locale;
  803. this.dayNames = locale.daysMin;
  804. this.switchLabelFormat = locale.titleFormat;
  805. updateDOW = true;
  806. }
  807. if (options.beforeShowDay !== undefined) {
  808. this.beforeShow = typeof options.beforeShowDay === 'function'
  809. ? options.beforeShowDay
  810. : undefined;
  811. }
  812. if (options.calendarWeeks !== undefined) {
  813. if (options.calendarWeeks && !this.calendarWeeks) {
  814. const weeksElem = parseHTML(calendarWeeksTemplate).firstChild;
  815. this.calendarWeeks = {
  816. element: weeksElem,
  817. dow: weeksElem.firstChild,
  818. weeks: weeksElem.lastChild,
  819. };
  820. this.element.insertBefore(weeksElem, this.element.firstChild);
  821. } else if (this.calendarWeeks && !options.calendarWeeks) {
  822. this.element.removeChild(this.calendarWeeks.element);
  823. this.calendarWeeks = null;
  824. }
  825. }
  826. if (options.showDaysOfWeek !== undefined) {
  827. if (options.showDaysOfWeek) {
  828. showElement(this.dow);
  829. if (this.calendarWeeks) {
  830. showElement(this.calendarWeeks.dow);
  831. }
  832. } else {
  833. hideElement(this.dow);
  834. if (this.calendarWeeks) {
  835. hideElement(this.calendarWeeks.dow);
  836. }
  837. }
  838. }
  839. // update days-of-week when locale, daysOfweekDisabled or weekStart is changed
  840. if (updateDOW) {
  841. Array.from(this.dow.children).forEach((el, index) => {
  842. const dow = (this.weekStart + index) % 7;
  843. el.textContent = this.dayNames[dow];
  844. el.className = this.daysOfWeekDisabled.includes(dow) ? 'dow disabled' : 'dow';
  845. });
  846. }
  847. }
  848. // Apply update on the focused date to view's settings
  849. updateFocus() {
  850. const viewDate = new Date(this.picker.viewDate);
  851. const viewYear = viewDate.getFullYear();
  852. const viewMonth = viewDate.getMonth();
  853. const firstOfMonth = dateValue(viewYear, viewMonth, 1);
  854. const start = dayOfTheWeekOf(firstOfMonth, this.weekStart, this.weekStart);
  855. this.first = firstOfMonth;
  856. this.last = dateValue(viewYear, viewMonth + 1, 0);
  857. this.start = start;
  858. this.focused = this.picker.viewDate;
  859. }
  860. // Apply update on the selected dates to view's settings
  861. updateSelection() {
  862. const {dates, rangepicker} = this.picker.datepicker;
  863. this.selected = dates;
  864. if (rangepicker) {
  865. this.range = rangepicker.dates;
  866. }
  867. }
  868. // Update the entire view UI
  869. render() {
  870. // update today marker on ever render
  871. this.today = this.todayHighlight ? today() : undefined;
  872. // refresh disabled dates on every render in order to clear the ones added
  873. // by beforeShow hook at previous render
  874. this.disabled = [...this.datesDisabled];
  875. const switchLabel = formatDate(this.focused, this.switchLabelFormat, this.locale);
  876. this.picker.setViewSwitchLabel(switchLabel);
  877. this.picker.setPrevBtnDisabled(this.first <= this.minDate);
  878. this.picker.setNextBtnDisabled(this.last >= this.maxDate);
  879. if (this.calendarWeeks) {
  880. // start of the UTC week (Monday) of the 1st of the month
  881. const startOfWeek = dayOfTheWeekOf(this.first, 1, 1);
  882. Array.from(this.calendarWeeks.weeks.children).forEach((el, index) => {
  883. el.textContent = getWeek(addWeeks(startOfWeek, index));
  884. });
  885. }
  886. Array.from(this.grid.children).forEach((el, index) => {
  887. const classList = el.classList;
  888. const current = addDays(this.start, index);
  889. const date = new Date(current);
  890. const day = date.getDay();
  891. el.className = `datepicker-cell ${this.cellClass}`;
  892. el.dataset.date = current;
  893. el.textContent = date.getDate();
  894. if (current < this.first) {
  895. classList.add('prev');
  896. } else if (current > this.last) {
  897. classList.add('next');
  898. }
  899. if (this.today === current) {
  900. classList.add('today');
  901. }
  902. if (current < this.minDate || current > this.maxDate || this.disabled.includes(current)) {
  903. classList.add('disabled');
  904. }
  905. if (this.daysOfWeekDisabled.includes(day)) {
  906. classList.add('disabled');
  907. pushUnique(this.disabled, current);
  908. }
  909. if (this.daysOfWeekHighlighted.includes(day)) {
  910. classList.add('highlighted');
  911. }
  912. if (this.range) {
  913. const [rangeStart, rangeEnd] = this.range;
  914. if (current > rangeStart && current < rangeEnd) {
  915. classList.add('range');
  916. }
  917. if (current === rangeStart) {
  918. classList.add('range-start');
  919. }
  920. if (current === rangeEnd) {
  921. classList.add('range-end');
  922. }
  923. }
  924. if (this.selected.includes(current)) {
  925. classList.add('selected');
  926. }
  927. if (current === this.focused) {
  928. classList.add('focused');
  929. }
  930. if (this.beforeShow) {
  931. this.performBeforeHook(el, current, current);
  932. }
  933. });
  934. }
  935. // Update the view UI by applying the changes of selected and focused items
  936. refresh() {
  937. const [rangeStart, rangeEnd] = this.range || [];
  938. this.grid
  939. .querySelectorAll('.range, .range-start, .range-end, .selected, .focused')
  940. .forEach((el) => {
  941. el.classList.remove('range', 'range-start', 'range-end', 'selected', 'focused');
  942. });
  943. Array.from(this.grid.children).forEach((el) => {
  944. const current = Number(el.dataset.date);
  945. const classList = el.classList;
  946. if (current > rangeStart && current < rangeEnd) {
  947. classList.add('range');
  948. }
  949. if (current === rangeStart) {
  950. classList.add('range-start');
  951. }
  952. if (current === rangeEnd) {
  953. classList.add('range-end');
  954. }
  955. if (this.selected.includes(current)) {
  956. classList.add('selected');
  957. }
  958. if (current === this.focused) {
  959. classList.add('focused');
  960. }
  961. });
  962. }
  963. // Update the view UI by applying the change of focused item
  964. refreshFocus() {
  965. const index = Math.round((this.focused - this.start) / 86400000);
  966. this.grid.querySelectorAll('.focused').forEach((el) => {
  967. el.classList.remove('focused');
  968. });
  969. this.grid.children[index].classList.add('focused');
  970. }
  971. }
  972. function computeMonthRange(range, thisYear) {
  973. if (!range || !range[0] || !range[1]) {
  974. return;
  975. }
  976. const [[startY, startM], [endY, endM]] = range;
  977. if (startY > thisYear || endY < thisYear) {
  978. return;
  979. }
  980. return [
  981. startY === thisYear ? startM : -1,
  982. endY === thisYear ? endM : 12,
  983. ];
  984. }
  985. class MonthsView extends View {
  986. constructor(picker) {
  987. super(picker, {
  988. id: 1,
  989. name: 'months',
  990. cellClass: 'month',
  991. });
  992. }
  993. init(options, onConstruction = true) {
  994. if (onConstruction) {
  995. this.grid = this.element;
  996. this.element.classList.add('months', 'datepicker-grid');
  997. this.grid.appendChild(parseHTML(createTagRepeat('span', 12, {'data-month': ix => ix})));
  998. }
  999. super.init(options);
  1000. }
  1001. setOptions(options) {
  1002. if (options.locale) {
  1003. this.monthNames = options.locale.monthsShort;
  1004. }
  1005. if (hasProperty(options, 'minDate')) {
  1006. if (options.minDate === undefined) {
  1007. this.minYear = this.minMonth = this.minDate = undefined;
  1008. } else {
  1009. const minDateObj = new Date(options.minDate);
  1010. this.minYear = minDateObj.getFullYear();
  1011. this.minMonth = minDateObj.getMonth();
  1012. this.minDate = minDateObj.setDate(1);
  1013. }
  1014. }
  1015. if (hasProperty(options, 'maxDate')) {
  1016. if (options.maxDate === undefined) {
  1017. this.maxYear = this.maxMonth = this.maxDate = undefined;
  1018. } else {
  1019. const maxDateObj = new Date(options.maxDate);
  1020. this.maxYear = maxDateObj.getFullYear();
  1021. this.maxMonth = maxDateObj.getMonth();
  1022. this.maxDate = dateValue(this.maxYear, this.maxMonth + 1, 0);
  1023. }
  1024. }
  1025. if (options.beforeShowMonth !== undefined) {
  1026. this.beforeShow = typeof options.beforeShowMonth === 'function'
  1027. ? options.beforeShowMonth
  1028. : undefined;
  1029. }
  1030. }
  1031. // Update view's settings to reflect the viewDate set on the picker
  1032. updateFocus() {
  1033. const viewDate = new Date(this.picker.viewDate);
  1034. this.year = viewDate.getFullYear();
  1035. this.focused = viewDate.getMonth();
  1036. }
  1037. // Update view's settings to reflect the selected dates
  1038. updateSelection() {
  1039. const {dates, rangepicker} = this.picker.datepicker;
  1040. this.selected = dates.reduce((selected, timeValue) => {
  1041. const date = new Date(timeValue);
  1042. const year = date.getFullYear();
  1043. const month = date.getMonth();
  1044. if (selected[year] === undefined) {
  1045. selected[year] = [month];
  1046. } else {
  1047. pushUnique(selected[year], month);
  1048. }
  1049. return selected;
  1050. }, {});
  1051. if (rangepicker && rangepicker.dates) {
  1052. this.range = rangepicker.dates.map(timeValue => {
  1053. const date = new Date(timeValue);
  1054. return isNaN(date) ? undefined : [date.getFullYear(), date.getMonth()];
  1055. });
  1056. }
  1057. }
  1058. // Update the entire view UI
  1059. render() {
  1060. // refresh disabled months on every render in order to clear the ones added
  1061. // by beforeShow hook at previous render
  1062. this.disabled = [];
  1063. this.picker.setViewSwitchLabel(this.year);
  1064. this.picker.setPrevBtnDisabled(this.year <= this.minYear);
  1065. this.picker.setNextBtnDisabled(this.year >= this.maxYear);
  1066. const selected = this.selected[this.year] || [];
  1067. const yrOutOfRange = this.year < this.minYear || this.year > this.maxYear;
  1068. const isMinYear = this.year === this.minYear;
  1069. const isMaxYear = this.year === this.maxYear;
  1070. const range = computeMonthRange(this.range, this.year);
  1071. Array.from(this.grid.children).forEach((el, index) => {
  1072. const classList = el.classList;
  1073. const date = dateValue(this.year, index, 1);
  1074. el.className = `datepicker-cell ${this.cellClass}`;
  1075. if (this.isMinView) {
  1076. el.dataset.date = date;
  1077. }
  1078. // reset text on every render to clear the custom content set
  1079. // by beforeShow hook at previous render
  1080. el.textContent = this.monthNames[index];
  1081. if (
  1082. yrOutOfRange
  1083. || isMinYear && index < this.minMonth
  1084. || isMaxYear && index > this.maxMonth
  1085. ) {
  1086. classList.add('disabled');
  1087. }
  1088. if (range) {
  1089. const [rangeStart, rangeEnd] = range;
  1090. if (index > rangeStart && index < rangeEnd) {
  1091. classList.add('range');
  1092. }
  1093. if (index === rangeStart) {
  1094. classList.add('range-start');
  1095. }
  1096. if (index === rangeEnd) {
  1097. classList.add('range-end');
  1098. }
  1099. }
  1100. if (selected.includes(index)) {
  1101. classList.add('selected');
  1102. }
  1103. if (index === this.focused) {
  1104. classList.add('focused');
  1105. }
  1106. if (this.beforeShow) {
  1107. this.performBeforeHook(el, index, date);
  1108. }
  1109. });
  1110. }
  1111. // Update the view UI by applying the changes of selected and focused items
  1112. refresh() {
  1113. const selected = this.selected[this.year] || [];
  1114. const [rangeStart, rangeEnd] = computeMonthRange(this.range, this.year) || [];
  1115. this.grid
  1116. .querySelectorAll('.range, .range-start, .range-end, .selected, .focused')
  1117. .forEach((el) => {
  1118. el.classList.remove('range', 'range-start', 'range-end', 'selected', 'focused');
  1119. });
  1120. Array.from(this.grid.children).forEach((el, index) => {
  1121. const classList = el.classList;
  1122. if (index > rangeStart && index < rangeEnd) {
  1123. classList.add('range');
  1124. }
  1125. if (index === rangeStart) {
  1126. classList.add('range-start');
  1127. }
  1128. if (index === rangeEnd) {
  1129. classList.add('range-end');
  1130. }
  1131. if (selected.includes(index)) {
  1132. classList.add('selected');
  1133. }
  1134. if (index === this.focused) {
  1135. classList.add('focused');
  1136. }
  1137. });
  1138. }
  1139. // Update the view UI by applying the change of focused item
  1140. refreshFocus() {
  1141. this.grid.querySelectorAll('.focused').forEach((el) => {
  1142. el.classList.remove('focused');
  1143. });
  1144. this.grid.children[this.focused].classList.add('focused');
  1145. }
  1146. }
  1147. function toTitleCase(word) {
  1148. return [...word].reduce((str, ch, ix) => str += ix ? ch : ch.toUpperCase(), '');
  1149. }
  1150. // Class representing the years and decades view elements
  1151. class YearsView extends View {
  1152. constructor(picker, config) {
  1153. super(picker, config);
  1154. }
  1155. init(options, onConstruction = true) {
  1156. if (onConstruction) {
  1157. this.navStep = this.step * 10;
  1158. this.beforeShowOption = `beforeShow${toTitleCase(this.cellClass)}`;
  1159. this.grid = this.element;
  1160. this.element.classList.add(this.name, 'datepicker-grid');
  1161. this.grid.appendChild(parseHTML(createTagRepeat('span', 12)));
  1162. }
  1163. super.init(options);
  1164. }
  1165. setOptions(options) {
  1166. if (hasProperty(options, 'minDate')) {
  1167. if (options.minDate === undefined) {
  1168. this.minYear = this.minDate = undefined;
  1169. } else {
  1170. this.minYear = startOfYearPeriod(options.minDate, this.step);
  1171. this.minDate = dateValue(this.minYear, 0, 1);
  1172. }
  1173. }
  1174. if (hasProperty(options, 'maxDate')) {
  1175. if (options.maxDate === undefined) {
  1176. this.maxYear = this.maxDate = undefined;
  1177. } else {
  1178. this.maxYear = startOfYearPeriod(options.maxDate, this.step);
  1179. this.maxDate = dateValue(this.maxYear, 11, 31);
  1180. }
  1181. }
  1182. if (options[this.beforeShowOption] !== undefined) {
  1183. const beforeShow = options[this.beforeShowOption];
  1184. this.beforeShow = typeof beforeShow === 'function' ? beforeShow : undefined;
  1185. }
  1186. }
  1187. // Update view's settings to reflect the viewDate set on the picker
  1188. updateFocus() {
  1189. const viewDate = new Date(this.picker.viewDate);
  1190. const first = startOfYearPeriod(viewDate, this.navStep);
  1191. const last = first + 9 * this.step;
  1192. this.first = first;
  1193. this.last = last;
  1194. this.start = first - this.step;
  1195. this.focused = startOfYearPeriod(viewDate, this.step);
  1196. }
  1197. // Update view's settings to reflect the selected dates
  1198. updateSelection() {
  1199. const {dates, rangepicker} = this.picker.datepicker;
  1200. this.selected = dates.reduce((years, timeValue) => {
  1201. return pushUnique(years, startOfYearPeriod(timeValue, this.step));
  1202. }, []);
  1203. if (rangepicker && rangepicker.dates) {
  1204. this.range = rangepicker.dates.map(timeValue => {
  1205. if (timeValue !== undefined) {
  1206. return startOfYearPeriod(timeValue, this.step);
  1207. }
  1208. });
  1209. }
  1210. }
  1211. // Update the entire view UI
  1212. render() {
  1213. // refresh disabled years on every render in order to clear the ones added
  1214. // by beforeShow hook at previous render
  1215. this.disabled = [];
  1216. this.picker.setViewSwitchLabel(`${this.first}-${this.last}`);
  1217. this.picker.setPrevBtnDisabled(this.first <= this.minYear);
  1218. this.picker.setNextBtnDisabled(this.last >= this.maxYear);
  1219. Array.from(this.grid.children).forEach((el, index) => {
  1220. const classList = el.classList;
  1221. const current = this.start + (index * this.step);
  1222. const date = dateValue(current, 0, 1);
  1223. el.className = `datepicker-cell ${this.cellClass}`;
  1224. if (this.isMinView) {
  1225. el.dataset.date = date;
  1226. }
  1227. el.textContent = el.dataset.year = current;
  1228. if (index === 0) {
  1229. classList.add('prev');
  1230. } else if (index === 11) {
  1231. classList.add('next');
  1232. }
  1233. if (current < this.minYear || current > this.maxYear) {
  1234. classList.add('disabled');
  1235. }
  1236. if (this.range) {
  1237. const [rangeStart, rangeEnd] = this.range;
  1238. if (current > rangeStart && current < rangeEnd) {
  1239. classList.add('range');
  1240. }
  1241. if (current === rangeStart) {
  1242. classList.add('range-start');
  1243. }
  1244. if (current === rangeEnd) {
  1245. classList.add('range-end');
  1246. }
  1247. }
  1248. if (this.selected.includes(current)) {
  1249. classList.add('selected');
  1250. }
  1251. if (current === this.focused) {
  1252. classList.add('focused');
  1253. }
  1254. if (this.beforeShow) {
  1255. this.performBeforeHook(el, current, date);
  1256. }
  1257. });
  1258. }
  1259. // Update the view UI by applying the changes of selected and focused items
  1260. refresh() {
  1261. const [rangeStart, rangeEnd] = this.range || [];
  1262. this.grid
  1263. .querySelectorAll('.range, .range-start, .range-end, .selected, .focused')
  1264. .forEach((el) => {
  1265. el.classList.remove('range', 'range-start', 'range-end', 'selected', 'focused');
  1266. });
  1267. Array.from(this.grid.children).forEach((el) => {
  1268. const current = Number(el.textContent);
  1269. const classList = el.classList;
  1270. if (current > rangeStart && current < rangeEnd) {
  1271. classList.add('range');
  1272. }
  1273. if (current === rangeStart) {
  1274. classList.add('range-start');
  1275. }
  1276. if (current === rangeEnd) {
  1277. classList.add('range-end');
  1278. }
  1279. if (this.selected.includes(current)) {
  1280. classList.add('selected');
  1281. }
  1282. if (current === this.focused) {
  1283. classList.add('focused');
  1284. }
  1285. });
  1286. }
  1287. // Update the view UI by applying the change of focused item
  1288. refreshFocus() {
  1289. const index = Math.round((this.focused - this.start) / this.step);
  1290. this.grid.querySelectorAll('.focused').forEach((el) => {
  1291. el.classList.remove('focused');
  1292. });
  1293. this.grid.children[index].classList.add('focused');
  1294. }
  1295. }
  1296. function triggerDatepickerEvent(datepicker, type) {
  1297. const detail = {
  1298. date: datepicker.getDate(),
  1299. viewDate: new Date(datepicker.picker.viewDate),
  1300. viewId: datepicker.picker.currentView.id,
  1301. datepicker,
  1302. };
  1303. datepicker.element.dispatchEvent(new CustomEvent(type, {detail}));
  1304. }
  1305. // direction: -1 (to previous), 1 (to next)
  1306. function goToPrevOrNext(datepicker, direction) {
  1307. const {minDate, maxDate} = datepicker.config;
  1308. const {currentView, viewDate} = datepicker.picker;
  1309. let newViewDate;
  1310. switch (currentView.id) {
  1311. case 0:
  1312. newViewDate = addMonths(viewDate, direction);
  1313. break;
  1314. case 1:
  1315. newViewDate = addYears(viewDate, direction);
  1316. break;
  1317. default:
  1318. newViewDate = addYears(viewDate, direction * currentView.navStep);
  1319. }
  1320. newViewDate = limitToRange(newViewDate, minDate, maxDate);
  1321. datepicker.picker.changeFocus(newViewDate).render();
  1322. }
  1323. function switchView(datepicker) {
  1324. const viewId = datepicker.picker.currentView.id;
  1325. if (viewId === datepicker.config.maxView) {
  1326. return;
  1327. }
  1328. datepicker.picker.changeView(viewId + 1).render();
  1329. }
  1330. function unfocus(datepicker) {
  1331. if (datepicker.config.updateOnBlur) {
  1332. datepicker.update({autohide: true});
  1333. } else {
  1334. datepicker.refresh('input');
  1335. datepicker.hide();
  1336. }
  1337. }
  1338. function goToSelectedMonthOrYear(datepicker, selection) {
  1339. const picker = datepicker.picker;
  1340. const viewDate = new Date(picker.viewDate);
  1341. const viewId = picker.currentView.id;
  1342. const newDate = viewId === 1
  1343. ? addMonths(viewDate, selection - viewDate.getMonth())
  1344. : addYears(viewDate, selection - viewDate.getFullYear());
  1345. picker.changeFocus(newDate).changeView(viewId - 1).render();
  1346. }
  1347. function onClickTodayBtn(datepicker) {
  1348. const picker = datepicker.picker;
  1349. const currentDate = today();
  1350. if (datepicker.config.todayBtnMode === 1) {
  1351. if (datepicker.config.autohide) {
  1352. datepicker.setDate(currentDate);
  1353. return;
  1354. }
  1355. datepicker.setDate(currentDate, {render: false});
  1356. picker.update();
  1357. }
  1358. if (picker.viewDate !== currentDate) {
  1359. picker.changeFocus(currentDate);
  1360. }
  1361. picker.changeView(0).render();
  1362. }
  1363. function onClickClearBtn(datepicker) {
  1364. datepicker.setDate({clear: true});
  1365. }
  1366. function onClickViewSwitch(datepicker) {
  1367. switchView(datepicker);
  1368. }
  1369. function onClickPrevBtn(datepicker) {
  1370. goToPrevOrNext(datepicker, -1);
  1371. }
  1372. function onClickNextBtn(datepicker) {
  1373. goToPrevOrNext(datepicker, 1);
  1374. }
  1375. // For the picker's main block to delegete the events from `datepicker-cell`s
  1376. function onClickView(datepicker, ev) {
  1377. const target = findElementInEventPath(ev, '.datepicker-cell');
  1378. if (!target || target.classList.contains('disabled')) {
  1379. return;
  1380. }
  1381. const {id, isMinView} = datepicker.picker.currentView;
  1382. if (isMinView) {
  1383. datepicker.setDate(Number(target.dataset.date));
  1384. } else if (id === 1) {
  1385. goToSelectedMonthOrYear(datepicker, Number(target.dataset.month));
  1386. } else {
  1387. goToSelectedMonthOrYear(datepicker, Number(target.dataset.year));
  1388. }
  1389. }
  1390. function onClickPicker(datepicker) {
  1391. if (!datepicker.inline && !datepicker.config.disableTouchKeyboard) {
  1392. datepicker.inputField.focus();
  1393. }
  1394. }
  1395. function processPickerOptions(picker, options) {
  1396. if (options.title !== undefined) {
  1397. if (options.title) {
  1398. picker.controls.title.textContent = options.title;
  1399. showElement(picker.controls.title);
  1400. } else {
  1401. picker.controls.title.textContent = '';
  1402. hideElement(picker.controls.title);
  1403. }
  1404. }
  1405. if (options.prevArrow) {
  1406. const prevBtn = picker.controls.prevBtn;
  1407. emptyChildNodes(prevBtn);
  1408. options.prevArrow.forEach((node) => {
  1409. prevBtn.appendChild(node.cloneNode(true));
  1410. });
  1411. }
  1412. if (options.nextArrow) {
  1413. const nextBtn = picker.controls.nextBtn;
  1414. emptyChildNodes(nextBtn);
  1415. options.nextArrow.forEach((node) => {
  1416. nextBtn.appendChild(node.cloneNode(true));
  1417. });
  1418. }
  1419. if (options.locale) {
  1420. picker.controls.todayBtn.textContent = options.locale.today;
  1421. picker.controls.clearBtn.textContent = options.locale.clear;
  1422. }
  1423. if (options.todayBtn !== undefined) {
  1424. if (options.todayBtn) {
  1425. showElement(picker.controls.todayBtn);
  1426. } else {
  1427. hideElement(picker.controls.todayBtn);
  1428. }
  1429. }
  1430. if (hasProperty(options, 'minDate') || hasProperty(options, 'maxDate')) {
  1431. const {minDate, maxDate} = picker.datepicker.config;
  1432. picker.controls.todayBtn.disabled = !isInRange(today(), minDate, maxDate);
  1433. }
  1434. if (options.clearBtn !== undefined) {
  1435. if (options.clearBtn) {
  1436. showElement(picker.controls.clearBtn);
  1437. } else {
  1438. hideElement(picker.controls.clearBtn);
  1439. }
  1440. }
  1441. }
  1442. // Compute view date to reset, which will be...
  1443. // - the last item of the selected dates or defaultViewDate if no selection
  1444. // - limitted to minDate or maxDate if it exceeds the range
  1445. function computeResetViewDate(datepicker) {
  1446. const {dates, config} = datepicker;
  1447. const viewDate = dates.length > 0 ? lastItemOf(dates) : config.defaultViewDate;
  1448. return limitToRange(viewDate, config.minDate, config.maxDate);
  1449. }
  1450. // Change current view's view date
  1451. function setViewDate(picker, newDate) {
  1452. const oldViewDate = new Date(picker.viewDate);
  1453. const newViewDate = new Date(newDate);
  1454. const {id, year, first, last} = picker.currentView;
  1455. const viewYear = newViewDate.getFullYear();
  1456. picker.viewDate = newDate;
  1457. if (viewYear !== oldViewDate.getFullYear()) {
  1458. triggerDatepickerEvent(picker.datepicker, 'changeYear');
  1459. }
  1460. if (newViewDate.getMonth() !== oldViewDate.getMonth()) {
  1461. triggerDatepickerEvent(picker.datepicker, 'changeMonth');
  1462. }
  1463. // return whether the new date is in different period on time from the one
  1464. // displayed in the current view
  1465. // when true, the view needs to be re-rendered on the next UI refresh.
  1466. switch (id) {
  1467. case 0:
  1468. return newDate < first || newDate > last;
  1469. case 1:
  1470. return viewYear !== year;
  1471. default:
  1472. return viewYear < first || viewYear > last;
  1473. }
  1474. }
  1475. function getTextDirection(el) {
  1476. return window.getComputedStyle(el).direction;
  1477. }
  1478. // Class representing the picker UI
  1479. class Picker {
  1480. constructor(datepicker) {
  1481. this.datepicker = datepicker;
  1482. const template = pickerTemplate.replace(/%buttonClass%/g, datepicker.config.buttonClass);
  1483. const element = this.element = parseHTML(template).firstChild;
  1484. const [header, main, footer] = element.firstChild.children;
  1485. const title = header.firstElementChild;
  1486. const [prevBtn, viewSwitch, nextBtn] = header.lastElementChild.children;
  1487. const [todayBtn, clearBtn] = footer.firstChild.children;
  1488. const controls = {
  1489. title,
  1490. prevBtn,
  1491. viewSwitch,
  1492. nextBtn,
  1493. todayBtn,
  1494. clearBtn,
  1495. };
  1496. this.main = main;
  1497. this.controls = controls;
  1498. const elementClass = datepicker.inline ? 'inline' : 'dropdown';
  1499. element.classList.add(`datepicker-${elementClass}`);
  1500. processPickerOptions(this, datepicker.config);
  1501. this.viewDate = computeResetViewDate(datepicker);
  1502. // set up event listeners
  1503. registerListeners(datepicker, [
  1504. [element, 'click', onClickPicker.bind(null, datepicker), {capture: true}],
  1505. [main, 'click', onClickView.bind(null, datepicker)],
  1506. [controls.viewSwitch, 'click', onClickViewSwitch.bind(null, datepicker)],
  1507. [controls.prevBtn, 'click', onClickPrevBtn.bind(null, datepicker)],
  1508. [controls.nextBtn, 'click', onClickNextBtn.bind(null, datepicker)],
  1509. [controls.todayBtn, 'click', onClickTodayBtn.bind(null, datepicker)],
  1510. [controls.clearBtn, 'click', onClickClearBtn.bind(null, datepicker)],
  1511. ]);
  1512. // set up views
  1513. this.views = [
  1514. new DaysView(this),
  1515. new MonthsView(this),
  1516. new YearsView(this, {id: 2, name: 'years', cellClass: 'year', step: 1}),
  1517. new YearsView(this, {id: 3, name: 'decades', cellClass: 'decade', step: 10}),
  1518. ];
  1519. this.currentView = this.views[datepicker.config.startView];
  1520. this.currentView.render();
  1521. this.main.appendChild(this.currentView.element);
  1522. datepicker.config.container.appendChild(this.element);
  1523. }
  1524. setOptions(options) {
  1525. processPickerOptions(this, options);
  1526. this.views.forEach((view) => {
  1527. view.init(options, false);
  1528. });
  1529. this.currentView.render();
  1530. }
  1531. detach() {
  1532. this.datepicker.config.container.removeChild(this.element);
  1533. }
  1534. show() {
  1535. if (this.active) {
  1536. return;
  1537. }
  1538. this.element.classList.add('active');
  1539. this.active = true;
  1540. const datepicker = this.datepicker;
  1541. if (!datepicker.inline) {
  1542. // ensure picker's direction matches input's
  1543. const inputDirection = getTextDirection(datepicker.inputField);
  1544. if (inputDirection !== getTextDirection(datepicker.config.container)) {
  1545. this.element.dir = inputDirection;
  1546. } else if (this.element.dir) {
  1547. this.element.removeAttribute('dir');
  1548. }
  1549. this.place();
  1550. if (datepicker.config.disableTouchKeyboard) {
  1551. datepicker.inputField.blur();
  1552. }
  1553. }
  1554. triggerDatepickerEvent(datepicker, 'show');
  1555. }
  1556. hide() {
  1557. if (!this.active) {
  1558. return;
  1559. }
  1560. this.datepicker.exitEditMode();
  1561. this.element.classList.remove('active');
  1562. this.active = false;
  1563. triggerDatepickerEvent(this.datepicker, 'hide');
  1564. }
  1565. place() {
  1566. const {classList, style} = this.element;
  1567. const {config, inputField} = this.datepicker;
  1568. const container = config.container;
  1569. const {
  1570. width: calendarWidth,
  1571. height: calendarHeight,
  1572. } = this.element.getBoundingClientRect();
  1573. const {
  1574. left: containerLeft,
  1575. top: containerTop,
  1576. width: containerWidth,
  1577. } = container.getBoundingClientRect();
  1578. const {
  1579. left: inputLeft,
  1580. top: inputTop,
  1581. width: inputWidth,
  1582. height: inputHeight
  1583. } = inputField.getBoundingClientRect();
  1584. let {x: orientX, y: orientY} = config.orientation;
  1585. let scrollTop;
  1586. let left;
  1587. let top;
  1588. if (container === document.body) {
  1589. scrollTop = window.scrollY;
  1590. left = inputLeft + window.scrollX;
  1591. top = inputTop + scrollTop;
  1592. } else {
  1593. scrollTop = container.scrollTop;
  1594. left = inputLeft - containerLeft;
  1595. top = inputTop - containerTop + scrollTop;
  1596. }
  1597. if (orientX === 'auto') {
  1598. if (left < 0) {
  1599. // align to the left and move into visible area if input's left edge < window's
  1600. orientX = 'left';
  1601. left = 10;
  1602. } else if (left + calendarWidth > containerWidth) {
  1603. // align to the right if canlendar's right edge > container's
  1604. orientX = 'right';
  1605. } else {
  1606. orientX = getTextDirection(inputField) === 'rtl' ? 'right' : 'left';
  1607. }
  1608. }
  1609. if (orientX === 'right') {
  1610. left -= calendarWidth - inputWidth;
  1611. }
  1612. if (orientY === 'auto') {
  1613. orientY = top - calendarHeight < scrollTop ? 'bottom' : 'top';
  1614. }
  1615. if (orientY === 'top') {
  1616. top -= calendarHeight;
  1617. } else {
  1618. top += inputHeight;
  1619. }
  1620. classList.remove(
  1621. 'datepicker-orient-top',
  1622. 'datepicker-orient-bottom',
  1623. 'datepicker-orient-right',
  1624. 'datepicker-orient-left'
  1625. );
  1626. classList.add(`datepicker-orient-${orientY}`, `datepicker-orient-${orientX}`);
  1627. style.top = top ? `${top}px` : top;
  1628. style.left = left ? `${left}px` : left;
  1629. }
  1630. setViewSwitchLabel(labelText) {
  1631. this.controls.viewSwitch.textContent = labelText;
  1632. }
  1633. setPrevBtnDisabled(disabled) {
  1634. this.controls.prevBtn.disabled = disabled;
  1635. }
  1636. setNextBtnDisabled(disabled) {
  1637. this.controls.nextBtn.disabled = disabled;
  1638. }
  1639. changeView(viewId) {
  1640. const oldView = this.currentView;
  1641. const newView = this.views[viewId];
  1642. if (newView.id !== oldView.id) {
  1643. this.currentView = newView;
  1644. this._renderMethod = 'render';
  1645. triggerDatepickerEvent(this.datepicker, 'changeView');
  1646. this.main.replaceChild(newView.element, oldView.element);
  1647. }
  1648. return this;
  1649. }
  1650. // Change the focused date (view date)
  1651. changeFocus(newViewDate) {
  1652. this._renderMethod = setViewDate(this, newViewDate) ? 'render' : 'refreshFocus';
  1653. this.views.forEach((view) => {
  1654. view.updateFocus();
  1655. });
  1656. return this;
  1657. }
  1658. // Apply the change of the selected dates
  1659. update() {
  1660. const newViewDate = computeResetViewDate(this.datepicker);
  1661. this._renderMethod = setViewDate(this, newViewDate) ? 'render' : 'refresh';
  1662. this.views.forEach((view) => {
  1663. view.updateFocus();
  1664. view.updateSelection();
  1665. });
  1666. return this;
  1667. }
  1668. // Refresh the picker UI
  1669. render(quickRender = true) {
  1670. const renderMethod = (quickRender && this._renderMethod) || 'render';
  1671. delete this._renderMethod;
  1672. this.currentView[renderMethod]();
  1673. }
  1674. }
  1675. // Find the closest date that doesn't meet the condition for unavailable date
  1676. // Returns undefined if no available date is found
  1677. // addFn: function to calculate the next date
  1678. // - args: time value, amount
  1679. // increase: amount to pass to addFn
  1680. // testFn: function to test the unavailablity of the date
  1681. // - args: time value; retun: true if unavailable
  1682. function findNextAvailableOne(date, addFn, increase, testFn, min, max) {
  1683. if (!isInRange(date, min, max)) {
  1684. return;
  1685. }
  1686. if (testFn(date)) {
  1687. const newDate = addFn(date, increase);
  1688. return findNextAvailableOne(newDate, addFn, increase, testFn, min, max);
  1689. }
  1690. return date;
  1691. }
  1692. // direction: -1 (left/up), 1 (right/down)
  1693. // vertical: true for up/down, false for left/right
  1694. function moveByArrowKey(datepicker, ev, direction, vertical) {
  1695. const picker = datepicker.picker;
  1696. const currentView = picker.currentView;
  1697. const step = currentView.step || 1;
  1698. let viewDate = picker.viewDate;
  1699. let addFn;
  1700. let testFn;
  1701. switch (currentView.id) {
  1702. case 0:
  1703. if (vertical) {
  1704. viewDate = addDays(viewDate, direction * 7);
  1705. } else if (ev.ctrlKey || ev.metaKey) {
  1706. viewDate = addYears(viewDate, direction);
  1707. } else {
  1708. viewDate = addDays(viewDate, direction);
  1709. }
  1710. addFn = addDays;
  1711. testFn = (date) => currentView.disabled.includes(date);
  1712. break;
  1713. case 1:
  1714. viewDate = addMonths(viewDate, vertical ? direction * 4 : direction);
  1715. addFn = addMonths;
  1716. testFn = (date) => {
  1717. const dt = new Date(date);
  1718. const {year, disabled} = currentView;
  1719. return dt.getFullYear() === year && disabled.includes(dt.getMonth());
  1720. };
  1721. break;
  1722. default:
  1723. viewDate = addYears(viewDate, direction * (vertical ? 4 : 1) * step);
  1724. addFn = addYears;
  1725. testFn = date => currentView.disabled.includes(startOfYearPeriod(date, step));
  1726. }
  1727. viewDate = findNextAvailableOne(
  1728. viewDate,
  1729. addFn,
  1730. direction < 0 ? -step : step,
  1731. testFn,
  1732. currentView.minDate,
  1733. currentView.maxDate
  1734. );
  1735. if (viewDate !== undefined) {
  1736. picker.changeFocus(viewDate).render();
  1737. }
  1738. }
  1739. function onKeydown(datepicker, ev) {
  1740. if (ev.key === 'Tab') {
  1741. unfocus(datepicker);
  1742. return;
  1743. }
  1744. const picker = datepicker.picker;
  1745. const {id, isMinView} = picker.currentView;
  1746. if (!picker.active) {
  1747. switch (ev.key) {
  1748. case 'ArrowDown':
  1749. case 'Escape':
  1750. picker.show();
  1751. break;
  1752. case 'Enter':
  1753. datepicker.update();
  1754. break;
  1755. default:
  1756. return;
  1757. }
  1758. } else if (datepicker.editMode) {
  1759. switch (ev.key) {
  1760. case 'Escape':
  1761. picker.hide();
  1762. break;
  1763. case 'Enter':
  1764. datepicker.exitEditMode({update: true, autohide: datepicker.config.autohide});
  1765. break;
  1766. default:
  1767. return;
  1768. }
  1769. } else {
  1770. switch (ev.key) {
  1771. case 'Escape':
  1772. picker.hide();
  1773. break;
  1774. case 'ArrowLeft':
  1775. if (ev.ctrlKey || ev.metaKey) {
  1776. goToPrevOrNext(datepicker, -1);
  1777. } else if (ev.shiftKey) {
  1778. datepicker.enterEditMode();
  1779. return;
  1780. } else {
  1781. moveByArrowKey(datepicker, ev, -1, false);
  1782. }
  1783. break;
  1784. case 'ArrowRight':
  1785. if (ev.ctrlKey || ev.metaKey) {
  1786. goToPrevOrNext(datepicker, 1);
  1787. } else if (ev.shiftKey) {
  1788. datepicker.enterEditMode();
  1789. return;
  1790. } else {
  1791. moveByArrowKey(datepicker, ev, 1, false);
  1792. }
  1793. break;
  1794. case 'ArrowUp':
  1795. if (ev.ctrlKey || ev.metaKey) {
  1796. switchView(datepicker);
  1797. } else if (ev.shiftKey) {
  1798. datepicker.enterEditMode();
  1799. return;
  1800. } else {
  1801. moveByArrowKey(datepicker, ev, -1, true);
  1802. }
  1803. break;
  1804. case 'ArrowDown':
  1805. if (ev.shiftKey && !ev.ctrlKey && !ev.metaKey) {
  1806. datepicker.enterEditMode();
  1807. return;
  1808. }
  1809. moveByArrowKey(datepicker, ev, 1, true);
  1810. break;
  1811. case 'Enter':
  1812. if (isMinView) {
  1813. datepicker.setDate(picker.viewDate);
  1814. } else {
  1815. picker.changeView(id - 1).render();
  1816. }
  1817. break;
  1818. case 'Backspace':
  1819. case 'Delete':
  1820. datepicker.enterEditMode();
  1821. return;
  1822. default:
  1823. if (ev.key.length === 1 && !ev.ctrlKey && !ev.metaKey) {
  1824. datepicker.enterEditMode();
  1825. }
  1826. return;
  1827. }
  1828. }
  1829. ev.preventDefault();
  1830. ev.stopPropagation();
  1831. }
  1832. function onFocus(datepicker) {
  1833. if (datepicker.config.showOnFocus && !datepicker._showing) {
  1834. datepicker.show();
  1835. }
  1836. }
  1837. // for the prevention for entering edit mode while getting focus on click
  1838. function onMousedown(datepicker, ev) {
  1839. const el = ev.target;
  1840. if (datepicker.picker.active || datepicker.config.showOnClick) {
  1841. el._active = el === document.activeElement;
  1842. el._clicking = setTimeout(() => {
  1843. delete el._active;
  1844. delete el._clicking;
  1845. }, 2000);
  1846. }
  1847. }
  1848. function onClickInput(datepicker, ev) {
  1849. const el = ev.target;
  1850. if (!el._clicking) {
  1851. return;
  1852. }
  1853. clearTimeout(el._clicking);
  1854. delete el._clicking;
  1855. if (el._active) {
  1856. datepicker.enterEditMode();
  1857. }
  1858. delete el._active;
  1859. if (datepicker.config.showOnClick) {
  1860. datepicker.show();
  1861. }
  1862. }
  1863. function onPaste(datepicker, ev) {
  1864. if (ev.clipboardData.types.includes('text/plain')) {
  1865. datepicker.enterEditMode();
  1866. }
  1867. }
  1868. // for the `document` to delegate the events from outside the picker/input field
  1869. function onClickOutside(datepicker, ev) {
  1870. const element = datepicker.element;
  1871. if (element !== document.activeElement) {
  1872. return;
  1873. }
  1874. const pickerElem = datepicker.picker.element;
  1875. if (findElementInEventPath(ev, el => el === element || el === pickerElem)) {
  1876. return;
  1877. }
  1878. unfocus(datepicker);
  1879. }
  1880. function stringifyDates(dates, config) {
  1881. return dates
  1882. .map(dt => formatDate(dt, config.format, config.locale))
  1883. .join(config.dateDelimiter);
  1884. }
  1885. // parse input dates and create an array of time values for selection
  1886. // returns undefined if there are no valid dates in inputDates
  1887. // when origDates (current selection) is passed, the function works to mix
  1888. // the input dates into the current selection
  1889. function processInputDates(datepicker, inputDates, clear = false) {
  1890. const {config, dates: origDates, rangepicker} = datepicker;
  1891. if (inputDates.length === 0) {
  1892. // empty input is considered valid unless origiDates is passed
  1893. return clear ? [] : undefined;
  1894. }
  1895. const rangeEnd = rangepicker && datepicker === rangepicker.datepickers[1];
  1896. let newDates = inputDates.reduce((dates, dt) => {
  1897. let date = parseDate(dt, config.format, config.locale);
  1898. if (date === undefined) {
  1899. return dates;
  1900. }
  1901. if (config.pickLevel > 0) {
  1902. // adjust to 1st of the month/Jan 1st of the year
  1903. // or to the last day of the monh/Dec 31st of the year if the datepicker
  1904. // is the range-end picker of a rangepicker
  1905. const dt = new Date(date);
  1906. if (config.pickLevel === 1) {
  1907. date = rangeEnd
  1908. ? dt.setMonth(dt.getMonth() + 1, 0)
  1909. : dt.setDate(1);
  1910. } else {
  1911. date = rangeEnd
  1912. ? dt.setFullYear(dt.getFullYear() + 1, 0, 0)
  1913. : dt.setMonth(0, 1);
  1914. }
  1915. }
  1916. if (
  1917. isInRange(date, config.minDate, config.maxDate)
  1918. && !dates.includes(date)
  1919. && !config.datesDisabled.includes(date)
  1920. && !config.daysOfWeekDisabled.includes(new Date(date).getDay())
  1921. ) {
  1922. dates.push(date);
  1923. }
  1924. return dates;
  1925. }, []);
  1926. if (newDates.length === 0) {
  1927. return;
  1928. }
  1929. if (config.multidate && !clear) {
  1930. // get the synmetric difference between origDates and newDates
  1931. newDates = newDates.reduce((dates, date) => {
  1932. if (!origDates.includes(date)) {
  1933. dates.push(date);
  1934. }
  1935. return dates;
  1936. }, origDates.filter(date => !newDates.includes(date)));
  1937. }
  1938. // do length check always because user can input multiple dates regardless of the mode
  1939. return config.maxNumberOfDates && newDates.length > config.maxNumberOfDates
  1940. ? newDates.slice(config.maxNumberOfDates * -1)
  1941. : newDates;
  1942. }
  1943. // refresh the UI elements
  1944. // modes: 1: input only, 2, picker only, 3 both
  1945. function refreshUI(datepicker, mode = 3, quickRender = true) {
  1946. const {config, picker, inputField} = datepicker;
  1947. if (mode & 2) {
  1948. const newView = picker.active ? config.pickLevel : config.startView;
  1949. picker.update().changeView(newView).render(quickRender);
  1950. }
  1951. if (mode & 1 && inputField) {
  1952. inputField.value = stringifyDates(datepicker.dates, config);
  1953. }
  1954. }
  1955. function setDate(datepicker, inputDates, options) {
  1956. let {clear, render, autohide} = options;
  1957. if (render === undefined) {
  1958. render = true;
  1959. }
  1960. if (!render) {
  1961. autohide = false;
  1962. } else if (autohide === undefined) {
  1963. autohide = datepicker.config.autohide;
  1964. }
  1965. const newDates = processInputDates(datepicker, inputDates, clear);
  1966. if (!newDates) {
  1967. return;
  1968. }
  1969. if (newDates.toString() !== datepicker.dates.toString()) {
  1970. datepicker.dates = newDates;
  1971. refreshUI(datepicker, render ? 3 : 1);
  1972. triggerDatepickerEvent(datepicker, 'changeDate');
  1973. } else {
  1974. refreshUI(datepicker, 1);
  1975. }
  1976. if (autohide) {
  1977. datepicker.hide();
  1978. }
  1979. }
  1980. /**
  1981. * Class representing a date picker
  1982. */
  1983. class Datepicker {
  1984. /**
  1985. * Create a date picker
  1986. * @param {Element} element - element to bind a date picker
  1987. * @param {Object} [options] - config options
  1988. * @param {DateRangePicker} [rangepicker] - DateRangePicker instance the
  1989. * date picker belongs to. Use this only when creating date picker as a part
  1990. * of date range picker
  1991. */
  1992. constructor(element, options = {}, rangepicker = undefined) {
  1993. element.datepicker = this;
  1994. this.element = element;
  1995. // set up config
  1996. const config = this.config = Object.assign({
  1997. buttonClass: (options.buttonClass && String(options.buttonClass)) || 'button',
  1998. container: document.body,
  1999. defaultViewDate: today(),
  2000. maxDate: undefined,
  2001. minDate: undefined,
  2002. }, processOptions(defaultOptions, this));
  2003. this._options = options;
  2004. Object.assign(config, processOptions(options, this));
  2005. // configure by type
  2006. const inline = this.inline = element.tagName !== 'INPUT';
  2007. let inputField;
  2008. let initialDates;
  2009. if (inline) {
  2010. config.container = element;
  2011. initialDates = stringToArray(element.dataset.date, config.dateDelimiter);
  2012. delete element.dataset.date;
  2013. } else {
  2014. const container = options.container ? document.querySelector(options.container) : null;
  2015. if (container) {
  2016. config.container = container;
  2017. }
  2018. inputField = this.inputField = element;
  2019. inputField.classList.add('datepicker-input');
  2020. initialDates = stringToArray(inputField.value, config.dateDelimiter);
  2021. }
  2022. if (rangepicker) {
  2023. // check validiry
  2024. const index = rangepicker.inputs.indexOf(inputField);
  2025. const datepickers = rangepicker.datepickers;
  2026. if (index < 0 || index > 1 || !Array.isArray(datepickers)) {
  2027. throw Error('Invalid rangepicker object.');
  2028. }
  2029. // attach itaelf to the rangepicker here so that processInputDates() can
  2030. // determine if this is the range-end picker of the rangepicker while
  2031. // setting inital values when pickLevel > 0
  2032. datepickers[index] = this;
  2033. // add getter for rangepicker
  2034. Object.defineProperty(this, 'rangepicker', {
  2035. get() {
  2036. return rangepicker;
  2037. },
  2038. });
  2039. }
  2040. // set initial dates
  2041. this.dates = [];
  2042. // process initial value
  2043. const inputDateValues = processInputDates(this, initialDates);
  2044. if (inputDateValues && inputDateValues.length > 0) {
  2045. this.dates = inputDateValues;
  2046. }
  2047. if (inputField) {
  2048. inputField.value = stringifyDates(this.dates, config);
  2049. }
  2050. const picker = this.picker = new Picker(this);
  2051. if (inline) {
  2052. this.show();
  2053. } else {
  2054. // set up event listeners in other modes
  2055. const onMousedownDocument = onClickOutside.bind(null, this);
  2056. const listeners = [
  2057. [inputField, 'keydown', onKeydown.bind(null, this)],
  2058. [inputField, 'focus', onFocus.bind(null, this)],
  2059. [inputField, 'mousedown', onMousedown.bind(null, this)],
  2060. [inputField, 'click', onClickInput.bind(null, this)],
  2061. [inputField, 'paste', onPaste.bind(null, this)],
  2062. [document, 'mousedown', onMousedownDocument],
  2063. [document, 'touchstart', onMousedownDocument],
  2064. [window, 'resize', picker.place.bind(picker)]
  2065. ];
  2066. registerListeners(this, listeners);
  2067. }
  2068. }
  2069. /**
  2070. * Format Date object or time value in given format and language
  2071. * @param {Date|Number} date - date or time value to format
  2072. * @param {String|Object} format - format string or object that contains
  2073. * toDisplay() custom formatter, whose signature is
  2074. * - args:
  2075. * - date: {Date} - Date instance of the date passed to the method
  2076. * - format: {Object} - the format object passed to the method
  2077. * - locale: {Object} - locale for the language specified by `lang`
  2078. * - return:
  2079. * {String} formatted date
  2080. * @param {String} [lang=en] - language code for the locale to use
  2081. * @return {String} formatted date
  2082. */
  2083. static formatDate(date, format, lang) {
  2084. return formatDate(date, format, lang && locales[lang] || locales.en);
  2085. }
  2086. /**
  2087. * Parse date string
  2088. * @param {String|Date|Number} dateStr - date string, Date object or time
  2089. * value to parse
  2090. * @param {String|Object} format - format string or object that contains
  2091. * toValue() custom parser, whose signature is
  2092. * - args:
  2093. * - dateStr: {String|Date|Number} - the dateStr passed to the method
  2094. * - format: {Object} - the format object passed to the method
  2095. * - locale: {Object} - locale for the language specified by `lang`
  2096. * - return:
  2097. * {Date|Number} parsed date or its time value
  2098. * @param {String} [lang=en] - language code for the locale to use
  2099. * @return {Number} time value of parsed date
  2100. */
  2101. static parseDate(dateStr, format, lang) {
  2102. return parseDate(dateStr, format, lang && locales[lang] || locales.en);
  2103. }
  2104. /**
  2105. * @type {Object} - Installed locales in `[languageCode]: localeObject` format
  2106. * en`:_English (US)_ is pre-installed.
  2107. */
  2108. static get locales() {
  2109. return locales;
  2110. }
  2111. /**
  2112. * @type {Boolean} - Whether the picker element is shown. `true` whne shown
  2113. */
  2114. get active() {
  2115. return !!(this.picker && this.picker.active);
  2116. }
  2117. /**
  2118. * @type {HTMLDivElement} - DOM object of picker element
  2119. */
  2120. get pickerElement() {
  2121. return this.picker ? this.picker.element : undefined;
  2122. }
  2123. /**
  2124. * Set new values to the config options
  2125. * @param {Object} options - config options to update
  2126. */
  2127. setOptions(options) {
  2128. const picker = this.picker;
  2129. const newOptions = processOptions(options, this);
  2130. Object.assign(this._options, options);
  2131. Object.assign(this.config, newOptions);
  2132. picker.setOptions(newOptions);
  2133. refreshUI(this, 3);
  2134. }
  2135. /**
  2136. * Show the picker element
  2137. */
  2138. show() {
  2139. if (this.inputField) {
  2140. if (this.inputField.disabled) {
  2141. return;
  2142. }
  2143. if (this.inputField !== document.activeElement) {
  2144. this._showing = true;
  2145. this.inputField.focus();
  2146. delete this._showing;
  2147. }
  2148. }
  2149. this.picker.show();
  2150. }
  2151. /**
  2152. * Hide the picker element
  2153. * Not available on inline picker
  2154. */
  2155. hide() {
  2156. if (this.inline) {
  2157. return;
  2158. }
  2159. this.picker.hide();
  2160. this.picker.update().changeView(this.config.startView).render();
  2161. }
  2162. /**
  2163. * Destroy the Datepicker instance
  2164. * @return {Detepicker} - the instance destroyed
  2165. */
  2166. destroy() {
  2167. this.hide();
  2168. unregisterListeners(this);
  2169. this.picker.detach();
  2170. if (!this.inline) {
  2171. this.inputField.classList.remove('datepicker-input');
  2172. }
  2173. delete this.element.datepicker;
  2174. return this;
  2175. }
  2176. /**
  2177. * Get the selected date(s)
  2178. *
  2179. * The method returns a Date object of selected date by default, and returns
  2180. * an array of selected dates in multidate mode. If format string is passed,
  2181. * it returns date string(s) formatted in given format.
  2182. *
  2183. * @param {String} [format] - Format string to stringify the date(s)
  2184. * @return {Date|String|Date[]|String[]} - selected date(s), or if none is
  2185. * selected, empty array in multidate mode and untitled in sigledate mode
  2186. */
  2187. getDate(format = undefined) {
  2188. const callback = format
  2189. ? date => formatDate(date, format, this.config.locale)
  2190. : date => new Date(date);
  2191. if (this.config.multidate) {
  2192. return this.dates.map(callback);
  2193. }
  2194. if (this.dates.length > 0) {
  2195. return callback(this.dates[0]);
  2196. }
  2197. }
  2198. /**
  2199. * Set selected date(s)
  2200. *
  2201. * In multidate mode, you can pass multiple dates as a series of arguments
  2202. * or an array. (Since each date is parsed individually, the type of the
  2203. * dates doesn't have to be the same.)
  2204. * The given dates are used to toggle the select status of each date. The
  2205. * number of selected dates is kept from exceeding the length set to
  2206. * maxNumberOfDates.
  2207. *
  2208. * With clear: true option, the method can be used to clear the selection
  2209. * and to replace the selection instead of toggling in multidate mode.
  2210. * If the option is passed with no date arguments or an empty dates array,
  2211. * it works as "clear" (clear the selection then set nothing), and if the
  2212. * option is passed with new dates to select, it works as "replace" (clear
  2213. * the selection then set the given dates)
  2214. *
  2215. * When render: false option is used, the method omits re-rendering the
  2216. * picker element. In this case, you need to call refresh() method later in
  2217. * order for the picker element to reflect the changes. The input field is
  2218. * refreshed always regardless of this option.
  2219. *
  2220. * When invalid (unparsable, repeated, disabled or out-of-range) dates are
  2221. * passed, the method ignores them and applies only valid ones. In the case
  2222. * that all the given dates are invalid, which is distinguished from passing
  2223. * no dates, the method considers it as an error and leaves the selection
  2224. * untouched.
  2225. *
  2226. * @param {...(Date|Number|String)|Array} [dates] - Date strings, Date
  2227. * objects, time values or mix of those for new selection
  2228. * @param {Object} [options] - function options
  2229. * - clear: {boolean} - Whether to clear the existing selection
  2230. * defualt: false
  2231. * - render: {boolean} - Whether to re-render the picker element
  2232. * default: true
  2233. * - autohide: {boolean} - Whether to hide the picker element after re-render
  2234. * Ignored when used with render: false
  2235. * default: config.autohide
  2236. */
  2237. setDate(...args) {
  2238. const dates = [...args];
  2239. const opts = {};
  2240. const lastArg = lastItemOf(args);
  2241. if (
  2242. typeof lastArg === 'object'
  2243. && !Array.isArray(lastArg)
  2244. && !(lastArg instanceof Date)
  2245. && lastArg
  2246. ) {
  2247. Object.assign(opts, dates.pop());
  2248. }
  2249. const inputDates = Array.isArray(dates[0]) ? dates[0] : dates;
  2250. setDate(this, inputDates, opts);
  2251. }
  2252. /**
  2253. * Update the selected date(s) with input field's value
  2254. * Not available on inline picker
  2255. *
  2256. * The input field will be refreshed with properly formatted date string.
  2257. *
  2258. * @param {Object} [options] - function options
  2259. * - autohide: {boolean} - whether to hide the picker element after refresh
  2260. * default: false
  2261. */
  2262. update(options = undefined) {
  2263. if (this.inline) {
  2264. return;
  2265. }
  2266. const opts = {clear: true, autohide: !!(options && options.autohide)};
  2267. const inputDates = stringToArray(this.inputField.value, this.config.dateDelimiter);
  2268. setDate(this, inputDates, opts);
  2269. }
  2270. /**
  2271. * Refresh the picker element and the associated input field
  2272. * @param {String} [target] - target item when refreshing one item only
  2273. * 'picker' or 'input'
  2274. * @param {Boolean} [forceRender] - whether to re-render the picker element
  2275. * regardless of its state instead of optimized refresh
  2276. */
  2277. refresh(target = undefined, forceRender = false) {
  2278. if (target && typeof target !== 'string') {
  2279. forceRender = target;
  2280. target = undefined;
  2281. }
  2282. let mode;
  2283. if (target === 'picker') {
  2284. mode = 2;
  2285. } else if (target === 'input') {
  2286. mode = 1;
  2287. } else {
  2288. mode = 3;
  2289. }
  2290. refreshUI(this, mode, !forceRender);
  2291. }
  2292. /**
  2293. * Enter edit mode
  2294. * Not available on inline picker or when the picker element is hidden
  2295. */
  2296. enterEditMode() {
  2297. if (this.inline || !this.picker.active || this.editMode) {
  2298. return;
  2299. }
  2300. this.editMode = true;
  2301. this.inputField.classList.add('in-edit');
  2302. }
  2303. /**
  2304. * Exit from edit mode
  2305. * Not available on inline picker
  2306. * @param {Object} [options] - function options
  2307. * - update: {boolean} - whether to call update() after exiting
  2308. * If false, input field is revert to the existing selection
  2309. * default: false
  2310. */
  2311. exitEditMode(options = undefined) {
  2312. if (this.inline || !this.editMode) {
  2313. return;
  2314. }
  2315. const opts = Object.assign({update: false}, options);
  2316. delete this.editMode;
  2317. this.inputField.classList.remove('in-edit');
  2318. if (opts.update) {
  2319. this.update(opts);
  2320. }
  2321. }
  2322. }
  2323. // filter out the config options inapproprite to pass to Datepicker
  2324. function filterOptions(options) {
  2325. const newOpts = Object.assign({}, options);
  2326. delete newOpts.inputs;
  2327. delete newOpts.allowOneSidedRange;
  2328. delete newOpts.maxNumberOfDates; // to ensure each datepicker handles a single date
  2329. return newOpts;
  2330. }
  2331. function setupDatepicker(rangepicker, changeDateListener, el, options) {
  2332. registerListeners(rangepicker, [
  2333. [el, 'changeDate', changeDateListener],
  2334. ]);
  2335. new Datepicker(el, options, rangepicker);
  2336. }
  2337. function onChangeDate(rangepicker, ev) {
  2338. // to prevent both datepickers trigger the other side's update each other
  2339. if (rangepicker._updating) {
  2340. return;
  2341. }
  2342. rangepicker._updating = true;
  2343. const target = ev.target;
  2344. if (target.datepicker === undefined) {
  2345. return;
  2346. }
  2347. const datepickers = rangepicker.datepickers;
  2348. const setDateOptions = {render: false};
  2349. const changedSide = rangepicker.inputs.indexOf(target);
  2350. const otherSide = changedSide === 0 ? 1 : 0;
  2351. const changedDate = datepickers[changedSide].dates[0];
  2352. const otherDate = datepickers[otherSide].dates[0];
  2353. if (changedDate !== undefined && otherDate !== undefined) {
  2354. // if the start of the range > the end, swap them
  2355. if (changedSide === 0 && changedDate > otherDate) {
  2356. datepickers[0].setDate(otherDate, setDateOptions);
  2357. datepickers[1].setDate(changedDate, setDateOptions);
  2358. } else if (changedSide === 1 && changedDate < otherDate) {
  2359. datepickers[0].setDate(changedDate, setDateOptions);
  2360. datepickers[1].setDate(otherDate, setDateOptions);
  2361. }
  2362. } else if (!rangepicker.allowOneSidedRange) {
  2363. // to prevent the range from becoming one-sided, copy changed side's
  2364. // selection (no matter if it's empty) to the other side
  2365. if (changedDate !== undefined || otherDate !== undefined) {
  2366. setDateOptions.clear = true;
  2367. datepickers[otherSide].setDate(datepickers[changedSide].dates, setDateOptions);
  2368. }
  2369. }
  2370. datepickers[0].picker.update().render();
  2371. datepickers[1].picker.update().render();
  2372. delete rangepicker._updating;
  2373. }
  2374. /**
  2375. * Class representing a date range picker
  2376. */
  2377. class DateRangePicker {
  2378. /**
  2379. * Create a date range picker
  2380. * @param {Element} element - element to bind a date range picker
  2381. * @param {Object} [options] - config options
  2382. */
  2383. constructor(element, options = {}) {
  2384. const inputs = Array.isArray(options.inputs)
  2385. ? options.inputs
  2386. : Array.from(element.querySelectorAll('input'));
  2387. if (inputs.length < 2) {
  2388. return;
  2389. }
  2390. element.rangepicker = this;
  2391. this.element = element;
  2392. this.inputs = inputs.slice(0, 2);
  2393. this.allowOneSidedRange = !!options.allowOneSidedRange;
  2394. const changeDateListener = onChangeDate.bind(null, this);
  2395. const cleanOptions = filterOptions(options);
  2396. // in order for initial date setup to work right when pcicLvel > 0,
  2397. // let Datepicker constructor add the instance to the rangepicker
  2398. const datepickers = [];
  2399. Object.defineProperty(this, 'datepickers', {
  2400. get() {
  2401. return datepickers;
  2402. },
  2403. });
  2404. setupDatepicker(this, changeDateListener, this.inputs[0], cleanOptions);
  2405. setupDatepicker(this, changeDateListener, this.inputs[1], cleanOptions);
  2406. Object.freeze(datepickers);
  2407. // normalize the range if inital dates are given
  2408. if (datepickers[0].dates.length > 0) {
  2409. onChangeDate(this, {target: this.inputs[0]});
  2410. } else if (datepickers[1].dates.length > 0) {
  2411. onChangeDate(this, {target: this.inputs[1]});
  2412. }
  2413. }
  2414. /**
  2415. * @type {Array} - selected date of the linked date pickers
  2416. */
  2417. get dates() {
  2418. return this.datepickers.length === 2
  2419. ? [
  2420. this.datepickers[0].dates[0],
  2421. this.datepickers[1].dates[0],
  2422. ]
  2423. : undefined;
  2424. }
  2425. /**
  2426. * Set new values to the config options
  2427. * @param {Object} options - config options to update
  2428. */
  2429. setOptions(options) {
  2430. this.allowOneSidedRange = !!options.allowOneSidedRange;
  2431. const cleanOptions = filterOptions(options);
  2432. this.datepickers[0].setOptions(cleanOptions);
  2433. this.datepickers[1].setOptions(cleanOptions);
  2434. }
  2435. /**
  2436. * Destroy the DateRangePicker instance
  2437. * @return {DateRangePicker} - the instance destroyed
  2438. */
  2439. destroy() {
  2440. this.datepickers[0].destroy();
  2441. this.datepickers[1].destroy();
  2442. unregisterListeners(this);
  2443. delete this.element.rangepicker;
  2444. }
  2445. /**
  2446. * Get the start and end dates of the date range
  2447. *
  2448. * The method returns Date objects by default. If format string is passed,
  2449. * it returns date strings formatted in given format.
  2450. * The result array always contains 2 items (start date/end date) and
  2451. * undefined is used for unselected side. (e.g. If none is selected,
  2452. * the result will be [undefined, undefined]. If only the end date is set
  2453. * when allowOneSidedRange config option is true, [undefined, endDate] will
  2454. * be returned.)
  2455. *
  2456. * @param {String} [format] - Format string to stringify the dates
  2457. * @return {Array} - Start and end dates
  2458. */
  2459. getDates(format = undefined) {
  2460. const callback = format
  2461. ? date => formatDate(date, format, this.datepickers[0].config.locale)
  2462. : date => new Date(date);
  2463. return this.dates.map(date => date === undefined ? date : callback(date));
  2464. }
  2465. /**
  2466. * Set the start and end dates of the date range
  2467. *
  2468. * The method calls datepicker.setDate() internally using each of the
  2469. * arguments in start→end order.
  2470. *
  2471. * When a clear: true option object is passed instead of a date, the method
  2472. * clears the date.
  2473. *
  2474. * If an invalid date, the same date as the current one or an option object
  2475. * without clear: true is passed, the method considers that argument as an
  2476. * "ineffective" argument because calling datepicker.setDate() with those
  2477. * values makes no changes to the date selection.
  2478. *
  2479. * When the allowOneSidedRange config option is false, passing {clear: true}
  2480. * to clear the range works only when it is done to the last effective
  2481. * argument (in other words, passed to rangeEnd or to rangeStart along with
  2482. * ineffective rangeEnd). This is because when the date range is changed,
  2483. * it gets normalized based on the last change at the end of the changing
  2484. * process.
  2485. *
  2486. * @param {Date|Number|String|Object} rangeStart - Start date of the range
  2487. * or {clear: true} to clear the date
  2488. * @param {Date|Number|String|Object} rangeEnd - End date of the range
  2489. * or {clear: true} to clear the date
  2490. */
  2491. setDates(rangeStart, rangeEnd) {
  2492. const [datepicker0, datepicker1] = this.datepickers;
  2493. const origDates = this.dates;
  2494. // If range normalization runs on every change, we can't set a new range
  2495. // that starts after the end of the current range correctly because the
  2496. // normalization process swaps start↔︎end right after setting the new start
  2497. // date. To prevent this, the normalization process needs to run once after
  2498. // both of the new dates are set.
  2499. this._updating = true;
  2500. datepicker0.setDate(rangeStart);
  2501. datepicker1.setDate(rangeEnd);
  2502. delete this._updating;
  2503. if (datepicker1.dates[0] !== origDates[1]) {
  2504. onChangeDate(this, {target: this.inputs[1]});
  2505. } else if (datepicker0.dates[0] !== origDates[0]) {
  2506. onChangeDate(this, {target: this.inputs[0]});
  2507. }
  2508. }
  2509. }
  2510. window.Datepicker = Datepicker;
  2511. window.DateRangePicker = DateRangePicker;
  2512. }());