Socket
Socket
Sign inDemoInstall

nested-sort

Package Overview
Dependencies
0
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 5.1.3 to 5.2.0

dist/src/data-engine.d.ts

11

dist/data-engine.d.ts

@@ -1,2 +0,2 @@

import { DataEngineOptions, DataItem, ListElement, MappedDataItem, PropertyMap, RenderListItemFn } from './types';
import { AddNewItemArgs, DataEngineOptions, DataItem, ListElement, MappedDataItem, PropertyMap, RenderListItemFn } from './types';
declare class DataEngine {

@@ -6,4 +6,5 @@ data: Array<DataItem>;

sortedDataDomArray: Array<HTMLElement>;
propertyMap: Partial<PropertyMap>;
propertyMap: PropertyMap;
renderListItem: RenderListItemFn;
boundGetItemPropProxyName: (prop: string | symbol) => string;
/**

@@ -13,6 +14,8 @@ * @constructor

constructor({ data, propertyMap, renderListItem }: DataEngineOptions);
addMappingProxyToItem(item: DataItem): DataItem;
maybeTransformData(): void;
getItemPropProxyName(prop: string): string;
getItemPropProxyName(prop: string | symbol): string | symbol;
isTopLevelItem(item: DataItem): boolean;
sortListItems(): Array<DataItem>;
addNewItem({ item, asLastChild }: AddNewItemArgs): HTMLElement;
createItemElement(item: Partial<DataItem>, nodeName?: string): HTMLElement;

@@ -26,4 +29,4 @@ elementIsParentOfItem(node: HTMLElement, item: DataItem): boolean;

convertDomToData(list: ListElement): Array<MappedDataItem>;
render(): Node;
render(): HTMLOListElement;
}
export default DataEngine;
import DataEngine from './data-engine';
import { Actions, ClassNames, ClassNamesList, Cursor, DataItem, Distances, DropLocation, EventListeners, ListElement, ListInterface, ListTagName, Options, PlaceholderMaintenanceActions, PropertyMap, RenderListItemFn, TargetNode } from './types';
import { Actions, AddNewItemArgs, ClassNames, ClassNamesList, Cursor, DataItem, Distances, DropLocation, EventListeners, ListElement, ListInterface, ListTagName, MappedDataItem, Options, PlaceholderMaintenanceActions, PropertyMap, RenderListItemFn, TargetNode } from './types';
declare class NestedSort {

@@ -70,3 +70,6 @@ actions: Actions;

getPlaceholderList(): HTMLElement;
addNewItem({ item, asLastChild }: AddNewItemArgs): {
data: MappedDataItem[];
};
}
export default NestedSort;

@@ -94,2 +94,3 @@ 'use strict';

_defineProperty(this, "renderListItem", void 0);
_defineProperty(this, "boundGetItemPropProxyName", void 0);
this.data = data;

@@ -100,16 +101,20 @@ this.sortedData = [];

this.renderListItem = renderListItem;
this.boundGetItemPropProxyName = this.getItemPropProxyName.bind(this);
this.maybeTransformData();
}
_createClass(DataEngine, [{
key: "addMappingProxyToItem",
value: function addMappingProxyToItem(item) {
var _this = this;
return new Proxy(item, {
get: function get(target, prop, receiver) {
return Reflect.get(target, _this.boundGetItemPropProxyName(prop), receiver);
}
});
}
}, {
key: "maybeTransformData",
value: function maybeTransformData() {
if (!Object.keys(this.propertyMap).length || !Array.isArray(this.data)) return;
var getItemPropProxyName = this.getItemPropProxyName.bind(this);
this.data = this.data.map(function (item) {
return new Proxy(item, {
get: function get(target, prop, receiver) {
return Reflect.get(target, getItemPropProxyName(prop), receiver);
}
});
});
this.data = this.data.map(this.addMappingProxyToItem.bind(this));
}

@@ -132,6 +137,6 @@ }, {

value: function sortListItems() {
var _this = this;
var _this2 = this;
var items = _toConsumableArray(this.data);
var topLevelItems = items.filter(function (a) {
return _this.isTopLevelItem(a);
return _this2.isTopLevelItem(a);
}).sort(function (a, b) {

@@ -141,4 +146,5 @@ return a.order && b.order ? a.order - b.order : 0;

var childItems = items.filter(function (a) {
return !_this.isTopLevelItem(a);
return !_this2.isTopLevelItem(a);
}).reduce(function (groups, item) {
if (!item.parent) return groups;
if (Object.prototype.hasOwnProperty.call(groups, item.parent)) {

@@ -160,2 +166,14 @@ groups[item.parent].push(item);

}, {
key: "addNewItem",
value: function addNewItem(_ref2) {
var item = _ref2.item,
_ref2$asLastChild = _ref2.asLastChild,
asLastChild = _ref2$asLastChild === void 0 ? false : _ref2$asLastChild;
var mappedItem = this.addMappingProxyToItem(item);
if (Array.isArray(this.data)) {
this.data[asLastChild ? 'push' : 'unshift'](mappedItem);
}
return this.createItemElement(mappedItem);
}
}, {
key: "createItemElement",

@@ -194,6 +212,6 @@ value: function createItemElement(item) {

value: function maybeAppendItemToParentDom(item) {
var _this2 = this;
var _this3 = this;
var parent = item.parent;
var topParent = this.sortedDataDomArray.find(function (topLevelListItem) {
return _this2.elementIsParentOfItem(topLevelListItem, item) || _this2.elementIsAncestorOfItem(topLevelListItem, item);
return _this3.elementIsParentOfItem(topLevelListItem, item) || _this3.elementIsAncestorOfItem(topLevelListItem, item);
});

@@ -217,3 +235,3 @@ if (!topParent) return false;

value: function getListItemsDom() {
var _this3 = this;
var _this4 = this;
this.sortedDataDomArray = [];

@@ -223,2 +241,3 @@ var processedItems = [];

processedItems = this.sortedData.reduce(function (processedItems, item) {
if (!item.id) return processedItems;
var id = item.id.toString();

@@ -228,7 +247,7 @@ if (processedItems.includes(id)) return processedItems;

if (!item.parent) {
var listItem = _this3.createItemElement(item);
_this3.sortedDataDomArray.push(listItem);
var listItem = _this4.createItemElement(item);
_this4.sortedDataDomArray.push(listItem);
itemAdded = true;
} else {
itemAdded = _this3.maybeAppendItemToParentDom(item);
itemAdded = _this4.maybeAppendItemToParentDom(item);
}

@@ -244,5 +263,5 @@ if (itemAdded) processedItems.push(id);

value: function convertDomToData(list) {
var _this4 = this;
var _this5 = this;
return Array.from((list === null || list === void 0 ? void 0 : list.querySelectorAll('li')) || []).map(function (li) {
var _ref2;
var _ref3;
var parentListItem = li.parentNode;

@@ -253,3 +272,3 @@ var parent = parentListItem.dataset.id;

}) + 1;
return _ref2 = {}, _defineProperty(_ref2, _this4.getItemPropProxyName('id'), li.dataset.id), _defineProperty(_ref2, _this4.getItemPropProxyName('parent'), parent), _defineProperty(_ref2, _this4.getItemPropProxyName('order'), order), _ref2;
return _ref3 = {}, _defineProperty(_ref3, _this5.getItemPropProxyName('id'), li.dataset.id), _defineProperty(_ref3, _this5.getItemPropProxyName('parent'), parent), _defineProperty(_ref3, _this5.getItemPropProxyName('order'), order), _ref3;
});

@@ -375,2 +394,3 @@ }

this.wrapper.appendChild(list);
this.sortableList = list;
}

@@ -625,2 +645,9 @@ }, {

var list = this.getSortableList();
var selfDepth = 0;
if (this.draggedNode) {
// the dragged node might be a nested list contributing to the final nesting levels
var depthUL = this.draggedNode.querySelectorAll('ul').length || 0;
var depthOL = this.draggedNode.querySelectorAll('ol').length || 0;
selfDepth = depthUL > depthOL ? depthUL : depthOL;
}
while (list !== ((_el = el) === null || _el === void 0 ? void 0 : _el.parentElement)) {

@@ -631,3 +658,3 @@ var _el, _el2, _el3;

}
return depth;
return depth + selfDepth;
}

@@ -723,2 +750,19 @@ }, {

}
}, {
key: "addNewItem",
value: function addNewItem(_ref2) {
var _this$getSortableList3;
var item = _ref2.item,
_ref2$asLastChild = _ref2.asLastChild,
asLastChild = _ref2$asLastChild === void 0 ? false : _ref2$asLastChild;
var listItem = this.getDataEngine().addNewItem({
item: item,
asLastChild: asLastChild
});
listItem.setAttribute('draggable', String(this.initialised));
(_this$getSortableList3 = this.getSortableList()) === null || _this$getSortableList3 === void 0 ? void 0 : _this$getSortableList3[asLastChild ? 'append' : 'prepend'](listItem);
return {
data: this.getDataEngine().convertDomToData(this.getSortableList())
};
}
}]);

@@ -725,0 +769,0 @@ return NestedSort;

@@ -92,2 +92,3 @@ function _classCallCheck(instance, Constructor) {

_defineProperty(this, "renderListItem", void 0);
_defineProperty(this, "boundGetItemPropProxyName", void 0);
this.data = data;

@@ -98,16 +99,20 @@ this.sortedData = [];

this.renderListItem = renderListItem;
this.boundGetItemPropProxyName = this.getItemPropProxyName.bind(this);
this.maybeTransformData();
}
_createClass(DataEngine, [{
key: "addMappingProxyToItem",
value: function addMappingProxyToItem(item) {
var _this = this;
return new Proxy(item, {
get: function get(target, prop, receiver) {
return Reflect.get(target, _this.boundGetItemPropProxyName(prop), receiver);
}
});
}
}, {
key: "maybeTransformData",
value: function maybeTransformData() {
if (!Object.keys(this.propertyMap).length || !Array.isArray(this.data)) return;
var getItemPropProxyName = this.getItemPropProxyName.bind(this);
this.data = this.data.map(function (item) {
return new Proxy(item, {
get: function get(target, prop, receiver) {
return Reflect.get(target, getItemPropProxyName(prop), receiver);
}
});
});
this.data = this.data.map(this.addMappingProxyToItem.bind(this));
}

@@ -130,6 +135,6 @@ }, {

value: function sortListItems() {
var _this = this;
var _this2 = this;
var items = _toConsumableArray(this.data);
var topLevelItems = items.filter(function (a) {
return _this.isTopLevelItem(a);
return _this2.isTopLevelItem(a);
}).sort(function (a, b) {

@@ -139,4 +144,5 @@ return a.order && b.order ? a.order - b.order : 0;

var childItems = items.filter(function (a) {
return !_this.isTopLevelItem(a);
return !_this2.isTopLevelItem(a);
}).reduce(function (groups, item) {
if (!item.parent) return groups;
if (Object.prototype.hasOwnProperty.call(groups, item.parent)) {

@@ -158,2 +164,14 @@ groups[item.parent].push(item);

}, {
key: "addNewItem",
value: function addNewItem(_ref2) {
var item = _ref2.item,
_ref2$asLastChild = _ref2.asLastChild,
asLastChild = _ref2$asLastChild === void 0 ? false : _ref2$asLastChild;
var mappedItem = this.addMappingProxyToItem(item);
if (Array.isArray(this.data)) {
this.data[asLastChild ? 'push' : 'unshift'](mappedItem);
}
return this.createItemElement(mappedItem);
}
}, {
key: "createItemElement",

@@ -192,6 +210,6 @@ value: function createItemElement(item) {

value: function maybeAppendItemToParentDom(item) {
var _this2 = this;
var _this3 = this;
var parent = item.parent;
var topParent = this.sortedDataDomArray.find(function (topLevelListItem) {
return _this2.elementIsParentOfItem(topLevelListItem, item) || _this2.elementIsAncestorOfItem(topLevelListItem, item);
return _this3.elementIsParentOfItem(topLevelListItem, item) || _this3.elementIsAncestorOfItem(topLevelListItem, item);
});

@@ -215,3 +233,3 @@ if (!topParent) return false;

value: function getListItemsDom() {
var _this3 = this;
var _this4 = this;
this.sortedDataDomArray = [];

@@ -221,2 +239,3 @@ var processedItems = [];

processedItems = this.sortedData.reduce(function (processedItems, item) {
if (!item.id) return processedItems;
var id = item.id.toString();

@@ -226,7 +245,7 @@ if (processedItems.includes(id)) return processedItems;

if (!item.parent) {
var listItem = _this3.createItemElement(item);
_this3.sortedDataDomArray.push(listItem);
var listItem = _this4.createItemElement(item);
_this4.sortedDataDomArray.push(listItem);
itemAdded = true;
} else {
itemAdded = _this3.maybeAppendItemToParentDom(item);
itemAdded = _this4.maybeAppendItemToParentDom(item);
}

@@ -242,5 +261,5 @@ if (itemAdded) processedItems.push(id);

value: function convertDomToData(list) {
var _this4 = this;
var _this5 = this;
return Array.from((list === null || list === void 0 ? void 0 : list.querySelectorAll('li')) || []).map(function (li) {
var _ref2;
var _ref3;
var parentListItem = li.parentNode;

@@ -251,3 +270,3 @@ var parent = parentListItem.dataset.id;

}) + 1;
return _ref2 = {}, _defineProperty(_ref2, _this4.getItemPropProxyName('id'), li.dataset.id), _defineProperty(_ref2, _this4.getItemPropProxyName('parent'), parent), _defineProperty(_ref2, _this4.getItemPropProxyName('order'), order), _ref2;
return _ref3 = {}, _defineProperty(_ref3, _this5.getItemPropProxyName('id'), li.dataset.id), _defineProperty(_ref3, _this5.getItemPropProxyName('parent'), parent), _defineProperty(_ref3, _this5.getItemPropProxyName('order'), order), _ref3;
});

@@ -373,2 +392,3 @@ }

this.wrapper.appendChild(list);
this.sortableList = list;
}

@@ -623,2 +643,9 @@ }, {

var list = this.getSortableList();
var selfDepth = 0;
if (this.draggedNode) {
// the dragged node might be a nested list contributing to the final nesting levels
var depthUL = this.draggedNode.querySelectorAll('ul').length || 0;
var depthOL = this.draggedNode.querySelectorAll('ol').length || 0;
selfDepth = depthUL > depthOL ? depthUL : depthOL;
}
while (list !== ((_el = el) === null || _el === void 0 ? void 0 : _el.parentElement)) {

@@ -629,3 +656,3 @@ var _el, _el2, _el3;

}
return depth;
return depth + selfDepth;
}

@@ -721,2 +748,19 @@ }, {

}
}, {
key: "addNewItem",
value: function addNewItem(_ref2) {
var _this$getSortableList3;
var item = _ref2.item,
_ref2$asLastChild = _ref2.asLastChild,
asLastChild = _ref2$asLastChild === void 0 ? false : _ref2$asLastChild;
var listItem = this.getDataEngine().addNewItem({
item: item,
asLastChild: asLastChild
});
listItem.setAttribute('draggable', String(this.initialised));
(_this$getSortableList3 = this.getSortableList()) === null || _this$getSortableList3 === void 0 ? void 0 : _this$getSortableList3[asLastChild ? 'append' : 'prepend'](listItem);
return {
data: this.getDataEngine().convertDomToData(this.getSortableList())
};
}
}]);

@@ -723,0 +767,0 @@ return NestedSort;

@@ -98,2 +98,3 @@ (function (global, factory) {

_defineProperty(this, "renderListItem", void 0);
_defineProperty(this, "boundGetItemPropProxyName", void 0);
this.data = data;

@@ -104,16 +105,20 @@ this.sortedData = [];

this.renderListItem = renderListItem;
this.boundGetItemPropProxyName = this.getItemPropProxyName.bind(this);
this.maybeTransformData();
}
_createClass(DataEngine, [{
key: "addMappingProxyToItem",
value: function addMappingProxyToItem(item) {
var _this = this;
return new Proxy(item, {
get: function get(target, prop, receiver) {
return Reflect.get(target, _this.boundGetItemPropProxyName(prop), receiver);
}
});
}
}, {
key: "maybeTransformData",
value: function maybeTransformData() {
if (!Object.keys(this.propertyMap).length || !Array.isArray(this.data)) return;
var getItemPropProxyName = this.getItemPropProxyName.bind(this);
this.data = this.data.map(function (item) {
return new Proxy(item, {
get: function get(target, prop, receiver) {
return Reflect.get(target, getItemPropProxyName(prop), receiver);
}
});
});
this.data = this.data.map(this.addMappingProxyToItem.bind(this));
}

@@ -136,6 +141,6 @@ }, {

value: function sortListItems() {
var _this = this;
var _this2 = this;
var items = _toConsumableArray(this.data);
var topLevelItems = items.filter(function (a) {
return _this.isTopLevelItem(a);
return _this2.isTopLevelItem(a);
}).sort(function (a, b) {

@@ -145,4 +150,5 @@ return a.order && b.order ? a.order - b.order : 0;

var childItems = items.filter(function (a) {
return !_this.isTopLevelItem(a);
return !_this2.isTopLevelItem(a);
}).reduce(function (groups, item) {
if (!item.parent) return groups;
if (Object.prototype.hasOwnProperty.call(groups, item.parent)) {

@@ -164,2 +170,14 @@ groups[item.parent].push(item);

}, {
key: "addNewItem",
value: function addNewItem(_ref2) {
var item = _ref2.item,
_ref2$asLastChild = _ref2.asLastChild,
asLastChild = _ref2$asLastChild === void 0 ? false : _ref2$asLastChild;
var mappedItem = this.addMappingProxyToItem(item);
if (Array.isArray(this.data)) {
this.data[asLastChild ? 'push' : 'unshift'](mappedItem);
}
return this.createItemElement(mappedItem);
}
}, {
key: "createItemElement",

@@ -198,6 +216,6 @@ value: function createItemElement(item) {

value: function maybeAppendItemToParentDom(item) {
var _this2 = this;
var _this3 = this;
var parent = item.parent;
var topParent = this.sortedDataDomArray.find(function (topLevelListItem) {
return _this2.elementIsParentOfItem(topLevelListItem, item) || _this2.elementIsAncestorOfItem(topLevelListItem, item);
return _this3.elementIsParentOfItem(topLevelListItem, item) || _this3.elementIsAncestorOfItem(topLevelListItem, item);
});

@@ -221,3 +239,3 @@ if (!topParent) return false;

value: function getListItemsDom() {
var _this3 = this;
var _this4 = this;
this.sortedDataDomArray = [];

@@ -227,2 +245,3 @@ var processedItems = [];

processedItems = this.sortedData.reduce(function (processedItems, item) {
if (!item.id) return processedItems;
var id = item.id.toString();

@@ -232,7 +251,7 @@ if (processedItems.includes(id)) return processedItems;

if (!item.parent) {
var listItem = _this3.createItemElement(item);
_this3.sortedDataDomArray.push(listItem);
var listItem = _this4.createItemElement(item);
_this4.sortedDataDomArray.push(listItem);
itemAdded = true;
} else {
itemAdded = _this3.maybeAppendItemToParentDom(item);
itemAdded = _this4.maybeAppendItemToParentDom(item);
}

@@ -248,5 +267,5 @@ if (itemAdded) processedItems.push(id);

value: function convertDomToData(list) {
var _this4 = this;
var _this5 = this;
return Array.from((list === null || list === void 0 ? void 0 : list.querySelectorAll('li')) || []).map(function (li) {
var _ref2;
var _ref3;
var parentListItem = li.parentNode;

@@ -257,3 +276,3 @@ var parent = parentListItem.dataset.id;

}) + 1;
return _ref2 = {}, _defineProperty(_ref2, _this4.getItemPropProxyName('id'), li.dataset.id), _defineProperty(_ref2, _this4.getItemPropProxyName('parent'), parent), _defineProperty(_ref2, _this4.getItemPropProxyName('order'), order), _ref2;
return _ref3 = {}, _defineProperty(_ref3, _this5.getItemPropProxyName('id'), li.dataset.id), _defineProperty(_ref3, _this5.getItemPropProxyName('parent'), parent), _defineProperty(_ref3, _this5.getItemPropProxyName('order'), order), _ref3;
});

@@ -379,2 +398,3 @@ }

this.wrapper.appendChild(list);
this.sortableList = list;
}

@@ -629,2 +649,9 @@ }, {

var list = this.getSortableList();
var selfDepth = 0;
if (this.draggedNode) {
// the dragged node might be a nested list contributing to the final nesting levels
var depthUL = this.draggedNode.querySelectorAll('ul').length || 0;
var depthOL = this.draggedNode.querySelectorAll('ol').length || 0;
selfDepth = depthUL > depthOL ? depthUL : depthOL;
}
while (list !== ((_el = el) === null || _el === void 0 ? void 0 : _el.parentElement)) {

@@ -635,3 +662,3 @@ var _el, _el2, _el3;

}
return depth;
return depth + selfDepth;
}

@@ -727,2 +754,19 @@ }, {

}
}, {
key: "addNewItem",
value: function addNewItem(_ref2) {
var _this$getSortableList3;
var item = _ref2.item,
_ref2$asLastChild = _ref2.asLastChild,
asLastChild = _ref2$asLastChild === void 0 ? false : _ref2$asLastChild;
var listItem = this.getDataEngine().addNewItem({
item: item,
asLastChild: asLastChild
});
listItem.setAttribute('draggable', String(this.initialised));
(_this$getSortableList3 = this.getSortableList()) === null || _this$getSortableList3 === void 0 ? void 0 : _this$getSortableList3[asLastChild ? 'append' : 'prepend'](listItem);
return {
data: this.getDataEngine().convertDomToData(this.getSortableList())
};
}
}]);

@@ -729,0 +773,0 @@ return NestedSort;

@@ -1,1 +0,1 @@

!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).NestedSort=t()}(this,(function(){"use strict";function e(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function t(e,t){for(var i=0;i<t.length;i++){var s=t[i];s.enumerable=s.enumerable||!1,s.configurable=!0,"value"in s&&(s.writable=!0),Object.defineProperty(e,n(s.key),s)}}function i(e,i,s){return i&&t(e.prototype,i),s&&t(e,s),Object.defineProperty(e,"prototype",{writable:!1}),e}function s(e,t,i){return(t=n(t))in e?Object.defineProperty(e,t,{value:i,enumerable:!0,configurable:!0,writable:!0}):e[t]=i,e}function r(e){return function(e){if(Array.isArray(e))return a(e)}(e)||function(e){if("undefined"!=typeof Symbol&&null!=e[Symbol.iterator]||null!=e["@@iterator"])return Array.from(e)}(e)||function(e,t){if(!e)return;if("string"==typeof e)return a(e,t);var i=Object.prototype.toString.call(e).slice(8,-1);"Object"===i&&e.constructor&&(i=e.constructor.name);if("Map"===i||"Set"===i)return Array.from(e);if("Arguments"===i||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(i))return a(e,t)}(e)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function a(e,t){(null==t||t>e.length)&&(t=e.length);for(var i=0,s=new Array(t);i<t;i++)s[i]=e[i];return s}function n(e){var t=function(e,t){if("object"!=typeof e||null===e)return e;var i=e[Symbol.toPrimitive];if(void 0!==i){var s=i.call(e,t||"default");if("object"!=typeof s)return s;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(e)}(e,"string");return"symbol"==typeof t?t:String(t)}var o=function(){function t(i){var r=i.data,a=i.propertyMap,n=void 0===a?{}:a,o=i.renderListItem;e(this,t),s(this,"data",void 0),s(this,"sortedData",void 0),s(this,"sortedDataDomArray",void 0),s(this,"propertyMap",void 0),s(this,"renderListItem",void 0),this.data=r,this.sortedData=[],this.sortedDataDomArray=[],this.propertyMap=n,this.renderListItem=o,this.maybeTransformData()}return i(t,[{key:"maybeTransformData",value:function(){if(Object.keys(this.propertyMap).length&&Array.isArray(this.data)){var e=this.getItemPropProxyName.bind(this);this.data=this.data.map((function(t){return new Proxy(t,{get:function(t,i,s){return Reflect.get(t,e(i),s)}})}))}}},{key:"getItemPropProxyName",value:function(e){return Object.prototype.hasOwnProperty.call(this.propertyMap,e)?this.propertyMap[e]:e}},{key:"isTopLevelItem",value:function(e){return!e.parent}},{key:"sortListItems",value:function(){var e=this,t=r(this.data),i=t.filter((function(t){return e.isTopLevelItem(t)})).sort((function(e,t){return e.order&&t.order?e.order-t.order:0})),s=t.filter((function(t){return!e.isTopLevelItem(t)})).reduce((function(e,t){return Object.prototype.hasOwnProperty.call(e,t.parent)?e[t.parent].push(t):e[t.parent]=[t],e}),{});return Object.keys(s).forEach((function(e){s[e].sort((function(e,t){return e.order&&t.order?e.order-t.order:0}))})),this.sortedData=[].concat(r(i),r(Object.values(s).flat())),this.sortedData}},{key:"createItemElement",value:function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"li",i=e.id,s=e.text,r=document.createElement(t);return r.dataset.id=i,"li"===t&&s&&(r.innerHTML=s),"li"===t&&"function"==typeof this.renderListItem?this.renderListItem(r,e):r}},{key:"elementIsParentOfItem",value:function(e,t){return e.dataset.id==="".concat(t.parent)}},{key:"getParentNodeOfItem",value:function(e,t,i){return e.querySelector("".concat(i,'[data-id="').concat(t.parent,'"]'))}},{key:"elementIsAncestorOfItem",value:function(e,t){return!!this.getParentNodeOfItem(e,t,"li")}},{key:"getDirectListParentOfItem",value:function(e,t){return this.getParentNodeOfItem(e,t,"ol")}},{key:"maybeAppendItemToParentDom",value:function(e){var t=this,i=e.parent,s=this.sortedDataDomArray.find((function(i){return t.elementIsParentOfItem(i,e)||t.elementIsAncestorOfItem(i,e)}));if(!s)return!1;var r=this.createItemElement(e),a=this.getDirectListParentOfItem(s,e);a||(a=this.createItemElement({id:i},"ol"),(this.getParentNodeOfItem(s,e,"li")||s).appendChild(a));return a.appendChild(r),!0}},{key:"getListItemsDom",value:function(){var e=this;this.sortedDataDomArray=[];for(var t=[];t.length!==this.sortListItems().length;)t=this.sortedData.reduce((function(t,i){var s,r=i.id.toString();if(t.includes(r))return t;if(i.parent)s=e.maybeAppendItemToParentDom(i);else{var a=e.createItemElement(i);e.sortedDataDomArray.push(a),s=!0}return s&&t.push(r),t}),t);return this.sortedDataDomArray}},{key:"convertDomToData",value:function(e){var t=this;return Array.from((null==e?void 0:e.querySelectorAll("li"))||[]).map((function(e){var i,r=e.parentNode,a=r.dataset.id,n=Array.from(r.children).findIndex((function(t){return t===e}))+1;return s(i={},t.getItemPropProxyName("id"),e.dataset.id),s(i,t.getItemPropProxyName("parent"),a),s(i,t.getItemPropProxyName("order"),n),i}))}},{key:"render",value:function(){var e=document.createElement("ol");return this.getListItemsDom().forEach((function(t){return e.appendChild(t)})),e}}]),t}(),l=function(){function t(i){var r=i.actions,a=void 0===r?{}:r,n=i.data,o=i.droppingEdge,l=void 0===o?15:o,d=i.el,h=i.init,c=void 0===h||h,u=i.listClassNames,g=i.listItemClassNames,f=i.nestingLevels,v=i.propertyMap,p=void 0===v?{}:v,m=i.renderListItem;e(this,t),s(this,"actions",void 0),s(this,"classNames",void 0),s(this,"cursor",void 0),s(this,"data",void 0),s(this,"dataEngine",void 0),s(this,"distances",void 0),s(this,"draggedNode",void 0),s(this,"initialised",void 0),s(this,"listClassNames",void 0),s(this,"listEventListeners",void 0),s(this,"listInterface",void 0),s(this,"listItemClassNames",void 0),s(this,"mainListClassName",void 0),s(this,"nestingLevels",void 0),s(this,"placeholderList",void 0),s(this,"placeholderInUse",void 0),s(this,"propertyMap",void 0),s(this,"renderListItem",void 0),s(this,"sortableList",void 0),s(this,"targetedNode",void 0),s(this,"targetNode",void 0),s(this,"wrapper",void 0),this.renderListItem=m;var y="string"==typeof d?document.querySelector(d):d,L=y instanceof HTMLOListElement||y instanceof HTMLUListElement;this.wrapper=L?void 0:y,this.sortableList=L?y:null,this.data=n,this.listClassNames=this.createListClassNamesArray(u),this.mainListClassName=this.listClassNames[0]||"nested-sort",this.listItemClassNames=this.createListClassNamesArray(g),this.propertyMap=p,this.actions={onDrop:a.onDrop},this.initialised=!1,this.distances={droppingEdge:l},this.classNames={dragged:"ns-dragged",placeholder:"ns-placeholder",targeted:"ns-targeted"},this.listEventListeners={dragover:this.onDragOver.bind(this),dragstart:this.onDragStart.bind(this),dragenter:this.onDragEnter.bind(this),dragend:this.onDragEnd.bind(this),drop:this.onDrop.bind(this)};var N=parseInt(f);this.nestingLevels=isNaN(N)?-1:N,this.listInterface=this.getListInterface(),this.maybeInitDataDom(),this.addListAttributes(),c&&this.initDragAndDrop()}return i(t,[{key:"getListInterface",value:function(){return Array.isArray(this.data)&&this.data.length||this.sortableList instanceof HTMLOListElement?HTMLOListElement:HTMLUListElement}},{key:"getDataEngine",value:function(){return this.dataEngine||(this.dataEngine=new o({data:this.data,propertyMap:this.propertyMap,renderListItem:this.renderListItem})),this.dataEngine}},{key:"createListClassNamesArray",value:function(e){return e?Array.isArray(e)?e:e.split(" "):[]}},{key:"maybeInitDataDom",value:function(){if(Array.isArray(this.data)&&this.data.length&&this.wrapper){var e=this.getDataEngine().render();this.wrapper.innerHTML="",this.wrapper.appendChild(e)}}},{key:"getListTagName",value:function(){return this.listInterface===HTMLOListElement?"ol":"ul"}},{key:"getSortableList",value:function(){var e;return this.sortableList instanceof this.listInterface||(this.sortableList=null===(e=this.wrapper)||void 0===e?void 0:e.querySelector(this.getListTagName())),this.sortableList}},{key:"addListAttributes",value:function(){var e,t=this,i=this.getSortableList();i&&((e=i.classList).add.apply(e,r(this.listClassNames.concat(this.mainListClassName))),i.querySelectorAll(this.getListTagName()).forEach((function(e){var i;(i=e.classList).add.apply(i,r(t.listClassNames))})),i.querySelectorAll("li").forEach((function(e){var i;(i=e.classList).add.apply(i,r(t.listItemClassNames))})))}},{key:"toggleMainListLifeCycleClassName",value:function(){var e=!(arguments.length>0&&void 0!==arguments[0])||arguments[0],t=this.getSortableList();if(t){var i="".concat(this.mainListClassName,"--enabled");e?t.classList.add(i):t.classList.remove(i)}}},{key:"toggleListItemAttributes",value:function(){var e,t=!(arguments.length>0&&void 0!==arguments[0])||arguments[0];null===(e=this.getSortableList())||void 0===e||e.querySelectorAll("li").forEach((function(e){e.setAttribute("draggable",t.toString())}))}},{key:"toggleListEventListeners",value:function(){var e=this,t=arguments.length>0&&void 0!==arguments[0]&&arguments[0],i=this.getSortableList();i&&Object.keys(this.listEventListeners).forEach((function(s){t?i.removeEventListener(s,e.listEventListeners[s]):i.addEventListener(s,e.listEventListeners[s],!1)}))}},{key:"initDragAndDrop",value:function(){this.initialised||(this.toggleListEventListeners(),this.initPlaceholderList(),this.toggleListItemAttributes(),this.toggleMainListLifeCycleClassName(),this.initialised=!0)}},{key:"init",value:function(){this.initDragAndDrop()}},{key:"destroy",value:function(){this.toggleListEventListeners(!0),this.toggleListItemAttributes(!1),this.toggleMainListLifeCycleClassName(!1),this.initialised=!1}},{key:"removeClassFromEl",value:function(e,t){t&&t.classList.contains(e)&&t.classList.remove(e)}},{key:"canBeTargeted",value:function(e){return!(!this.draggedNode||this.draggedNode===e)&&("LI"===e.nodeName?!this.nestingThresholdReached(e):e instanceof this.listInterface&&e.classList.contains(this.classNames.placeholder))}},{key:"onDragStart",value:function(e){var t;this.draggedNode=e.target,this.draggedNode.classList.add(this.classNames.dragged),null===(t=e.dataTransfer)||void 0===t||t.setData("text","")}},{key:"onDragOver",value:function(e){e.preventDefault(),this.updateCoordination(e),this.managePlaceholderLists()}},{key:"onDragEnter",value:function(e){this.canBeTargeted(e.target)&&(this.removeClassFromEl(this.classNames.targeted,this.targetedNode),this.targetedNode=e.target,this.targetedNode.classList.add(this.classNames.targeted))}},{key:"onDragEnd",value:function(e){e.stopPropagation(),this.removeClassFromEl(this.classNames.dragged,this.draggedNode),this.removeClassFromEl(this.classNames.targeted,this.targetedNode),this.cleanupPlaceholderLists(),delete this.draggedNode,delete this.targetedNode}},{key:"onDrop",value:function(e){e.stopPropagation(),this.maybeDrop(),this.cleanupPlaceholderLists(),"function"==typeof this.actions.onDrop&&this.actions.onDrop(this.getDataEngine().convertDomToData(this.getSortableList()))}},{key:"updateCoordination",value:function(e){this.calcMouseCoords(e),this.calcMouseToTargetedElDist()}},{key:"getDropLocation",value:function(){if(this.canBeDropped()){var e;if("LI"===(null===(e=this.targetedNode)||void 0===e?void 0:e.nodeName))return"before";if(this.targetedNode instanceof this.listInterface)return"inside"}}},{key:"maybeDrop",value:function(){var e=this.getDropLocation();e&&this.dropTheItem(e)}},{key:"dropTheItem",value:function(e){var t,i,s;switch(e){case"before":null===(t=this.targetedNode)||void 0===t||null===(i=t.parentNode)||void 0===i||i.insertBefore(this.draggedNode,this.targetedNode);break;case"inside":null===(s=this.targetedNode)||void 0===s||s.appendChild(this.draggedNode)}}},{key:"calcMouseCoords",value:function(e){this.cursor={X:e.clientX,Y:e.clientY}}},{key:"calcMouseToTargetedElDist",value:function(){if(this.targetedNode){var e=this.targetedNode.getBoundingClientRect();this.targetNode={X:e.left,Y:e.top};var t=this.targetNode.Y-this.cursor.Y,i=Math.abs(t);this.distances.mouseTo={targetedElTop:t,targetedElTopAbs:i,targetedElBot:i-this.targetedNode.clientHeight}}}},{key:"areNested",value:function(e,t){return!!e&&!!t&&Array.from(null==t?void 0:t.querySelectorAll("li")).some((function(t){return t===e}))}},{key:"cursorIsIndentedEnough",value:function(){return this.cursor.X-this.targetNode.X>50}},{key:"mouseIsTooCloseToTop",value:function(){var e;return!(null===(e=this.distances)||void 0===e||!e.droppingEdge)&&this.cursor.Y-this.targetNode.Y<this.distances.droppingEdge}},{key:"managePlaceholderLists",value:function(){var e=this;this.analysePlaceHolderSituation().forEach((function(t){switch(t){case"add":e.cleanupPlaceholderLists(),e.addPlaceholderList();break;case"cleanup":e.cleanupPlaceholderLists()}}))}},{key:"targetedNodeIsPlaceholder",value:function(){return this.targetedNode instanceof this.listInterface&&this.targetedNode.classList.contains(this.classNames.placeholder)}},{key:"getNodeDepth",value:function(e){for(var t=0,i=this.getSortableList();i!==(null===(s=e)||void 0===s?void 0:s.parentElement);){var s,r,a;(null===(r=e)||void 0===r?void 0:r.parentElement)instanceof this.listInterface&&t++,e=null===(a=e)||void 0===a?void 0:a.parentElement}return t}},{key:"nestingThresholdReached",value:function(e){var t=arguments.length>1&&void 0!==arguments[1]&&arguments[1];return!(this.nestingLevels<0)&&(t?this.getNodeDepth(e)>=this.nestingLevels:this.getNodeDepth(e)>this.nestingLevels)}},{key:"analysePlaceHolderSituation",value:function(){if(!this.targetedNode||this.areNested(this.targetedNode,this.draggedNode))return[];var e=[];return!this.cursorIsIndentedEnough()||this.mouseIsTooCloseToTop()?this.targetedNodeIsPlaceholder()||e.push("cleanup"):this.targetedNode===this.draggedNode||"LI"!==this.targetedNode.nodeName||this.targetedNode.querySelectorAll(this.getListTagName()).length||this.nestingThresholdReached(this.targetedNode,!0)||e.push("add"),e}},{key:"animatePlaceholderList",value:function(){var e;this.placeholderInUse.style.minHeight="0",this.placeholderInUse.style.transition="min-height ease .2s",this.placeholderInUse.style.minHeight="".concat(null===(e=this.draggedNode)||void 0===e?void 0:e.offsetHeight,"px")}},{key:"addPlaceholderList",value:function(){var e;this.getPlaceholderList(),null===(e=this.targetedNode)||void 0===e||e.appendChild(this.placeholderInUse),this.animatePlaceholderList()}},{key:"targetNodeIsIdentified",value:function(){return!!this.targetedNode}},{key:"targetNodeIsBeingDragged",value:function(){return this.targetNodeIsIdentified()&&this.targetedNode===this.draggedNode}},{key:"targetNodeIsListWithItems",value:function(){return this.targetNodeIsIdentified()&&this.targetedNode instanceof this.listInterface&&!!this.targetedNode.querySelectorAll("li").length}},{key:"canBeDropped",value:function(){return this.targetNodeIsIdentified()&&!this.targetNodeIsBeingDragged()&&!this.targetNodeIsListWithItems()&&!this.areNested(this.targetedNode,this.draggedNode)}},{key:"cleanupPlaceholderLists",value:function(){var e,t=this,i=this.getListTagName(),s=(null===(e=this.getSortableList())||void 0===e?void 0:e.querySelectorAll(i))||[];Array.from(s).forEach((function(e){e.querySelectorAll("li").length?e.classList.contains(t.classNames.placeholder)&&(e.classList.remove(t.classNames.placeholder),e.style.minHeight="auto",e.dataset.id=e.parentNode.dataset.id):e.remove()}))}},{key:"initPlaceholderList",value:function(){var e;this.placeholderList=document.createElement(this.getListTagName()),(e=this.placeholderList.classList).add.apply(e,[this.classNames.placeholder].concat(r(this.listClassNames)))}},{key:"getPlaceholderList",value:function(){return this.placeholderInUse=this.placeholderList.cloneNode(!0),this.placeholderInUse}}]),t}();return l}));
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).NestedSort=t()}(this,(function(){"use strict";function e(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function t(e,t){for(var i=0;i<t.length;i++){var r=t[i];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,n(r.key),r)}}function i(e,i,r){return i&&t(e.prototype,i),r&&t(e,r),Object.defineProperty(e,"prototype",{writable:!1}),e}function r(e,t,i){return(t=n(t))in e?Object.defineProperty(e,t,{value:i,enumerable:!0,configurable:!0,writable:!0}):e[t]=i,e}function s(e){return function(e){if(Array.isArray(e))return a(e)}(e)||function(e){if("undefined"!=typeof Symbol&&null!=e[Symbol.iterator]||null!=e["@@iterator"])return Array.from(e)}(e)||function(e,t){if(!e)return;if("string"==typeof e)return a(e,t);var i=Object.prototype.toString.call(e).slice(8,-1);"Object"===i&&e.constructor&&(i=e.constructor.name);if("Map"===i||"Set"===i)return Array.from(e);if("Arguments"===i||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(i))return a(e,t)}(e)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function a(e,t){(null==t||t>e.length)&&(t=e.length);for(var i=0,r=new Array(t);i<t;i++)r[i]=e[i];return r}function n(e){var t=function(e,t){if("object"!=typeof e||null===e)return e;var i=e[Symbol.toPrimitive];if(void 0!==i){var r=i.call(e,t||"default");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(e)}(e,"string");return"symbol"==typeof t?t:String(t)}var o=function(){function t(i){var s=i.data,a=i.propertyMap,n=void 0===a?{}:a,o=i.renderListItem;e(this,t),r(this,"data",void 0),r(this,"sortedData",void 0),r(this,"sortedDataDomArray",void 0),r(this,"propertyMap",void 0),r(this,"renderListItem",void 0),r(this,"boundGetItemPropProxyName",void 0),this.data=s,this.sortedData=[],this.sortedDataDomArray=[],this.propertyMap=n,this.renderListItem=o,this.boundGetItemPropProxyName=this.getItemPropProxyName.bind(this),this.maybeTransformData()}return i(t,[{key:"addMappingProxyToItem",value:function(e){var t=this;return new Proxy(e,{get:function(e,i,r){return Reflect.get(e,t.boundGetItemPropProxyName(i),r)}})}},{key:"maybeTransformData",value:function(){Object.keys(this.propertyMap).length&&Array.isArray(this.data)&&(this.data=this.data.map(this.addMappingProxyToItem.bind(this)))}},{key:"getItemPropProxyName",value:function(e){return Object.prototype.hasOwnProperty.call(this.propertyMap,e)?this.propertyMap[e]:e}},{key:"isTopLevelItem",value:function(e){return!e.parent}},{key:"sortListItems",value:function(){var e=this,t=s(this.data),i=t.filter((function(t){return e.isTopLevelItem(t)})).sort((function(e,t){return e.order&&t.order?e.order-t.order:0})),r=t.filter((function(t){return!e.isTopLevelItem(t)})).reduce((function(e,t){return t.parent?(Object.prototype.hasOwnProperty.call(e,t.parent)?e[t.parent].push(t):e[t.parent]=[t],e):e}),{});return Object.keys(r).forEach((function(e){r[e].sort((function(e,t){return e.order&&t.order?e.order-t.order:0}))})),this.sortedData=[].concat(s(i),s(Object.values(r).flat())),this.sortedData}},{key:"addNewItem",value:function(e){var t=e.item,i=e.asLastChild,r=void 0!==i&&i,s=this.addMappingProxyToItem(t);return Array.isArray(this.data)&&this.data[r?"push":"unshift"](s),this.createItemElement(s)}},{key:"createItemElement",value:function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"li",i=e.id,r=e.text,s=document.createElement(t);return s.dataset.id=i,"li"===t&&r&&(s.innerHTML=r),"li"===t&&"function"==typeof this.renderListItem?this.renderListItem(s,e):s}},{key:"elementIsParentOfItem",value:function(e,t){return e.dataset.id==="".concat(t.parent)}},{key:"getParentNodeOfItem",value:function(e,t,i){return e.querySelector("".concat(i,'[data-id="').concat(t.parent,'"]'))}},{key:"elementIsAncestorOfItem",value:function(e,t){return!!this.getParentNodeOfItem(e,t,"li")}},{key:"getDirectListParentOfItem",value:function(e,t){return this.getParentNodeOfItem(e,t,"ol")}},{key:"maybeAppendItemToParentDom",value:function(e){var t=this,i=e.parent,r=this.sortedDataDomArray.find((function(i){return t.elementIsParentOfItem(i,e)||t.elementIsAncestorOfItem(i,e)}));if(!r)return!1;var s=this.createItemElement(e),a=this.getDirectListParentOfItem(r,e);a||(a=this.createItemElement({id:i},"ol"),(this.getParentNodeOfItem(r,e,"li")||r).appendChild(a));return a.appendChild(s),!0}},{key:"getListItemsDom",value:function(){var e=this;this.sortedDataDomArray=[];for(var t=[];t.length!==this.sortListItems().length;)t=this.sortedData.reduce((function(t,i){if(!i.id)return t;var r,s=i.id.toString();if(t.includes(s))return t;if(i.parent)r=e.maybeAppendItemToParentDom(i);else{var a=e.createItemElement(i);e.sortedDataDomArray.push(a),r=!0}return r&&t.push(s),t}),t);return this.sortedDataDomArray}},{key:"convertDomToData",value:function(e){var t=this;return Array.from((null==e?void 0:e.querySelectorAll("li"))||[]).map((function(e){var i,s=e.parentNode,a=s.dataset.id,n=Array.from(s.children).findIndex((function(t){return t===e}))+1;return r(i={},t.getItemPropProxyName("id"),e.dataset.id),r(i,t.getItemPropProxyName("parent"),a),r(i,t.getItemPropProxyName("order"),n),i}))}},{key:"render",value:function(){var e=document.createElement("ol");return this.getListItemsDom().forEach((function(t){return e.appendChild(t)})),e}}]),t}(),d=function(){function t(i){var s=i.actions,a=void 0===s?{}:s,n=i.data,o=i.droppingEdge,d=void 0===o?15:o,l=i.el,h=i.init,u=void 0===h||h,c=i.listClassNames,g=i.listItemClassNames,p=i.nestingLevels,m=i.propertyMap,v=void 0===m?{}:m,f=i.renderListItem;e(this,t),r(this,"actions",void 0),r(this,"classNames",void 0),r(this,"cursor",void 0),r(this,"data",void 0),r(this,"dataEngine",void 0),r(this,"distances",void 0),r(this,"draggedNode",void 0),r(this,"initialised",void 0),r(this,"listClassNames",void 0),r(this,"listEventListeners",void 0),r(this,"listInterface",void 0),r(this,"listItemClassNames",void 0),r(this,"mainListClassName",void 0),r(this,"nestingLevels",void 0),r(this,"placeholderList",void 0),r(this,"placeholderInUse",void 0),r(this,"propertyMap",void 0),r(this,"renderListItem",void 0),r(this,"sortableList",void 0),r(this,"targetedNode",void 0),r(this,"targetNode",void 0),r(this,"wrapper",void 0),this.renderListItem=f;var y="string"==typeof l?document.querySelector(l):l,L=y instanceof HTMLOListElement||y instanceof HTMLUListElement;this.wrapper=L?void 0:y,this.sortableList=L?y:null,this.data=n,this.listClassNames=this.createListClassNamesArray(c),this.mainListClassName=this.listClassNames[0]||"nested-sort",this.listItemClassNames=this.createListClassNamesArray(g),this.propertyMap=v,this.actions={onDrop:a.onDrop},this.initialised=!1,this.distances={droppingEdge:d},this.classNames={dragged:"ns-dragged",placeholder:"ns-placeholder",targeted:"ns-targeted"},this.listEventListeners={dragover:this.onDragOver.bind(this),dragstart:this.onDragStart.bind(this),dragenter:this.onDragEnter.bind(this),dragend:this.onDragEnd.bind(this),drop:this.onDrop.bind(this)};var N=parseInt(p);this.nestingLevels=isNaN(N)?-1:N,this.listInterface=this.getListInterface(),this.maybeInitDataDom(),this.addListAttributes(),u&&this.initDragAndDrop()}return i(t,[{key:"getListInterface",value:function(){return Array.isArray(this.data)&&this.data.length||this.sortableList instanceof HTMLOListElement?HTMLOListElement:HTMLUListElement}},{key:"getDataEngine",value:function(){return this.dataEngine||(this.dataEngine=new o({data:this.data,propertyMap:this.propertyMap,renderListItem:this.renderListItem})),this.dataEngine}},{key:"createListClassNamesArray",value:function(e){return e?Array.isArray(e)?e:e.split(" "):[]}},{key:"maybeInitDataDom",value:function(){if(Array.isArray(this.data)&&this.data.length&&this.wrapper){var e=this.getDataEngine().render();this.wrapper.innerHTML="",this.wrapper.appendChild(e),this.sortableList=e}}},{key:"getListTagName",value:function(){return this.listInterface===HTMLOListElement?"ol":"ul"}},{key:"getSortableList",value:function(){var e;return this.sortableList instanceof this.listInterface||(this.sortableList=null===(e=this.wrapper)||void 0===e?void 0:e.querySelector(this.getListTagName())),this.sortableList}},{key:"addListAttributes",value:function(){var e,t=this,i=this.getSortableList();i&&((e=i.classList).add.apply(e,s(this.listClassNames.concat(this.mainListClassName))),i.querySelectorAll(this.getListTagName()).forEach((function(e){var i;(i=e.classList).add.apply(i,s(t.listClassNames))})),i.querySelectorAll("li").forEach((function(e){var i;(i=e.classList).add.apply(i,s(t.listItemClassNames))})))}},{key:"toggleMainListLifeCycleClassName",value:function(){var e=!(arguments.length>0&&void 0!==arguments[0])||arguments[0],t=this.getSortableList();if(t){var i="".concat(this.mainListClassName,"--enabled");e?t.classList.add(i):t.classList.remove(i)}}},{key:"toggleListItemAttributes",value:function(){var e,t=!(arguments.length>0&&void 0!==arguments[0])||arguments[0];null===(e=this.getSortableList())||void 0===e||e.querySelectorAll("li").forEach((function(e){e.setAttribute("draggable",t.toString())}))}},{key:"toggleListEventListeners",value:function(){var e=this,t=arguments.length>0&&void 0!==arguments[0]&&arguments[0],i=this.getSortableList();i&&Object.keys(this.listEventListeners).forEach((function(r){t?i.removeEventListener(r,e.listEventListeners[r]):i.addEventListener(r,e.listEventListeners[r],!1)}))}},{key:"initDragAndDrop",value:function(){this.initialised||(this.toggleListEventListeners(),this.initPlaceholderList(),this.toggleListItemAttributes(),this.toggleMainListLifeCycleClassName(),this.initialised=!0)}},{key:"init",value:function(){this.initDragAndDrop()}},{key:"destroy",value:function(){this.toggleListEventListeners(!0),this.toggleListItemAttributes(!1),this.toggleMainListLifeCycleClassName(!1),this.initialised=!1}},{key:"removeClassFromEl",value:function(e,t){t&&t.classList.contains(e)&&t.classList.remove(e)}},{key:"canBeTargeted",value:function(e){return!(!this.draggedNode||this.draggedNode===e)&&("LI"===e.nodeName?!this.nestingThresholdReached(e):e instanceof this.listInterface&&e.classList.contains(this.classNames.placeholder))}},{key:"onDragStart",value:function(e){var t;this.draggedNode=e.target,this.draggedNode.classList.add(this.classNames.dragged),null===(t=e.dataTransfer)||void 0===t||t.setData("text","")}},{key:"onDragOver",value:function(e){e.preventDefault(),this.updateCoordination(e),this.managePlaceholderLists()}},{key:"onDragEnter",value:function(e){this.canBeTargeted(e.target)&&(this.removeClassFromEl(this.classNames.targeted,this.targetedNode),this.targetedNode=e.target,this.targetedNode.classList.add(this.classNames.targeted))}},{key:"onDragEnd",value:function(e){e.stopPropagation(),this.removeClassFromEl(this.classNames.dragged,this.draggedNode),this.removeClassFromEl(this.classNames.targeted,this.targetedNode),this.cleanupPlaceholderLists(),delete this.draggedNode,delete this.targetedNode}},{key:"onDrop",value:function(e){e.stopPropagation(),this.maybeDrop(),this.cleanupPlaceholderLists(),"function"==typeof this.actions.onDrop&&this.actions.onDrop(this.getDataEngine().convertDomToData(this.getSortableList()))}},{key:"updateCoordination",value:function(e){this.calcMouseCoords(e),this.calcMouseToTargetedElDist()}},{key:"getDropLocation",value:function(){if(this.canBeDropped()){var e;if("LI"===(null===(e=this.targetedNode)||void 0===e?void 0:e.nodeName))return"before";if(this.targetedNode instanceof this.listInterface)return"inside"}}},{key:"maybeDrop",value:function(){var e=this.getDropLocation();e&&this.dropTheItem(e)}},{key:"dropTheItem",value:function(e){var t,i,r;switch(e){case"before":null===(t=this.targetedNode)||void 0===t||null===(i=t.parentNode)||void 0===i||i.insertBefore(this.draggedNode,this.targetedNode);break;case"inside":null===(r=this.targetedNode)||void 0===r||r.appendChild(this.draggedNode)}}},{key:"calcMouseCoords",value:function(e){this.cursor={X:e.clientX,Y:e.clientY}}},{key:"calcMouseToTargetedElDist",value:function(){if(this.targetedNode){var e=this.targetedNode.getBoundingClientRect();this.targetNode={X:e.left,Y:e.top};var t=this.targetNode.Y-this.cursor.Y,i=Math.abs(t);this.distances.mouseTo={targetedElTop:t,targetedElTopAbs:i,targetedElBot:i-this.targetedNode.clientHeight}}}},{key:"areNested",value:function(e,t){return!!e&&!!t&&Array.from(null==t?void 0:t.querySelectorAll("li")).some((function(t){return t===e}))}},{key:"cursorIsIndentedEnough",value:function(){return this.cursor.X-this.targetNode.X>50}},{key:"mouseIsTooCloseToTop",value:function(){var e;return!(null===(e=this.distances)||void 0===e||!e.droppingEdge)&&this.cursor.Y-this.targetNode.Y<this.distances.droppingEdge}},{key:"managePlaceholderLists",value:function(){var e=this;this.analysePlaceHolderSituation().forEach((function(t){switch(t){case"add":e.cleanupPlaceholderLists(),e.addPlaceholderList();break;case"cleanup":e.cleanupPlaceholderLists()}}))}},{key:"targetedNodeIsPlaceholder",value:function(){return this.targetedNode instanceof this.listInterface&&this.targetedNode.classList.contains(this.classNames.placeholder)}},{key:"getNodeDepth",value:function(e){var t=0,i=this.getSortableList(),r=0;if(this.draggedNode){var s=this.draggedNode.querySelectorAll("ul").length||0,a=this.draggedNode.querySelectorAll("ol").length||0;r=s>a?s:a}for(;i!==(null===(n=e)||void 0===n?void 0:n.parentElement);){var n,o,d;(null===(o=e)||void 0===o?void 0:o.parentElement)instanceof this.listInterface&&t++,e=null===(d=e)||void 0===d?void 0:d.parentElement}return t+r}},{key:"nestingThresholdReached",value:function(e){var t=arguments.length>1&&void 0!==arguments[1]&&arguments[1];return!(this.nestingLevels<0)&&(t?this.getNodeDepth(e)>=this.nestingLevels:this.getNodeDepth(e)>this.nestingLevels)}},{key:"analysePlaceHolderSituation",value:function(){if(!this.targetedNode||this.areNested(this.targetedNode,this.draggedNode))return[];var e=[];return!this.cursorIsIndentedEnough()||this.mouseIsTooCloseToTop()?this.targetedNodeIsPlaceholder()||e.push("cleanup"):this.targetedNode===this.draggedNode||"LI"!==this.targetedNode.nodeName||this.targetedNode.querySelectorAll(this.getListTagName()).length||this.nestingThresholdReached(this.targetedNode,!0)||e.push("add"),e}},{key:"animatePlaceholderList",value:function(){var e;this.placeholderInUse.style.minHeight="0",this.placeholderInUse.style.transition="min-height ease .2s",this.placeholderInUse.style.minHeight="".concat(null===(e=this.draggedNode)||void 0===e?void 0:e.offsetHeight,"px")}},{key:"addPlaceholderList",value:function(){var e;this.getPlaceholderList(),null===(e=this.targetedNode)||void 0===e||e.appendChild(this.placeholderInUse),this.animatePlaceholderList()}},{key:"targetNodeIsIdentified",value:function(){return!!this.targetedNode}},{key:"targetNodeIsBeingDragged",value:function(){return this.targetNodeIsIdentified()&&this.targetedNode===this.draggedNode}},{key:"targetNodeIsListWithItems",value:function(){return this.targetNodeIsIdentified()&&this.targetedNode instanceof this.listInterface&&!!this.targetedNode.querySelectorAll("li").length}},{key:"canBeDropped",value:function(){return this.targetNodeIsIdentified()&&!this.targetNodeIsBeingDragged()&&!this.targetNodeIsListWithItems()&&!this.areNested(this.targetedNode,this.draggedNode)}},{key:"cleanupPlaceholderLists",value:function(){var e,t=this,i=this.getListTagName(),r=(null===(e=this.getSortableList())||void 0===e?void 0:e.querySelectorAll(i))||[];Array.from(r).forEach((function(e){e.querySelectorAll("li").length?e.classList.contains(t.classNames.placeholder)&&(e.classList.remove(t.classNames.placeholder),e.style.minHeight="auto",e.dataset.id=e.parentNode.dataset.id):e.remove()}))}},{key:"initPlaceholderList",value:function(){var e;this.placeholderList=document.createElement(this.getListTagName()),(e=this.placeholderList.classList).add.apply(e,[this.classNames.placeholder].concat(s(this.listClassNames)))}},{key:"getPlaceholderList",value:function(){return this.placeholderInUse=this.placeholderList.cloneNode(!0),this.placeholderInUse}},{key:"addNewItem",value:function(e){var t,i=e.item,r=e.asLastChild,s=void 0!==r&&r,a=this.getDataEngine().addNewItem({item:i,asLastChild:s});return a.setAttribute("draggable",String(this.initialised)),null===(t=this.getSortableList())||void 0===t||t[s?"append":"prepend"](a),{data:this.getDataEngine().convertDomToData(this.getSortableList())}}}]),t}();return d}));
{
"name": "nested-sort",
"version": "5.1.3",
"version": "5.2.0",
"author": "Hesam Bahrami (hesamurai)",

@@ -27,2 +27,4 @@ "description": "A JavaScript library for sorting a nested list of items via drag and drop.",

"@rollup/plugin-node-resolve": "^10.0.0",
"@types/jest": "^29.5.2",
"@types/node": "^20.3.1",
"@typescript-eslint/eslint-plugin": "^5.59.5",

@@ -39,2 +41,3 @@ "@typescript-eslint/parser": "^5.59.5",

"serve": "^14.2.0",
"ts-node": "^10.9.1",
"typescript": "^5.0.4"

@@ -53,12 +56,2 @@ },

},
"renovate": {
"extends": [
"config:js-lib"
],
"automerge": true,
"major": {
"automerge": false
},
"automergeType": "branch"
},
"files": [

@@ -65,0 +58,0 @@ "dist"

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc