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