-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
245 lines (217 loc) · 5.56 KB
/
script.js
File metadata and controls
245 lines (217 loc) · 5.56 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
var _jm = null;
function open_empty() {
var options = {
container: "jsmind_container",
theme: "default",
editable: true,
view: {
engine: "svg",
draggable: true,
},
};
_jm = jsMind.show(options);
var empty_map_data = {
meta: {
name: "Mindmap",
author: "Jaydip Dey",
version: "1",
},
format: "node_tree",
data: {
id: "root",
topic: "Root",
direction: "right",
expanded: true,
},
};
// Load the empty map data into jsMind
_jm.show(empty_map_data);
}
let jsonData;
document
.getElementById("formFile")
.addEventListener("change", handleFileUpload);
function handleFileUpload(event) {
const file = event.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = function (e) {
try {
jsonData = JSON.parse(e.target.result);
console.log(jsonData);
let jsMindNodeTree = convertToJsMindNodeTree(jsonData);
open_json(jsMindNodeTree);
//document.querySelector(".root").style.visibility = "visible";
} catch (error) {
console.error("Error parsing JSON:", error);
}
};
reader.readAsText(file);
} else {
console.error("No file selected.");
}
}
function convertToJsMindNodeTree(obj) {
function traverse(object, parentId) {
let nodes = [];
for (let key in object) {
const nodeId = parentId ? parentId + "-" + key : key;
const nodeData = {
id: nodeId,
topic: key,
direction: "right",
expanded: true,
val: object[key],
};
if (typeof object[key] === "object" && object[key] !== null) {
const children = traverse(object[key], nodeId);
if (children.length > 0) {
nodeData.children = children;
}
}
nodes.push(nodeData);
}
return nodes;
}
const rootNode = {
id: "root",
topic: "Root",
direction: "right",
expanded: true,
};
rootNode.children = traverse(obj, rootNode.id);
return rootNode;
}
function open_json(jsMindNodeTree) {
var mind = {
meta: {
name: "Mindmap",
author: "Jaydip Dey",
version: "1",
},
format: "node_tree",
data: jsMindNodeTree,
};
_jm.show(mind);
}
function toggle_editable(btn) {
var editable = _jm.get_editable();
if (editable) {
_jm.disable_edit();
btn.innerHTML = "enable editable";
} else {
_jm.enable_edit();
btn.innerHTML = "disable editable";
}
}
function add_node() {
var selected_node = _jm.get_selected_node(); // as parent of new node
if (!selected_node) {
prompt_info("please select a node first.");
return;
}
var nodeid = jsMind.util.uuid.newid();
var topic = "New Node";
var node = _jm.add_node(selected_node, nodeid, topic);
}
function get_selected_nodeid() {
var selected_node = _jm.get_selected_node();
if (!!selected_node) {
return selected_node.id;
} else {
return null;
}
}
function remove_node() {
var selected_id = get_selected_nodeid();
if (!selected_id) {
prompt_info("please select a node first.");
return;
}
_jm.remove_node(selected_id);
}
function set_theme(theme_name) {
_jm.set_theme(theme_name);
}
function expand_all() {
_jm.expand_all();
}
function expand_n() {
_jm.expand_to_depth(parseInt(document.querySelector("#nlevel").value));
}
function collapse_all() {
_jm.collapse_all();
}
function convertFromJsMindNodeTree(jsMindNode) {
function traverse(node) {
//console.log(node);
const obj = {};
if (node.children) {
node.children.forEach((child) => {
const childKey = child.topic;
obj[childKey] = traverse(child);
});
} else {
// console.log()
obj.value = node.val;
}
return obj;
}
if (jsMindNode.id === "root") {
return traverse(jsMindNode);
} else {
throw new Error("Invalid root node for jsMind tree.");
}
}
function show_selected() {
var selected_node = _jm.get_selected_node();
console.log(selected_node);
if (!!selected_node && typeof selected_node.data.val == "string") {
alert(selected_node.data.val);
} else if (!!selected_node && typeof selected_node.data.val == "object") {
alert(JSON.stringify(selected_node.data.val));
}
}
function show_data() {
var mind_data = _jm.get_data();
var mind_string = jsMind.util.json.json2string(mind_data);
let data = JSON.parse(mind_string);
data = convertFromJsMindNodeTree(data.data);
const blob = new Blob([JSON.stringify(data)], { type: "application/json" });
const url = URL.createObjectURL(blob);
const link = document.createElement("a");
link.download = "mindmap_data.json";
link.href = url;
link.click();
URL.revokeObjectURL(url);
}
document.getElementById("zoom-in-button").addEventListener("click", zoomIn);
document.getElementById("zoom-out-button").addEventListener("click", zoomOut);
var zoomInButton = document.getElementById("zoom-in-button");
var zoomOutButton = document.getElementById("zoom-out-button");
let f = false;
function zoomIn() {
// console.log(document.querySelector("jmnode").innerText);
// if (!f) {
// console.log("IF");
// document.querySelector("jmnode").style.visibility = "hidden !important";
// return;
// }
if (_jm.view.zoomIn()) {
zoomOutButton.disabled = false;
} else {
zoomInButton.disabled = true;
}
}
function zoomOut() {
// if ((document.querySelector("jmnode").innerText = "jsMind Example")) {
// zoomOutButton.disabled = true;
// return;
// }
if (_jm.view.zoomOut()) {
zoomInButton.disabled = false;
} else {
zoomOutButton.disabled = true;
}
}
open_empty();