forked from arvindr21/jsTree-directive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsTree.directive.js
209 lines (193 loc) · 6.8 KB
/
jsTree.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
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
/*
* jstree.directive [http://www.jstree.com]
* http://arvindr21.github.io/jsTree-Angular-Directive
*
* Copyright (c) 2014 Arvind Ravulavaru
* Licensed under the MIT license.
*/
/* global $ */
/* global angular */
var ngJSTree = angular.module('jsTree.directive', []);
ngJSTree.directive('jsTree', ['$http', function($http) {
var treeDir = {
restrict: 'EA',
fetchResource: function(url, cb) {
return $http.get(url).then(function(data) {
if (cb) cb(data.data);
});
},
// Added functionnality for instance: GitHUb #22
manageInstance: function (s, e, a, config) {
if (config.treeInstance) {
// Using the first way to invoke method on instance
s[config.treeInstance] = this.tree.jstree(true);
}
},
// Added functionnality for tree
manageId: function (s, e, a, config) {
if (config.treeId) {
// Using the first way to invoke method on instance
s[config.treeId] = this.tree;
}
},
managePlugins: function(s, e, a, config) {
if (config.plugins || config.treePlugins) {
// Allow definitions of plugins through array or comma-separated list
config.plugins = config.plugins || [];
if(config.treePlugins){
config.plugins = config.plugins.concat(config.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 = config.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) {
// Context menu is defined with the contextmenu variable
// treeContextMenu is a string referencing a scope variable, can override the former
if (config.treeContextmenu) {
config.contextmenu = {};
if (config.treeContextmenuaction != undefined) {
config.contextmenu.items = function(e) {
return s.$eval(config.treeContextmenuaction)(e);
};
} else {
config.contextmenu.items = function() {
return s[config.treeContextmenu];
};
}
}
}
if (config.plugins.indexOf('types') >= 0) {
if (config.treeTypes) {
config.types = s[config.treeTypes];
}
}
if (config.plugins.indexOf('dnd') >= 0) {
if (config.treeDnd) {
config.dnd = s[config.treeDnd];
}
}
}
return config;
},
manageEvents: function(s, e, a, config) {
// Allow definitions of events through array or comma-separated list
if (config.events || config.treeEvents) {
var evMap = config.events || [];
if(config.treeEvents){
evMap = evMap.concat(config.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]);
}
}
}
},
// Initialisation function
link: function(s, e, a) { // scope, element, attribute \O/
$(function() {
var config = {};
// Load a config from the scope
if (a.treeConfig && s[a.treeConfig] !== undefined) {
config = s[a.treeConfig];
}
delete a.treeConfig;
delete a.$attr.treeConfig;
if(!config.core){
config.core = {};
}
// Override with inline statements
if(Object.keys(a.$attr).length > 0){
// Only read inline attributes
for(var _prop in a.$attr){
config[_prop] = a[_prop];
}
}
// // Special treatment to only load core from scope
// if (a.treeCore) {
// config.core = $.extend(config.core, s[a.treeCore]);
// }
// clean Case
config.treeData = config.treeData ? config.treeData.toLowerCase() : '';
config.treeSrc = config.treeSrc ? config.treeSrc.toLowerCase() : '';
// Specify data directly inside scope
if(config.core.data){
treeDir.init(s, e, a, config);
}else{
// HTML data
if (config.treeData == 'html') {
treeDir.fetchResource(config.treeSrc, function(data) {
e.html(data);
treeDir.init(s, e, a, config);
});
} else if (config.treeData == 'json') {
treeDir.fetchResource(config.treeSrc, function(data) {
config.core.data = data;
treeDir.init(s, e, a, config);
});
} else if (config.treeData == 'scope') {
s.$watch(config.treeModel, function(n, o) {
if (n) {
config.core.data = s[config.treeModel];
$(e).jstree('destroy');
treeDir.init(s, e, a, config);
}
}, true);
// Trigger it initally
// Fix issue #13
config.core.data = s[config.treeModel];
treeDir.init(s, e, a, config);
} else if (config.treeAjax) {
config.core.data = {
'url': config.treeAjax,
'data': function(node) {
return {
'id': node.id != '#' ? node.id : 1
};
}
};
treeDir.init(s, e, a, config);
}
}
});
},
init: function(s, e, a, config) { // scope, element, attribute
// Load from scope config or inline
treeDir.managePlugins(s, e, a, config);
this.tree = $(e).jstree(config);
treeDir.manageEvents(s, e, a, config);
// Github #22
treeDir.manageInstance(s, e, a, config);
treeDir.manageId(s, e, a, config);
}
};
return treeDir;
}]);