forked from arvindr21/jsTree-directive
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjsTree.directive.js
More file actions
179 lines (159 loc) · 5.15 KB
/
jsTree.directive.js
File metadata and controls
179 lines (159 loc) · 5.15 KB
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
/*
* jstree.directive [http://www.jstree.com]
* http://arvindr21.github.io/jsTree-Angular-Directive
*
* Copyright (c) 2014 Arvind Ravulavaru
* Licensed under the MIT license.
*/
var ngJSTree = angular.module('jsTree.directive', []);
ngJSTree.directive('jsTree', ['$http','$parse', function($http,$parse) {
var treeDir = {
restrict: 'EA',
scope: true,
fetchResource: function(url, cb) {
return $http.get(url).then(function(data) {
if (cb) cb(data.data);
});
},
managePlugins: function(s, e, a, config) {
if (a.treePlugins) {
config.plugins = a.treePlugins.split(',');
config.core = config.core || {};
config.core.check_callback = config.core.check_callback || true;
if (config.plugins.indexOf('state') >= 0) {
config.state = config.state || {};
config.state.key = a.treeStateKey;
}
if (config.plugins.indexOf('search') >= 0) {
var to = false;
if (e.next().attr('class') !== 'ng-tree-search') {
e.after('<input type="text" placeholder="Search Tree" class="ng-tree-search"/>')
.next()
.on('keyup', function(ev) {
if (to) {
clearTimeout(to);
}
to = setTimeout(function() {
treeDir.tree.jstree(true).search(ev.target.value);
}, 250);
});
}
}
if (config.plugins.indexOf('checkbox') >= 0) {
config.checkbox = config.checkbox || {};
config.checkbox.keep_selected_style = false;
}
if (config.plugins.indexOf('contextmenu') >= 0) {
if (a.treeContextmenu) {
config.contextmenu = config.contextmenu || {};
if (a.treeContextmenuaction != undefined) {
config.contextmenu.items = function(e) {
return s.$eval(a.treeContextmenuaction)(e);
}
} else {
config.contextmenu.items = function() {
return s[a.treeContextmenu];
}
}
}
}
if (config.plugins.indexOf('types') >= 0) {
if (a.treeTypes) {
config.types = s[a.treeTypes];
console.log(config);
}
}
if (config.plugins.indexOf('dnd') >= 0) {
if (a.treeDnd) {
config.dnd = s[a.treeDnd];
console.log(config);
}
}
}
return config;
},
manageEvents: function(s, e, a) {
if (a.treeEvents) {
var evMap = a.treeEvents.split(';');
for (var i = 0; i < evMap.length; i++) {
if (evMap[i].length > 0) {
// plugins could have events with suffixes other than '.jstree'
var evt = evMap[i].split(':')[0];
if (evt.indexOf('.') < 0) {
evt = evt + '.jstree';
}
var cb = evMap[i].split(':')[1];
treeDir.tree.on(evt, s[cb]);
}
}
}
},
link: function(s, e, a) { // scope, element, attribute \O/
$(function() {
var config = {};
// users can define 'core'
config.core = {};
if (a.treeCore) {
config.core = $.extend(config.core, s[a.treeCore]);
}
//Add model by ng-model or tree-model
var model = null;
if(a.ngModel == undefined){
model = $parse(a.treeModel);
}else{
model = $parse(a.ngModel);
}
//get value model
var valueModel = model(s);
// clean Case
a.treeData = a.treeData ? a.treeData.toLowerCase() : '';
a.treeSrc = a.treeSrc ? a.treeSrc.toLowerCase() : '';
if (a.treeData == 'html') {
treeDir.fetchResource(a.treeSrc, function(data) {
e.html(data);
treeDir.init(s, e, a, config);
});
} else if (a.treeData == 'json') {
treeDir.fetchResource(a.treeSrc, function(data) {
config.core.data = data;
treeDir.init(s, e, a, config);
});
} else if (a.treeData == 'scope') {
//if valueModel not is undefined
if(valueModel){
config.core.data = valueModel;
treeDir.init(s, e, a, config);
}
//if value change execute rebuild tree
s.$watch(valueModel, function(n, o) {
if (n) {
config.core.data = valueModel;
$(e).jstree('destroy');
treeDir.init(s, e, a, config);
}
}, true);
// Trigger it initally
// Fix issue #13
config.core.data = s[a.treeModel];
treeDir.init(s, e, a, config);
} else if (a.treeAjax) {
config.core.data = {
'url': a.treeAjax,
'data': function(node) {
return {
'id': node.id != '#' ? node.id : 1
};
}
};
treeDir.init(s, e, a, config);
}
});
},
init: function(s, e, a, config) {
treeDir.managePlugins(s, e, a, config);
this.tree = $(e).jstree(config);
treeDir.manageEvents(s, e, a);
}
};
return treeDir;
}]);