forked from swiftcarrot/js-tree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
140 lines (112 loc) · 3.44 KB
/
index.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
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
function Tree(obj) {
this.cnt = 1;
this.obj = obj || {children:[]};
this.indexes = {};
this.build(this.obj);
}
var proto = Tree.prototype;
proto.build = function(obj) {
var indexes = this.indexes;
var startId = this.cnt;
var self = this;
var index = {id: startId, node: obj};
indexes[this.cnt+''] = index;
this.cnt++;
if(obj.children && obj.children.length) walk(obj.children, index);
function walk(objs, parent) {
var children = [];
objs.forEach(function(obj, i) {
var index = {};
index.id = self.cnt;
index.node = obj;
if(parent) index.parent = parent.id;
indexes[self.cnt+''] = index;
children.push(self.cnt);
self.cnt++;
if(obj.children && obj.children.length) walk(obj.children, index);
});
parent.children = children;
children.forEach(function(id, i) {
var index = indexes[id+''];
if(i > 0) index.prev = children[i-1];
if(i < children.length-1) index.next = children[i+1];
});
}
return index;
};
proto.getIndex = function(id) {
var index = this.indexes[id+''];
if(index) return index;
};
proto.removeIndex = function(index) {
var self = this;
del(index);
function del(index) {
delete self.indexes[index.id+''];
if(index.children && index.children.length) {
index.children.forEach(function(child) {
del(self.getIndex(child));
});
}
}
};
proto.get = function(id) {
var index = this.getIndex(id);
if(index && index.node) return index.node;
return null;
};
proto.remove = function(id) {
var index = this.getIndex(id);
var node = this.get(id);
var parentIndex = this.getIndex(index.parent);
var parentNode = this.get(index.parent);
parentNode.children.splice(parentNode.children.indexOf(node), 1);
parentIndex.children.splice(parentIndex.children.indexOf(id), 1);
this.removeIndex(index);
this.updateChildren(parentIndex.children);
return node;
};
proto.updateChildren = function(children) {
children.forEach(function(id, i) {
var index = this.getIndex(id);
index.prev = index.next = null;
if(i > 0) index.prev = children[i-1];
if(i < children.length-1) index.next = children[i+1];
}.bind(this));
};
proto.insert = function(obj, parentId, i) {
var parentIndex = this.getIndex(parentId);
var parentNode = this.get(parentId);
var index = this.build(obj);
index.parent = parentId;
parentNode.children = parentNode.children || [];
parentIndex.children = parentIndex.children || [];
parentNode.children.splice(i, 0, obj);
parentIndex.children.splice(i, 0, index.id);
this.updateChildren(parentIndex.children);
if(parentIndex.parent) {
this.updateChildren(this.getIndex(parentIndex.parent).children);
}
return index;
};
proto.insertBefore = function(obj, destId) {
var destIndex = this.getIndex(destId);
var parentId = destIndex.parent;
var i = this.getIndex(parentId).children.indexOf(destId);
return this.insert(obj, parentId, i);
};
proto.insertAfter = function(obj, destId) {
var destIndex = this.getIndex(destId);
var parentId = destIndex.parent;
var i = this.getIndex(parentId).children.indexOf(destId);
return this.insert(obj, parentId, i+1);
};
proto.prepend = function(obj, destId) {
return this.insert(obj, destId, 0);
};
proto.append = function(obj, destId) {
var destIndex = this.getIndex(destId);
destIndex.children = destIndex.children || [];
return this.insert(obj, destId, destIndex.children.length);
};
module.exports = Tree;