-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInteractiveTree.js
More file actions
136 lines (116 loc) · 3.53 KB
/
Copy pathInteractiveTree.js
File metadata and controls
136 lines (116 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/// <reference path="../Scripts/jspack-vsdoc.js" />
var common = MindFusion.Common;
var ui = MindFusion.Common.UI;
// Create some data to use for tree nodes.
var treeData = [
{
title: "Parent 1",
items: [{ title: "Child 1.1" }, { title: "Child 1.2" }]
},
{
title: "Parent 2",
items: [{ title: "Child 2.1" }, { title: "Child 2.2" }]
},
{
title: "Parent 3",
items: [{ title: "Child 3.1" }, { title: "Child 3.2" }, { title: "Child 3.3" }]
}];
// Create a new TreeView control.
var tree = new ui.TreeView();
tree.theme = "gray";
tree.allowDrag = false;
tree.fromObject(treeData);
tree.itemMouseDown.addEventListener(nodeMouseDown);
document.body.appendChild(tree.draw());
tree.attach();
// Handle the contextmenu JS event to prevent the default context menu from showing.
tree.element.addEventListener('contextmenu', function (e) { e.preventDefault() });
// Handle the mousedown JS event to show the menu.
tree.element.addEventListener('mousedown', treeMouseDown);
tree.selectionChanged.addEventListener(treeSelectionChanged);
// Create a new Menu control.
var menu = new ui.Menu();
menu.theme = "gray";
menu.cssClass = "popmenu";
menu.visible = false;
var item1 = new ui.MenuItem();
item1.template = "<input type='text' id='node_title'></input>➕";
item1.tooltip = "Add child node";
item1.data = "child";
menu.items.add(item1);
var item2 = new ui.MenuItem("Remove selected node");
item2.data = "remove";
menu.items.add(item2);
menu.itemClick.addEventListener(menuItemClick);
document.body.appendChild(menu.draw());
menu.attach();
function treeSelectionChanged(sender, args)
{
if (args.oldItems)
args.oldItems.forEach((item) => console.log(item.title + " deselected"));
if (args.newItems)
args.newItems.forEach((item) => console.log(item.title + " selected"));
}
// This is the handler function for tree mousedown JS event.
// Modify the menu accordingly and show it on right mouse button click.
function treeMouseDown(e)
{
if (e.button == 2)
{
if (!menu.visible)
{
menu.left = common.Unit.pixel(e.clientX + 20);
menu.top = common.Unit.pixel(e.clientY);
menu.items.first().tooltip = "Add root item";
menu.items.first().data = "root";
menu.items.last().enabled = false;
menu.visible = true;
}
}
else
menu.visible = false;
}
// This is the handler function for tree itemMouseDown event.
// Modify the menu accordingly and show it on right mouse button click.
function nodeMouseDown(sender, args)
{
if (args.rawEventArgs.button == 2)
{
menu.left = common.Unit.pixel(args.rawEventArgs.clientX + 20);
menu.top = common.Unit.pixel(args.rawEventArgs.clientY);
menu.items.first().tooltip = "Add child item";
menu.items.first().data = "child";
menu.items.last().enabled = true;
menu.visible = true;
}
else
menu.visible = false;
}
// This is the handler function for menu itemClick event.
// If the "remove" item was clicked, remove the selected node.
// If the "add" item was clicked, add a new child node (if there is a selected node),
// or a new root node.
function menuItemClick(sender, args)
{
if (args.item.data == "remove")
{
var node = tree.selection.first();
if (node.parentNode)
node.parentNode.items.remove(node);
else
tree.items.remove(node);
}
else
{
var input = args.item.element.querySelector("#node_title");
if (args.rawEventArgs.target == input) return;
var title = input.value || "<untitled>";
var node = new ui.TreeNode(title);
if (args.item.data == "root")
tree.items.add(node);
else
tree.selection.first().items.add(node);
input.value = "";
}
menu.visible = false;
}