UNPKG

@esri/calcite-components

Version:

Web Components for Esri's Calcite Design System.

56 lines (55 loc) 2.28 kB
/*! * 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 Sortable from "sortablejs"; import { containsCrossShadowBoundary } from "./dom"; const sortableComponentSet = new Set(); const inactiveSortableComponentSet = new WeakSet(); /** * Helper to keep track of a SortableComponent. This should be called in the `connectedCallback` lifecycle method as well as any other method necessary to rebuild the sortable instance. * * @param {SortableComponent} component - The sortable component. * @param {SortableComponent} [options] - Sortable options object. */ export function connectSortableComponent(component, options) { disconnectSortableComponent(component); sortableComponentSet.add(component); if (inactiveSortableComponentSet.has(component)) { return; } component.sortable = Sortable.create(component.el, options); } /** * Helper to remove track of a SortableComponent. This should be called in the `disconnectedCallback` lifecycle method. * * @param {SortableComponent} component - The sortable component. */ export function disconnectSortableComponent(component) { sortableComponentSet.delete(component); if (inactiveSortableComponentSet.has(component)) { return; } component.sortable?.destroy(); component.sortable = null; } function getNestedSortableComponents(activeComponent) { return Array.from(sortableComponentSet).filter((component) => component !== activeComponent && containsCrossShadowBoundary(activeComponent.el, component.el)); } /** * Helper to handle nested SortableComponents on `Sortable.onStart`. * * @param {SortableComponent} activeComponent - The active sortable component. */ export function onSortingStart(activeComponent) { getNestedSortableComponents(activeComponent).forEach((component) => inactiveSortableComponentSet.add(component)); } /** * Helper to handle nested SortableComponents on `Sortable.onEnd`. * * @param {SortableComponent} activeComponent - The active sortable component. */ export function onSortingEnd(activeComponent) { getNestedSortableComponents(activeComponent).forEach((component) => inactiveSortableComponentSet.delete(component)); }