Skip to content

Commit adb8ff0

Browse files
author
Hesam Bahrami
authored
Merge gilgaz/release/4.2.0 into master #20
Release/4.2.0
2 parents e9ebe2f + 4345294 commit adb8ff0

File tree

11 files changed

+467
-94
lines changed

11 files changed

+467
-94
lines changed

.github/workflows/test.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
on:
2+
pull_request:
3+
push:
4+
branches:
5+
- master
6+
- develop
7+
8+
jobs:
9+
test:
10+
name: Test
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
15+
- uses: actions/checkout@v2
16+
17+
- name: Use Node.js v14
18+
uses: actions/setup-node@v1
19+
with:
20+
node-version: '14'
21+
check-latest: true
22+
23+
- name: Install
24+
run: yarn install
25+
26+
- name: Test
27+
run: yarn test

.huskyrc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"hooks": {
3+
"pre-commit": "yarn test",
4+
"pre-push": "yarn test"
5+
}
6+
}

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Nested Sort
22

3+
[![npm version](https://badge.fury.io/js/nested-sort.svg)](https://badge.fury.io/js/nested-sort)
4+
35
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.
46

57
![](demo.gif)

demo.gif

-1.27 MB
Loading

dev/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ <h1>Samples</h1>
1717
<li><a href="server-rendered-list.html" target="_blank">Server-rendered List</a></li>
1818
<li><a href="server-rendered-multiple-lists.html" target="_blank">Server-rendered Multiple Lists</a></li>
1919
<li><a href="data-driven-list.html" target="_blank">Data-driven List</a></li>
20+
<li><a href="ordered-data-driven-list.html" target="_blank">Ordered Data-driven List</a></li>
2021
<li><a href="mapped-data-driven-list.html" target="_blank">Mapped Data-driven List</a></li>
2122
<li><a href="styling-data-driven-list.html" target="_blank">Styling</a></li>
2223
</ol>

dev/mapped-data-driven-list.html

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,15 @@ <h1>A mapped data-driven list</h1>
2323
<script>
2424
(function() {
2525
const data = [
26-
{ item_id: 1, item_title: 'One' },
27-
{ item_id: 11, item_title: 'One-One', item_parent: 1 },
28-
{ item_id: 2, item_title: 'Two' },
29-
{ item_id: 3, item_title: 'Three' },
30-
{ item_id: 1121, item_title: 'One-One-Two-One', item_parent: 112 },
31-
{ item_id: 1122, item_title: 'One-One-Two-Two', item_parent: 112 },
32-
{ item_id: 1123, item_title: 'One-One-Two-Three', item_parent: 112 },
33-
{ item_id: 12, item_title: 'One-Two', item_parent: 1 },
34-
{ item_id: 111, item_title: 'One-One-One', item_parent: 11 },
35-
{ item_id: 112, item_title: 'One-One-Two', item_parent: 11 },
36-
{ item_id: 113, item_title: 'One-One-Three', item_parent: 11 },
26+
{ item_id: 1, item_title: 'One', position: 5 },
27+
{ item_id: 11, item_title: 'One-One', item_parent: 1, position: 1 },
28+
{ item_id: 2, item_title: 'Two', position: 1 },
29+
{ item_id: 3, item_title: 'Three', position: 2 },
30+
{ item_id: 1121, item_title: 'One-One-Two-One', item_parent: 112, position: 2 },
31+
{ item_id: 1123, item_title: 'One-One-Two-Three', item_parent: 112, position: 4 },
32+
{ item_id: 12, item_title: 'One-Two', item_parent: 1, position: 2 },
33+
{ item_id: 111, item_title: 'One-One-One', item_parent: 11, position: 1 },
34+
{ item_id: 112, item_title: 'One-One-Two', item_parent: 11, position: 2 },
3735
]
3836

3937
new NestedSort({
@@ -47,6 +45,7 @@ <h1>A mapped data-driven list</h1>
4745
id: 'item_id',
4846
parent: 'item_parent',
4947
text: 'item_title',
48+
order: 'position',
5049
},
5150
el: '#draggable',
5251
droppingEdge: 5,

dev/ordered-data-driven-list.html

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1">
6+
7+
<title>Nested Sort Ordered Data-Driven List Demo</title>
8+
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@300&display=swap" rel="stylesheet">
9+
<link href="css/main.css" rel="stylesheet">
10+
</head>
11+
<body>
12+
13+
<div class="container">
14+
<h1>An ordered data-driven list</h1>
15+
16+
<p>The main goal is to create a tree-like list of nested items. You should be able to achieve that by simply dragging and dropping the items using your mouse. Touch screens are not supported yet! 😐</p>
17+
18+
<div id="draggable"></div>
19+
</div>
20+
21+
<!-- Scripts -->
22+
<script src="../dist/nested-sort.umd.js"></script>
23+
<script>
24+
(function() {
25+
const data = [
26+
{ id: 1, text: 'One', order: 2 },
27+
{ id: 11, text: 'One-One', parent: 1, order: 3 },
28+
{ id: 2, text: 'Two', order: 1 },
29+
{ id: 3, text: 'Three', order: 3 },
30+
{ id: 4, text: 'Four', order: 5 },
31+
{ id: 5, text: 'Five', order: 4 },
32+
{ id: 12, text: 'One-Two', parent: 1, order: 1 },
33+
{ id: 111, text: 'One-One-One', parent: 11, order: 3 },
34+
{ id: 112, text: 'One-One-Two', parent: 11, order: 1 },
35+
{ id: 113, text: 'One-One-Three', parent: 11, order: 2 },
36+
]
37+
38+
new NestedSort({
39+
actions: {
40+
onDrop: function (data) {
41+
console.log(data)
42+
}
43+
},
44+
data: data,
45+
el: '#draggable',
46+
droppingEdge: 5,
47+
listClassNames: ['nested-sort']
48+
});
49+
})();
50+
</script>
51+
</body>
52+
</html>

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nested-sort",
3-
"version": "4.1.0",
3+
"version": "4.2.0",
44
"author": "Hesam Bahrami (Genzo)",
55
"description": "A JavaScript library to create a nested list of elements",
66
"main": "dist/nested-sort.cjs.js",
@@ -17,6 +17,7 @@
1717
"babel-core": "7.0.0-bridge.0",
1818
"babel-jest": "24.9.0",
1919
"concurrently": "4.1.2",
20+
"husky": "^4.2.5",
2021
"jest": "24.9.0",
2122
"rollup": "1.32.1",
2223
"rollup-plugin-babel": "4.4.0",

src/data-engine.js

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,39 @@ class DataEngine {
3838
return prop
3939
}
4040

41+
isTopLevelItem(item) {
42+
return !item.parent
43+
}
44+
4145
/**
4246
* @returns {object[]}
4347
*/
4448
sortListItems() {
45-
this.sortedData = [...this.data].sort((item1, item2) => {
46-
if (!item1.parent && item2.parent) return -1
47-
return (!item2.parent && item1.parent) ? 1 : 0;
48-
});
49+
const items = [...this.data]
50+
51+
const topLevelItems = items
52+
.filter(a => this.isTopLevelItem(a))
53+
.sort((a, b) => a.order && b.order ? a.order - b.order : 0)
54+
55+
const childItems = items
56+
.filter(a => !this.isTopLevelItem(a))
57+
.reduce((groups, item) => {
58+
if (groups.hasOwnProperty(item.parent)) {
59+
groups[item.parent].push(item)
60+
} else {
61+
groups[item.parent] = [item]
62+
}
63+
return groups
64+
}, {})
65+
66+
Object.keys(childItems).forEach(parentId => {
67+
childItems[parentId].sort((a, b) => a.order && b.order ? a.order - b.order : 0)
68+
})
69+
70+
this.sortedData = [
71+
...topLevelItems,
72+
...Object.values(childItems).flat()
73+
]
4974

5075
return this.sortedData
5176
}
@@ -166,10 +191,12 @@ class DataEngine {
166191
return Array.from(ul.querySelectorAll('li')).map(li => {
167192
const parentListItem = li.parentNode
168193
const parent = parentListItem.dataset.id
194+
const order = Array.from(parentListItem.children).findIndex(item => item === li) + 1
169195

170196
return {
171197
[this.getItemPropProxyName('id')]: li.dataset.id,
172198
[this.getItemPropProxyName('parent')]: parent,
199+
[this.getItemPropProxyName('order')]: order,
173200
}
174201
})
175202
}

0 commit comments

Comments
 (0)