123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329 |
- /**
- * @author: Rinat G. http://coding.kz
- * @copyright: Copyright (c) 2019 Rinat G.
- * @license: Licensed under the MIT license. See http://www.opensource.org/licenses/mit-license.php
- */
- // Following the UMD template https://github.com/umdjs/umd/blob/master/templates/returnExportsGlobal.js
- (function (root, factory) {
- if (typeof define === 'function' && define.amd) {
- // AMD. Make globaly available as well
- define(['moment'], function (moment) {
- return factory(moment);
- });
- } else if (typeof module === 'object' && module.exports) {
- // Node / Browserify
- var moment = (typeof window != 'undefined' && typeof window.moment != 'undefined') ? window.moment : require('moment');
- module.exports = factory(moment);
- } else {
- // Browser globals
- root.Lightpick = factory(root.moment);
- }
- }(this, function(moment) {
- 'use strict';
- var document = window.document,
- defaults = {
- field: null,
- secondField: null,
- firstDay: 1,
- parentEl: 'body',
- lang: 'auto',
- format: 'DD/MM/YYYY',
- separator: ' - ',
- numberOfMonths: 1,
- numberOfColumns: 2,
- singleDate: true,
- autoclose: true,
- repick: false,
- startDate: null,
- endDate: null,
- minDate: null,
- maxDate: null,
- disableDates: null,
- selectForward: false,
- selectBackward: false,
- minDays: null,
- maxDays: null,
- hoveringTooltip: true,
- hideOnBodyClick: true,
- footer: false,
- disabledDatesInRange: true,
- tooltipNights: false,
- orientation: 'auto',
- disableWeekends: false,
- inline: false,
- weekdayStyle: 'short',
- dropdowns: {
- years: {
- min: 1900,
- max: null,
- },
- months: true,
- },
- locale: {
- buttons: {
- prev: '←',
- next: '→',
- close: '×',
- reset: 'Reset',
- apply: 'Apply',
- },
- tooltip: {
- one: 'day',
- other: 'days',
- },
- tooltipOnDisabled: null,
- pluralize: function(i, locale){
- if (typeof i === "string") i = parseInt(i, 10);
- if (i === 1 && 'one' in locale) return locale.one;
- if ('other' in locale) return locale.other;
- return '';
- },
- },
- onSelect: null,
- onSelectStart: null,
- onSelectEnd: null,
- onOpen: null,
- onClose: null,
- onError: null,
- onMonthsChange: null,
- onYearsChange: null
- },
- renderTopButtons = function(opts)
- {
- return '<div class="lightpick__toolbar">'
- + ''
- + '<button type="button" class="lightpick__previous-action">' + opts.locale.buttons.prev + '</button>'
- + '<button type="button" class="lightpick__next-action">' + opts.locale.buttons.next + '</button>'
- + (!opts.autoclose && !opts.inline ? '<button type="button" class="lightpick__close-action">' + opts.locale.buttons.close + '</button>' : '')
- + '</div>';
- },
- weekdayName = function(opts, day, weekdayStyle)
- {
- return new Date(1970, 0, day, 12, 0, 0, 0).toLocaleString(opts.lang, { weekday: weekdayStyle || opts.weekdayStyle });
- },
- renderDay = function(opts, date, dummy, extraClass)
- {
- if (dummy) return '<div></div>';
- var date = moment(date),
- prevMonth = moment(date).subtract(1, 'month'),
- nextMonth = moment(date).add(1, 'month');
- var day = {
- time: moment(date).valueOf(),
- className: ['lightpick__day', 'is-available']
- };
- if (extraClass instanceof Array || Object.prototype.toString.call(extraClass) === '[object Array]') {
- extraClass = extraClass.filter( function( el ) {
- return ['lightpick__day', 'is-available', 'is-previous-month', 'is-next-month'].indexOf( el ) >= 0;
- });
- day.className = day.className.concat(extraClass);
- }
- else {
- day.className.push(extraClass);
- }
- if (opts.disableDates) {
- for (var i = 0; i < opts.disableDates.length; i++) {
- if (opts.disableDates[i] instanceof Array || Object.prototype.toString.call(opts.disableDates[i]) === '[object Array]') {
- var _from = moment(opts.disableDates[i][0], opts.format),
- _to = moment(opts.disableDates[i][1], opts.format);
- if (_from.isValid() && _to.isValid() && date.isBetween(_from, _to, 'day', '[]')){
- day.className.push('is-disabled');
- }
- }
- else if (moment(opts.disableDates[i], opts.format).isValid() && moment(opts.disableDates[i], opts.format).isSame(date, 'day')) {
- day.className.push('is-disabled');
- }
- if (day.className.indexOf('is-disabled') >= 0) {
- if (opts.locale.tooltipOnDisabled && (!opts.startDate || date.isAfter(opts.startDate) || opts.startDate && opts.endDate)) {
- day.className.push('disabled-tooltip');
- }
- if (day.className.indexOf('is-start-date') >= 0) {
- this.setStartDate(null);
- this.setEndDate(null);
- }
- else if (day.className.indexOf('is-end-date') >= 0) {
- this.setEndDate(null);
- }
- }
- }
- }
- if (opts.minDays && opts.startDate && !opts.endDate) {
- if (date.isBetween(moment(opts.startDate).subtract(opts.minDays - 1, 'day'), moment(opts.startDate).add(opts.minDays - 1, 'day'), 'day')) {
- day.className.push('is-disabled');
- if (opts.selectForward && date.isSameOrAfter(opts.startDate)) {
- day.className.push('is-forward-selected');
- day.className.push('is-in-range');
- }
- }
- }
- if (opts.maxDays && opts.startDate && !opts.endDate) {
- if (date.isSameOrBefore(moment(opts.startDate).subtract(opts.maxDays, 'day'), 'day')) {
- day.className.push('is-disabled');
- }
- else if (date.isSameOrAfter(moment(opts.startDate).add(opts.maxDays, 'day'), 'day')) {
- day.className.push('is-disabled');
- }
- }
- if (opts.repick && (opts.minDays || opts.maxDays) && opts.startDate && opts.endDate) {
- var tempStartDate = moment(opts.repickTrigger == opts.field ? opts.endDate : opts.startDate);
- if (opts.minDays) {
- if (date.isBetween(moment(tempStartDate).subtract(opts.minDays - 1, 'day'), moment(tempStartDate).add(opts.minDays - 1, 'day'), 'day')) {
- day.className.push('is-disabled');
- }
- }
- if (opts.maxDays) {
- if (date.isSameOrBefore(moment(tempStartDate).subtract(opts.maxDays, 'day'), 'day')) {
- day.className.push('is-disabled');
- }
- else if (date.isSameOrAfter(moment(tempStartDate).add(opts.maxDays, 'day'), 'day')) {
- day.className.push('is-disabled');
- }
- }
- }
- if (date.isSame(new Date(), 'day')) {
- day.className.push('is-today');
- }
- if (date.isSame(opts.startDate, 'day')) {
- day.className.push('is-start-date');
- }
- if (date.isSame(opts.endDate, 'day')) {
- day.className.push('is-end-date');
- }
- if (opts.startDate && opts.endDate && date.isBetween(opts.startDate, opts.endDate, 'day', '[]')) {
- day.className.push('is-in-range');
- }
- if (moment().isSame(date, 'month')) {
- }
- else if (prevMonth.isSame(date, 'month')) {
- day.className.push('is-previous-month');
- }
- else if (nextMonth.isSame(date, 'month')) {
- day.className.push('is-next-month');
- }
- if (opts.minDate && date.isBefore(opts.minDate, 'day')) {
- day.className.push('is-disabled');
- }
- if (opts.maxDate && date.isAfter(opts.maxDate, 'day')) {
- day.className.push('is-disabled');
- }
- if (opts.selectForward && !opts.singleDate && opts.startDate && !opts.endDate && date.isBefore(opts.startDate, 'day')) {
- day.className.push('is-disabled');
- }
- if (opts.selectBackward && !opts.singleDate && opts.startDate && !opts.endDate && date.isAfter(opts.startDate, 'day')) {
- day.className.push('is-disabled');
- }
- if (opts.disableWeekends && (date.isoWeekday() == 6 || date.isoWeekday() == 7)) {
- day.className.push('is-disabled');
- }
- day.className = day.className.filter(function(value, index, self) {
- return self.indexOf(value) === index;
- });
- if (day.className.indexOf('is-disabled') >= 0 && day.className.indexOf('is-available') >= 0) {
- day.className.splice(day.className.indexOf('is-available'), 1);
- }
- var div = document.createElement('div');
- div.className = day.className.join(' ');
- div.innerHTML = date.get('date');
- div.setAttribute('data-time', day.time);
- return div.outerHTML;
- },
- renderMonthsList = function(date, opts)
- {
- var d = moment(date),
- select = document.createElement('select');
- for (var idx = 0; idx < 12; idx++) {
- d.set('month', idx);
- var option = document.createElement('option');
- option.value = d.toDate().getMonth();
- option.text = d.toDate().toLocaleString(opts.lang, { month: 'long' });
- if (idx === date.toDate().getMonth()) {
- option.setAttribute('selected', 'selected');
- }
- select.appendChild(option);
- }
- select.className = 'lightpick__select lightpick__select-months';
- // for text align to right
- select.dir = 'rtl';
- if (!opts.dropdowns || !opts.dropdowns.months) {
- select.disabled = true;
- }
- return select.outerHTML;
- },
- renderYearsList = function(date, opts)
- {
- var d = moment(date),
- select = document.createElement('select'),
- years = opts.dropdowns && opts.dropdowns.years ? opts.dropdowns.years : null,
- minYear = years && years.min ? years.min : 1900,
- maxYear = years && years.max ? years.max : Number.parseInt(moment().format('YYYY'));
- if (Number.parseInt(date.format('YYYY')) < minYear) {
- minYear = Number.parseInt(date.format('YYYY'));
- }
- if (Number.parseInt(date.format('YYYY')) > maxYear) {
- maxYear = Number.parseInt(date.format('YYYY'));
- }
- for (var idx = minYear; idx <= maxYear; idx++) {
- d.set('year', idx);
- var option = document.createElement('option');
- option.value = d.toDate().getFullYear();
- option.text = d.toDate().getFullYear();
- if (idx === date.toDate().getFullYear()) {
- option.setAttribute('selected', 'selected');
- }
- select.appendChild(option);
- }
- select.className = 'lightpick__select lightpick__select-years';
- if (!opts.dropdowns || !opts.dropdowns.years) {
- select.disabled = true;
- }
- return select.outerHTML;
- },
- renderCalendar = function(el, opts)
- {
- var html = '',
- monthDate = moment(opts.calendar[0]);
- for (var i = 0; i < opts.numberOfMonths; i++) {
- var day = moment(monthDate);
- html += '<section class="lightpick__month">';
- html += '<header class="lightpick__month-title-bar">'
- html += '<div class="lightpick__month-title">'
- + renderMonthsList(day, opts)
- + renderYearsList(day, opts)
- + '</div>';
- if (opts.numberOfMonths === 1) {
- html += renderTopButtons(opts, 'days');
- }
- html += '</header>'; // lightpick__month-title-bar
- html += '<div class="lightpick__days-of-the-week">';
- for (var w = opts.firstDay + 4; w < 7 + opts.firstDay + 4; ++w) {
- html += '<div class="lightpick__day-of-the-week" title="' + weekdayName(opts, w, 'long') + '">' + weekdayName(opts, w) + '</div>';
- }
- html += '</div>'; // lightpick__days-of-the-week
- html += '<div class="lightpick__days">';
- if (day.isoWeekday() !== opts.firstDay) {
- var prevDays = day.isoWeekday() - opts.firstDay > 0 ? day.isoWeekday() - opts.firstDay : day.isoWeekday(),
- prevMonth = moment(day).subtract(prevDays, 'day'),
- daysInMonth = prevMonth.daysInMonth();
- for (var d = prevMonth.get('date'); d <= daysInMonth; d++) {
- html += renderDay(opts, prevMonth, i > 0, 'is-previous-month');
- prevMonth.add(1, 'day');
- }
- }
- var daysInMonth = day.daysInMonth(),
- today = new Date();
- for (var d = 0; d < daysInMonth; d++) {
- html += renderDay(opts, day);
- day.add(1, 'day');
- }
- var nextMonth = moment(day),
- nextDays = 7 - nextMonth.isoWeekday() + opts.firstDay;
- if (nextDays < 7) {
- for (var d = nextMonth.get('date'); d <= nextDays; d++) {
- html += renderDay(opts, nextMonth, i < opts.numberOfMonths - 1, 'is-next-month');
- nextMonth.add(1, 'day');
- }
- }
- html += '</div>'; // lightpick__days
- html += '</section>'; // lightpick__month
- monthDate.add(1, 'month');
- }
- opts.calendar[1] = moment(monthDate);
- el.querySelector('.lightpick__months').innerHTML = html;
- },
- updateDates = function(el, opts)
- {
- var days = el.querySelectorAll('.lightpick__day');
- [].forEach.call(days, function(day) {
- day.outerHTML = renderDay(opts, parseInt(day.getAttribute('data-time')), false, day.className.split(' '));
- });
- checkDisabledDatesInRange(el, opts);
- },
- checkDisabledDatesInRange = function(el, opts)
- {
- if (opts.disabledDatesInRange || !opts.startDate || opts.endDate || !opts.disableDates) return;
- var days = el.querySelectorAll('.lightpick__day'),
- disabledArray = opts.disableDates.map(function(entry){
- return entry instanceof Array || Object.prototype.toString.call(entry) === '[object Array]' ? entry[0] : entry;
- }),
- closestPrev = moment(disabledArray.filter(function(d) {
- return moment(d).isBefore(opts.startDate);
- }).sort(function(a,b){
- return moment(b).isAfter(moment(a));
- })[0]),
- closestNext = moment(disabledArray.filter(function(d) {
- return moment(d).isAfter(opts.startDate);
- }).sort(function(a,b){
- return moment(a).isAfter(moment(b));
- })[0]);
- [].forEach.call(days, function(dayCell) {
- var day = moment(parseInt(dayCell.getAttribute('data-time')));
- if (
- (closestPrev && day.isBefore(closestPrev) && opts.startDate.isAfter(closestPrev))
- || (closestNext && day.isAfter(closestNext) && closestNext.isAfter(opts.startDate))
- ) {
- dayCell.classList.remove('is-available');
- dayCell.classList.add('is-disabled');
- }
- });
- },
- Lightpick = function(options)
- {
- var self = this,
- opts = self.config(options);
- self.el = document.createElement('section');
- self.el.className = 'lightpick lightpick--' + opts.numberOfColumns + '-columns is-hidden';
- if (opts.inline) {
- self.el.className += ' lightpick--inlined';
- }
- var html = '<div class="lightpick__inner">'
- + (opts.numberOfMonths > 1 ? renderTopButtons(opts, 'days') : '')
- + '<div class="lightpick__months"></div>'
- + '<div class="lightpick__tooltip" style="visibility: hidden"></div>';
- if (opts.footer) {
- html += '<div class="lightpick__footer">';
- if (opts.footer === true) {
- html += '<button type="button" class="lightpick__reset-action">' + opts.locale.buttons.reset + '</button>';
- html += '<div class="lightpick__footer-message"></div>';
- html += '<button type="button" class="lightpick__apply-action">' + opts.locale.buttons.apply + '</button>';
- }
- else {
- html += opts.footer;
- }
- html += '</div>';
- }
- html += '</div>';
- self.el.innerHTML = html;
- if (opts.parentEl instanceof Node) {
- opts.parentEl.appendChild(self.el)
- }
- else if (opts.parentEl === 'body' && opts.inline) {
- opts.field.parentNode.appendChild(self.el);
- }
- else {
- document.querySelector(opts.parentEl).appendChild(self.el);
- }
- self._onMouseDown = function(e)
- {
- if (!self.isShowing) {
- return;
- }
- e = e || window.event;
- var target = e.target || e.srcElement;
- if (!target) {
- return;
- }
- e.stopPropagation();
- if (!target.classList.contains('lightpick__select')) {
- e.preventDefault();
- }
- var opts = self._opts;
- if (target.classList.contains('lightpick__day') && target.classList.contains('is-available')) {
- var day = moment(parseInt(target.getAttribute('data-time')));
- if (!opts.disabledDatesInRange && opts.disableDates && opts.startDate) {
- var start = day.isAfter(opts.startDate) ? moment(opts.startDate) : moment(day),
- end = day.isAfter(opts.startDate) ? moment(day) : moment(opts.startDate),
- isInvalidRange = opts.disableDates.filter(function(d) {
- if (d instanceof Array || Object.prototype.toString.call(d) === '[object Array]') {
- var _from = moment(d[0]),
- _to = moment(d[1]);
- return _from.isValid() && _to.isValid() && (_from.isBetween(start, end, 'day', '[]') || _to.isBetween(start, end, 'day', '[]'));
- }
- return moment(d).isBetween(start, end, 'day', '[]');
- });
- if (isInvalidRange.length) {
- self.setStartDate(null);
- self.setEndDate(null);
- target.dispatchEvent(new Event('mousedown'));
- self.el.querySelector('.lightpick__tooltip').style.visibility = 'hidden';
- updateDates(self.el, opts);
- return;
- }
- }
- if (opts.singleDate || (!opts.startDate && !opts.endDate) || (opts.startDate && opts.endDate)) {
- if (opts.repick && opts.startDate && opts.endDate) {
- if (opts.repickTrigger === opts.field) {
- self.setStartDate(day);
- target.classList.add('is-start-date');
- }
- else {
- self.setEndDate(day);
- target.classList.add('is-end-date');
- }
- if (opts.startDate.isAfter(opts.endDate)) {
- self.swapDate();
- }
- if (opts.autoclose) {
- setTimeout(function() {
- self.hide();
- }, 100);
- }
- }
- else {
- self.setStartDate(day);
- self.setEndDate(null);
- target.classList.add('is-start-date');
- if (opts.singleDate && opts.autoclose) {
- setTimeout(function() {
- self.hide();
- }, 100);
- }
- else if (!opts.singleDate || opts.inline || !opts.autoclose) {
- updateDates(self.el, opts);
- }
- }
- }
- else if (opts.startDate && !opts.endDate) {
- self.setEndDate(day);
- if (opts.startDate.isAfter(opts.endDate)) {
- self.swapDate();
- }
- target.classList.add('is-end-date');
- if (opts.autoclose) {
- setTimeout(function() {
- self.hide();
- }, 100);
- }
- else {
- updateDates(self.el, opts);
- }
- }
- if (!opts.disabledDatesInRange) {
- if (self.el.querySelectorAll('.lightpick__day.is-available').length === 0) {
- self.setStartDate(null);
- updateDates(self.el, opts);
- if (opts.footer) {
- if (typeof self._opts.onError === 'function') {
- self._opts.onError.call(self, 'Invalid range');
- }
- else {
- var footerMessage = self.el.querySelector('.lightpick__footer-message');
- if (footerMessage) {
- footerMessage.innerHTML = opts.locale.not_allowed_range;
- setTimeout(function(){
- footerMessage.innerHTML = '';
- }, 3000);
- }
- }
- }
- }
- }
- }
- else if (target.classList.contains('lightpick__previous-action')) {
- self.prevMonth();
- }
- else if (target.classList.contains('lightpick__next-action')) {
- self.nextMonth();
- }
- else if (target.classList.contains('lightpick__close-action') || target.classList.contains('lightpick__apply-action')) {
- self.hide();
- }
- else if (target.classList.contains('lightpick__reset-action')) {
- self.reset();
- }
- };
- self._onMouseEnter = function(e)
- {
- if (!self.isShowing) {
- return;
- }
- e = e || window.event;
- var target = e.target || e.srcElement;
- if (!target) {
- return;
- }
- var opts = self._opts;
- if (target.classList.contains('lightpick__day') && target.classList.contains('disabled-tooltip') && opts.locale.tooltipOnDisabled) {
- self.showTooltip(target, opts.locale.tooltipOnDisabled);
- return;
- }
- else {
- self.hideTooltip();
- }
- if (opts.singleDate || (!opts.startDate && !opts.endDate)) {
- return;
- }
- if (!target.classList.contains('lightpick__day') && !target.classList.contains('is-available')) {
- return;
- }
- if ((opts.startDate && !opts.endDate) || opts.repick) {
- var hoverDate = moment(parseInt(target.getAttribute('data-time')));
- if (!hoverDate.isValid()) {
- return;
- }
- var startDate = (opts.startDate && !opts.endDate) || (opts.repick && opts.repickTrigger === opts.secondField) ? opts.startDate : opts.endDate;
- var days = self.el.querySelectorAll('.lightpick__day');
- [].forEach.call(days, function(day) {
- var dt = moment(parseInt(day.getAttribute('data-time')));
- day.classList.remove('is-flipped');
- if (dt.isValid() && dt.isSameOrAfter(startDate, 'day') && dt.isSameOrBefore(hoverDate, 'day')) {
- day.classList.add('is-in-range');
- if (opts.repickTrigger === opts.field && dt.isSameOrAfter(opts.endDate)) {
- day.classList.add('is-flipped');
- }
- }
- else if (dt.isValid() && dt.isSameOrAfter(hoverDate, 'day') && dt.isSameOrBefore(startDate, 'day')) {
- day.classList.add('is-in-range');
- if (((opts.startDate && !opts.endDate) || opts.repickTrigger === opts.secondField) && dt.isSameOrBefore(opts.startDate)) {
- day.classList.add('is-flipped');
- }
- }
- else {
- day.classList.remove('is-in-range');
- }
- if (opts.startDate && opts.endDate && opts.repick && opts.repickTrigger === opts.field) {
- day.classList.remove('is-start-date');
- }
- else {
- day.classList.remove('is-end-date');
- }
- });
- if (opts.hoveringTooltip) {
- days = Math.abs(hoverDate.isAfter(startDate) ? hoverDate.diff(startDate, 'day') : startDate.diff(hoverDate, 'day'));
- if (!opts.tooltipNights) {
- days += 1;
- }
- var tooltip = self.el.querySelector('.lightpick__tooltip');
- if (days > 0 && !target.classList.contains('is-disabled')) {
- var pluralText = '';
- if (typeof opts.locale.pluralize === 'function') {
- pluralText = opts.locale.pluralize.call(self, days, opts.locale.tooltip);
- }
- self.showTooltip(target, days + ' ' + pluralText);
- }
- else {
- self.hideTooltip();
- }
- }
- if (opts.startDate && opts.endDate && opts.repick && opts.repickTrigger === opts.field) {
- target.classList.add('is-start-date');
- }
- else {
- target.classList.add('is-end-date');
- }
- }
- };
- self._onChange = function(e)
- {
- e = e || window.event;
- var target = e.target || e.srcElement;
- if (!target) {
- return;
- }
- if (target.classList.contains('lightpick__select-months')) {
- if (typeof self._opts.onMonthsChange === 'function') {
- self._opts.onMonthsChange.call(this, target.value);
- }
- self.gotoMonth(target.value);
- }
- else if (target.classList.contains('lightpick__select-years')) {
- if (typeof self._opts.onYearsChange === 'function') {
- self._opts.onYearsChange.call(this, target.value);
- }
- self.gotoYear(target.value);
- }
- };
- self._onInputChange = function(e)
- {
- var target = e.target || e.srcElement;
- if (self._opts.singleDate) {
- if (!self._opts.autoclose) {
- self.gotoDate(opts.field.value);
- }
- }
- self.syncFields();
- if (!self.isShowing) {
- self.show();
- }
- };
- self._onInputFocus = function(e)
- {
- var target = e.target || e.srcElement;
- self.show(target);
- };
- self._onInputClick = function(e)
- {
- var target = e.target || e.srcElement;
- self.show(target);
- };
- self._onClick = function(e)
- {
- e = e || window.event;
- var target = e.target || e.srcElement,
- parentEl = target;
- if (!target) {
- return;
- }
- do {
- if ((parentEl.classList && parentEl.classList.contains('lightpick')) || parentEl === opts.field || (opts.secondField && parentEl === opts.secondField)) {
- return;
- }
- }
- while ((parentEl = parentEl.parentNode));
- if (self.isShowing && opts.hideOnBodyClick && target !== opts.field && parentEl !== opts.field) {
- self.hide();
- }
- };
- self.showTooltip = function(target, text)
- {
- var tooltip = self.el.querySelector('.lightpick__tooltip');
- var hasParentEl = self.el.classList.contains('lightpick--inlined'),
- dayBounding = target.getBoundingClientRect(),
- pickerBouding = hasParentEl ? self.el.parentNode.getBoundingClientRect() : self.el.getBoundingClientRect(),
- _left = (dayBounding.left - pickerBouding.left) + (dayBounding.width / 2),
- _top = dayBounding.top - pickerBouding.top;
- tooltip.style.visibility = 'visible';
- tooltip.textContent = text;
- var tooltipBounding = tooltip.getBoundingClientRect();
- _top -= tooltipBounding.height;
- _left -= (tooltipBounding.width / 2);
- setTimeout(function(){
- tooltip.style.top = _top + 'px';
- tooltip.style.left = _left + 'px';
- }, 10);
- };
- self.hideTooltip = function()
- {
- var tooltip = self.el.querySelector('.lightpick__tooltip');
- tooltip.style.visibility = 'hidden';
- };
- self.el.addEventListener('mousedown', self._onMouseDown, true);
- self.el.addEventListener('mouseenter', self._onMouseEnter, true);
- self.el.addEventListener('touchend', self._onMouseDown, true);
- self.el.addEventListener('change', self._onChange, true);
- if (opts.inline) {
- self.show();
- }
- else {
- self.hide();
- }
- opts.field.addEventListener('change', self._onInputChange);
- opts.field.addEventListener('click', self._onInputClick);
- opts.field.addEventListener('focus', self._onInputFocus);
- if (opts.secondField) {
- opts.secondField.addEventListener('change', self._onInputChange);
- opts.secondField.addEventListener('click', self._onInputClick);
- opts.secondField.addEventListener('focus', self._onInputFocus);
- }
- };
- Lightpick.prototype = {
- config: function(options)
- {
- var opts = Object.assign({}, defaults, options);
- opts.field = (opts.field && opts.field.nodeName) ? opts.field : null;
- opts.calendar = [moment().set('date', 1)];
- if (opts.numberOfMonths === 1 && opts.numberOfColumns > 1) {
- opts.numberOfColumns = 1;
- }
- opts.minDate = opts.minDate && moment(opts.minDate, opts.format).isValid() ? moment(opts.minDate, opts.format) : null;
- opts.maxDate = opts.maxDate && moment(opts.maxDate, opts.format).isValid() ? moment(opts.maxDate, opts.format) : null;
- if (opts.lang === 'auto') {
- var browserLang = navigator.language || navigator.userLanguage;
- if (browserLang) {
- opts.lang = browserLang;
- }
- else {
- opts.lang = 'en-US';
- }
- }
- if (opts.secondField && opts.singleDate) {
- opts.singleDate = false;
- }
- if (opts.hoveringTooltip && opts.singleDate) {
- opts.hoveringTooltip = false;
- }
- if (Object.prototype.toString.call(options.locale) === '[object Object]') {
- opts.locale = Object.assign({}, defaults.locale, options.locale);
- }
- if (window.innerWidth < 480 && opts.numberOfMonths > 1) {
- opts.numberOfMonths = 1;
- opts.numberOfColumns = 1;
- }
- if (opts.repick && !opts.secondField) {
- opts.repick = false;
- }
- if (opts.inline) {
- opts.autoclose = false;
- opts.hideOnBodyClick = false;
- }
- this._opts = Object.assign({}, opts);
- this.syncFields();
- this.setStartDate(this._opts.startDate, true);
- this.setEndDate(this._opts.endDate, true);
- return this._opts;
- },
- syncFields: function()
- {
- if (this._opts.singleDate || this._opts.secondField) {
- if (moment(this._opts.field.value, this._opts.format).isValid()) {
- this._opts.startDate = moment(this._opts.field.value, this._opts.format);
- }
- if (this._opts.secondField && moment(this._opts.secondField.value, this._opts.format).isValid()) {
- this._opts.endDate = moment(this._opts.secondField.value, this._opts.format);
- }
- }
- else {
- var dates = this._opts.field.value.split(this._opts.separator);
- if (dates.length === 2) {
- if (moment(dates[0], this._opts.format).isValid()) {
- this._opts.startDate = moment(dates[0], this._opts.format);
- }
- if (moment(dates[1], this._opts.format).isValid()) {
- this._opts.endDate = moment(dates[1], this._opts.format);
- }
- }
- }
- },
- swapDate: function()
- {
- var tmp = moment(this._opts.startDate);
- this.setDateRange(this._opts.endDate, tmp);
- },
- gotoToday: function()
- {
- this.gotoDate(new Date());
- },
- gotoDate: function(date)
- {
- var date = moment(date, this._opts.format);
- if (!date.isValid()) {
- date = moment();
- }
- date.set('date', 1);
- this._opts.calendar = [moment(date)];
- renderCalendar(this.el, this._opts);
- },
- gotoMonth: function(month)
- {
- if (isNaN(month)) {
- return;
- }
- this._opts.calendar[0].set('month', month);
- renderCalendar(this.el, this._opts);
- },
- gotoYear: function(year)
- {
- if (isNaN(year)) {
- return;
- }
- this._opts.calendar[0].set('year', year);
- renderCalendar(this.el, this._opts);
- },
- prevMonth: function()
- {
- this._opts.calendar[0] = moment(this._opts.calendar[0]).subtract(this._opts.numberOfMonths, 'month');
- renderCalendar(this.el, this._opts);
- checkDisabledDatesInRange(this.el, this._opts);
- },
- nextMonth: function()
- {
- this._opts.calendar[0] = moment(this._opts.calendar[1]);
- renderCalendar(this.el, this._opts);
- checkDisabledDatesInRange(this.el, this._opts);
- },
- updatePosition: function()
- {
- if (this.el.classList.contains('lightpick--inlined')) return;
- // remove `is-hidden` class for getBoundingClientRect
- this.el.classList.remove('is-hidden');
- var rect = this._opts.field.getBoundingClientRect(),
- calRect = this.el.getBoundingClientRect(),
- orientation = this._opts.orientation.split(' '),
- top = 0,
- left = 0;
- if (orientation[0] == 'auto' || !(/top|bottom/.test(orientation[0]))) {
- if (rect.bottom + calRect.height > window.innerHeight && window.pageYOffset > calRect.height) {
- top = (rect.top + window.pageYOffset) - calRect.height;
- }
- else {
- top = rect.bottom + window.pageYOffset;
- }
- }
- else {
- top = rect[orientation[0]] + window.pageYOffset;
- if (orientation[0] == 'top') {
- top -= calRect.height;
- }
- }
- if (!(/left|right/.test(orientation[0])) && (!orientation[1] || orientation[1] == 'auto' || !(/left|right/.test(orientation[1])))) {
- if (rect.left + calRect.width > window.innerWidth) {
- left = (rect.right + window.pageXOffset) - calRect.width;
- }
- else {
- left = rect.left + window.pageXOffset;
- }
- }
- else {
- if (/left|right/.test(orientation[0])) {
- left = rect[orientation[0]] + window.pageXOffset;
- }
- else {
- left = rect[orientation[1]] + window.pageXOffset;
- }
- if (orientation[0] == 'right' || orientation[1] == 'right') {
- left -= calRect.width;
- }
- }
- this.el.classList.add('is-hidden');
- this.el.style.top = top + 'px';
- this.el.style.left = left + 'px';
- },
- setStartDate: function(date, preventOnSelect)
- {
- var dateISO = moment(date, moment.ISO_8601),
- dateOptFormat = moment(date, this._opts.format);
- if (!dateISO.isValid() && !dateOptFormat.isValid()) {
- this._opts.startDate = null;
- this._opts.field.value = '';
- return;
- }
- this._opts.startDate = moment(dateISO.isValid() ? dateISO : dateOptFormat);
- if (this._opts.singleDate || this._opts.secondField) {
- this._opts.field.value = this._opts.startDate.format(this._opts.format);
- }
- else {
- this._opts.field.value = this._opts.startDate.format(this._opts.format) + this._opts.separator + '...'
- }
- if (!preventOnSelect && typeof this._opts.onSelect === 'function') {
- this._opts.onSelect.call(this, this.getStartDate(), this.getEndDate());
- }
- if (!preventOnSelect && !this._opts.singleDate && typeof this._opts.onSelectStart === 'function') {
- this._opts.onSelectStart.call(this, this.getStartDate());
- }
- },
- setEndDate: function(date, preventOnSelect)
- {
- var dateISO = moment(date, moment.ISO_8601),
- dateOptFormat = moment(date, this._opts.format);
- if (!dateISO.isValid() && !dateOptFormat.isValid()) {
- this._opts.endDate = null;
- if (this._opts.secondField) {
- this._opts.secondField.value = '';
- }
- else if (!this._opts.singleDate && this._opts.startDate) {
- this._opts.field.value = this._opts.startDate.format(this._opts.format) + this._opts.separator + '...'
- }
- return;
- }
- this._opts.endDate = moment(dateISO.isValid() ? dateISO : dateOptFormat);
- if (this._opts.secondField) {
- this._opts.field.value = this._opts.startDate.format(this._opts.format);
- this._opts.secondField.value = this._opts.endDate.format(this._opts.format);
- }
- else {
- this._opts.field.value = this._opts.startDate.format(this._opts.format) + this._opts.separator + this._opts.endDate.format(this._opts.format);
- }
- if (!preventOnSelect && typeof this._opts.onSelect === 'function') {
- this._opts.onSelect.call(this, this.getStartDate(), this.getEndDate());
- }
- if (!preventOnSelect && !this._opts.singleDate && typeof this._opts.onSelectEnd === 'function') {
- this._opts.onSelectEnd.call(this, this.getEndDate());
- }
- },
- setDate: function(date, preventOnSelect)
- {
- if (!this._opts.singleDate) {
- return;
- }
- this.setStartDate(date, preventOnSelect);
- if (this.isShowing) {
- updateDates(this.el, this._opts);
- }
- },
- setDateRange: function(start, end, preventOnSelect)
- {
- if (this._opts.singleDate) {
- return;
- }
- this.setStartDate(start, true);
- this.setEndDate(end, true);
- if (this.isShowing) {
- updateDates(this.el, this._opts);
- }
- if (!preventOnSelect && typeof this._opts.onSelect === 'function') {
- this._opts.onSelect.call(this, this.getStartDate(), this.getEndDate());
- }
- },
- setDisableDates: function(dates)
- {
- this._opts.disableDates = dates;
- if (this.isShowing) {
- updateDates(this.el, this._opts);
- }
- },
- getStartDate: function()
- {
- return moment(this._opts.startDate).isValid() ? this._opts.startDate.clone() : null;
- },
- getEndDate: function()
- {
- return moment(this._opts.endDate).isValid() ? this._opts.endDate.clone() : null;
- },
- getDate: function()
- {
- return moment(this._opts.startDate).isValid() ? this._opts.startDate.clone() : null;
- },
- toString: function(format)
- {
- if (this._opts.singleDate) {
- return moment(this._opts.startDate).isValid() ? this._opts.startDate.format(format) : '';
- }
- if (moment(this._opts.startDate).isValid() && moment(this._opts.endDate).isValid()) {
- return this._opts.startDate.format(format) + this._opts.separator + this._opts.endDate.format(format);
- }
- if (moment(this._opts.startDate).isValid() && !moment(this._opts.endDate).isValid()) {
- return this._opts.startDate.format(format) + this._opts.separator + '...';
- }
- if (!moment(this._opts.startDate).isValid() && moment(this._opts.endDate).isValid()) {
- return '...' + this._opts.separator + this._opts.endDate.format(format);
- }
- return '';
- },
- show: function(target)
- {
- if (!this.isShowing) {
- this.isShowing = true;
- if (this._opts.repick) {
- this._opts.repickTrigger = target;
- }
- this.syncFields();
- if (this._opts.secondField && this._opts.secondField === target && this._opts.endDate) {
- this.gotoDate(this._opts.endDate);
- }
- else {
- this.gotoDate(this._opts.startDate);
- }
- document.addEventListener('click', this._onClick);
- this.updatePosition();
- this.el.classList.remove('is-hidden');
- if (typeof this._opts.onOpen === 'function') {
- this._opts.onOpen.call(this);
- }
- if (document.activeElement && document.activeElement != document.body) {
- document.activeElement.blur();
- }
- }
- },
- hide: function()
- {
- if (this.isShowing) {
- this.isShowing = false;
- document.removeEventListener('click', this._onClick);
- this.el.classList.add('is-hidden');
- this.el.querySelector('.lightpick__tooltip').style.visibility = 'hidden';
- if (typeof this._opts.onClose === 'function') {
- this._opts.onClose.call(this);
- }
- }
- },
- destroy: function()
- {
- var opts = this._opts;
- this.hide();
- this.el.removeEventListener('mousedown', self._onMouseDown, true);
- this.el.removeEventListener('mouseenter', self._onMouseEnter, true);
- this.el.removeEventListener('touchend', self._onMouseDown, true);
- this.el.removeEventListener('change', self._onChange, true);
- opts.field.removeEventListener('change', this._onInputChange);
- opts.field.removeEventListener('click', this._onInputClick);
- opts.field.removeEventListener('focus', this._onInputFocus);
- if (opts.secondField) {
- opts.secondField.removeEventListener('change', this._onInputChange);
- opts.secondField.removeEventListener('click', this._onInputClick);
- opts.secondField.removeEventListener('focus', this._onInputFocus);
- }
- if (this.el.parentNode) {
- this.el.parentNode.removeChild(this.el);
- }
- },
- reset: function()
- {
- this.setStartDate(null, true);
- this.setEndDate(null, true);
- updateDates(this.el, this._opts);
- if (typeof this._opts.onSelect === 'function') {
- this._opts.onSelect.call(this, this.getStartDate(), this.getEndDate());
- }
- this.el.querySelector('.lightpick__tooltip').style.visibility = 'hidden';
- },
- reloadOptions: function(options)
- {
- var dropdowns = this._opts.dropdowns;
- var locale = this._opts.locale;
- Object.assign(this._opts, this._opts, options);
- Object.assign(this._opts.dropdowns, dropdowns, options.dropdowns);
- Object.assign(this._opts.locale, locale, options.locale);
- }
- };
- return Lightpick;
- }));
|