@esri/calcite-components
Version:
Web Components for Esri's Calcite Design System.
526 lines (520 loc) • 19.9 kB
JavaScript
/*!
* All material copyright ESRI, All Rights Reserved, unless otherwise specified.
* See https://github.com/Esri/calcite-components/blob/master/LICENSE.md for details.
*/
import { getAssetPath, proxyCustomElement, HTMLElement, createEvent, h, Host, Build } from '@stencil/core/internal/client';
import { a as locales } from './locale.js';
import { g as getDaysDiff, d as dateToISO, a as dateFromISO, b as dateFromRange } from './date.js';
import { d as defineCustomElement$4 } from './date-picker-day.js';
import { d as defineCustomElement$3 } from './date-picker-month.js';
import { d as defineCustomElement$2 } from './date-picker-month-header.js';
import { d as defineCustomElement$1 } from './icon.js';
/**
* Get supported locale code from raw user input
* Exported for testing purposes.
* @private
*/
function getSupportedLocale(lang = "") {
if (locales.indexOf(lang) > -1) {
return lang;
}
lang = lang.toLowerCase();
if (lang.includes("-")) {
lang = lang.replace(/(\w+)-(\w+)/, (_match, language, region) => `${language}-${region.toUpperCase()}`);
if (!locales.includes(lang)) {
lang = lang.split("-")[0];
}
}
return locales.includes(lang) ? lang : "en";
}
/**
* CLDR cache.
* Exported for testing purposes.
* @private
*/
const translationCache = {};
/**
* CLDR request cache.
* Exported for testing purposes.
* @private
*/
const requestCache = {};
/**
* Fetch calendar data for a given locale from list of supported languages
* @public
*/
async function getLocaleData(lang) {
const locale = getSupportedLocale(lang);
if (translationCache[locale]) {
return translationCache[locale];
}
if (!requestCache[locale]) {
requestCache[locale] = fetch(getAssetPath(`./assets/date-picker/nls/${locale}.json`))
.then((resp) => resp.json())
.catch(() => {
console.error(`Translations for "${locale}" not found or invalid, falling back to english`);
return getLocaleData("en");
});
}
const data = await requestCache[locale];
translationCache[locale] = data;
return data;
}
const HEADING_LEVEL = 2;
const TEXT = {
nextMonth: "Next month",
prevMonth: "Previous month",
year: "Year"
};
const datePickerCss = "@-webkit-keyframes in{0%{opacity:0}100%{opacity:1}}@keyframes in{0%{opacity:0}100%{opacity:1}}@-webkit-keyframes in-down{0%{opacity:0;-webkit-transform:translate3D(0, -5px, 0);transform:translate3D(0, -5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@keyframes in-down{0%{opacity:0;-webkit-transform:translate3D(0, -5px, 0);transform:translate3D(0, -5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@-webkit-keyframes in-up{0%{opacity:0;-webkit-transform:translate3D(0, 5px, 0);transform:translate3D(0, 5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@keyframes in-up{0%{opacity:0;-webkit-transform:translate3D(0, 5px, 0);transform:translate3D(0, 5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@-webkit-keyframes in-scale{0%{opacity:0;-webkit-transform:scale3D(0.95, 0.95, 1);transform:scale3D(0.95, 0.95, 1)}100%{opacity:1;-webkit-transform:scale3D(1, 1, 1);transform:scale3D(1, 1, 1)}}@keyframes in-scale{0%{opacity:0;-webkit-transform:scale3D(0.95, 0.95, 1);transform:scale3D(0.95, 0.95, 1)}100%{opacity:1;-webkit-transform:scale3D(1, 1, 1);transform:scale3D(1, 1, 1)}}:root{--calcite-animation-timing:calc(150ms * var(--calcite-internal-duration-factor));--calcite-internal-duration-factor:var(--calcite-duration-factor, 1);--calcite-internal-animation-timing-fast:calc(100ms * var(--calcite-internal-duration-factor));--calcite-internal-animation-timing-medium:calc(200ms * var(--calcite-internal-duration-factor));--calcite-internal-animation-timing-slow:calc(300ms * var(--calcite-internal-duration-factor))}.calcite-animate{opacity:0;-webkit-animation-fill-mode:both;animation-fill-mode:both;-webkit-animation-duration:var(--calcite-animation-timing);animation-duration:var(--calcite-animation-timing)}.calcite-animate__in{-webkit-animation-name:in;animation-name:in}.calcite-animate__in-down{-webkit-animation-name:in-down;animation-name:in-down}.calcite-animate__in-up{-webkit-animation-name:in-up;animation-name:in-up}.calcite-animate__in-scale{-webkit-animation-name:in-scale;animation-name:in-scale}:root{--calcite-popper-transition:var(--calcite-animation-timing)}:host([hidden]){display:none}:host{position:relative;display:inline-block;width:100%;overflow:visible;border-radius:0px;border-width:1px;border-style:solid;border-color:var(--calcite-ui-border-2);background-color:var(--calcite-ui-foreground-1);vertical-align:top}:host([scale=s]){max-width:216px}:host([scale=m]){max-width:286px}:host([scale=l]){max-width:398px}";
const DatePicker = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
constructor() {
super();
this.__registerHost();
this.__attachShadow();
this.calciteDatePickerChange = createEvent(this, "calciteDatePickerChange", 7);
this.calciteDatePickerRangeChange = createEvent(this, "calciteDatePickerRangeChange", 7);
/** Localized string for "previous month" (used for aria label)
* @default "Previous month"
*/
this.intlPrevMonth = TEXT.prevMonth;
/** Localized string for "next month" (used for aria label)
* @default "Next month"
*/
this.intlNextMonth = TEXT.nextMonth;
/** Localized string for "year" (used for aria label)
* @default "Year"
*/
this.intlYear = TEXT.year;
/** BCP 47 language tag for desired language and country format */
this.locale = document.documentElement.lang || "en";
/** specify the scale of the date picker */
this.scale = "m";
/** Range mode activation */
this.range = false;
/** Disables the default behaviour on the third click of narrowing or extending the range and instead starts a new range. */
this.proximitySelectionDisabled = false;
//--------------------------------------------------------------------------
//
// Private Methods
//
//--------------------------------------------------------------------------
this.keyUpHandler = (e) => {
if (e.key === "Escape") {
this.reset();
}
};
this.monthHeaderSelectChange = (e) => {
const date = new Date(e.detail);
if (!this.range) {
this.activeDate = date;
}
else {
if (this.activeRange === "end") {
this.activeEndDate = date;
}
else {
this.activeStartDate = date;
}
this.mostRecentRangeValue = date;
}
};
this.monthActiveDateChange = (e) => {
const date = new Date(e.detail);
if (!this.range) {
this.activeDate = date;
}
else {
if (this.activeRange === "end") {
this.activeEndDate = date;
}
else {
this.activeStartDate = date;
}
this.mostRecentRangeValue = date;
}
};
this.monthHoverChange = (e) => {
if (!this.startAsDate) {
this.hoverRange = undefined;
return;
}
const date = new Date(e.detail);
this.hoverRange = {
focused: this.activeRange || "start",
start: this.startAsDate,
end: this.endAsDate
};
if (!this.proximitySelectionDisabled) {
if (this.endAsDate) {
const startDiff = getDaysDiff(date, this.startAsDate);
const endDiff = getDaysDiff(date, this.endAsDate);
if (endDiff > 0) {
this.hoverRange.end = date;
this.hoverRange.focused = "end";
}
else if (startDiff < 0) {
this.hoverRange.start = date;
this.hoverRange.focused = "start";
}
else if (startDiff > endDiff) {
this.hoverRange.start = date;
this.hoverRange.focused = "start";
}
else {
this.hoverRange.end = date;
this.hoverRange.focused = "end";
}
}
else {
if (date < this.startAsDate) {
this.hoverRange = {
focused: "start",
start: date,
end: this.startAsDate
};
}
else {
this.hoverRange.end = date;
this.hoverRange.focused = "end";
}
}
}
else {
if (!this.endAsDate) {
if (date < this.startAsDate) {
this.hoverRange = {
focused: "start",
start: date,
end: this.startAsDate
};
}
else {
this.hoverRange.end = date;
this.hoverRange.focused = "end";
}
}
else {
this.hoverRange = undefined;
}
}
};
this.monthMouseOutChange = () => {
if (this.hoverRange) {
this.hoverRange = undefined;
}
};
/**
* Reset active date and close
*/
this.reset = () => {
var _a, _b, _c, _d, _e, _f;
if (!Array.isArray(this.valueAsDate) &&
this.valueAsDate &&
((_a = this.valueAsDate) === null || _a === void 0 ? void 0 : _a.getTime()) !== ((_b = this.activeDate) === null || _b === void 0 ? void 0 : _b.getTime())) {
this.activeDate = new Date(this.valueAsDate);
}
if (this.startAsDate && ((_c = this.startAsDate) === null || _c === void 0 ? void 0 : _c.getTime()) !== ((_d = this.activeStartDate) === null || _d === void 0 ? void 0 : _d.getTime())) {
this.activeStartDate = new Date(this.startAsDate);
}
if (this.endAsDate && ((_e = this.endAsDate) === null || _e === void 0 ? void 0 : _e.getTime()) !== ((_f = this.activeEndDate) === null || _f === void 0 ? void 0 : _f.getTime())) {
this.activeEndDate = new Date(this.endAsDate);
}
};
/**
* Event handler for when the selected date changes
*/
this.monthDateChange = (e) => {
const date = new Date(e.detail);
if (!this.range) {
this.value = date ? dateToISO(date) : "";
this.valueAsDate = date || null;
this.activeDate = date || null;
this.calciteDatePickerChange.emit(date);
return;
}
if (!this.startAsDate || (!this.endAsDate && date < this.startAsDate)) {
if (this.startAsDate) {
this.setEndDate(new Date(this.startAsDate));
}
if (this.activeRange == "end") {
this.setEndDate(date);
}
else {
this.setStartDate(date);
}
}
else if (!this.endAsDate) {
this.setEndDate(date);
}
else {
if (!this.proximitySelectionDisabled) {
if (this.activeRange) {
if (this.activeRange == "end") {
this.setEndDate(date);
}
else {
this.setStartDate(date);
}
}
else {
const startDiff = getDaysDiff(date, this.startAsDate);
const endDiff = getDaysDiff(date, this.endAsDate);
if (endDiff === 0 || startDiff < 0) {
this.setStartDate(date);
}
else if (startDiff === 0 || endDiff < 0) {
this.setEndDate(date);
}
else if (startDiff < endDiff) {
this.setStartDate(date);
}
else {
this.setEndDate(date);
}
}
}
else {
this.setStartDate(date);
this.endAsDate = this.activeEndDate = this.end = undefined;
}
}
this.calciteDatePickerChange.emit(date);
};
}
handleValueAsDate(date) {
if (!Array.isArray(date) && date && date !== this.activeDate) {
this.activeDate = date;
}
}
handleRangeChange() {
const { startAsDate: startDate, endAsDate: endDate } = this;
this.activeEndDate = endDate;
this.activeStartDate = startDate;
}
onMinChanged(min) {
this.minAsDate = dateFromISO(min);
}
onMaxChanged(max) {
this.maxAsDate = dateFromISO(max);
}
// --------------------------------------------------------------------------
//
// Lifecycle
//
// --------------------------------------------------------------------------
connectedCallback() {
if (Array.isArray(this.value)) {
this.valueAsDate = this.value.map((v) => dateFromISO(v));
this.start = this.value[0];
this.end = this.value[1];
}
else if (this.value) {
this.valueAsDate = dateFromISO(this.value);
}
if (this.start) {
this.setStartAsDate(dateFromISO(this.start));
}
if (this.end) {
this.setEndAsDate(dateFromISO(this.end));
}
if (this.min) {
this.minAsDate = dateFromISO(this.min);
}
if (this.max) {
this.maxAsDate = dateFromISO(this.max);
}
}
async componentWillLoad() {
await this.loadLocaleData();
this.onMinChanged(this.min);
this.onMaxChanged(this.max);
}
render() {
var _a;
const date = dateFromRange(this.range ? this.startAsDate : this.valueAsDate, this.minAsDate, this.maxAsDate);
const activeStartDate = this.range
? this.getActiveStartDate(date, this.minAsDate, this.maxAsDate)
: this.getActiveDate(date, this.minAsDate, this.maxAsDate);
let activeDate = activeStartDate;
const endDate = this.range
? dateFromRange(this.endAsDate, this.minAsDate, this.maxAsDate)
: null;
const activeEndDate = this.getActiveEndDate(endDate, this.minAsDate, this.maxAsDate);
if ((this.activeRange === "end" ||
(((_a = this.hoverRange) === null || _a === void 0 ? void 0 : _a.focused) === "end" && (!this.proximitySelectionDisabled || endDate))) &&
activeEndDate) {
activeDate = activeEndDate;
}
if (this.range && this.mostRecentRangeValue) {
activeDate = this.mostRecentRangeValue;
}
const minDate = this.range && this.activeRange
? this.activeRange === "start"
? this.minAsDate
: date || this.minAsDate
: this.minAsDate;
const maxDate = this.range && this.activeRange
? this.activeRange === "start"
? endDate || this.maxAsDate
: this.maxAsDate
: this.maxAsDate;
return (h(Host, { onBlur: this.reset, onKeyUp: this.keyUpHandler, role: "application" }, this.renderCalendar(activeDate, maxDate, minDate, date, endDate)));
}
valueHandler(value) {
if (Array.isArray(value)) {
this.valueAsDate = value.map((v) => dateFromISO(v));
this.start = value[0];
this.end = value[1];
}
else if (value) {
this.valueAsDate = dateFromISO(value);
this.start = "";
this.end = "";
}
}
startWatcher(start) {
this.setStartAsDate(dateFromISO(start));
}
endWatcher(end) {
this.setEndAsDate(dateFromISO(end));
}
async loadLocaleData() {
if (!Build.isBrowser) {
return;
}
const { locale } = this;
this.localeData = await getLocaleData(locale);
}
/**
* Render calcite-date-picker-month-header and calcite-date-picker-month
*/
renderCalendar(activeDate, maxDate, minDate, date, endDate) {
return (this.localeData && [
h("calcite-date-picker-month-header", { activeDate: activeDate, headingLevel: this.headingLevel || HEADING_LEVEL, intlNextMonth: this.intlNextMonth, intlPrevMonth: this.intlPrevMonth, intlYear: this.intlYear, localeData: this.localeData, max: maxDate, min: minDate, onCalciteDatePickerSelect: this.monthHeaderSelectChange, scale: this.scale, selectedDate: this.activeRange === "end" ? endDate : date || new Date() }),
h("calcite-date-picker-month", { activeDate: activeDate, endDate: this.range ? endDate : undefined, hoverRange: this.hoverRange, localeData: this.localeData, max: maxDate, min: minDate, onCalciteDatePickerActiveDateChange: this.monthActiveDateChange, onCalciteDatePickerHover: this.monthHoverChange, onCalciteDatePickerMouseOut: this.monthMouseOutChange, onCalciteDatePickerSelect: this.monthDateChange, scale: this.scale, selectedDate: this.activeRange === "end" ? endDate : date, startDate: this.range ? date : undefined })
]);
}
/**
* Update date instance of start if valid
*/
setStartAsDate(startDate, emit) {
this.startAsDate = startDate;
this.mostRecentRangeValue = this.startAsDate;
if (emit) {
this.calciteDatePickerRangeChange.emit({
startDate,
endDate: this.endAsDate
});
}
}
/**
* Update date instance of end if valid
*/
setEndAsDate(endDate, emit) {
this.endAsDate = endDate;
this.mostRecentRangeValue = this.endAsDate;
if (emit) {
this.calciteDatePickerRangeChange.emit({
startDate: this.startAsDate,
endDate
});
}
}
setEndDate(date) {
this.end = date ? dateToISO(date) : "";
this.setEndAsDate(date, true);
this.activeEndDate = date || null;
}
setStartDate(date) {
this.start = date ? dateToISO(date) : "";
this.setStartAsDate(date, true);
this.activeStartDate = date || null;
}
/**
* Get an active date using the value, or current date as default
*/
getActiveDate(value, min, max) {
return dateFromRange(this.activeDate, min, max) || value || dateFromRange(new Date(), min, max);
}
getActiveStartDate(value, min, max) {
return (dateFromRange(this.activeStartDate, min, max) || value || dateFromRange(new Date(), min, max));
}
getActiveEndDate(value, min, max) {
return (dateFromRange(this.activeEndDate, min, max) || value || dateFromRange(new Date(), min, max));
}
static get assetsDirs() { return ["assets"]; }
get el() { return this; }
static get watchers() { return {
"valueAsDate": ["handleValueAsDate"],
"startAsDate": ["handleRangeChange"],
"endAsDate": ["handleRangeChange"],
"min": ["onMinChanged"],
"max": ["onMaxChanged"],
"value": ["valueHandler"],
"start": ["startWatcher"],
"end": ["endWatcher"],
"locale": ["loadLocaleData"]
}; }
static get style() { return datePickerCss; }
}, [1, "calcite-date-picker", {
"activeRange": [1, "active-range"],
"value": [1025],
"headingLevel": [2, "heading-level"],
"valueAsDate": [1040],
"startAsDate": [1040],
"endAsDate": [1040],
"minAsDate": [1040],
"maxAsDate": [1040],
"min": [1025],
"max": [1025],
"intlPrevMonth": [1, "intl-prev-month"],
"intlNextMonth": [1, "intl-next-month"],
"intlYear": [1, "intl-year"],
"locale": [1],
"scale": [513],
"range": [516],
"start": [1025],
"end": [1025],
"proximitySelectionDisabled": [4, "proximity-selection-disabled"],
"activeDate": [32],
"activeStartDate": [32],
"activeEndDate": [32],
"localeData": [32],
"hoverRange": [32]
}]);
function defineCustomElement() {
if (typeof customElements === "undefined") {
return;
}
const components = ["calcite-date-picker", "calcite-date-picker-day", "calcite-date-picker-month", "calcite-date-picker-month-header", "calcite-icon"];
components.forEach(tagName => { switch (tagName) {
case "calcite-date-picker":
if (!customElements.get(tagName)) {
customElements.define(tagName, DatePicker);
}
break;
case "calcite-date-picker-day":
if (!customElements.get(tagName)) {
defineCustomElement$4();
}
break;
case "calcite-date-picker-month":
if (!customElements.get(tagName)) {
defineCustomElement$3();
}
break;
case "calcite-date-picker-month-header":
if (!customElements.get(tagName)) {
defineCustomElement$2();
}
break;
case "calcite-icon":
if (!customElements.get(tagName)) {
defineCustomElement$1();
}
break;
} });
}
defineCustomElement();
export { DatePicker as D, TEXT as T, defineCustomElement as d, getLocaleData as g };