lightpick.js 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329
  1. /**
  2. * @author: Rinat G. http://coding.kz
  3. * @copyright: Copyright (c) 2019 Rinat G.
  4. * @license: Licensed under the MIT license. See http://www.opensource.org/licenses/mit-license.php
  5. */
  6. // Following the UMD template https://github.com/umdjs/umd/blob/master/templates/returnExportsGlobal.js
  7. (function (root, factory) {
  8. if (typeof define === 'function' && define.amd) {
  9. // AMD. Make globaly available as well
  10. define(['moment'], function (moment) {
  11. return factory(moment);
  12. });
  13. } else if (typeof module === 'object' && module.exports) {
  14. // Node / Browserify
  15. var moment = (typeof window != 'undefined' && typeof window.moment != 'undefined') ? window.moment : require('moment');
  16. module.exports = factory(moment);
  17. } else {
  18. // Browser globals
  19. root.Lightpick = factory(root.moment);
  20. }
  21. }(this, function(moment) {
  22. 'use strict';
  23. var document = window.document,
  24. defaults = {
  25. field: null,
  26. secondField: null,
  27. firstDay: 1,
  28. parentEl: 'body',
  29. lang: 'auto',
  30. format: 'DD/MM/YYYY',
  31. separator: ' - ',
  32. numberOfMonths: 1,
  33. numberOfColumns: 2,
  34. singleDate: true,
  35. autoclose: true,
  36. repick: false,
  37. startDate: null,
  38. endDate: null,
  39. minDate: null,
  40. maxDate: null,
  41. disableDates: null,
  42. selectForward: false,
  43. selectBackward: false,
  44. minDays: null,
  45. maxDays: null,
  46. hoveringTooltip: true,
  47. hideOnBodyClick: true,
  48. footer: false,
  49. disabledDatesInRange: true,
  50. tooltipNights: false,
  51. orientation: 'auto',
  52. disableWeekends: false,
  53. inline: false,
  54. weekdayStyle: 'short',
  55. dropdowns: {
  56. years: {
  57. min: 1900,
  58. max: null,
  59. },
  60. months: true,
  61. },
  62. locale: {
  63. buttons: {
  64. prev: '←',
  65. next: '→',
  66. close: '×',
  67. reset: 'Reset',
  68. apply: 'Apply',
  69. },
  70. tooltip: {
  71. one: 'day',
  72. other: 'days',
  73. },
  74. tooltipOnDisabled: null,
  75. pluralize: function(i, locale){
  76. if (typeof i === "string") i = parseInt(i, 10);
  77. if (i === 1 && 'one' in locale) return locale.one;
  78. if ('other' in locale) return locale.other;
  79. return '';
  80. },
  81. },
  82. onSelect: null,
  83. onSelectStart: null,
  84. onSelectEnd: null,
  85. onOpen: null,
  86. onClose: null,
  87. onError: null,
  88. onMonthsChange: null,
  89. onYearsChange: null
  90. },
  91. renderTopButtons = function(opts)
  92. {
  93. return '<div class="lightpick__toolbar">'
  94. + ''
  95. + '<button type="button" class="lightpick__previous-action">' + opts.locale.buttons.prev + '</button>'
  96. + '<button type="button" class="lightpick__next-action">' + opts.locale.buttons.next + '</button>'
  97. + (!opts.autoclose && !opts.inline ? '<button type="button" class="lightpick__close-action">' + opts.locale.buttons.close + '</button>' : '')
  98. + '</div>';
  99. },
  100. weekdayName = function(opts, day, weekdayStyle)
  101. {
  102. return new Date(1970, 0, day, 12, 0, 0, 0).toLocaleString(opts.lang, { weekday: weekdayStyle || opts.weekdayStyle });
  103. },
  104. renderDay = function(opts, date, dummy, extraClass)
  105. {
  106. if (dummy) return '<div></div>';
  107. var date = moment(date),
  108. prevMonth = moment(date).subtract(1, 'month'),
  109. nextMonth = moment(date).add(1, 'month');
  110. var day = {
  111. time: moment(date).valueOf(),
  112. className: ['lightpick__day', 'is-available']
  113. };
  114. if (extraClass instanceof Array || Object.prototype.toString.call(extraClass) === '[object Array]') {
  115. extraClass = extraClass.filter( function( el ) {
  116. return ['lightpick__day', 'is-available', 'is-previous-month', 'is-next-month'].indexOf( el ) >= 0;
  117. });
  118. day.className = day.className.concat(extraClass);
  119. }
  120. else {
  121. day.className.push(extraClass);
  122. }
  123. if (opts.disableDates) {
  124. for (var i = 0; i < opts.disableDates.length; i++) {
  125. if (opts.disableDates[i] instanceof Array || Object.prototype.toString.call(opts.disableDates[i]) === '[object Array]') {
  126. var _from = moment(opts.disableDates[i][0], opts.format),
  127. _to = moment(opts.disableDates[i][1], opts.format);
  128. if (_from.isValid() && _to.isValid() && date.isBetween(_from, _to, 'day', '[]')){
  129. day.className.push('is-disabled');
  130. }
  131. }
  132. else if (moment(opts.disableDates[i], opts.format).isValid() && moment(opts.disableDates[i], opts.format).isSame(date, 'day')) {
  133. day.className.push('is-disabled');
  134. }
  135. if (day.className.indexOf('is-disabled') >= 0) {
  136. if (opts.locale.tooltipOnDisabled && (!opts.startDate || date.isAfter(opts.startDate) || opts.startDate && opts.endDate)) {
  137. day.className.push('disabled-tooltip');
  138. }
  139. if (day.className.indexOf('is-start-date') >= 0) {
  140. this.setStartDate(null);
  141. this.setEndDate(null);
  142. }
  143. else if (day.className.indexOf('is-end-date') >= 0) {
  144. this.setEndDate(null);
  145. }
  146. }
  147. }
  148. }
  149. if (opts.minDays && opts.startDate && !opts.endDate) {
  150. if (date.isBetween(moment(opts.startDate).subtract(opts.minDays - 1, 'day'), moment(opts.startDate).add(opts.minDays - 1, 'day'), 'day')) {
  151. day.className.push('is-disabled');
  152. if (opts.selectForward && date.isSameOrAfter(opts.startDate)) {
  153. day.className.push('is-forward-selected');
  154. day.className.push('is-in-range');
  155. }
  156. }
  157. }
  158. if (opts.maxDays && opts.startDate && !opts.endDate) {
  159. if (date.isSameOrBefore(moment(opts.startDate).subtract(opts.maxDays, 'day'), 'day')) {
  160. day.className.push('is-disabled');
  161. }
  162. else if (date.isSameOrAfter(moment(opts.startDate).add(opts.maxDays, 'day'), 'day')) {
  163. day.className.push('is-disabled');
  164. }
  165. }
  166. if (opts.repick && (opts.minDays || opts.maxDays) && opts.startDate && opts.endDate) {
  167. var tempStartDate = moment(opts.repickTrigger == opts.field ? opts.endDate : opts.startDate);
  168. if (opts.minDays) {
  169. if (date.isBetween(moment(tempStartDate).subtract(opts.minDays - 1, 'day'), moment(tempStartDate).add(opts.minDays - 1, 'day'), 'day')) {
  170. day.className.push('is-disabled');
  171. }
  172. }
  173. if (opts.maxDays) {
  174. if (date.isSameOrBefore(moment(tempStartDate).subtract(opts.maxDays, 'day'), 'day')) {
  175. day.className.push('is-disabled');
  176. }
  177. else if (date.isSameOrAfter(moment(tempStartDate).add(opts.maxDays, 'day'), 'day')) {
  178. day.className.push('is-disabled');
  179. }
  180. }
  181. }
  182. if (date.isSame(new Date(), 'day')) {
  183. day.className.push('is-today');
  184. }
  185. if (date.isSame(opts.startDate, 'day')) {
  186. day.className.push('is-start-date');
  187. }
  188. if (date.isSame(opts.endDate, 'day')) {
  189. day.className.push('is-end-date');
  190. }
  191. if (opts.startDate && opts.endDate && date.isBetween(opts.startDate, opts.endDate, 'day', '[]')) {
  192. day.className.push('is-in-range');
  193. }
  194. if (moment().isSame(date, 'month')) {
  195. }
  196. else if (prevMonth.isSame(date, 'month')) {
  197. day.className.push('is-previous-month');
  198. }
  199. else if (nextMonth.isSame(date, 'month')) {
  200. day.className.push('is-next-month');
  201. }
  202. if (opts.minDate && date.isBefore(opts.minDate, 'day')) {
  203. day.className.push('is-disabled');
  204. }
  205. if (opts.maxDate && date.isAfter(opts.maxDate, 'day')) {
  206. day.className.push('is-disabled');
  207. }
  208. if (opts.selectForward && !opts.singleDate && opts.startDate && !opts.endDate && date.isBefore(opts.startDate, 'day')) {
  209. day.className.push('is-disabled');
  210. }
  211. if (opts.selectBackward && !opts.singleDate && opts.startDate && !opts.endDate && date.isAfter(opts.startDate, 'day')) {
  212. day.className.push('is-disabled');
  213. }
  214. if (opts.disableWeekends && (date.isoWeekday() == 6 || date.isoWeekday() == 7)) {
  215. day.className.push('is-disabled');
  216. }
  217. day.className = day.className.filter(function(value, index, self) {
  218. return self.indexOf(value) === index;
  219. });
  220. if (day.className.indexOf('is-disabled') >= 0 && day.className.indexOf('is-available') >= 0) {
  221. day.className.splice(day.className.indexOf('is-available'), 1);
  222. }
  223. var div = document.createElement('div');
  224. div.className = day.className.join(' ');
  225. div.innerHTML = date.get('date');
  226. div.setAttribute('data-time', day.time);
  227. return div.outerHTML;
  228. },
  229. renderMonthsList = function(date, opts)
  230. {
  231. var d = moment(date),
  232. select = document.createElement('select');
  233. for (var idx = 0; idx < 12; idx++) {
  234. d.set('month', idx);
  235. var option = document.createElement('option');
  236. option.value = d.toDate().getMonth();
  237. option.text = d.toDate().toLocaleString(opts.lang, { month: 'long' });
  238. if (idx === date.toDate().getMonth()) {
  239. option.setAttribute('selected', 'selected');
  240. }
  241. select.appendChild(option);
  242. }
  243. select.className = 'lightpick__select lightpick__select-months';
  244. // for text align to right
  245. select.dir = 'rtl';
  246. if (!opts.dropdowns || !opts.dropdowns.months) {
  247. select.disabled = true;
  248. }
  249. return select.outerHTML;
  250. },
  251. renderYearsList = function(date, opts)
  252. {
  253. var d = moment(date),
  254. select = document.createElement('select'),
  255. years = opts.dropdowns && opts.dropdowns.years ? opts.dropdowns.years : null,
  256. minYear = years && years.min ? years.min : 1900,
  257. maxYear = years && years.max ? years.max : Number.parseInt(moment().format('YYYY'));
  258. if (Number.parseInt(date.format('YYYY')) < minYear) {
  259. minYear = Number.parseInt(date.format('YYYY'));
  260. }
  261. if (Number.parseInt(date.format('YYYY')) > maxYear) {
  262. maxYear = Number.parseInt(date.format('YYYY'));
  263. }
  264. for (var idx = minYear; idx <= maxYear; idx++) {
  265. d.set('year', idx);
  266. var option = document.createElement('option');
  267. option.value = d.toDate().getFullYear();
  268. option.text = d.toDate().getFullYear();
  269. if (idx === date.toDate().getFullYear()) {
  270. option.setAttribute('selected', 'selected');
  271. }
  272. select.appendChild(option);
  273. }
  274. select.className = 'lightpick__select lightpick__select-years';
  275. if (!opts.dropdowns || !opts.dropdowns.years) {
  276. select.disabled = true;
  277. }
  278. return select.outerHTML;
  279. },
  280. renderCalendar = function(el, opts)
  281. {
  282. var html = '',
  283. monthDate = moment(opts.calendar[0]);
  284. for (var i = 0; i < opts.numberOfMonths; i++) {
  285. var day = moment(monthDate);
  286. html += '<section class="lightpick__month">';
  287. html += '<header class="lightpick__month-title-bar">'
  288. html += '<div class="lightpick__month-title">'
  289. + renderMonthsList(day, opts)
  290. + renderYearsList(day, opts)
  291. + '</div>';
  292. if (opts.numberOfMonths === 1) {
  293. html += renderTopButtons(opts, 'days');
  294. }
  295. html += '</header>'; // lightpick__month-title-bar
  296. html += '<div class="lightpick__days-of-the-week">';
  297. for (var w = opts.firstDay + 4; w < 7 + opts.firstDay + 4; ++w) {
  298. html += '<div class="lightpick__day-of-the-week" title="' + weekdayName(opts, w, 'long') + '">' + weekdayName(opts, w) + '</div>';
  299. }
  300. html += '</div>'; // lightpick__days-of-the-week
  301. html += '<div class="lightpick__days">';
  302. if (day.isoWeekday() !== opts.firstDay) {
  303. var prevDays = day.isoWeekday() - opts.firstDay > 0 ? day.isoWeekday() - opts.firstDay : day.isoWeekday(),
  304. prevMonth = moment(day).subtract(prevDays, 'day'),
  305. daysInMonth = prevMonth.daysInMonth();
  306. for (var d = prevMonth.get('date'); d <= daysInMonth; d++) {
  307. html += renderDay(opts, prevMonth, i > 0, 'is-previous-month');
  308. prevMonth.add(1, 'day');
  309. }
  310. }
  311. var daysInMonth = day.daysInMonth(),
  312. today = new Date();
  313. for (var d = 0; d < daysInMonth; d++) {
  314. html += renderDay(opts, day);
  315. day.add(1, 'day');
  316. }
  317. var nextMonth = moment(day),
  318. nextDays = 7 - nextMonth.isoWeekday() + opts.firstDay;
  319. if (nextDays < 7) {
  320. for (var d = nextMonth.get('date'); d <= nextDays; d++) {
  321. html += renderDay(opts, nextMonth, i < opts.numberOfMonths - 1, 'is-next-month');
  322. nextMonth.add(1, 'day');
  323. }
  324. }
  325. html += '</div>'; // lightpick__days
  326. html += '</section>'; // lightpick__month
  327. monthDate.add(1, 'month');
  328. }
  329. opts.calendar[1] = moment(monthDate);
  330. el.querySelector('.lightpick__months').innerHTML = html;
  331. },
  332. updateDates = function(el, opts)
  333. {
  334. var days = el.querySelectorAll('.lightpick__day');
  335. [].forEach.call(days, function(day) {
  336. day.outerHTML = renderDay(opts, parseInt(day.getAttribute('data-time')), false, day.className.split(' '));
  337. });
  338. checkDisabledDatesInRange(el, opts);
  339. },
  340. checkDisabledDatesInRange = function(el, opts)
  341. {
  342. if (opts.disabledDatesInRange || !opts.startDate || opts.endDate || !opts.disableDates) return;
  343. var days = el.querySelectorAll('.lightpick__day'),
  344. disabledArray = opts.disableDates.map(function(entry){
  345. return entry instanceof Array || Object.prototype.toString.call(entry) === '[object Array]' ? entry[0] : entry;
  346. }),
  347. closestPrev = moment(disabledArray.filter(function(d) {
  348. return moment(d).isBefore(opts.startDate);
  349. }).sort(function(a,b){
  350. return moment(b).isAfter(moment(a));
  351. })[0]),
  352. closestNext = moment(disabledArray.filter(function(d) {
  353. return moment(d).isAfter(opts.startDate);
  354. }).sort(function(a,b){
  355. return moment(a).isAfter(moment(b));
  356. })[0]);
  357. [].forEach.call(days, function(dayCell) {
  358. var day = moment(parseInt(dayCell.getAttribute('data-time')));
  359. if (
  360. (closestPrev && day.isBefore(closestPrev) && opts.startDate.isAfter(closestPrev))
  361. || (closestNext && day.isAfter(closestNext) && closestNext.isAfter(opts.startDate))
  362. ) {
  363. dayCell.classList.remove('is-available');
  364. dayCell.classList.add('is-disabled');
  365. }
  366. });
  367. },
  368. Lightpick = function(options)
  369. {
  370. var self = this,
  371. opts = self.config(options);
  372. self.el = document.createElement('section');
  373. self.el.className = 'lightpick lightpick--' + opts.numberOfColumns + '-columns is-hidden';
  374. if (opts.inline) {
  375. self.el.className += ' lightpick--inlined';
  376. }
  377. var html = '<div class="lightpick__inner">'
  378. + (opts.numberOfMonths > 1 ? renderTopButtons(opts, 'days') : '')
  379. + '<div class="lightpick__months"></div>'
  380. + '<div class="lightpick__tooltip" style="visibility: hidden"></div>';
  381. if (opts.footer) {
  382. html += '<div class="lightpick__footer">';
  383. if (opts.footer === true) {
  384. html += '<button type="button" class="lightpick__reset-action">' + opts.locale.buttons.reset + '</button>';
  385. html += '<div class="lightpick__footer-message"></div>';
  386. html += '<button type="button" class="lightpick__apply-action">' + opts.locale.buttons.apply + '</button>';
  387. }
  388. else {
  389. html += opts.footer;
  390. }
  391. html += '</div>';
  392. }
  393. html += '</div>';
  394. self.el.innerHTML = html;
  395. if (opts.parentEl instanceof Node) {
  396. opts.parentEl.appendChild(self.el)
  397. }
  398. else if (opts.parentEl === 'body' && opts.inline) {
  399. opts.field.parentNode.appendChild(self.el);
  400. }
  401. else {
  402. document.querySelector(opts.parentEl).appendChild(self.el);
  403. }
  404. self._onMouseDown = function(e)
  405. {
  406. if (!self.isShowing) {
  407. return;
  408. }
  409. e = e || window.event;
  410. var target = e.target || e.srcElement;
  411. if (!target) {
  412. return;
  413. }
  414. e.stopPropagation();
  415. if (!target.classList.contains('lightpick__select')) {
  416. e.preventDefault();
  417. }
  418. var opts = self._opts;
  419. if (target.classList.contains('lightpick__day') && target.classList.contains('is-available')) {
  420. var day = moment(parseInt(target.getAttribute('data-time')));
  421. if (!opts.disabledDatesInRange && opts.disableDates && opts.startDate) {
  422. var start = day.isAfter(opts.startDate) ? moment(opts.startDate) : moment(day),
  423. end = day.isAfter(opts.startDate) ? moment(day) : moment(opts.startDate),
  424. isInvalidRange = opts.disableDates.filter(function(d) {
  425. if (d instanceof Array || Object.prototype.toString.call(d) === '[object Array]') {
  426. var _from = moment(d[0]),
  427. _to = moment(d[1]);
  428. return _from.isValid() && _to.isValid() && (_from.isBetween(start, end, 'day', '[]') || _to.isBetween(start, end, 'day', '[]'));
  429. }
  430. return moment(d).isBetween(start, end, 'day', '[]');
  431. });
  432. if (isInvalidRange.length) {
  433. self.setStartDate(null);
  434. self.setEndDate(null);
  435. target.dispatchEvent(new Event('mousedown'));
  436. self.el.querySelector('.lightpick__tooltip').style.visibility = 'hidden';
  437. updateDates(self.el, opts);
  438. return;
  439. }
  440. }
  441. if (opts.singleDate || (!opts.startDate && !opts.endDate) || (opts.startDate && opts.endDate)) {
  442. if (opts.repick && opts.startDate && opts.endDate) {
  443. if (opts.repickTrigger === opts.field) {
  444. self.setStartDate(day);
  445. target.classList.add('is-start-date');
  446. }
  447. else {
  448. self.setEndDate(day);
  449. target.classList.add('is-end-date');
  450. }
  451. if (opts.startDate.isAfter(opts.endDate)) {
  452. self.swapDate();
  453. }
  454. if (opts.autoclose) {
  455. setTimeout(function() {
  456. self.hide();
  457. }, 100);
  458. }
  459. }
  460. else {
  461. self.setStartDate(day);
  462. self.setEndDate(null);
  463. target.classList.add('is-start-date');
  464. if (opts.singleDate && opts.autoclose) {
  465. setTimeout(function() {
  466. self.hide();
  467. }, 100);
  468. }
  469. else if (!opts.singleDate || opts.inline || !opts.autoclose) {
  470. updateDates(self.el, opts);
  471. }
  472. }
  473. }
  474. else if (opts.startDate && !opts.endDate) {
  475. self.setEndDate(day);
  476. if (opts.startDate.isAfter(opts.endDate)) {
  477. self.swapDate();
  478. }
  479. target.classList.add('is-end-date');
  480. if (opts.autoclose) {
  481. setTimeout(function() {
  482. self.hide();
  483. }, 100);
  484. }
  485. else {
  486. updateDates(self.el, opts);
  487. }
  488. }
  489. if (!opts.disabledDatesInRange) {
  490. if (self.el.querySelectorAll('.lightpick__day.is-available').length === 0) {
  491. self.setStartDate(null);
  492. updateDates(self.el, opts);
  493. if (opts.footer) {
  494. if (typeof self._opts.onError === 'function') {
  495. self._opts.onError.call(self, 'Invalid range');
  496. }
  497. else {
  498. var footerMessage = self.el.querySelector('.lightpick__footer-message');
  499. if (footerMessage) {
  500. footerMessage.innerHTML = opts.locale.not_allowed_range;
  501. setTimeout(function(){
  502. footerMessage.innerHTML = '';
  503. }, 3000);
  504. }
  505. }
  506. }
  507. }
  508. }
  509. }
  510. else if (target.classList.contains('lightpick__previous-action')) {
  511. self.prevMonth();
  512. }
  513. else if (target.classList.contains('lightpick__next-action')) {
  514. self.nextMonth();
  515. }
  516. else if (target.classList.contains('lightpick__close-action') || target.classList.contains('lightpick__apply-action')) {
  517. self.hide();
  518. }
  519. else if (target.classList.contains('lightpick__reset-action')) {
  520. self.reset();
  521. }
  522. };
  523. self._onMouseEnter = function(e)
  524. {
  525. if (!self.isShowing) {
  526. return;
  527. }
  528. e = e || window.event;
  529. var target = e.target || e.srcElement;
  530. if (!target) {
  531. return;
  532. }
  533. var opts = self._opts;
  534. if (target.classList.contains('lightpick__day') && target.classList.contains('disabled-tooltip') && opts.locale.tooltipOnDisabled) {
  535. self.showTooltip(target, opts.locale.tooltipOnDisabled);
  536. return;
  537. }
  538. else {
  539. self.hideTooltip();
  540. }
  541. if (opts.singleDate || (!opts.startDate && !opts.endDate)) {
  542. return;
  543. }
  544. if (!target.classList.contains('lightpick__day') && !target.classList.contains('is-available')) {
  545. return;
  546. }
  547. if ((opts.startDate && !opts.endDate) || opts.repick) {
  548. var hoverDate = moment(parseInt(target.getAttribute('data-time')));
  549. if (!hoverDate.isValid()) {
  550. return;
  551. }
  552. var startDate = (opts.startDate && !opts.endDate) || (opts.repick && opts.repickTrigger === opts.secondField) ? opts.startDate : opts.endDate;
  553. var days = self.el.querySelectorAll('.lightpick__day');
  554. [].forEach.call(days, function(day) {
  555. var dt = moment(parseInt(day.getAttribute('data-time')));
  556. day.classList.remove('is-flipped');
  557. if (dt.isValid() && dt.isSameOrAfter(startDate, 'day') && dt.isSameOrBefore(hoverDate, 'day')) {
  558. day.classList.add('is-in-range');
  559. if (opts.repickTrigger === opts.field && dt.isSameOrAfter(opts.endDate)) {
  560. day.classList.add('is-flipped');
  561. }
  562. }
  563. else if (dt.isValid() && dt.isSameOrAfter(hoverDate, 'day') && dt.isSameOrBefore(startDate, 'day')) {
  564. day.classList.add('is-in-range');
  565. if (((opts.startDate && !opts.endDate) || opts.repickTrigger === opts.secondField) && dt.isSameOrBefore(opts.startDate)) {
  566. day.classList.add('is-flipped');
  567. }
  568. }
  569. else {
  570. day.classList.remove('is-in-range');
  571. }
  572. if (opts.startDate && opts.endDate && opts.repick && opts.repickTrigger === opts.field) {
  573. day.classList.remove('is-start-date');
  574. }
  575. else {
  576. day.classList.remove('is-end-date');
  577. }
  578. });
  579. if (opts.hoveringTooltip) {
  580. days = Math.abs(hoverDate.isAfter(startDate) ? hoverDate.diff(startDate, 'day') : startDate.diff(hoverDate, 'day'));
  581. if (!opts.tooltipNights) {
  582. days += 1;
  583. }
  584. var tooltip = self.el.querySelector('.lightpick__tooltip');
  585. if (days > 0 && !target.classList.contains('is-disabled')) {
  586. var pluralText = '';
  587. if (typeof opts.locale.pluralize === 'function') {
  588. pluralText = opts.locale.pluralize.call(self, days, opts.locale.tooltip);
  589. }
  590. self.showTooltip(target, days + ' ' + pluralText);
  591. }
  592. else {
  593. self.hideTooltip();
  594. }
  595. }
  596. if (opts.startDate && opts.endDate && opts.repick && opts.repickTrigger === opts.field) {
  597. target.classList.add('is-start-date');
  598. }
  599. else {
  600. target.classList.add('is-end-date');
  601. }
  602. }
  603. };
  604. self._onChange = function(e)
  605. {
  606. e = e || window.event;
  607. var target = e.target || e.srcElement;
  608. if (!target) {
  609. return;
  610. }
  611. if (target.classList.contains('lightpick__select-months')) {
  612. if (typeof self._opts.onMonthsChange === 'function') {
  613. self._opts.onMonthsChange.call(this, target.value);
  614. }
  615. self.gotoMonth(target.value);
  616. }
  617. else if (target.classList.contains('lightpick__select-years')) {
  618. if (typeof self._opts.onYearsChange === 'function') {
  619. self._opts.onYearsChange.call(this, target.value);
  620. }
  621. self.gotoYear(target.value);
  622. }
  623. };
  624. self._onInputChange = function(e)
  625. {
  626. var target = e.target || e.srcElement;
  627. if (self._opts.singleDate) {
  628. if (!self._opts.autoclose) {
  629. self.gotoDate(opts.field.value);
  630. }
  631. }
  632. self.syncFields();
  633. if (!self.isShowing) {
  634. self.show();
  635. }
  636. };
  637. self._onInputFocus = function(e)
  638. {
  639. var target = e.target || e.srcElement;
  640. self.show(target);
  641. };
  642. self._onInputClick = function(e)
  643. {
  644. var target = e.target || e.srcElement;
  645. self.show(target);
  646. };
  647. self._onClick = function(e)
  648. {
  649. e = e || window.event;
  650. var target = e.target || e.srcElement,
  651. parentEl = target;
  652. if (!target) {
  653. return;
  654. }
  655. do {
  656. if ((parentEl.classList && parentEl.classList.contains('lightpick')) || parentEl === opts.field || (opts.secondField && parentEl === opts.secondField)) {
  657. return;
  658. }
  659. }
  660. while ((parentEl = parentEl.parentNode));
  661. if (self.isShowing && opts.hideOnBodyClick && target !== opts.field && parentEl !== opts.field) {
  662. self.hide();
  663. }
  664. };
  665. self.showTooltip = function(target, text)
  666. {
  667. var tooltip = self.el.querySelector('.lightpick__tooltip');
  668. var hasParentEl = self.el.classList.contains('lightpick--inlined'),
  669. dayBounding = target.getBoundingClientRect(),
  670. pickerBouding = hasParentEl ? self.el.parentNode.getBoundingClientRect() : self.el.getBoundingClientRect(),
  671. _left = (dayBounding.left - pickerBouding.left) + (dayBounding.width / 2),
  672. _top = dayBounding.top - pickerBouding.top;
  673. tooltip.style.visibility = 'visible';
  674. tooltip.textContent = text;
  675. var tooltipBounding = tooltip.getBoundingClientRect();
  676. _top -= tooltipBounding.height;
  677. _left -= (tooltipBounding.width / 2);
  678. setTimeout(function(){
  679. tooltip.style.top = _top + 'px';
  680. tooltip.style.left = _left + 'px';
  681. }, 10);
  682. };
  683. self.hideTooltip = function()
  684. {
  685. var tooltip = self.el.querySelector('.lightpick__tooltip');
  686. tooltip.style.visibility = 'hidden';
  687. };
  688. self.el.addEventListener('mousedown', self._onMouseDown, true);
  689. self.el.addEventListener('mouseenter', self._onMouseEnter, true);
  690. self.el.addEventListener('touchend', self._onMouseDown, true);
  691. self.el.addEventListener('change', self._onChange, true);
  692. if (opts.inline) {
  693. self.show();
  694. }
  695. else {
  696. self.hide();
  697. }
  698. opts.field.addEventListener('change', self._onInputChange);
  699. opts.field.addEventListener('click', self._onInputClick);
  700. opts.field.addEventListener('focus', self._onInputFocus);
  701. if (opts.secondField) {
  702. opts.secondField.addEventListener('change', self._onInputChange);
  703. opts.secondField.addEventListener('click', self._onInputClick);
  704. opts.secondField.addEventListener('focus', self._onInputFocus);
  705. }
  706. };
  707. Lightpick.prototype = {
  708. config: function(options)
  709. {
  710. var opts = Object.assign({}, defaults, options);
  711. opts.field = (opts.field && opts.field.nodeName) ? opts.field : null;
  712. opts.calendar = [moment().set('date', 1)];
  713. if (opts.numberOfMonths === 1 && opts.numberOfColumns > 1) {
  714. opts.numberOfColumns = 1;
  715. }
  716. opts.minDate = opts.minDate && moment(opts.minDate, opts.format).isValid() ? moment(opts.minDate, opts.format) : null;
  717. opts.maxDate = opts.maxDate && moment(opts.maxDate, opts.format).isValid() ? moment(opts.maxDate, opts.format) : null;
  718. if (opts.lang === 'auto') {
  719. var browserLang = navigator.language || navigator.userLanguage;
  720. if (browserLang) {
  721. opts.lang = browserLang;
  722. }
  723. else {
  724. opts.lang = 'en-US';
  725. }
  726. }
  727. if (opts.secondField && opts.singleDate) {
  728. opts.singleDate = false;
  729. }
  730. if (opts.hoveringTooltip && opts.singleDate) {
  731. opts.hoveringTooltip = false;
  732. }
  733. if (Object.prototype.toString.call(options.locale) === '[object Object]') {
  734. opts.locale = Object.assign({}, defaults.locale, options.locale);
  735. }
  736. if (window.innerWidth < 480 && opts.numberOfMonths > 1) {
  737. opts.numberOfMonths = 1;
  738. opts.numberOfColumns = 1;
  739. }
  740. if (opts.repick && !opts.secondField) {
  741. opts.repick = false;
  742. }
  743. if (opts.inline) {
  744. opts.autoclose = false;
  745. opts.hideOnBodyClick = false;
  746. }
  747. this._opts = Object.assign({}, opts);
  748. this.syncFields();
  749. this.setStartDate(this._opts.startDate, true);
  750. this.setEndDate(this._opts.endDate, true);
  751. return this._opts;
  752. },
  753. syncFields: function()
  754. {
  755. if (this._opts.singleDate || this._opts.secondField) {
  756. if (moment(this._opts.field.value, this._opts.format).isValid()) {
  757. this._opts.startDate = moment(this._opts.field.value, this._opts.format);
  758. }
  759. if (this._opts.secondField && moment(this._opts.secondField.value, this._opts.format).isValid()) {
  760. this._opts.endDate = moment(this._opts.secondField.value, this._opts.format);
  761. }
  762. }
  763. else {
  764. var dates = this._opts.field.value.split(this._opts.separator);
  765. if (dates.length === 2) {
  766. if (moment(dates[0], this._opts.format).isValid()) {
  767. this._opts.startDate = moment(dates[0], this._opts.format);
  768. }
  769. if (moment(dates[1], this._opts.format).isValid()) {
  770. this._opts.endDate = moment(dates[1], this._opts.format);
  771. }
  772. }
  773. }
  774. },
  775. swapDate: function()
  776. {
  777. var tmp = moment(this._opts.startDate);
  778. this.setDateRange(this._opts.endDate, tmp);
  779. },
  780. gotoToday: function()
  781. {
  782. this.gotoDate(new Date());
  783. },
  784. gotoDate: function(date)
  785. {
  786. var date = moment(date, this._opts.format);
  787. if (!date.isValid()) {
  788. date = moment();
  789. }
  790. date.set('date', 1);
  791. this._opts.calendar = [moment(date)];
  792. renderCalendar(this.el, this._opts);
  793. },
  794. gotoMonth: function(month)
  795. {
  796. if (isNaN(month)) {
  797. return;
  798. }
  799. this._opts.calendar[0].set('month', month);
  800. renderCalendar(this.el, this._opts);
  801. },
  802. gotoYear: function(year)
  803. {
  804. if (isNaN(year)) {
  805. return;
  806. }
  807. this._opts.calendar[0].set('year', year);
  808. renderCalendar(this.el, this._opts);
  809. },
  810. prevMonth: function()
  811. {
  812. this._opts.calendar[0] = moment(this._opts.calendar[0]).subtract(this._opts.numberOfMonths, 'month');
  813. renderCalendar(this.el, this._opts);
  814. checkDisabledDatesInRange(this.el, this._opts);
  815. },
  816. nextMonth: function()
  817. {
  818. this._opts.calendar[0] = moment(this._opts.calendar[1]);
  819. renderCalendar(this.el, this._opts);
  820. checkDisabledDatesInRange(this.el, this._opts);
  821. },
  822. updatePosition: function()
  823. {
  824. if (this.el.classList.contains('lightpick--inlined')) return;
  825. // remove `is-hidden` class for getBoundingClientRect
  826. this.el.classList.remove('is-hidden');
  827. var rect = this._opts.field.getBoundingClientRect(),
  828. calRect = this.el.getBoundingClientRect(),
  829. orientation = this._opts.orientation.split(' '),
  830. top = 0,
  831. left = 0;
  832. if (orientation[0] == 'auto' || !(/top|bottom/.test(orientation[0]))) {
  833. if (rect.bottom + calRect.height > window.innerHeight && window.pageYOffset > calRect.height) {
  834. top = (rect.top + window.pageYOffset) - calRect.height;
  835. }
  836. else {
  837. top = rect.bottom + window.pageYOffset;
  838. }
  839. }
  840. else {
  841. top = rect[orientation[0]] + window.pageYOffset;
  842. if (orientation[0] == 'top') {
  843. top -= calRect.height;
  844. }
  845. }
  846. if (!(/left|right/.test(orientation[0])) && (!orientation[1] || orientation[1] == 'auto' || !(/left|right/.test(orientation[1])))) {
  847. if (rect.left + calRect.width > window.innerWidth) {
  848. left = (rect.right + window.pageXOffset) - calRect.width;
  849. }
  850. else {
  851. left = rect.left + window.pageXOffset;
  852. }
  853. }
  854. else {
  855. if (/left|right/.test(orientation[0])) {
  856. left = rect[orientation[0]] + window.pageXOffset;
  857. }
  858. else {
  859. left = rect[orientation[1]] + window.pageXOffset;
  860. }
  861. if (orientation[0] == 'right' || orientation[1] == 'right') {
  862. left -= calRect.width;
  863. }
  864. }
  865. this.el.classList.add('is-hidden');
  866. this.el.style.top = top + 'px';
  867. this.el.style.left = left + 'px';
  868. },
  869. setStartDate: function(date, preventOnSelect)
  870. {
  871. var dateISO = moment(date, moment.ISO_8601),
  872. dateOptFormat = moment(date, this._opts.format);
  873. if (!dateISO.isValid() && !dateOptFormat.isValid()) {
  874. this._opts.startDate = null;
  875. this._opts.field.value = '';
  876. return;
  877. }
  878. this._opts.startDate = moment(dateISO.isValid() ? dateISO : dateOptFormat);
  879. if (this._opts.singleDate || this._opts.secondField) {
  880. this._opts.field.value = this._opts.startDate.format(this._opts.format);
  881. }
  882. else {
  883. this._opts.field.value = this._opts.startDate.format(this._opts.format) + this._opts.separator + '...'
  884. }
  885. if (!preventOnSelect && typeof this._opts.onSelect === 'function') {
  886. this._opts.onSelect.call(this, this.getStartDate(), this.getEndDate());
  887. }
  888. if (!preventOnSelect && !this._opts.singleDate && typeof this._opts.onSelectStart === 'function') {
  889. this._opts.onSelectStart.call(this, this.getStartDate());
  890. }
  891. },
  892. setEndDate: function(date, preventOnSelect)
  893. {
  894. var dateISO = moment(date, moment.ISO_8601),
  895. dateOptFormat = moment(date, this._opts.format);
  896. if (!dateISO.isValid() && !dateOptFormat.isValid()) {
  897. this._opts.endDate = null;
  898. if (this._opts.secondField) {
  899. this._opts.secondField.value = '';
  900. }
  901. else if (!this._opts.singleDate && this._opts.startDate) {
  902. this._opts.field.value = this._opts.startDate.format(this._opts.format) + this._opts.separator + '...'
  903. }
  904. return;
  905. }
  906. this._opts.endDate = moment(dateISO.isValid() ? dateISO : dateOptFormat);
  907. if (this._opts.secondField) {
  908. this._opts.field.value = this._opts.startDate.format(this._opts.format);
  909. this._opts.secondField.value = this._opts.endDate.format(this._opts.format);
  910. }
  911. else {
  912. this._opts.field.value = this._opts.startDate.format(this._opts.format) + this._opts.separator + this._opts.endDate.format(this._opts.format);
  913. }
  914. if (!preventOnSelect && typeof this._opts.onSelect === 'function') {
  915. this._opts.onSelect.call(this, this.getStartDate(), this.getEndDate());
  916. }
  917. if (!preventOnSelect && !this._opts.singleDate && typeof this._opts.onSelectEnd === 'function') {
  918. this._opts.onSelectEnd.call(this, this.getEndDate());
  919. }
  920. },
  921. setDate: function(date, preventOnSelect)
  922. {
  923. if (!this._opts.singleDate) {
  924. return;
  925. }
  926. this.setStartDate(date, preventOnSelect);
  927. if (this.isShowing) {
  928. updateDates(this.el, this._opts);
  929. }
  930. },
  931. setDateRange: function(start, end, preventOnSelect)
  932. {
  933. if (this._opts.singleDate) {
  934. return;
  935. }
  936. this.setStartDate(start, true);
  937. this.setEndDate(end, true);
  938. if (this.isShowing) {
  939. updateDates(this.el, this._opts);
  940. }
  941. if (!preventOnSelect && typeof this._opts.onSelect === 'function') {
  942. this._opts.onSelect.call(this, this.getStartDate(), this.getEndDate());
  943. }
  944. },
  945. setDisableDates: function(dates)
  946. {
  947. this._opts.disableDates = dates;
  948. if (this.isShowing) {
  949. updateDates(this.el, this._opts);
  950. }
  951. },
  952. getStartDate: function()
  953. {
  954. return moment(this._opts.startDate).isValid() ? this._opts.startDate.clone() : null;
  955. },
  956. getEndDate: function()
  957. {
  958. return moment(this._opts.endDate).isValid() ? this._opts.endDate.clone() : null;
  959. },
  960. getDate: function()
  961. {
  962. return moment(this._opts.startDate).isValid() ? this._opts.startDate.clone() : null;
  963. },
  964. toString: function(format)
  965. {
  966. if (this._opts.singleDate) {
  967. return moment(this._opts.startDate).isValid() ? this._opts.startDate.format(format) : '';
  968. }
  969. if (moment(this._opts.startDate).isValid() && moment(this._opts.endDate).isValid()) {
  970. return this._opts.startDate.format(format) + this._opts.separator + this._opts.endDate.format(format);
  971. }
  972. if (moment(this._opts.startDate).isValid() && !moment(this._opts.endDate).isValid()) {
  973. return this._opts.startDate.format(format) + this._opts.separator + '...';
  974. }
  975. if (!moment(this._opts.startDate).isValid() && moment(this._opts.endDate).isValid()) {
  976. return '...' + this._opts.separator + this._opts.endDate.format(format);
  977. }
  978. return '';
  979. },
  980. show: function(target)
  981. {
  982. if (!this.isShowing) {
  983. this.isShowing = true;
  984. if (this._opts.repick) {
  985. this._opts.repickTrigger = target;
  986. }
  987. this.syncFields();
  988. if (this._opts.secondField && this._opts.secondField === target && this._opts.endDate) {
  989. this.gotoDate(this._opts.endDate);
  990. }
  991. else {
  992. this.gotoDate(this._opts.startDate);
  993. }
  994. document.addEventListener('click', this._onClick);
  995. this.updatePosition();
  996. this.el.classList.remove('is-hidden');
  997. if (typeof this._opts.onOpen === 'function') {
  998. this._opts.onOpen.call(this);
  999. }
  1000. if (document.activeElement && document.activeElement != document.body) {
  1001. document.activeElement.blur();
  1002. }
  1003. }
  1004. },
  1005. hide: function()
  1006. {
  1007. if (this.isShowing) {
  1008. this.isShowing = false;
  1009. document.removeEventListener('click', this._onClick);
  1010. this.el.classList.add('is-hidden');
  1011. this.el.querySelector('.lightpick__tooltip').style.visibility = 'hidden';
  1012. if (typeof this._opts.onClose === 'function') {
  1013. this._opts.onClose.call(this);
  1014. }
  1015. }
  1016. },
  1017. destroy: function()
  1018. {
  1019. var opts = this._opts;
  1020. this.hide();
  1021. this.el.removeEventListener('mousedown', self._onMouseDown, true);
  1022. this.el.removeEventListener('mouseenter', self._onMouseEnter, true);
  1023. this.el.removeEventListener('touchend', self._onMouseDown, true);
  1024. this.el.removeEventListener('change', self._onChange, true);
  1025. opts.field.removeEventListener('change', this._onInputChange);
  1026. opts.field.removeEventListener('click', this._onInputClick);
  1027. opts.field.removeEventListener('focus', this._onInputFocus);
  1028. if (opts.secondField) {
  1029. opts.secondField.removeEventListener('change', this._onInputChange);
  1030. opts.secondField.removeEventListener('click', this._onInputClick);
  1031. opts.secondField.removeEventListener('focus', this._onInputFocus);
  1032. }
  1033. if (this.el.parentNode) {
  1034. this.el.parentNode.removeChild(this.el);
  1035. }
  1036. },
  1037. reset: function()
  1038. {
  1039. this.setStartDate(null, true);
  1040. this.setEndDate(null, true);
  1041. updateDates(this.el, this._opts);
  1042. if (typeof this._opts.onSelect === 'function') {
  1043. this._opts.onSelect.call(this, this.getStartDate(), this.getEndDate());
  1044. }
  1045. this.el.querySelector('.lightpick__tooltip').style.visibility = 'hidden';
  1046. },
  1047. reloadOptions: function(options)
  1048. {
  1049. var dropdowns = this._opts.dropdowns;
  1050. var locale = this._opts.locale;
  1051. Object.assign(this._opts, this._opts, options);
  1052. Object.assign(this._opts.dropdowns, dropdowns, options.dropdowns);
  1053. Object.assign(this._opts.locale, locale, options.locale);
  1054. }
  1055. };
  1056. return Lightpick;
  1057. }));