This repository has been archived by the owner on Apr 23, 2019. It is now read-only.
forked from nelsonblaha/draggable-tree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraggableTree.js
272 lines (214 loc) · 9.56 KB
/
draggableTree.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
var app = angular.module('app', []);
app.directive('yaTree', function () {
return {
restrict: 'A',
transclude: 'element',
priority: 1000,
terminal: true,
compile: function (tElement, tAttrs, transclude) {
var repeatExpr, childExpr, rootExpr, childrenExpr;
repeatExpr = tAttrs.yaTree.match(/^(.*) in ((?:.*\.)?(.*)) at (.*)$/);
childExpr = repeatExpr[1];
rootExpr = repeatExpr[2];
childrenExpr = repeatExpr[3];
branchExpr = repeatExpr[4];
return function link(scope, element, attrs) {
var rootElement = element[0].parentNode,
cache = [];
// Reverse lookup object to avoid re-rendering elements
function lookup(child) {
var i = cache.length;
while (i--) {
if (cache[i].scope[childExpr] === child) {
return cache.splice(i, 1)[0];
}
}
}
scope.$watch(rootExpr, function (root) {
var currentCache = [];
// Recurse the data structure
(function walk(children, parentNode, parentScope, depth) {
var i = 0,
n = children.length,
last = n - 1,
cursor,
child,
cached,
childScope,
grandchildren;
// Iterate the children at the current level
for (; i < n; ++i) {
// We will compare the cached element to the element in
// at the destination index. If it does not match, then
// the cached element is being moved into this position.
cursor = parentNode.childNodes[i];
child = children[i];
// See if this child has been previously rendered
// using a reverse lookup by object reference
cached = lookup(child);
// If the parentScope no longer matches, we've moved.
// We'll have to transclude again so that scopes
// and controllers are properly inherited
if (cached && cached.parentScope !== parentScope) {
cache.push(cached);
cached = null;
}
// If it has not, render a new element and prepare its scope
// We also cache a reference to its branch node which will
// be used as the parentNode in the next level of recursion
if (!cached) {
transclude(parentScope.$new(), function (clone, childScope) {
childScope[childExpr] = child;
cached = {
scope: childScope,
parentScope: parentScope,
element: clone[0],
branch: clone.find(branchExpr)[0]
};
// This had to happen during transclusion so inherited
// controllers, among other things, work properly
if (!cursor) parentNode.appendChild(cached.element);
else parentNode.insertBefore(cached.element, cursor);
});
} else if (cached.element !== cursor) {
if (!cursor) parentNode.appendChild(cached.element);
else parentNode.insertBefore(cached.element, cursor);
}
// Lets's set some scope values
childScope = cached.scope;
// Store the current depth on the scope in case you want
// to use it (for good or evil, no judgment).
childScope.$depth = depth;
// Emulate some ng-repeat values
childScope.$index = i;
childScope.$first = (i === 0);
childScope.$last = (i === last);
childScope.$middle = !(childScope.$first || childScope.$last);
// Push the object onto the new cache which will replace
// the old cache at the end of the walk.
currentCache.push(cached);
// If the child has children of its own, recurse 'em.
grandchildren = child[childrenExpr];
if (grandchildren && grandchildren.length) {
walk(grandchildren, cached.branch, childScope, depth + 1);
}
}
})(root, rootElement, scope, 0);
// Cleanup objects which have been removed.
// Remove DOM elements and destroy scopes to prevent memory leaks.
i = cache.length;
while (i--) {
cached = cache[i];
if (cached.scope) {
cached.scope.$destroy();
}
if (cached.element) {
cached.element.parentNode.removeChild(cached.element);
}
}
// Replace previous cache.
cache = currentCache;
}, true);
};
}
};
});
app.controller('TreeController', function ($scope, $timeout) {
$scope.data = {
children: [{
title: 'hello, world',
children: []
}]
};
$scope.toggleMinimized = function (child) {
child.minimized = !child.minimized;
};
$scope.addChild = function (child) {
child.children.push({
title: '',
children: []
});
};
$scope.remove = function (child) {
function walk(target) {
var children = target.children,
i;
if (children) {
i = children.length;
while (i--) {
if (children[i] === child) {
return children.splice(i, 1);
} else {
walk(children[i])
}
}
}
}
walk($scope.data);
}
$scope.update = function (event, ui) {
var root = event.target,
item = ui.item,
parent = item.parent(),
target = (parent[0] === root) ? $scope.data : parent.scope().child,
child = item.scope().child,
index = item.index();
target.children || (target.children = []);
function walk(target, child) {
var children = target.children,
i;
if (children) {
i = children.length;
while (i--) {
if (children[i] === child) {
return children.splice(i, 1);
} else {
walk(children[i], child);
}
}
}
}
walk($scope.data, child);
target.children.splice(index, 0, child);
};
});
app.directive('uiNestedSortable', ['$parse', function ($parse) {
'use strict';
var eventTypes = 'Create Start Sort Change BeforeStop Stop Update Receive Remove Over Out Activate Deactivate'.split(' ');
return {
restrict: 'A',
link: function (scope, element, attrs) {
var options = attrs.uiNestedSortable ? $parse(attrs.uiNestedSortable)() : {};
angular.forEach(eventTypes, function (eventType) {
var attr = attrs['uiNestedSortable' + eventType],
callback;
if (attr) {
callback = $parse(attr);
options[eventType.charAt(0).toLowerCase() + eventType.substr(1)] = function (event, ui) {
scope.$apply(function () {
callback(scope, {
$event: event,
$ui: ui
});
});
};
}
});
//note the item="{{child}}" attribute on line 17
options.isAllowed = function(item, parent) {
if (!parent) {
parent = scope.data;
} else {
var attrs = parent.context.attributes;
parent = attrs.getNamedItem('item');
}
attrs = item.context.attributes;
item = attrs.getNamedItem('item');
console.log(item, parent);
//if ( ... ) return false;
return true;
};
element.nestedSortable(options);
}
};
}]);