@esri/calcite-components
Version:
Web Components for Esri's Calcite Design System.
197 lines (191 loc) • 8.97 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 { proxyCustomElement, HTMLElement, createEvent, h } from '@stencil/core/internal/client';
import { S as Sortable } from './sortable.esm.js';
import { c as createObserver } from './observers.js';
import { u as updateHostInteraction } from './interactive.js';
const CSS = {
sortItem: "sort-item",
container: "container",
containerHorizontal: "container--horizontal",
containerVertical: "container--vertical"
};
const sortableListCss = "@-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{display:-ms-flexbox;display:flex}:host([disabled]){pointer-events:none;cursor:default;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;opacity:var(--calcite-ui-opacity-disabled)}:host([disabled]) ::slotted([calcite-hydrated][disabled]),:host([disabled]) [calcite-hydrated][disabled]{opacity:1}.container{display:-ms-flexbox;display:flex;-ms-flex:1 1 auto;flex:1 1 auto}.container--vertical{-ms-flex-direction:column;flex-direction:column}.container--horizontal{-ms-flex-direction:row;flex-direction:row}";
const SortableList = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
constructor() {
super();
this.__registerHost();
this.__attachShadow();
this.calciteListOrderChange = createEvent(this, "calciteListOrderChange", 7);
/**
* The selector for the handle elements.
*/
this.handleSelector = "calcite-handle";
/**
* Indicates the horizontal or vertical orientation of the component.
*/
this.layout = "vertical";
/**
* When true, disabled prevents interaction. This state shows items with lower opacity/grayed.
*/
this.disabled = false;
/**
* When true, content is waiting to be loaded. This state shows a busy indicator.
*/
this.loading = false;
this.handleActivated = false;
this.items = [];
this.mutationObserver = createObserver("mutation", () => {
this.cleanUpDragAndDrop();
this.items = Array.from(this.el.children);
this.setUpDragAndDrop();
});
}
// --------------------------------------------------------------------------
//
// Lifecycle
//
// --------------------------------------------------------------------------
connectedCallback() {
this.items = Array.from(this.el.children);
this.setUpDragAndDrop();
this.beginObserving();
}
disconnectedCallback() {
var _a;
(_a = this.mutationObserver) === null || _a === void 0 ? void 0 : _a.disconnect();
this.cleanUpDragAndDrop();
}
componentDidRender() {
updateHostInteraction(this);
}
calciteHandleNudgeHandler(event) {
var _a;
const sortItem = this.items.find((item) => {
return item.contains(event.detail.handle) || event.composedPath().includes(item);
});
const lastIndex = this.items.length - 1;
const startingIndex = this.items.indexOf(sortItem);
let appendInstead = false;
let buddyIndex;
switch (event.detail.direction) {
case "up":
event.preventDefault();
if (startingIndex === 0) {
appendInstead = true;
}
else {
buddyIndex = startingIndex - 1;
}
break;
case "down":
event.preventDefault();
if (startingIndex === lastIndex) {
buddyIndex = 0;
}
else if (startingIndex === lastIndex - 1) {
appendInstead = true;
}
else {
buddyIndex = startingIndex + 2;
}
break;
default:
return;
}
(_a = this.mutationObserver) === null || _a === void 0 ? void 0 : _a.disconnect();
if (appendInstead) {
sortItem.parentElement.appendChild(sortItem);
}
else {
sortItem.parentElement.insertBefore(sortItem, this.items[buddyIndex]);
}
this.items = Array.from(this.el.children);
event.detail.handle.activated = true;
event.detail.handle.setFocus();
this.beginObserving();
}
// --------------------------------------------------------------------------
//
// Private Methods
//
// --------------------------------------------------------------------------
setUpDragAndDrop() {
this.cleanUpDragAndDrop();
const options = {
dataIdAttr: "id",
group: this.group,
handle: this.handleSelector,
// Changed sorting within list
onUpdate: () => {
this.items = Array.from(this.el.children);
this.calciteListOrderChange.emit();
},
// Element dragging started
onStart: () => {
var _a;
(_a = this.mutationObserver) === null || _a === void 0 ? void 0 : _a.disconnect();
},
// Element dragging ended
onEnd: () => {
this.beginObserving();
}
};
if (this.dragSelector) {
options.draggable = this.dragSelector;
}
this.sortable = Sortable.create(this.el, options);
}
cleanUpDragAndDrop() {
var _a;
(_a = this.sortable) === null || _a === void 0 ? void 0 : _a.destroy();
this.sortable = null;
}
beginObserving() {
var _a;
(_a = this.mutationObserver) === null || _a === void 0 ? void 0 : _a.observe(this.el, { childList: true, subtree: true });
}
// --------------------------------------------------------------------------
//
// 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 this; }
static get style() { return sortableListCss; }
}, [1, "calcite-sortable-list", {
"dragSelector": [1, "drag-selector"],
"group": [1],
"handleSelector": [1, "handle-selector"],
"layout": [1],
"disabled": [516],
"loading": [516],
"handleActivated": [32]
}, [[0, "calciteHandleNudge", "calciteHandleNudgeHandler"]]]);
function defineCustomElement$1() {
if (typeof customElements === "undefined") {
return;
}
const components = ["calcite-sortable-list"];
components.forEach(tagName => { switch (tagName) {
case "calcite-sortable-list":
if (!customElements.get(tagName)) {
customElements.define(tagName, SortableList);
}
break;
} });
}
defineCustomElement$1();
const CalciteSortableList = SortableList;
const defineCustomElement = defineCustomElement$1;
export { CalciteSortableList, defineCustomElement };