@esri/calcite-components
Version:
Web Components for Esri's Calcite Design System.
160 lines (155 loc) • 7.06 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.
* v1.5.0-next.4
*/
import { r as registerInstance, c as createEvent, h, a as getElement } from './index-c8875db5.js';
import { c as connectInteractive, d as disconnectInteractive, u as updateHostInteraction } from './interactive-cff0a7ea.js';
import { c as createObserver } from './observers-d19b0d93.js';
import { d as disconnectSortableComponent, c as connectSortableComponent, o as onSortingStart, a as onSortingEnd } from './sortableComponent-5b95dae2.js';
import { f as focusElement } from './dom-4f5d1224.js';
import './browser-4be2add2.js';
import './guid-bdb80778.js';
import './resources-35f23920.js';
const CSS = {
sortItem: "sort-item",
container: "container",
containerHorizontal: "container--horizontal",
containerVertical: "container--vertical"
};
const sortableListCss = "@keyframes in{0%{opacity:0}100%{opacity:1}}@keyframes in-down{0%{opacity:0;transform:translate3D(0, -5px, 0)}100%{opacity:1;transform:translate3D(0, 0, 0)}}@keyframes in-up{0%{opacity:0;transform:translate3D(0, 5px, 0)}100%{opacity:1;transform:translate3D(0, 0, 0)}}@keyframes in-right{0%{opacity:0;transform:translate3D(-5px, 0, 0)}100%{opacity:1;transform:translate3D(0, 0, 0)}}@keyframes in-left{0%{opacity:0;transform:translate3D(5px, 0, 0)}100%{opacity:1;transform:translate3D(0, 0, 0)}}@keyframes in-scale{0%{opacity:0;transform:scale3D(0.95, 0.95, 1)}100%{opacity: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;animation-fill-mode:both;animation-duration:var(--calcite-animation-timing)}.calcite-animate__in{animation-name:in}.calcite-animate__in-down{animation-name:in-down}.calcite-animate__in-up{animation-name:in-up}.calcite-animate__in-right{animation-name:in-right}.calcite-animate__in-left{animation-name:in-left}.calcite-animate__in-scale{animation-name:in-scale}@media (prefers-reduced-motion: reduce){:root{--calcite-internal-duration-factor:0.01}}:root{--calcite-floating-ui-transition:var(--calcite-animation-timing);--calcite-floating-ui-z-index:var(--calcite-app-z-index-dropdown)}:host([hidden]){display:none}:host([disabled]){cursor:default;-webkit-user-select:none;user-select:none;opacity:var(--calcite-ui-opacity-disabled)}:host([disabled]) *,:host([disabled]) ::slotted(*){pointer-events:none}:host{display:flex}:host([disabled]) ::slotted([calcite-hydrated][disabled]),:host([disabled]) [calcite-hydrated][disabled]{opacity:1}.container{display:flex;flex:1 1 auto}.container--vertical{flex-direction:column}.container--horizontal{flex-direction:row}";
const SortableList = class {
constructor(hostRef) {
registerInstance(this, hostRef);
this.calciteListOrderChange = createEvent(this, "calciteListOrderChange", 6);
this.items = [];
this.mutationObserver = createObserver("mutation", () => {
this.setUpSorting();
});
this.dragSelector = undefined;
this.group = undefined;
this.handleSelector = "calcite-handle";
this.layout = "vertical";
this.disabled = false;
this.loading = false;
}
// --------------------------------------------------------------------------
//
// Lifecycle
//
// --------------------------------------------------------------------------
connectedCallback() {
this.setUpSorting();
this.beginObserving();
connectInteractive(this);
}
disconnectedCallback() {
disconnectInteractive(this);
disconnectSortableComponent(this);
this.endObserving();
}
componentDidRender() {
updateHostInteraction(this);
}
calciteHandleNudgeNextHandler(event) {
this.handleNudgeEvent(event);
}
// --------------------------------------------------------------------------
//
// Private Methods
//
// --------------------------------------------------------------------------
handleNudgeEvent(event) {
const { direction } = event.detail;
const handle = event
.composedPath()
.find((el) => el.matches(this.handleSelector));
const sortItem = this.items.find((item) => {
return item.contains(handle) || event.composedPath().includes(item);
});
const lastIndex = this.items.length - 1;
const startingIndex = this.items.indexOf(sortItem);
let appendInstead = false;
let buddyIndex;
if (direction === "up") {
if (startingIndex === 0) {
appendInstead = true;
}
else {
buddyIndex = startingIndex - 1;
}
}
else {
if (startingIndex === lastIndex) {
buddyIndex = 0;
}
else if (startingIndex === lastIndex - 1) {
appendInstead = true;
}
else {
buddyIndex = startingIndex + 2;
}
}
this.endObserving();
if (appendInstead) {
sortItem.parentElement.appendChild(sortItem);
}
else {
sortItem.parentElement.insertBefore(sortItem, this.items[buddyIndex]);
}
this.items = Array.from(this.el.children);
this.beginObserving();
requestAnimationFrame(() => focusElement(handle));
if ("activated" in handle) {
handle.activated = true;
}
}
setUpSorting() {
const { dragSelector, group, handleSelector } = this;
this.items = Array.from(this.el.children);
const sortableOptions = {
dataIdAttr: "id",
group,
handle: handleSelector,
onStart: () => {
this.endObserving();
onSortingStart(this);
},
onEnd: () => {
onSortingEnd(this);
this.beginObserving();
},
onUpdate: () => {
this.items = Array.from(this.el.children);
this.calciteListOrderChange.emit();
}
};
if (dragSelector) {
sortableOptions.draggable = dragSelector;
}
connectSortableComponent(this, sortableOptions);
}
beginObserving() {
this.mutationObserver?.observe(this.el, { childList: true, subtree: true });
}
endObserving() {
this.mutationObserver?.disconnect();
}
// --------------------------------------------------------------------------
//
// Render Methods
//
// --------------------------------------------------------------------------
render() {
const { layout } = this;
const horizontal = layout === "horizontal" || false;
return (h("div", { class: {
[CSS.container]: true,
[CSS.containerVertical]: !horizontal,
[CSS.containerHorizontal]: horizontal
} }, h("slot", null)));
}
get el() { return getElement(this); }
};
SortableList.style = sortableListCss;
export { SortableList as calcite_sortable_list };