vanillajs-datepicker
Version:
A vanilla JavaScript remake of bootstrap-datepicker for Bulma and other CSS frameworks
1,583 lines (1,433 loc) • 92.5 kB
JavaScript
var Datepicker = (function () {
'use strict';
function lastItemOf(arr) {
return arr[arr.length - 1];
}
// push only the items not included in the array
function pushUnique(arr, ...items) {
items.forEach((item) => {
if (arr.includes(item)) {
return;
}
arr.push(item);
});
return arr;
}
function stringToArray(str, separator) {
// convert empty string to an empty array
return str ? str.split(separator) : [];
}
function isInRange(testVal, min, max) {
const minOK = min === undefined || testVal >= min;
const maxOK = max === undefined || testVal <= max;
return minOK && maxOK;
}
function limitToRange(val, min, max) {
if (val < min) {
return min;
}
if (val > max) {
return max;
}
return val;
}
function createTagRepeat(tagName, repeat, attributes = {}, index = 0, html = '') {
const openTagSrc = Object.keys(attributes).reduce((src, attr) => {
let val = attributes[attr];
if (typeof val === 'function') {
val = val(index);
}
return `${src} ${attr}="${val}"`;
}, tagName);
html += `<${openTagSrc}></${tagName}>`;
const next = index + 1;
return next < repeat
? createTagRepeat(tagName, repeat, attributes, next, html)
: html;
}
// Remove the spacing surrounding tags for HTML parser not to create text nodes
// before/after elements
function optimizeTemplateHTML(html) {
return html.replace(/>\s+/g, '>').replace(/\s+</, '<');
}
function stripTime(timeValue) {
return new Date(timeValue).setHours(0, 0, 0, 0);
}
function today() {
return new Date().setHours(0, 0, 0, 0);
}
// Get the time value of the start of given date or year, month and day
function dateValue(...args) {
switch (args.length) {
case 0:
return today();
case 1:
return stripTime(args[0]);
}
// use setFullYear() to keep 2-digit year from being mapped to 1900-1999
const newDate = new Date(0);
newDate.setFullYear(...args);
return newDate.setHours(0, 0, 0, 0);
}
function addDays(date, amount) {
const newDate = new Date(date);
return newDate.setDate(newDate.getDate() + amount);
}
function addWeeks(date, amount) {
return addDays(date, amount * 7);
}
function addMonths(date, amount) {
// If the day of the date is not in the new month, the last day of the new
// month will be returned. e.g. Jan 31 + 1 month → Feb 28 (not Mar 03)
const newDate = new Date(date);
const monthsToSet = newDate.getMonth() + amount;
let expectedMonth = monthsToSet % 12;
if (expectedMonth < 0) {
expectedMonth += 12;
}
const time = newDate.setMonth(monthsToSet);
return newDate.getMonth() !== expectedMonth ? newDate.setDate(0) : time;
}
function addYears(date, amount) {
// If the date is Feb 29 and the new year is not a leap year, Feb 28 of the
// new year will be returned.
const newDate = new Date(date);
const expectedMonth = newDate.getMonth();
const time = newDate.setFullYear(newDate.getFullYear() + amount);
return expectedMonth === 1 && newDate.getMonth() === 2 ? newDate.setDate(0) : time;
}
// Calculate the distance bettwen 2 days of the week
function dayDiff(day, from) {
return (day - from + 7) % 7;
}
// Get the date of the specified day of the week of given base date
function dayOfTheWeekOf(baseDate, dayOfWeek, weekStart = 0) {
const baseDay = new Date(baseDate).getDay();
return addDays(baseDate, dayDiff(dayOfWeek, weekStart) - dayDiff(baseDay, weekStart));
}
function calcWeekNum(dayOfTheWeek, sameDayOfFirstWeek) {
return Math.round((dayOfTheWeek - sameDayOfFirstWeek) / 604800000) + 1;
}
// Get the ISO week number of a date
function getIsoWeek(date) {
// - Start of ISO week is Monday
// - Use Thursday for culculation because the first Thursday of ISO week is
// always in January
const thuOfTheWeek = dayOfTheWeekOf(date, 4, 1);
// - Week 1 in ISO week is the week including Jan 04
// - Use the Thu of given date's week (instead of given date itself) to
// calculate week 1 of the year so that Jan 01 - 03 won't be miscalculated
// as week 0 when Jan 04 is Mon - Wed
const firstThu = dayOfTheWeekOf(new Date(thuOfTheWeek).setMonth(0, 4), 4, 1);
// return Math.round((thuOfTheWeek - firstThu) / 604800000) + 1;
return calcWeekNum(thuOfTheWeek, firstThu);
}
// Calculate week number in traditional week number system
// @see https://en.wikipedia.org/wiki/Week#Other_week_numbering_systems
function calcTraditionalWeekNumber(date, weekStart) {
// - Week 1 of traditional week is the week including the Jan 01
// - Use Jan 01 of given date's year to calculate the start of week 1
const startOfFirstWeek = dayOfTheWeekOf(new Date(date).setMonth(0, 1), weekStart, weekStart);
const startOfTheWeek = dayOfTheWeekOf(date, weekStart, weekStart);
const weekNum = calcWeekNum(startOfTheWeek, startOfFirstWeek);
if (weekNum < 53) {
return weekNum;
}
// If the 53rd week includes Jan 01, it's actually next year's week 1
const weekOneOfNextYear = dayOfTheWeekOf(new Date(date).setDate(32), weekStart, weekStart);
return startOfTheWeek === weekOneOfNextYear ? 1 : weekNum;
}
// Get the Western traditional week number of a date
function getWesternTradWeek(date) {
// Start of Western traditionl week is Sunday
return calcTraditionalWeekNumber(date, 0);
}
// Get the Middle Eastern week number of a date
function getMidEasternWeek(date) {
// Start of Middle Eastern week is Saturday
return calcTraditionalWeekNumber(date, 6);
}
// Get the start year of the period of years that includes given date
// years: length of the year period
function startOfYearPeriod(date, years) {
/* @see https://en.wikipedia.org/wiki/Year_zero#ISO_8601 */
const year = new Date(date).getFullYear();
return Math.floor(year / years) * years;
}
// Convert date to the first/last date of the month/year of the date
function regularizeDate(date, timeSpan, useLastDate) {
if (timeSpan !== 1 && timeSpan !== 2) {
return date;
}
const newDate = new Date(date);
if (timeSpan === 1) {
useLastDate
? newDate.setMonth(newDate.getMonth() + 1, 0)
: newDate.setDate(1);
} else {
useLastDate
? newDate.setFullYear(newDate.getFullYear() + 1, 0, 0)
: newDate.setMonth(0, 1);
}
return newDate.setHours(0, 0, 0, 0);
}
// pattern for format parts
const reFormatTokens = /dd?|DD?|mm?|MM?|yy?(?:yy)?/;
// pattern for non date parts
const reNonDateParts = /[\s!-/:-@[-`{-~年月日]+/;
// cache for persed formats
let knownFormats = {};
// parse funtions for date parts
const parseFns = {
y(date, year) {
return new Date(date).setFullYear(parseInt(year, 10));
},
m(date, month, locale) {
const newDate = new Date(date);
let monthIndex = parseInt(month, 10) - 1;
if (isNaN(monthIndex)) {
if (!month) {
return NaN;
}
const monthName = month.toLowerCase();
const compareNames = name => name.toLowerCase().startsWith(monthName);
// compare with both short and full names because some locales have periods
// in the short names (not equal to the first X letters of the full names)
monthIndex = locale.monthsShort.findIndex(compareNames);
if (monthIndex < 0) {
monthIndex = locale.months.findIndex(compareNames);
}
if (monthIndex < 0) {
return NaN;
}
}
newDate.setMonth(monthIndex);
return newDate.getMonth() !== normalizeMonth(monthIndex)
? newDate.setDate(0)
: newDate.getTime();
},
d(date, day) {
return new Date(date).setDate(parseInt(day, 10));
},
};
// format functions for date parts
const formatFns = {
d(date) {
return date.getDate();
},
dd(date) {
return padZero(date.getDate(), 2);
},
D(date, locale) {
return locale.daysShort[date.getDay()];
},
DD(date, locale) {
return locale.days[date.getDay()];
},
m(date) {
return date.getMonth() + 1;
},
mm(date) {
return padZero(date.getMonth() + 1, 2);
},
M(date, locale) {
return locale.monthsShort[date.getMonth()];
},
MM(date, locale) {
return locale.months[date.getMonth()];
},
y(date) {
return date.getFullYear();
},
yy(date) {
return padZero(date.getFullYear(), 2).slice(-2);
},
yyyy(date) {
return padZero(date.getFullYear(), 4);
},
};
// get month index in normal range (0 - 11) from any number
function normalizeMonth(monthIndex) {
return monthIndex > -1 ? monthIndex % 12 : normalizeMonth(monthIndex + 12);
}
function padZero(num, length) {
return num.toString().padStart(length, '0');
}
function parseFormatString(format) {
if (typeof format !== 'string') {
throw new Error("Invalid date format.");
}
if (format in knownFormats) {
return knownFormats[format];
}
// sprit the format string into parts and seprators
const separators = format.split(reFormatTokens);
const parts = format.match(new RegExp(reFormatTokens, 'g'));
if (separators.length === 0 || !parts) {
throw new Error("Invalid date format.");
}
// collect format functions used in the format
const partFormatters = parts.map(token => formatFns[token]);
// collect parse function keys used in the format
// iterate over parseFns' keys in order to keep the order of the keys.
const partParserKeys = Object.keys(parseFns).reduce((keys, key) => {
const token = parts.find(part => part[0] !== 'D' && part[0].toLowerCase() === key);
if (token) {
keys.push(key);
}
return keys;
}, []);
return knownFormats[format] = {
parser(dateStr, locale) {
const dateParts = dateStr.split(reNonDateParts).reduce((dtParts, part, index) => {
if (part.length > 0 && parts[index]) {
const token = parts[index][0];
if (token === 'M') {
dtParts.m = part;
} else if (token !== 'D') {
dtParts[token] = part;
}
}
return dtParts;
}, {});
// iterate over partParserkeys so that the parsing is made in the oder
// of year, month and day to prevent the day parser from correcting last
// day of month wrongly
return partParserKeys.reduce((origDate, key) => {
const newDate = parseFns[key](origDate, dateParts[key], locale);
// ingnore the part failed to parse
return isNaN(newDate) ? origDate : newDate;
}, today());
},
formatter(date, locale) {
let dateStr = partFormatters.reduce((str, fn, index) => {
return str += `${separators[index]}${fn(date, locale)}`;
}, '');
// separators' length is always parts' length + 1,
return dateStr += lastItemOf(separators);
},
};
}
function parseDate(dateStr, format, locale) {
if (dateStr instanceof Date || typeof dateStr === 'number') {
const date = stripTime(dateStr);
return isNaN(date) ? undefined : date;
}
if (!dateStr) {
return undefined;
}
if (dateStr === 'today') {
return today();
}
if (format && format.toValue) {
const date = format.toValue(dateStr, format, locale);
return isNaN(date) ? undefined : stripTime(date);
}
return parseFormatString(format).parser(dateStr, locale);
}
function formatDate(date, format, locale) {
if (isNaN(date) || (!date && date !== 0)) {
return '';
}
const dateObj = typeof date === 'number' ? new Date(date) : date;
if (format.toDisplay) {
return format.toDisplay(dateObj, format, locale);
}
return parseFormatString(format).formatter(dateObj, locale);
}
const range = document.createRange();
function parseHTML(html) {
return range.createContextualFragment(html);
}
function getParent(el) {
return el.parentElement
|| (el.parentNode instanceof ShadowRoot ? el.parentNode.host : undefined);
}
function isActiveElement(el) {
return el.getRootNode().activeElement === el;
}
function hideElement(el) {
if (el.style.display === 'none') {
return;
}
// back up the existing display setting in data-style-display
if (el.style.display) {
el.dataset.styleDisplay = el.style.display;
}
el.style.display = 'none';
}
function showElement(el) {
if (el.style.display !== 'none') {
return;
}
if (el.dataset.styleDisplay) {
// restore backed-up dispay property
el.style.display = el.dataset.styleDisplay;
delete el.dataset.styleDisplay;
} else {
el.style.display = '';
}
}
function emptyChildNodes(el) {
if (el.firstChild) {
el.removeChild(el.firstChild);
emptyChildNodes(el);
}
}
function replaceChildNodes(el, newChildNodes) {
emptyChildNodes(el);
if (newChildNodes instanceof DocumentFragment) {
el.appendChild(newChildNodes);
} else if (typeof newChildNodes === 'string') {
el.appendChild(parseHTML(newChildNodes));
} else if (typeof newChildNodes.forEach === 'function') {
newChildNodes.forEach((node) => {
el.appendChild(node);
});
}
}
const listenerRegistry = new WeakMap();
const {addEventListener, removeEventListener} = EventTarget.prototype;
// Register event listeners to a key object
// listeners: array of listener definitions;
// - each definition must be a flat array of event target and the arguments
// used to call addEventListener() on the target
function registerListeners(keyObj, listeners) {
let registered = listenerRegistry.get(keyObj);
if (!registered) {
registered = [];
listenerRegistry.set(keyObj, registered);
}
listeners.forEach((listener) => {
addEventListener.call(...listener);
registered.push(listener);
});
}
function unregisterListeners(keyObj) {
let listeners = listenerRegistry.get(keyObj);
if (!listeners) {
return;
}
listeners.forEach((listener) => {
removeEventListener.call(...listener);
});
listenerRegistry.delete(keyObj);
}
// Event.composedPath() polyfill for Edge
// based on https://gist.github.com/kleinfreund/e9787d73776c0e3750dcfcdc89f100ec
if (!Event.prototype.composedPath) {
const getComposedPath = (node, path = []) => {
path.push(node);
let parent;
if (node.parentNode) {
parent = node.parentNode;
} else if (node.host) { // ShadowRoot
parent = node.host;
} else if (node.defaultView) { // Document
parent = node.defaultView;
}
return parent ? getComposedPath(parent, path) : path;
};
Event.prototype.composedPath = function () {
return getComposedPath(this.target);
};
}
function findFromPath(path, criteria, currentTarget) {
const [node, ...rest] = path;
if (criteria(node)) {
return node;
}
if (node === currentTarget || node.tagName === 'HTML' || rest.length === 0) {
// stop when reaching currentTarget or <html>
return;
}
return findFromPath(rest, criteria, currentTarget);
}
// Search for the actual target of a delegated event
function findElementInEventPath(ev, selector) {
const criteria = typeof selector === 'function'
? selector
: el => el instanceof Element && el.matches(selector);
return findFromPath(ev.composedPath(), criteria, ev.currentTarget);
}
// default locales
const locales = {
en: {
days: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],
daysShort: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
daysMin: ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"],
months: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
monthsShort: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
today: "Today",
clear: "Clear",
titleFormat: "MM y"
}
};
// config options updatable by setOptions() and their default values
const defaultOptions = {
autohide: false,
beforeShowDay: null,
beforeShowDecade: null,
beforeShowMonth: null,
beforeShowYear: null,
clearButton: false,
dateDelimiter: ',',
datesDisabled: [],
daysOfWeekDisabled: [],
daysOfWeekHighlighted: [],
defaultViewDate: undefined, // placeholder, defaults to today() by the program
disableTouchKeyboard: false,
enableOnReadonly: true,
format: 'mm/dd/yyyy',
language: 'en',
maxDate: null,
maxNumberOfDates: 1,
maxView: 3,
minDate: null,
nextArrow: '»',
orientation: 'auto',
pickLevel: 0,
prevArrow: '«',
showDaysOfWeek: true,
showOnClick: true,
showOnFocus: true,
startView: 0,
title: '',
todayButton: false,
todayButtonMode: 0,
todayHighlight: false,
updateOnBlur: true,
weekNumbers: 0,
weekStart: 0,
};
const {
language: defaultLang,
format: defaultFormat,
weekStart: defaultWeekStart,
} = defaultOptions;
// Reducer function to filter out invalid day-of-week from the input
function sanitizeDOW(dow, day) {
return dow.length < 6 && day >= 0 && day < 7
? pushUnique(dow, day)
: dow;
}
function determineGetWeekMethod(numberingMode, weekStart) {
const methodId = numberingMode === 4
? (weekStart === 6 ? 3 : !weekStart + 1)
: numberingMode;
switch (methodId) {
case 1:
return getIsoWeek;
case 2:
return getWesternTradWeek;
case 3:
return getMidEasternWeek;
}
}
function updateWeekStart(newValue, config, weekNumbers) {
config.weekStart = newValue;
config.weekEnd = (newValue + 6) % 7;
if (weekNumbers === 4) {
config.getWeekNumber = determineGetWeekMethod(4, newValue);
}
return newValue;
}
// validate input date. if invalid, fallback to the original value
function validateDate(value, format, locale, origValue) {
const date = parseDate(value, format, locale);
return date !== undefined ? date : origValue;
}
// Validate viewId. if invalid, fallback to the original value
function validateViewId(value, origValue, max = 3) {
const viewId = parseInt(value, 10);
return viewId >= 0 && viewId <= max ? viewId : origValue;
}
function replaceOptions(options, from, to, convert = undefined) {
if (from in options) {
if (!(to in options)) {
options[to] = convert ? convert(options[from]) : options[from];
}
delete options[from];
}
}
// Create Datepicker configuration to set
function processOptions(options, datepicker) {
const inOpts = Object.assign({}, options);
const config = {};
const locales = datepicker.constructor.locales;
const rangeEnd = !!datepicker.rangeSideIndex;
let {
datesDisabled,
format,
language,
locale,
maxDate,
maxView,
minDate,
pickLevel,
startView,
weekNumbers,
weekStart,
} = datepicker.config || {};
// for backword compatibility
replaceOptions(inOpts, 'calendarWeeks', 'weekNumbers', val => val ? 1 : 0);
replaceOptions(inOpts, 'clearBtn', 'clearButton');
replaceOptions(inOpts, 'todayBtn', 'todayButton');
replaceOptions(inOpts, 'todayBtnMode', 'todayButtonMode');
if (inOpts.language) {
let lang;
if (inOpts.language !== language) {
if (locales[inOpts.language]) {
lang = inOpts.language;
} else {
// Check if langauge + region tag can fallback to the one without
// region (e.g. fr-CA → fr)
lang = inOpts.language.split('-')[0];
if (!locales[lang]) {
lang = false;
}
}
}
delete inOpts.language;
if (lang) {
language = config.language = lang;
// update locale as well when updating language
const origLocale = locale || locales[defaultLang];
// use default language's properties for the fallback
locale = Object.assign({
format: defaultFormat,
weekStart: defaultWeekStart
}, locales[defaultLang]);
if (language !== defaultLang) {
Object.assign(locale, locales[language]);
}
config.locale = locale;
// if format and/or weekStart are the same as old locale's defaults,
// update them to new locale's defaults
if (format === origLocale.format) {
format = config.format = locale.format;
}
if (weekStart === origLocale.weekStart) {
weekStart = updateWeekStart(locale.weekStart, config, weekNumbers);
}
}
}
if (inOpts.format) {
const hasToDisplay = typeof inOpts.format.toDisplay === 'function';
const hasToValue = typeof inOpts.format.toValue === 'function';
const validFormatString = reFormatTokens.test(inOpts.format);
if ((hasToDisplay && hasToValue) || validFormatString) {
format = config.format = inOpts.format;
}
delete inOpts.format;
}
//*** pick level ***//
let newPickLevel = pickLevel;
if ('pickLevel' in inOpts) {
newPickLevel = validateViewId(inOpts.pickLevel, pickLevel, 2);
delete inOpts.pickLevel;
}
if (newPickLevel !== pickLevel) {
if (newPickLevel > pickLevel) {
// complement current minDate/madDate so that the existing range will be
// expanded to fit the new level later
if (!('minDate' in inOpts)) {
inOpts.minDate = minDate;
}
if (!('maxDate' in inOpts)) {
inOpts.maxDate = maxDate;
}
}
// complement datesDisabled so that it will be reset later
if (datesDisabled && !inOpts.datesDisabled) {
inOpts.datesDisabled = [];
}
pickLevel = config.pickLevel = newPickLevel;
}
//*** dates ***//
// while min and maxDate for "no limit" in the options are better to be null
// (especially when updating), the ones in the config have to be undefined
// because null is treated as 0 (= unix epoch) when comparing with time value
let minDt = minDate;
let maxDt = maxDate;
if ('minDate' in inOpts) {
const defaultMinDt = dateValue(0, 0, 1);
minDt = inOpts.minDate === null
? defaultMinDt // set 0000-01-01 to prevent negative values for year
: validateDate(inOpts.minDate, format, locale, minDt);
if (minDt !== defaultMinDt) {
minDt = regularizeDate(minDt, pickLevel, false);
}
delete inOpts.minDate;
}
if ('maxDate' in inOpts) {
maxDt = inOpts.maxDate === null
? undefined
: validateDate(inOpts.maxDate, format, locale, maxDt);
if (maxDt !== undefined) {
maxDt = regularizeDate(maxDt, pickLevel, true);
}
delete inOpts.maxDate;
}
if (maxDt < minDt) {
minDate = config.minDate = maxDt;
maxDate = config.maxDate = minDt;
} else {
if (minDate !== minDt) {
minDate = config.minDate = minDt;
}
if (maxDate !== maxDt) {
maxDate = config.maxDate = maxDt;
}
}
if (inOpts.datesDisabled) {
const dtsDisabled = inOpts.datesDisabled;
if (typeof dtsDisabled === 'function') {
config.datesDisabled = null;
config.checkDisabled = (timeValue, viewId) => dtsDisabled(
new Date(timeValue),
viewId,
rangeEnd
);
} else {
const disabled = config.datesDisabled = dtsDisabled.reduce((dates, dt) => {
const date = parseDate(dt, format, locale);
return date !== undefined
? pushUnique(dates, regularizeDate(date, pickLevel, rangeEnd))
: dates;
}, []);
config.checkDisabled = timeValue => disabled.includes(timeValue);
}
delete inOpts.datesDisabled;
}
if ('defaultViewDate' in inOpts) {
const viewDate = parseDate(inOpts.defaultViewDate, format, locale);
if (viewDate !== undefined) {
config.defaultViewDate = viewDate;
}
delete inOpts.defaultViewDate;
}
//*** days of week ***//
if ('weekStart' in inOpts) {
const wkStart = Number(inOpts.weekStart) % 7;
if (!isNaN(wkStart)) {
weekStart = updateWeekStart(wkStart, config, weekNumbers);
}
delete inOpts.weekStart;
}
if (inOpts.daysOfWeekDisabled) {
config.daysOfWeekDisabled = inOpts.daysOfWeekDisabled.reduce(sanitizeDOW, []);
delete inOpts.daysOfWeekDisabled;
}
if (inOpts.daysOfWeekHighlighted) {
config.daysOfWeekHighlighted = inOpts.daysOfWeekHighlighted.reduce(sanitizeDOW, []);
delete inOpts.daysOfWeekHighlighted;
}
//*** week numbers ***//
if ('weekNumbers' in inOpts) {
let method = inOpts.weekNumbers;
if (method) {
const getWeekNumber = typeof method === 'function'
? (timeValue, startOfWeek) => method(new Date(timeValue), startOfWeek)
: determineGetWeekMethod((method = parseInt(method, 10)), weekStart);
if (getWeekNumber) {
weekNumbers = config.weekNumbers = method;
config.getWeekNumber = getWeekNumber;
}
} else {
weekNumbers = config.weekNumbers = 0;
config.getWeekNumber = null;
}
delete inOpts.weekNumbers;
}
//*** multi date ***//
if ('maxNumberOfDates' in inOpts) {
const maxNumberOfDates = parseInt(inOpts.maxNumberOfDates, 10);
if (maxNumberOfDates >= 0) {
config.maxNumberOfDates = maxNumberOfDates;
config.multidate = maxNumberOfDates !== 1;
}
delete inOpts.maxNumberOfDates;
}
if (inOpts.dateDelimiter) {
config.dateDelimiter = String(inOpts.dateDelimiter);
delete inOpts.dateDelimiter;
}
//*** view ***//
let newMaxView = maxView;
if ('maxView' in inOpts) {
newMaxView = validateViewId(inOpts.maxView, maxView);
delete inOpts.maxView;
}
// ensure max view >= pick level
newMaxView = pickLevel > newMaxView ? pickLevel : newMaxView;
if (newMaxView !== maxView) {
maxView = config.maxView = newMaxView;
}
let newStartView = startView;
if ('startView' in inOpts) {
newStartView = validateViewId(inOpts.startView, newStartView);
delete inOpts.startView;
}
// ensure pick level <= start view <= max view
if (newStartView < pickLevel) {
newStartView = pickLevel;
} else if (newStartView > maxView) {
newStartView = maxView;
}
if (newStartView !== startView) {
config.startView = newStartView;
}
//*** template ***//
if (inOpts.prevArrow) {
const prevArrow = parseHTML(inOpts.prevArrow);
if (prevArrow.childNodes.length > 0) {
config.prevArrow = prevArrow.childNodes;
}
delete inOpts.prevArrow;
}
if (inOpts.nextArrow) {
const nextArrow = parseHTML(inOpts.nextArrow);
if (nextArrow.childNodes.length > 0) {
config.nextArrow = nextArrow.childNodes;
}
delete inOpts.nextArrow;
}
//*** misc ***//
if ('disableTouchKeyboard' in inOpts) {
config.disableTouchKeyboard = 'ontouchstart' in document && !!inOpts.disableTouchKeyboard;
delete inOpts.disableTouchKeyboard;
}
if (inOpts.orientation) {
const orientation = inOpts.orientation.toLowerCase().split(/\s+/g);
config.orientation = {
x: orientation.find(x => (x === 'left' || x === 'right')) || 'auto',
y: orientation.find(y => (y === 'top' || y === 'bottom')) || 'auto',
};
delete inOpts.orientation;
}
if ('todayButtonMode' in inOpts) {
switch(inOpts.todayButtonMode) {
case 0:
case 1:
config.todayButtonMode = inOpts.todayButtonMode;
}
delete inOpts.todayButtonMode;
}
//*** copy the rest ***//
Object.entries(inOpts).forEach(([key, value]) => {
if (value !== undefined && key in defaultOptions) {
config[key] = value;
}
});
return config;
}
const defaultShortcutKeys = {
show: {key: 'ArrowDown'},
hide: null,
toggle: {key: 'Escape'},
prevButton: {key: 'ArrowLeft', ctrlOrMetaKey: true},
nextButton: {key: 'ArrowRight', ctrlOrMetaKey: true},
viewSwitch: {key: 'ArrowUp', ctrlOrMetaKey: true},
clearButton: {key: 'Backspace', ctrlOrMetaKey: true},
todayButton: {key: '.', ctrlOrMetaKey: true},
exitEditMode: {key: 'ArrowDown', ctrlOrMetaKey: true},
};
function createShortcutKeyConfig(options) {
return Object.keys(defaultShortcutKeys).reduce((keyDefs, shortcut) => {
const keyDef = options[shortcut] === undefined
? defaultShortcutKeys[shortcut]
: options[shortcut];
const key = keyDef && keyDef.key;
if (!key || typeof key !== 'string') {
return keyDefs;
}
const normalizedDef = {
key,
ctrlOrMetaKey: !!(keyDef.ctrlOrMetaKey || keyDef.ctrlKey || keyDef.metaKey),
};
if (key.length > 1) {
normalizedDef.altKey = !!keyDef.altKey;
normalizedDef.shiftKey = !!keyDef.shiftKey;
}
keyDefs[shortcut] = normalizedDef;
return keyDefs;
}, {});
}
const pickerTemplate = optimizeTemplateHTML(`<div class="datepicker">
<div class="datepicker-picker">
<div class="datepicker-header">
<div class="datepicker-title"></div>
<div class="datepicker-controls">
<button type="button" class="%buttonClass% prev-button prev-btn"></button>
<button type="button" class="%buttonClass% view-switch"></button>
<button type="button" class="%buttonClass% next-button next-btn"></button>
</div>
</div>
<div class="datepicker-main"></div>
<div class="datepicker-footer">
<div class="datepicker-controls">
<button type="button" class="%buttonClass% today-button today-btn"></button>
<button type="button" class="%buttonClass% clear-button clear-btn"></button>
</div>
</div>
</div>
</div>`);
const daysTemplate = optimizeTemplateHTML(`<div class="days">
<div class="days-of-week">${createTagRepeat('span', 7, {class: 'dow'})}</div>
<div class="datepicker-grid">${createTagRepeat('span', 42)}</div>
</div>`);
const weekNumbersTemplate = optimizeTemplateHTML(`<div class="week-numbers calendar-weeks">
<div class="days-of-week"><span class="dow"></span></div>
<div class="weeks">${createTagRepeat('span', 6, {class: 'week'})}</div>
</div>`);
// Base class of the view classes
class View {
constructor(picker, config) {
Object.assign(this, config, {
picker,
element: parseHTML(`<div class="datepicker-view"></div>`).firstChild,
selected: [],
isRangeEnd: !!picker.datepicker.rangeSideIndex,
});
this.init(this.picker.datepicker.config);
}
init(options) {
if ('pickLevel' in options) {
this.isMinView = this.id === options.pickLevel;
}
this.setOptions(options);
this.updateFocus();
this.updateSelection();
}
prepareForRender(switchLabel, prevButtonDisabled, nextButtonDisabled) {
// refresh disabled years on every render in order to clear the ones added
// by beforeShow hook at previous render
this.disabled = [];
const picker = this.picker;
picker.setViewSwitchLabel(switchLabel);
picker.setPrevButtonDisabled(prevButtonDisabled);
picker.setNextButtonDisabled(nextButtonDisabled);
}
setDisabled(date, classList) {
classList.add('disabled');
pushUnique(this.disabled, date);
}
// Execute beforeShow() callback and apply the result to the element
// args:
performBeforeHook(el, timeValue) {
let result = this.beforeShow(new Date(timeValue));
switch (typeof result) {
case 'boolean':
result = {enabled: result};
break;
case 'string':
result = {classes: result};
}
if (result) {
const classList = el.classList;
if (result.enabled === false) {
this.setDisabled(timeValue, classList);
}
if (result.classes) {
const extraClasses = result.classes.split(/\s+/);
classList.add(...extraClasses);
if (extraClasses.includes('disabled')) {
this.setDisabled(timeValue, classList);
}
}
if (result.content) {
replaceChildNodes(el, result.content);
}
}
}
renderCell(el, content, cellVal, date, {selected, range}, outOfScope, extraClasses = []) {
el.textContent = content;
if (this.isMinView) {
el.dataset.date = date;
}
const classList = el.classList;
el.className = `datepicker-cell ${this.cellClass}`;
if (cellVal < this.first) {
classList.add('prev');
} else if (cellVal > this.last) {
classList.add('next');
}
classList.add(...extraClasses);
if (outOfScope || this.checkDisabled(date, this.id)) {
this.setDisabled(date, classList);
}
if (range) {
const [rangeStart, rangeEnd] = range;
if (cellVal > rangeStart && cellVal < rangeEnd) {
classList.add('range');
}
if (cellVal === rangeStart) {
classList.add('range-start');
}
if (cellVal === rangeEnd) {
classList.add('range-end');
}
}
if (selected.includes(cellVal)) {
classList.add('selected');
}
if (cellVal === this.focused) {
classList.add('focused');
}
if (this.beforeShow) {
this.performBeforeHook(el, date);
}
}
refreshCell(el, cellVal, selected, [rangeStart, rangeEnd]) {
const classList = el.classList;
classList.remove('range', 'range-start', 'range-end', 'selected', 'focused');
if (cellVal > rangeStart && cellVal < rangeEnd) {
classList.add('range');
}
if (cellVal === rangeStart) {
classList.add('range-start');
}
if (cellVal === rangeEnd) {
classList.add('range-end');
}
if (selected.includes(cellVal)) {
classList.add('selected');
}
if (cellVal === this.focused) {
classList.add('focused');
}
}
changeFocusedCell(cellIndex) {
this.grid.querySelectorAll('.focused').forEach((el) => {
el.classList.remove('focused');
});
this.grid.children[cellIndex].classList.add('focused');
}
}
class DaysView extends View {
constructor(picker) {
super(picker, {
id: 0,
name: 'days',
cellClass: 'day',
});
}
init(options, onConstruction = true) {
if (onConstruction) {
const inner = parseHTML(daysTemplate).firstChild;
this.dow = inner.firstChild;
this.grid = inner.lastChild;
this.element.appendChild(inner);
}
super.init(options);
}
setOptions(options) {
let updateDOW;
if ('minDate' in options) {
this.minDate = options.minDate;
}
if ('maxDate' in options) {
this.maxDate = options.maxDate;
}
if (options.checkDisabled) {
this.checkDisabled = options.checkDisabled;
}
if (options.daysOfWeekDisabled) {
this.daysOfWeekDisabled = options.daysOfWeekDisabled;
updateDOW = true;
}
if (options.daysOfWeekHighlighted) {
this.daysOfWeekHighlighted = options.daysOfWeekHighlighted;
}
if ('todayHighlight' in options) {
this.todayHighlight = options.todayHighlight;
}
if ('weekStart' in options) {
this.weekStart = options.weekStart;
this.weekEnd = options.weekEnd;
updateDOW = true;
}
if (options.locale) {
const locale = this.locale = options.locale;
this.dayNames = locale.daysMin;
this.switchLabelFormat = locale.titleFormat;
updateDOW = true;
}
if ('beforeShowDay' in options) {
this.beforeShow = typeof options.beforeShowDay === 'function'
? options.beforeShowDay
: undefined;
}
if ('weekNumbers' in options) {
if (options.weekNumbers && !this.weekNumbers) {
const weeksElem = parseHTML(weekNumbersTemplate).firstChild;
this.weekNumbers = {
element: weeksElem,
dow: weeksElem.firstChild,
weeks: weeksElem.lastChild,
};
this.element.insertBefore(weeksElem, this.element.firstChild);
} else if (this.weekNumbers && !options.weekNumbers) {
this.element.removeChild(this.weekNumbers.element);
this.weekNumbers = null;
}
}
if ('getWeekNumber' in options) {
this.getWeekNumber = options.getWeekNumber;
}
if ('showDaysOfWeek' in options) {
if (options.showDaysOfWeek) {
showElement(this.dow);
if (this.weekNumbers) {
showElement(this.weekNumbers.dow);
}
} else {
hideElement(this.dow);
if (this.weekNumbers) {
hideElement(this.weekNumbers.dow);
}
}
}
// update days-of-week when locale, daysOfweekDisabled or weekStart is changed
if (updateDOW) {
Array.from(this.dow.children).forEach((el, index) => {
const dow = (this.weekStart + index) % 7;
el.textContent = this.dayNames[dow];
el.className = this.daysOfWeekDisabled.includes(dow) ? 'dow disabled' : 'dow';
});
}
}
// Apply update on the focused date to view's settings
updateFocus() {
const viewDate = new Date(this.picker.viewDate);
const viewYear = viewDate.getFullYear();
const viewMonth = viewDate.getMonth();
const firstOfMonth = dateValue(viewYear, viewMonth, 1);
const start = dayOfTheWeekOf(firstOfMonth, this.weekStart, this.weekStart);
this.first = firstOfMonth;
this.last = dateValue(viewYear, viewMonth + 1, 0);
this.start = start;
this.focused = this.picker.viewDate;
}
// Apply update on the selected dates to view's settings
updateSelection() {
const {dates, rangepicker} = this.picker.datepicker;
this.selected = dates;
if (rangepicker) {
this.range = rangepicker.dates;
}
}
// Update the entire view UI
render() {
// update today marker on ever render
this.today = this.todayHighlight ? today() : undefined;
this.prepareForRender(
formatDate(this.focused, this.switchLabelFormat, this.locale),
this.first <= this.minDate,
this.last >= this.maxDate
);
if (this.weekNumbers) {
const weekStart = this.weekStart;
const startOfWeek = dayOfTheWeekOf(this.first, weekStart, weekStart);
Array.from(this.weekNumbers.weeks.children).forEach((el, index) => {
const dateOfWeekStart = addWeeks(startOfWeek, index);
el.textContent = this.getWeekNumber(dateOfWeekStart, weekStart);
if (index > 3) {
el.classList[dateOfWeekStart > this.last ? 'add' : 'remove']('next');
}
});
}
Array.from(this.grid.children).forEach((el, index) => {
const current = addDays(this.start, index);
const dateObj = new Date(current);
const day = dateObj.getDay();
const extraClasses = [];
if (this.today === current) {
extraClasses.push('today');
}
if (this.daysOfWeekHighlighted.includes(day)) {
extraClasses.push('highlighted');
}
this.renderCell(
el,
dateObj.getDate(),
current,
current,
this,
current < this.minDate
|| current > this.maxDate
|| this.daysOfWeekDisabled.includes(day),
extraClasses
);
});
}
// Update the view UI by applying the changes of selected and focused items
refresh() {
const range = this.range || [];
Array.from(this.grid.children).forEach((el) => {
this.refreshCell(el, Number(el.dataset.date), this.selected, range);
});
}
// Update the view UI by applying the change of focused item
refreshFocus() {
this.changeFocusedCell(Math.round((this.focused - this.start) / 86400000));
}
}
function computeMonthRange(range, thisYear) {
if (!range || !range[0] || !range[1]) {
return;
}
const [[startY, startM], [endY, endM]] = range;
if (startY > thisYear || endY < thisYear) {
return;
}
return [
startY === thisYear ? startM : -1,
endY === thisYear ? endM : 12,
];
}
class MonthsView extends View {
constructor(picker) {
super(picker, {
id: 1,
name: 'months',
cellClass: 'month',
});
}
init(options, onConstruction = true) {
if (onConstruction) {
this.grid = this.element;
this.element.classList.add('months', 'datepicker-grid');
this.grid.appendChild(parseHTML(createTagRepeat('span', 12, {'data-month': ix => ix})));
this.first = 0;
this.last = 11;
}
super.init(options);
}
setOptions(options) {
if (options.locale) {
this.monthNames = options.locale.monthsShort;
}
if ('minDate' in options) {
if (options.minDate === undefined) {
this.minYear = this.minMonth = this.minDate = undefined;
} else {
const minDateObj = new Date(options.minDate);
this.minYear = minDateObj.getFullYear();
this.minMonth = minDateObj.getMonth();
this.minDate = minDateObj.setDate(1);
}
}
if ('maxDate' in options) {
if (options.maxDate === undefined) {
this.maxYear = this.maxMonth = this.maxDate = undefined;
} else {
const maxDateObj = new Date(options.maxDate);
this.maxYear = maxDateObj.getFullYear();
this.maxMonth = maxDateObj.getMonth();
this.maxDate = dateValue(this.maxYear, this.maxMonth + 1, 0);
}
}
if (options.checkDisabled) {
this.checkDisabled = this.isMinView || options.datesDisabled === null
? options.checkDisabled
: () => false;
}
if ('beforeShowMonth' in options) {
this.beforeShow = typeof options.beforeShowMonth === 'function'
? options.beforeShowMonth
: undefined;
}
}
// Update view's settings to reflect the viewDate set on the picker
updateFocus() {
const viewDate = new Date(this.picker.viewDate);
this.year = viewDate.getFullYear();
this.focused = viewDate.getMonth();
}
// Update view's settings to reflect the selected dates
updateSelection() {
const {dates, rangepicker} = this.picker.datepicker;
this.selected = dates.reduce((selected, timeValue) => {
const date = new Date(timeValue);
const year = date.getFullYear();
const month = date.getMonth();
if (selected[year] === undefined) {
selected[year] = [month];
} else {
pushUnique(selected[year], month);
}
return selected;
}, {});
if (rangepicker && rangepicker.dates) {
this.range = rangepicker.dates.map(timeValue => {
const date = new Date(timeValue);
return isNaN(date) ? undefined : [date.getFullYear(), date.getMonth()];
});
}
}
// Update the entire view UI
render() {
this.prepareForRender(
this.year,
this.year <= this.minYear,
this.year >= this.maxYear
);
const selected = this.selected[this.year] || [];
const yrOutOfRange = this.year < this.minYear || this.year > this.maxYear;
const isMinYear = this.year === this.minYear;
const isMaxYear = this.year === this.maxYear;
const range = computeMonthRange(this.range, this.year);
Array.from(this.grid.children).forEach((el, index) => {
const date = regularizeDate(new Date(this.year, index, 1), 1, this.isRangeEnd);
this.renderCell(
el,
this.monthNames[index],
index,
date,
{selected, range},
yrOutOfRange
|| isMinYear && index < this.minMonth
|| isMaxYear && index > this.maxMonth
);
});
}
// Update the view UI by applying the changes of selected and focused items
refresh() {
const selected = this.selected[this.year] || [];
const range = computeMonthRange(this.range, this.year) || [];
Array.from(this.grid.children).forEach((el, index) => {
this.refreshCell(el, index, selected, range);
});
}
// Update the view UI by applying the change of focused item
refreshFocus() {
this.changeFocusedCell(this.focused);
}
}
function toTitleCase(word) {
return [...word].reduce((str, ch, ix) => str += ix ? ch : ch.toUpperCase(), '');
}
// Class representing the years and decades view elements
class YearsView extends View {
constructor(picker, config) {
super(picker, config);
}
init(options, onConstruction = true) {
if (onConstruction) {
this.navStep = this.step * 10;
this.beforeShowOption = `beforeShow${toTitleCase(this.cellClass)}`;
this.grid = this.element;
this.element.classList.add(this.name, 'datepicker-grid');
this.grid.appendChild(parseHTML(createTagRepeat('span', 12)));
}
super.init(options);
}
setOptions(options) {
if ('minDate' in options) {
if (options.minDate === undefined) {
this.minYear = this.minDate = undefined;
} else {
this.minYear = startOfYearPeriod(options.minDate, this.step);
this.minDate = dateValue(this.minYear, 0, 1);
}
}
if ('maxDate' in options) {
if (options.maxDate === undefined) {
this.maxYear = this.maxDate = undefined;
} else {
this.maxYear = startOfYearPeriod(options.maxDate, this.step);
this.maxDate = dateValue(this.maxYear, 11, 31);
}
}
if (options.checkDisabled) {
this.checkDisabled = this.isMinView || options.datesDisabled === null
? options.checkDisabled
: () => false;
}
if (this.beforeShowOption in options) {
const beforeShow = options[this.beforeShowOption];
this.beforeShow = typeof beforeShow === 'function' ? beforeShow : undefined;
}
}
// Update view's settings to reflect the viewDate set on the picker
updateFocus() {
const viewDate = new Date(this.picker.viewDate);
const first = startOfYearPeriod(viewDate, this.navStep);
const last = first + 9 * this.step;
this.first = first;
this.last = last;
this.start = first - this.step;
this.focused = startOfYearPeriod(viewDate, this.step);
}
// Update view's settings to reflect the selected dates
updateSelection() {
const {dates, rangepicker} = this.picker.datepicker;
this.selected = dates.reduce((years, timeValue) => {
return pushUnique(years, startOfYearPeriod(timeValue, this.step));
}, []);
if (rangepicker && rangepicker.dates) {
this.range = rangepicker.dates.map(timeValue => {
if (timeValue !== undefined) {
return startOfYearPeriod(timeValue, this.step);
}
});
}
}
// Update the entire view UI
render() {
this.prepareForRender(
`${this.first}-${this.last}`,
this.first <= this.minYear,
this.last >= this.maxYear
);
Array.from(this.grid.children).forEach((el, index) => {
const current = this.start + (index * this.step);
const date = regularizeDate(new Date(current, 0, 1), 2, this.isRangeEnd);
el.dataset.year = current;
this.renderCell(
el,
current,
current,
date,
this,
current < this.minYear || current > this.maxYear
);
});
}
// Update the view UI by applying the changes of selected and focused items
refresh() {
const range = this.range || [];
Array.from(this.grid.children).forEach((el) => {
this.refreshCell(el, Number(el.textContent), this.selected, range);
});
}
// Update the view UI by applying the change of focused item
refreshFocus() {
this.changeFocusedCell(Math.round((this.focused - this.start) / this.step));
}
}
function triggerDatepickerEvent(datepicker, type) {
const detail = {
date: datepicker.getDate(),
viewDate: new Date(datepicker.picker.viewDate),
viewId: datepicker.picker.currentView.id,
datepicker,
};
datepicker.element.dispatchEvent(new CustomEvent(type, {detail}));
}
// direction: -1 (to previous), 1 (to next)
function goToPrevOrNext(datepicker, direction) {
const {config, picker} = datepicker;
const {currentView, viewDate} = picker;
let newViewDate;
switch (currentView.id) {
case 0:
newViewDate = addMonths(viewDate, direction);
break;
case 1:
newViewDate = addYears(viewDate, direction);
break;
default:
newViewDate = addYears(viewDate, direction * currentView