forked from martinsbalodis/jquery-treetable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery-treetable.js
146 lines (118 loc) · 3.18 KB
/
jquery-treetable.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
141
142
143
144
145
jQuery.fn.extend({
treetable: function() {
var $table = $(this);
$table.addClass("tt-table");
var $items = $table.find("div.tt");
var index = {};
var items = [];
// add items to index
$items.each(function (i, el) {
var $el = $(el);
var id = $el.data('tt-id');
var parent = $el.data('tt-parent');
if(parent === '') {
parent = undefined;
}
var item = {
id: id,
parent: parent,
children: [],
el: $el,
left: 0,
width: $el.width() + 12
};
index[id] = item;
items.push(item);
});
// make a graph from parent relations
items.forEach(function (item) {
if (item.parent !== undefined) {
item.parent = index[item.parent];
item.parent.children.push(item);
}
});
// pad items
items.forEach(function (item) {
item.left = 0;
if (item.parent !== undefined) {
item.left = item.parent.left + item.parent.width;
}
});
// position items
items.forEach(function (item) {
//console.log(el.left);
item.el.css("left", item.left);
});
// wrap contents
items.forEach(function (item) {
item.el.html('<div class="content">' + item.el.html() + '</div>');
});
// add parent classes
items.forEach(function (item) {
if (item.children.length > 0) {
item.el.addClass("tt-parent");
item.showChildren = true;
}
});
function drawLines()
{
// draw lines
items.forEach(function (item) {
if (item.parent === undefined) {
return;
}
var childPos = item.el.position();
var parent = item.parent;
var parentPos = parent.el.position();
var height = childPos.top - parentPos.top;
var width = item.left - parent.left;
var left = parent.left - item.left + (parent.width / 2);
item.el.children('div.tail').first().remove();
var $tail = $('<div class="tail"></div>').css({
height: height,
width: width,
left: left
});
item.el.prepend($tail);
});
}
drawLines();
// handle click event
$table.on("click", "div.tt div.content", function (e) {
var $el = $(e.currentTarget).closest(".tt");
var $tr = $el.closest("tr");
var id = $el.data('tt-id');
var item = index[id];
if (item.showChildren === true) {
// hide all children
item.showChildren = false;
function hide(parentId) {
var item = index[parentId];
item.children.forEach(function (child) {
if (child.showChildren !== undefined) {
child.showChildren = false;
}
$(child.el).closest("tr").addClass("tt-hide");
hide(child.id);
});
}
hide(id);
drawLines();
}
else {
// show direct children
item.showChildren = true;
item.children.forEach(function (child) {
$(child.el).closest("tr").removeClass("tt-hide");
});
drawLines();
}
});
// initially hide all children
items.forEach(function (item) {
if (item.parent === undefined && item.children.length > 0) {
item.el.find(".content").click();
}
});
}
});