forked from olddigger/digger-html-for-angular
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
236 lines (213 loc) · 6.91 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
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
/*
we are in private scope (component.io)
*/
var template = require('./dist/abn_tree_template');
angular
.module('digger.html', [
])
.directive('diggerHtml', function($safeApply){
//field.required && showvalidate && containerForm[field.name].$invalid
return {
restrict:'EA',
scope:{
container:'='
},
replace:true,
template:'<div><digger-htmltree tree-data="treedata" on-select="container_select(branch)" expand-level="depth"></digger-htmltree></div>',
controller:function($scope){
$scope.depth = $scope.depth || 4;
$scope.treedata = [];
$scope.$watch('container', function(container){
if(!container){
return;
}
container.recurse(function(c){
c.data('showattributes', true);
})
$scope.treedata = container.models;
})
$scope.container_select = function(model){
//$scope.$emit('tree:selected', $scope.container.spawn(model));
}
}
}
})
.directive('diggerHtmltree', function($timeout) {
return {
restrict: 'E',
template: template,
scope: {
treeData: '=',
onSelect: '&'
},
link: function(scope, element, attrs) {
var expand_level, for_each_branch, on_treeData_change, select_branch, selected_branch;
if (attrs.iconExpand == null) {
attrs.iconExpand = 'icon-plus';
}
if (attrs.iconCollapse == null) {
attrs.iconCollapse = 'icon-minus';
}
if (attrs.iconLeaf == null) {
attrs.iconLeaf = '';//icon-chevron-right';
}
if (attrs.expandLevel == null) {
attrs.expandLevel = '3';
}
expand_level = parseInt(attrs.expandLevel, 10);
scope.header = attrs.header;
if (!scope.treeData) {
alert('no treeData defined for the tree!');
}
if (scope.treeData.length == null) {
if (treeData._digger != null) {
scope.treeData = [treeData];
} else {
alert('treeData should be an array of root branches');
}
}
for_each_branch = function(f) {
var do_f, root_branch, _i, _len, _ref, _results;
do_f = function(branch, level) {
var child, _i, _len, _ref, _results;
f(branch, level);
if (branch._children != null) {
_ref = branch._children;
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
child = _ref[_i];
_results.push(do_f(child, level + 1));
}
return _results;
}
};
_ref = scope.treeData;
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
root_branch = _ref[_i];
_results.push(do_f(root_branch, 1));
}
return _results;
};
for_each_branch(function(b, level) {
b.level = level;
return b._data.expanded = b.level < expand_level;
});
scope.selectedid = null;
select_branch = function(branch) {
scope.selectedid = branch._digger.diggerid;
if (branch.onSelect != null) {
return $timeout(function() {
return branch.onSelect(branch);
});
} else {
if (scope.onSelect != null) {
return $timeout(function() {
return scope.onSelect({
branch: branch
});
});
}
}
};
scope.$on('tree:reset', function(ev){
scope.selectedid = null;
})
scope.$on('tree:setselected', function(ev, selected){
scope.selectedid = selected._digger.diggerid;
})
scope.user_clicks_branch = function(branch) {
if (branch !== selected_branch){
return select_branch(branch);
}
};
scope.togglebranch = function(branch, value){
branch._data.expanded = arguments.length>1 ? value : !branch._data.expanded;
scope.$emit('tree:toggle', branch);
}
scope.tree_rows = [];
on_treeData_change = function() {
var add_branch_to_list, root_branch, _i, _len, _ref, _results;
scope.tree_rows = [];
for_each_branch(function(branch) {
if (branch._children) {
if (branch._children.length > 0) {
return branch._children = branch._children.map(function(e) {
if (typeof e === 'string') {
return {
name: e,
children: []
};
} else {
return e;
}
});
}
} else {
return branch._children = [];
}
});
add_branch_to_list = function(level, branch, visible) {
var child, child_visible, tree_icon, _i, _len, _ref, _results;
if(!branch._data){
branch._data = {};
}
if (branch._data.expanded == null) {
branch._data.expanded = false;
}
if (!branch._children || branch._children.length === 0) {
tree_icon = attrs.iconLeaf;
} else {
if (branch._data.expanded) {
tree_icon = attrs.iconCollapse;
} else {
tree_icon = attrs.iconExpand;
}
}
var digger = branch._digger || {};
var classnames = digger.class || [];
var classst = classnames.join(' ');
branch._data._classst = classst;
var cattrs = branch;
var display_attrs = [];
for(var prop in cattrs){
if(prop!='label' && prop.indexOf('_')!=0 && typeof(cattrs[prop])!='object'){
display_attrs.push({
value:cattrs[prop],
title:prop
})
}
}
branch._data._display_attrs = display_attrs;
scope.tree_rows.push({
level: level,
branch: branch,
tree_icon: tree_icon,
visible: visible
});
if (branch._children != null) {
_ref = branch._children;
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
child = _ref[_i];
child_visible = visible && branch._data.expanded;
_results.push(add_branch_to_list(level + 1, child, child_visible));
}
return _results;
}
};
_ref = scope.treeData;
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
root_branch = _ref[_i];
_results.push(add_branch_to_list(1, root_branch, true));
}
if(!scope.selectedid && scope.treeData[0] && scope.treeData[0]._digger){
scope.selectedid = scope.treeData[0]._digger.diggerid;
}
return _results;
};
return scope.$watch('treeData', on_treeData_change, true);
}
};
});