nested-sort
Advanced tools
Comparing version 4.1.0 to 4.2.0
@@ -43,2 +43,6 @@ 'use strict'; | ||
isTopLevelItem(item) { | ||
return !item.parent | ||
} | ||
/** | ||
@@ -48,7 +52,28 @@ * @returns {object[]} | ||
sortListItems() { | ||
this.sortedData = [...this.data].sort((item1, item2) => { | ||
if (!item1.parent && item2.parent) return -1 | ||
return (!item2.parent && item1.parent) ? 1 : 0; | ||
const items = [...this.data]; | ||
const topLevelItems = items | ||
.filter(a => this.isTopLevelItem(a)) | ||
.sort((a, b) => a.order && b.order ? a.order - b.order : 0); | ||
const childItems = items | ||
.filter(a => !this.isTopLevelItem(a)) | ||
.reduce((groups, item) => { | ||
if (groups.hasOwnProperty(item.parent)) { | ||
groups[item.parent].push(item); | ||
} else { | ||
groups[item.parent] = [item]; | ||
} | ||
return groups | ||
}, {}); | ||
Object.keys(childItems).forEach(parentId => { | ||
childItems[parentId].sort((a, b) => a.order && b.order ? a.order - b.order : 0); | ||
}); | ||
this.sortedData = [ | ||
...topLevelItems, | ||
...Object.values(childItems).flat() | ||
]; | ||
return this.sortedData | ||
@@ -173,2 +198,3 @@ } | ||
const parent = parentListItem.dataset.id; | ||
const order = Array.from(parentListItem.children).findIndex(item => item === li) + 1; | ||
@@ -178,2 +204,3 @@ return { | ||
[this.getItemPropProxyName('parent')]: parent, | ||
[this.getItemPropProxyName('order')]: order, | ||
} | ||
@@ -180,0 +207,0 @@ }) |
@@ -41,2 +41,6 @@ class DataEngine { | ||
isTopLevelItem(item) { | ||
return !item.parent | ||
} | ||
/** | ||
@@ -46,7 +50,28 @@ * @returns {object[]} | ||
sortListItems() { | ||
this.sortedData = [...this.data].sort((item1, item2) => { | ||
if (!item1.parent && item2.parent) return -1 | ||
return (!item2.parent && item1.parent) ? 1 : 0; | ||
const items = [...this.data]; | ||
const topLevelItems = items | ||
.filter(a => this.isTopLevelItem(a)) | ||
.sort((a, b) => a.order && b.order ? a.order - b.order : 0); | ||
const childItems = items | ||
.filter(a => !this.isTopLevelItem(a)) | ||
.reduce((groups, item) => { | ||
if (groups.hasOwnProperty(item.parent)) { | ||
groups[item.parent].push(item); | ||
} else { | ||
groups[item.parent] = [item]; | ||
} | ||
return groups | ||
}, {}); | ||
Object.keys(childItems).forEach(parentId => { | ||
childItems[parentId].sort((a, b) => a.order && b.order ? a.order - b.order : 0); | ||
}); | ||
this.sortedData = [ | ||
...topLevelItems, | ||
...Object.values(childItems).flat() | ||
]; | ||
return this.sortedData | ||
@@ -171,2 +196,3 @@ } | ||
const parent = parentListItem.dataset.id; | ||
const order = Array.from(parentListItem.children).findIndex(item => item === li) + 1; | ||
@@ -176,2 +202,3 @@ return { | ||
[this.getItemPropProxyName('parent')]: parent, | ||
[this.getItemPropProxyName('order')]: order, | ||
} | ||
@@ -178,0 +205,0 @@ }) |
@@ -124,2 +124,7 @@ (function (global, factory) { | ||
} | ||
}, { | ||
key: "isTopLevelItem", | ||
value: function isTopLevelItem(item) { | ||
return !item.parent; | ||
} | ||
/** | ||
@@ -132,6 +137,28 @@ * @returns {object[]} | ||
value: function sortListItems() { | ||
this.sortedData = _toConsumableArray(this.data).sort(function (item1, item2) { | ||
if (!item1.parent && item2.parent) return -1; | ||
return !item2.parent && item1.parent ? 1 : 0; | ||
var _this = this; | ||
var items = _toConsumableArray(this.data); | ||
var topLevelItems = items.filter(function (a) { | ||
return _this.isTopLevelItem(a); | ||
}).sort(function (a, b) { | ||
return a.order && b.order ? a.order - b.order : 0; | ||
}); | ||
var childItems = items.filter(function (a) { | ||
return !_this.isTopLevelItem(a); | ||
}).reduce(function (groups, item) { | ||
if (groups.hasOwnProperty(item.parent)) { | ||
groups[item.parent].push(item); | ||
} else { | ||
groups[item.parent] = [item]; | ||
} | ||
return groups; | ||
}, {}); | ||
Object.keys(childItems).forEach(function (parentId) { | ||
childItems[parentId].sort(function (a, b) { | ||
return a.order && b.order ? a.order - b.order : 0; | ||
}); | ||
}); | ||
this.sortedData = [].concat(_toConsumableArray(topLevelItems), _toConsumableArray(Object.values(childItems).flat())); | ||
return this.sortedData; | ||
@@ -209,7 +236,7 @@ } | ||
value: function maybeAppendItemToParentDom(item) { | ||
var _this = this; | ||
var _this2 = this; | ||
var parent = item.parent; | ||
var topParent = this.sortedDataDomArray.find(function (topLevelListItem) { | ||
return _this.elementIsParentOfItem(topLevelListItem, item) || _this.elementIsAncestorOfItem(topLevelListItem, item); | ||
return _this2.elementIsParentOfItem(topLevelListItem, item) || _this2.elementIsAncestorOfItem(topLevelListItem, item); | ||
}); | ||
@@ -239,3 +266,3 @@ if (!topParent) return false; | ||
value: function getListItemsDom() { | ||
var _this2 = this; | ||
var _this3 = this; | ||
@@ -252,9 +279,9 @@ this.sortedDataDomArray = []; | ||
if (!item.parent) { | ||
var listItem = _this2.createItemElement(item); | ||
var listItem = _this3.createItemElement(item); | ||
_this2.sortedDataDomArray.push(listItem); | ||
_this3.sortedDataDomArray.push(listItem); | ||
itemAdded = true; | ||
} else { | ||
itemAdded = _this2.maybeAppendItemToParentDom(item); | ||
itemAdded = _this3.maybeAppendItemToParentDom(item); | ||
} | ||
@@ -277,3 +304,3 @@ | ||
value: function convertDomToData(ul) { | ||
var _this3 = this; | ||
var _this4 = this; | ||
@@ -285,3 +312,6 @@ return Array.from(ul.querySelectorAll('li')).map(function (li) { | ||
var parent = parentListItem.dataset.id; | ||
return _ref2 = {}, _defineProperty(_ref2, _this3.getItemPropProxyName('id'), li.dataset.id), _defineProperty(_ref2, _this3.getItemPropProxyName('parent'), parent), _ref2; | ||
var order = Array.from(parentListItem.children).findIndex(function (item) { | ||
return item === li; | ||
}) + 1; | ||
return _ref2 = {}, _defineProperty(_ref2, _this4.getItemPropProxyName('id'), li.dataset.id), _defineProperty(_ref2, _this4.getItemPropProxyName('parent'), parent), _defineProperty(_ref2, _this4.getItemPropProxyName('order'), order), _ref2; | ||
}); | ||
@@ -288,0 +318,0 @@ } |
{ | ||
"name": "nested-sort", | ||
"version": "4.1.0", | ||
"version": "4.2.0", | ||
"author": "Hesam Bahrami (Genzo)", | ||
@@ -20,2 +20,3 @@ "description": "A JavaScript library to create a nested list of elements", | ||
"concurrently": "4.1.2", | ||
"husky": "^4.2.5", | ||
"jest": "24.9.0", | ||
@@ -22,0 +23,0 @@ "rollup": "1.32.1", |
# Nested Sort | ||
[![npm version](https://badge.fury.io/js/nested-sort.svg)](https://badge.fury.io/js/nested-sort) | ||
Nested Sort is a vanilla JavaScript library, without any dependencies, which helps you to sort a nested list of items via drag and drop. Unfortunately, it does not support touch screens yet. | ||
@@ -4,0 +6,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
69979
1660
58
12