-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree.js
121 lines (120 loc) · 3.12 KB
/
tree.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
;
'use strict';
function Tree(arr) {
this.tree = arr;
var roots = this.findRoots();
for (var i = 0; i < roots.length; i++) {
var index = _.findIndex(this.tree, function(el){return el.id===roots[i]});
var el = this.tree[index];
if ( el.childs ) {
this.replaceChilds(el);
}
};
}
Tree.prototype.replaceChilds = function(el) {
var childs = el.childs;
for (var j = 0; j < childs.length; j++) {
var child = childs[j];
var id = child.id;
var index = _.findIndex(this.tree, function(el){
return el.id === id;
});
if (index!==-1) childs[j] = this.tree.splice(index, 1)[0];
if (childs[j].childs) this.replaceChilds(childs[j], this.tree);
}
};
Tree.prototype.findRoots = function() {
var childIds = [];
var parentIds = [];
for (var i = 0; i < this.tree.length; i++) {
var el = this.tree[i];
if (el.hasOwnProperty('childs') && el.childs.length > 0) {
var childs = el.childs;
for (var j = 0; j < childs.length; j++) {
childIds.push(childs[j].pivot.child_id);
parentIds.push(childs[j].pivot.parent_id);
}
}
}
childIds = _.uniq(childIds);
parentIds = _.uniq(parentIds);
var differenceIds = _.difference(parentIds, childIds);
return differenceIds;
}
Tree.prototype.writeNode = function(el, node){
var crEl = document.createElement.bind(document);
// debugger;
var ul = crEl('ul');
var li = crEl('li');
li.innerHTML = el.name;
li.setAttribute('data-cats', el.id);
ul.appendChild(li);
if (node) {
node.appendChild(ul);
}
if (el.childs && el.childs.length>0) {
for (var i = 0; i < el.childs.length; i++) {
this.writeNode(el.childs[i], li);
}
}
return li;
}
Tree.prototype.domTree = function(arr) {
var crEl = document.createElement.bind(document);
var div = crEl('div');
if (!arr) {
var nodes = [];
for (var i = 0; i < this.tree.length; i++) {
var root = this.tree[i];
nodes.push(this.writeNode(root));
}
var uls = []
for (var i = 0; i < nodes.length; i++) {
var ul = crEl('ul');
ul.appendChild(nodes[i]);
uls.push(ul);
}
for (var i = 0; i < uls.length; i++) {
div.appendChild(uls[i])
}
} else {
ul = crEl('ul');
for (var i = 0; i < arr.length; i++) {
var li = crEl('li');
li.innerHTML = this.find(arr[i]).name;
li.setAttribute('data-cats', JSON.stringify(this.getAllChilds(arr[i])));
ul.appendChild(li);
}
div.appendChild(ul);
}
return div;
}
Tree.prototype.find = function (id) {
var obj;
function get(el){
if (el.id == id) {
obj = el;
return;
}
for (var i = 0; i < el.childs.length; i++) {
get(el.childs[i]);
}
}
var tree = this.tree;
for (var i = 0; i < tree.length; i++) {
get(tree[i])
}
return obj;
};
Tree.prototype.getAllChilds = function (id) {
var ids = [];
function get(el){
ids.push(el.id)
for (var i = 0; i < el.childs.length; i++) {
get(el.childs[i]);
}
}
var node = this.find(id);
get(node);
return ids;
};