forked from jaredly/treed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.js
73 lines (65 loc) · 1.24 KB
/
setup.js
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
function extend(a, b) {
for (var c in b) {
a[c] = b[c]
}
return a
}
function make_listed(data, nextid) {
var ids = {}
, children = []
, res
if (!nextid) nextid = 0
if (data.children) {
for (var i=0; i<data.children.length; i++) {
res = make_listed(data.children[i], nextid)
for (var id in res.tree) {
ids[id] = res.tree[id]
}
children.push(res.id)
nextid = res.id + 1
}
delete data.children
}
data.done = false
ids[nextid] = {
id: nextid,
data: data,
children: children
}
for (var i=0; i<children.length; i++) {
ids[children[i]].parent = nextid;
}
return {id: nextid, tree: ids}
}
var data = {
name: 'awesome',
children: [{
name: 'food'
}, {
name: 'people',
children: [{
name: 'my mother',
}, {
name: 'your mother'
}]
}, {
name: 'places',
children: [{
name: 'bostom',
}, {
name: 'germany',
children: [{
name: 'Frankfurt'
}, {
name: 'Frankfurt temple'
}, {
name: 'gladbach'
}]
}, {
name: 'italy'
}]
}]
}
var conved = make_listed(data)
, main = document.getElementById('main')
var listed = new Listed(conved.id, conved.tree, main)