|
16 | 16 | button(data-type="group").create-group Create group
|
17 | 17 | button.remove-selected Remove selected
|
18 | 18 | .selected-nodes No items selected
|
| 19 | + .dnd-result |
19 | 20 | script.
|
20 |
| - var treeView = new TreeView(document.querySelector("main"), { dropCallback: function(dropInfo, orderedNodes) { return true; } }); |
| 21 | + var treeView = new TreeView(document.querySelector("main"), { |
| 22 | + dragStartCallback: function(event, node) { |
| 23 | + event.dataTransfer.setData("text/plain", node.textContent); |
| 24 | + return true; |
| 25 | + }, |
| 26 | + dropCallback: function(event, dropLocation, orderedNodes) { |
| 27 | + document.querySelector("nav .dnd-result").textContent = |
| 28 | + event.dataTransfer.getData("text/plain") + " was dropped " + |
| 29 | + dropLocation.where + " " + dropLocation.target.textContent; |
| 30 | + return true; |
| 31 | + } |
| 32 | + }); |
21 | 33 |
|
22 |
| - function createItem(label) { |
23 |
| - itemElt = document.createElement("li"); |
| 34 | + function createItem(label) { |
| 35 | + itemElt = document.createElement("li"); |
24 | 36 |
|
25 |
| - iconElt = document.createElement("div"); |
26 |
| - iconElt.classList.add("icon"); |
27 |
| - itemElt.appendChild(iconElt); |
| 37 | + iconElt = document.createElement("div"); |
| 38 | + iconElt.classList.add("icon"); |
| 39 | + itemElt.appendChild(iconElt); |
28 | 40 |
|
29 |
| - spanElt = document.createElement("span"); |
30 |
| - spanElt.textContent = label; |
31 |
| - itemElt.appendChild(spanElt); |
| 41 | + spanElt = document.createElement("span"); |
| 42 | + spanElt.textContent = label; |
| 43 | + itemElt.appendChild(spanElt); |
32 | 44 |
|
33 |
| - return itemElt; |
34 |
| - } |
| 45 | + return itemElt; |
| 46 | + } |
35 | 47 |
|
36 |
| - function createGroup(label) { |
37 |
| - groupElt = document.createElement("li"); |
| 48 | + function createGroup(label) { |
| 49 | + groupElt = document.createElement("li"); |
38 | 50 |
|
39 |
| - spanElt = document.createElement("span"); |
40 |
| - spanElt.textContent = label; |
41 |
| - groupElt.appendChild(spanElt); |
| 51 | + spanElt = document.createElement("span"); |
| 52 | + spanElt.textContent = label; |
| 53 | + groupElt.appendChild(spanElt); |
42 | 54 |
|
43 |
| - return groupElt; |
44 |
| - } |
| 55 | + return groupElt; |
| 56 | + } |
45 | 57 |
|
46 |
| - for (var i = 0; i < 3; i++) { |
47 |
| - var group = createGroup("Group " + (i+1)); |
48 |
| - treeView.append(group, "group"); |
| 58 | + for (var i = 0; i < 3; i++) { |
| 59 | + var group = createGroup("Group " + (i+1)); |
| 60 | + treeView.append(group, "group"); |
49 | 61 |
|
50 |
| - for (var j = 0; j < 3; j++) { |
51 |
| - var item = createItem("Item " + (i*3+j+1)); |
52 |
| - treeView.append(item, "item", group); |
53 |
| - } |
| 62 | + for (var j = 0; j < 3; j++) { |
| 63 | + var item = createItem("Item " + (i*3+j+1)); |
| 64 | + treeView.append(item, "item", group); |
54 | 65 | }
|
55 |
| - |
56 |
| - group = createGroup("Empty Group 1"); |
57 |
| - treeView.append(group, "group", document.querySelector(".group")); |
58 |
| - |
59 |
| - group = createGroup("Empty Group 2"); |
60 |
| - treeView.append(group, "group", document.querySelector(".group:last-child")); |
61 |
| - |
62 |
| - function onClickCreate(event) { |
63 |
| - var type = event.target.dataset.type; |
64 |
| - var label = prompt("Enter a name", ""); |
65 |
| - if (label.length === 0) return; |
66 |
| - var node = (type === "item") ? createItem(label) : createGroup(label); |
67 |
| - |
68 |
| - var parentNode = treeView.selectedNodes[0]; |
69 |
| - if (parentNode != null && !parentNode.classList.contains("group")) { |
70 |
| - parentNode = parentNode.parentElement.classList.contains("children") ? parentNode.parentElement.previousSibling : null; |
71 |
| - } |
72 |
| - |
73 |
| - treeView.append(node, type, parentNode); |
| 66 | + } |
| 67 | + |
| 68 | + group = createGroup("Empty Group 1"); |
| 69 | + treeView.append(group, "group", document.querySelector(".group")); |
| 70 | + |
| 71 | + group = createGroup("Empty Group 2"); |
| 72 | + treeView.append(group, "group", document.querySelector(".group:last-child")); |
| 73 | + |
| 74 | + function onClickCreate(event) { |
| 75 | + var type = event.target.dataset.type; |
| 76 | + var label = prompt("Enter a name", ""); |
| 77 | + if (label.length === 0) return; |
| 78 | + var node = (type === "item") ? createItem(label) : createGroup(label); |
| 79 | + |
| 80 | + var parentNode = treeView.selectedNodes[0]; |
| 81 | + if (parentNode != null && !parentNode.classList.contains("group")) { |
| 82 | + parentNode = parentNode.parentElement.classList.contains("children") ? parentNode.parentElement.previousSibling : null; |
74 | 83 | }
|
75 | 84 |
|
76 |
| - document.querySelector("nav .create-item").addEventListener("click", onClickCreate); |
77 |
| - document.querySelector("nav .create-group").addEventListener("click", onClickCreate); |
| 85 | + treeView.append(node, type, parentNode); |
| 86 | + } |
78 | 87 |
|
79 |
| - document.querySelector("nav .remove-selected").addEventListener("click", function() { |
80 |
| - while (treeView.selectedNodes.length > 0) { |
81 |
| - treeView.remove(treeView.selectedNodes[treeView.selectedNodes.length - 1]); |
82 |
| - } |
83 |
| - }); |
| 88 | + document.querySelector("nav .create-item").addEventListener("click", onClickCreate); |
| 89 | + document.querySelector("nav .create-group").addEventListener("click", onClickCreate); |
| 90 | + |
| 91 | + document.querySelector("nav .remove-selected").addEventListener("click", function() { |
| 92 | + while (treeView.selectedNodes.length > 0) { |
| 93 | + treeView.remove(treeView.selectedNodes[treeView.selectedNodes.length - 1]); |
| 94 | + } |
| 95 | + }); |
84 | 96 |
|
85 |
| - treeView.on("selectionChange", function() { |
86 |
| - var text; |
87 |
| - if (treeView.selectedNodes.length > 1) text = "" + treeView.selectedNodes.length + " items selected"; |
88 |
| - else if (treeView.selectedNodes.length === 1) text = "1 item selected"; |
89 |
| - else text = "No items selected"; |
| 97 | + treeView.on("selectionChange", function() { |
| 98 | + var text; |
| 99 | + if (treeView.selectedNodes.length > 1) text = "" + treeView.selectedNodes.length + " items selected"; |
| 100 | + else if (treeView.selectedNodes.length === 1) text = "1 item selected"; |
| 101 | + else text = "No items selected"; |
90 | 102 |
|
91 |
| - document.querySelector("nav .selected-nodes").textContent = text; |
92 |
| - }); |
| 103 | + document.querySelector("nav .selected-nodes").textContent = text; |
| 104 | + }); |
93 | 105 |
|
94 |
| - treeView.on("activate", function() { |
95 |
| - alert("Activated " + treeView.selectedNodes[0].querySelector("span").textContent); |
96 |
| - }); |
| 106 | + treeView.on("activate", function() { |
| 107 | + alert("Activated " + treeView.selectedNodes[0].querySelector("span").textContent); |
| 108 | + }); |
0 commit comments