-
Notifications
You must be signed in to change notification settings - Fork 3
/
tree-view-directive.js
94 lines (78 loc) · 3.95 KB
/
tree-view-directive.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
angular.module('ui.bootstrap.treeview').directive('treeView', ['$compile', '$templateCache', function ($compile, $templateCache) {
return {
restrict: 'E',
scope: {},
bindToController: {
treeService: '='
},
controller: function () {
var that = this;
this.isCollapsed = function(node) {
return that.treeService.collapsed.indexOf(node) > -1;
};
// toggle when icon clicked
this.toggleNode = function (node) {
if (!node.children) return;
// collapse / expand
if (node.children && node.children.length > 0) {
// add the node to our collapsed array
var index = that.treeService.collapsed.indexOf(node);
if (index == -1)
that.treeService.collapsed.push(node);
else
that.treeService.collapsed.splice(index, 1);
}
};
// select when name clicked
this.selectNode = function (e, node) {
that.treeService.selectedNode = node;
e.stopPropagation();
e.preventDefault();
};
},
controllerAs: 'ctrl',
link: function (scope, element, attrs, ctrl) {
var isRoot = (!attrs.treeRoot ? true : false);
var nodeLabel = attrs.nodeLabel || 'label';
var itemClass = attrs.itemClass || '';
var itemInclude = attrs.itemNgInclude || '';
var itemIncludeHtml = '';
var emptyTreeMessage = attrs.emptyTreeMessage || 'No items to display';
var emptyTreeClass = attrs.emptyTreeClass || 'alert alert-info';
if (itemInclude && itemInclude.length > 0) {
itemIncludeHtml = $templateCache.get(attrs.itemNgInclude);
}
// remove attributes
element.removeAttr('node-label');
element.removeAttr('item-class');
element.removeAttr('item-ng-include');
element.removeAttr('tree-root');
// template
var template = '';
if (!isRoot)
template = '<ul>';
else
template = '<ul ng-if="ctrl.treeService.nodes.length">';
template += '<li ng-repeat="node in [REPLACENODES]">' +
'<div ng-click="ctrl.selectNode($event, node)" class="node" ng-class="{\'selected\' : node == ctrl.treeService.selectedNode}">' +
'<div ' + (itemClass != '' ? ' class="' + itemClass + '"' : '') + '>' +
'<i ng-click="ctrl.toggleNode(node)" ng-show="node.children && node.children.length > 0" ng-class="!ctrl.isCollapsed(node) ? \'has-child\' : \'has-child-open\'"></i>' +
'<i ng-click="ctrl.toggleNode(node)" class="no-child" ng-show="!node.children || node.children.length == 0"></i>' +
'<span ng-bind="node.' + nodeLabel + '" ng-class="{\'selected\' : node == ctrl.treeService.selectedNode}"></span>' +
'</div>' +
itemIncludeHtml +
'</div>' +
'<tree-view uib-collapse="!ctrl.isCollapsed(node)" tree-service="ctrl.treeService" node-children="node.children" tree-root="false" node-label="' + nodeLabel + '" item-ng-include="' + itemInclude + '" item-class="' + itemClass + '"></tree-view>' +
'</li>' +
'</ul>';
if (!isRoot) {
template = template.replace('[REPLACENODES]', '$parent.node.children');
} else {
template = template.replace('[REPLACENODES]', 'ctrl.treeService.nodes');
template += '<div class="' + emptyTreeClass + '" ng-show="ctrl.treeService.nodes.length == 0">' + emptyTreeMessage + '</div>';
}
var compiledHtml = $compile(template)(scope);
element.append(compiledHtml);
}
};
}]);