@esri/calcite-components
Version:
Web Components for Esri's Calcite Design System.
164 lines (157 loc) • 7.07 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
*/
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
const index = require('./index-55f8a3b7.js');
const interactive = require('./interactive-26294f2c.js');
const observers = require('./observers-83b3999d.js');
const sortableComponent = require('./sortableComponent-48a428a7.js');
const dom = require('./dom-18ca68ff.js');
require('./browser-28ea2ce1.js');
require('./guid-db20443e.js');
require('./resources-45d84c94.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) {
index.registerInstance(this, hostRef);
this.calciteListOrderChange = index.createEvent(this, "calciteListOrderChange", 6);
this.items = [];
this.mutationObserver = observers.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();
interactive.connectInteractive(this);
}
disconnectedCallback() {
interactive.disconnectInteractive(this);
sortableComponent.disconnectSortableComponent(this);
this.endObserving();
}
componentDidRender() {
interactive.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(() => dom.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();
sortableComponent.onSortingStart(this);
},
onEnd: () => {
sortableComponent.onSortingEnd(this);
this.beginObserving();
},
onUpdate: () => {
this.items = Array.from(this.el.children);
this.calciteListOrderChange.emit();
}
};
if (dragSelector) {
sortableOptions.draggable = dragSelector;
}
sortableComponent.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 (index.h("div", { class: {
[CSS.container]: true,
[CSS.containerVertical]: !horizontal,
[CSS.containerHorizontal]: horizontal
} }, index.h("slot", null)));
}
get el() { return index.getElement(this); }
};
SortableList.style = sortableListCss;
exports.calcite_sortable_list = SortableList;