Skip to content

Commit 7cb47fa

Browse files
author
Hesam Bahrami
authored
feat: server rendered list property mapping (#40)
* add a test for the maybeTransformData method invocation upon instantiation * make the data argument optional so property mapping can work on server rendered lists as well * update one of the server rendered samples to utilise property mapping
1 parent 386bc4c commit 7cb47fa

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

dev/server-rendered-list.html

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ <h1>A server-rendered list</h1>
1515

1616
<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>
1717

18+
<input type="checkbox" id="property-mapping" onchange="updateList()">
19+
<label for="property-mapping">Use property mapping (affects the list data structure logged to the console after each drag and drop)</label>
20+
1821
<ul id="draggable">
1922
<li data-id="1">Topic 1</li>
2023
<li data-id="2">Topic 2</li>
@@ -34,19 +37,28 @@ <h1>A server-rendered list</h1>
3437
</div>
3538

3639
<!-- Scripts -->
37-
<script src="../dist/nested-sort.umd.min.js"></script>
40+
<script src="../dist/nested-sort.umd.js"></script>
3841
<script>
39-
(function() {
40-
new NestedSort({
42+
function updateList() {
43+
if (window.ns) window.ns.destroy()
44+
45+
const usePropertyMapping = document.getElementById('property-mapping').checked
46+
window.ns = new NestedSort({
4147
actions: {
4248
onDrop: function (data) {
43-
console.log(data)
49+
console.log(`data ${usePropertyMapping ? 'with' : 'without'} property mapping`, data)
4450
}
4551
},
52+
propertyMap: usePropertyMapping ? {
53+
id: 'item_id',
54+
order: 'position',
55+
} : undefined,
4656
el: '#draggable',
4757
droppingEdge: 5
4858
})
49-
})()
59+
}
60+
61+
updateList()
5062
</script>
5163
</body>
5264
</html>

src/data-engine.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class DataEngine {
22
/**
33
* @constructor
4-
* @param {object[]} data
4+
* @param {object[]} [data]
55
* @param {object} [propertyMap={}]
66
*/
77
constructor({ data, propertyMap = {} }) {
@@ -14,7 +14,7 @@ class DataEngine {
1414
}
1515

1616
maybeTransformData() {
17-
if (!Object.keys(this.propertyMap).length) return
17+
if (!Object.keys(this.propertyMap).length || !Array.isArray(this.data)) return
1818

1919
const getItemPropProxyName = this.getItemPropProxyName.bind(this)
2020

test/data-engine.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,22 @@ describe('DataEngine class', () => {
5656
expect((new DataEngine(dataEngineConfig)).sortedData).toEqual([])
5757
expect((new DataEngine(dataEngineConfig)).sortedDataDomArray).toEqual([])
5858
})
59+
60+
it('should invoke the maybeTransformData method', () => {
61+
jest.spyOn(DataEngine.prototype, 'maybeTransformData')
62+
const de = new DataEngine({})
63+
expect(de.maybeTransformData).toHaveBeenCalledTimes(1)
64+
})
65+
66+
it('should not throw an error if only the propertyMap options is defined', () => {
67+
jest.spyOn(DataEngine.prototype, 'maybeTransformData')
68+
const de = new DataEngine({
69+
propertyMap: {
70+
id: 'item_id',
71+
}
72+
})
73+
expect(de.maybeTransformData).toHaveBeenCalledTimes(1)
74+
})
5975
})
6076

6177
describe('sortListItems method', () => {

0 commit comments

Comments
 (0)