Skip to content

Commit 1ef28b3

Browse files
Dropping node descendants function add
1 parent df9c4fd commit 1ef28b3

File tree

3 files changed

+67
-8
lines changed

3 files changed

+67
-8
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "iknow-entity-browser",
3-
"version": "0.3.1",
3+
"version": "0.4.0",
44
"description": "Visualizer for iKnow entities",
55
"main": "gulpfile.babel.js",
66
"scripts": {

src/static/js/controls.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
/**
22
* This file describes on-screen controls like UI link/remove buttons, etc.
33
*/
4-
import { onSelectionUpdate } from "./selection";
4+
import { onSelectionUpdate, updateSelection } from "./selection";
5+
import { dropDescendants } from "./model";
56

67
let dropChildrenButton = null,
78
removeButton = null,
@@ -14,17 +15,25 @@ onSelectionUpdate((sel) => {
1415

1516
function updateButtons () {
1617
let display = selection.length ? "block" : "none";
17-
dropChildrenButton.style.display = removeButton.style.display = display;
18+
dropChildrenButton.style.display = display;
19+
removeButton.classList.add("disabled"); // temporary
1820
}
1921

2022
function deleteSelection () {
2123

2224
}
2325

26+
function dropChildren () {
27+
if (!selection.length)
28+
return;
29+
dropDescendants(selection);
30+
updateSelection();
31+
}
32+
2433
export function init () {
2534
dropChildrenButton = document.getElementById(`dropChildrenButton`);
26-
(removeButton = document.getElementById(`removeButton`)).addEventListener("click",
27-
() => deleteSelection()
28-
);
35+
dropChildrenButton.addEventListener("click", () => dropChildren());
36+
removeButton = document.getElementById(`removeButton`);
37+
removeButton.addEventListener("click", () => deleteSelection());
2938
updateButtons();
3039
}

src/static/js/model/index.js

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,9 @@ function resetChildrenPosition (parent, children = []) {
228228
// }
229229

230230
/**
231-
* Unfold the node
231+
* Unfold the folder by specified amount of nodes.
232232
* @param node - Node to unfold
233-
* @param [children] - Number of nodes to unfold
233+
* @param [children=20] - Number of nodes to unfold
234234
* @returns {number} - Number of nodes left to unfold
235235
*/
236236
export function unfold (node, children = 20) {
@@ -263,4 +263,54 @@ export function unfold (node, children = 20) {
263263
}
264264
});
265265
return res.left;
266+
}
267+
268+
/**
269+
* Delete all node's descendants.
270+
* @param {Array|*} nodes - Node to unfold
271+
* @returns {number} - Number of nodes unlinked
272+
*/
273+
export function dropDescendants (nodes) {
274+
275+
if (!(nodes instanceof Array))
276+
nodes = [nodes];
277+
278+
let toDrop = 0;
279+
for (let node of nodes) {
280+
toDrop += (node.children ? node.children.length : 0)
281+
+ (node._children ? node._children.length : 0);
282+
}
283+
if (toDrop === 0)
284+
return 0;
285+
286+
let restore = nodes.slice().map(node => {
287+
return {
288+
node: node,
289+
children: node.children,
290+
_children: node._children
291+
};
292+
});
293+
294+
function f () {
295+
for (let node of nodes) {
296+
node.children = [];
297+
node._children = [];
298+
}
299+
dataUpdated();
300+
}
301+
f();
302+
303+
history.createState({
304+
redo: f,
305+
undo: () => {
306+
for (let res of restore) {
307+
res.node.children = res.children;
308+
res.node._children = res._children;
309+
}
310+
dataUpdated();
311+
}
312+
});
313+
314+
return toDrop;
315+
266316
}

0 commit comments

Comments
 (0)