-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree.js
268 lines (236 loc) · 12.5 KB
/
tree.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
var async = require('async');
var Q = require('q');
module.exports = function timestamp(Model) {
'use strict';
Model.defineProperty('lft', {type: Number, required: true});
Model.defineProperty('rgt', {type: Number, required: true});
Model.belongsTo(Model, {foreignKey: 'parentId', as: 'parent'});
Model.observe('before delete', function (ctx, next) {
if (ctx.where.__treeDoNothing) {
delete ctx.where.__treeDoNothing;
return next();
}
ctx.Model.find({where: ctx.where}, function (err, results) {
var parallel = [];
results.forEach(function (result) {
var myRgt = result.rgt;
var myWidth = result.rgt - result.lft + 1;
parallel.push(function (cb) {
ctx.Model.destroyAll({
lft: {gt: result.lft - 1},
rgt: {lt: result.rgt + 1},
__treeDoNothing: true
}, function () {
//close hole left behind
updateMultiples(ctx.Model, {where: {lft: {gt: myRgt}}}, function (result) {
result.lft -= myWidth;
}).then(function () {
//Close hole left behind
return updateMultiples(ctx.Model, {where: {rgt: {gt: myRgt}}}, function (result) {
result.rgt -= myWidth;
});
}).catch(function(err){
console.log(err);
}).done(function(){
cb();
})
});
});
});
async.parallel(parallel, function (err) {
if(err) console.log(err);
next();
});
});
});
Model.observe('before save', function event(ctx, next) {
//TODO: We need check if the lft and rgt properties was changed. We should not allow that
//sometimes this function will save others instances and will
//add the __treeDoNothing option. If so, we must do nothing
if (ctx.options.__treeDoNothing) return next();
if (ctx.instance) { //update
if (ctx.instance.id) {
ctx.Model.findById(ctx.instance.id, function (err, oldInstance) {
//check if the parent changes
if (oldInstance.parentId !== ctx.instance.parentId) {
//we have a parent. We need to move this instance to the right place
if (ctx.instance.parentId) {
//get the parent reference
ctx.Model.findById(ctx.instance.parentId, function (err, parent) {
var myLft = ctx.instance.lft;
var myRgt = ctx.instance.rgt;
var myWidth = myRgt - myLft + 1;
var parentRgt = parent.rgt;
var newLft, moveRgt;
//Temporary remove subtree being moved.
updateMultiples(ctx.Model, {where: {and: [{rgt: {lt: myRgt + 1}}, {lft: {gt: myLft - 1}}]}}, function (result) {
result.lft = -result.lft;
result.rgt = -result.rgt;
}).then(function () {
//Close hole left behind
return updateMultiples(ctx.Model, {where: {lft: {gt: myRgt}}}, function (result) {
result.lft -= myWidth;
});
}).then(function () {
//Close hole left behind
return updateMultiples(ctx.Model, {where: {rgt: {gt: myRgt}}}, function (result) {
result.rgt -= myWidth;
});
}).then(function () {
//Make a hole for the new items
newLft = (parentRgt > myRgt) ? parentRgt - myWidth : parentRgt;
return updateMultiples(ctx.Model, {where: {lft: {gt: newLft - 1}}}, function (result) {
result.lft += myWidth;
});
}).then(function () {
//Make a hole for the new items
return updateMultiples(ctx.Model, {where: {rgt: {gt: newLft - 1}}}, function (result) {
result.rgt += myWidth;
});
}).then(function () {
// Move node and subnodes
moveRgt = (parentRgt > myRgt) ? parentRgt - myRgt - 1 : parentRgt - myRgt - 1 + myWidth;
return updateMultiples(ctx.Model, {
where: {
rgt: {gt: -myRgt - 1},
lft: {lt: -myLft + 1}
}
}, function (result) {
result.lft = -result.lft + moveRgt;
result.rgt = -result.rgt + moveRgt;
if (ctx.instance.id == result.id) {
ctx.instance.lft = result.lft;
ctx.instance.rgt = result.rgt;
}
});
}).catch(function (err) {
console.error('there is a error: ', err);
}).done(function () {
next();
});
});
} else { //we are setting parentId to null.
//find the last node of the tree
ctx.Model.find({
order: 'rgt DESC',
limit: 1
}, function (err, results) {
var newLft = results[0].rgt + 1;
var myLft = ctx.instance.lft;
var myRgt = ctx.instance.rgt;
var myWidth = myRgt - myLft + 1;
var moveRgt = newLft - myLft - myWidth;
updateMultiples(ctx.Model, {where: {and: [{rgt: {lt: myRgt + 1}}, {lft: {gt: myLft - 1}}]}}, function (result) {
result.lft = -result.lft;
result.rgt = -result.rgt;
}).then(function () {
return updateMultiples(ctx.Model, {where: {lft: {gt: myRgt}}}, function (result) {
result.lft -= myWidth;
});
}).then(function () {
return updateMultiples(ctx.Model, {where: {rgt: {gt: myRgt}}}, function (result) {
result.rgt -= myWidth;
});
}).then(function () {
return updateMultiples(ctx.Model, {
where: {
rgt: {gt: -myRgt - 1},
lft: {lt: -myLft + 1}
}
}, function (result) {
result.lft = -result.lft + moveRgt;
result.rgt = -result.rgt + moveRgt;
if (ctx.instance.id == result.id) {
ctx.instance.lft = result.lft;
ctx.instance.rgt = result.rgt;
}
});
}).catch(function (err) {
console.error('there is a error: ', err);
}).done(function () {
next();
});
});
}
} else { //there is no change in parentId, so we don't need do nothing
next();
}
});
} else { //this is a new item
//if it has parent, we need to do a hole in the tree to insert it there.
if (ctx.instance.parentId) {
ctx.Model.findById(ctx.instance.parentId, function (err, parent) {
var parentRgt = parent.rgt;
ctx.instance.lft = parentRgt;
ctx.instance.rgt = parentRgt + 1;
parent.rgt += 2;
updateMultiples(ctx.Model, {where: {rgt: {gt: parentRgt - 1}}}, function (result) {
result.rgt += 2;
}).then(function () {
return updateMultiples(ctx.Model, {where: {lft: {gt: parentRgt}}}, function (result) {
result.lft += 2;
});
}).catch(function (err) {
console.error(err);
}).done(function () {
next();
});
}
);
}
else {//there is no parent. It will be a root.
ctx.Model.find({
order: 'rgt DESC',
limit: 1
}, function (err, results) {
if (!results || !results.length) {
ctx.instance.lft = 1;
ctx.instance.rgt = 2;
} else {
ctx.instance.lft = results[0].rgt + 1;
ctx.instance.rgt = results[0].rgt + 2;
}
next();
});
}
}
}
else {//for now, we don't need do nothing here
next();
}
}
);
};
//help function
function updateMultiples(model, where, change) {
var deferred = Q.defer();
model.find(where, function (err, results) {
var parallel = [];
results.forEach(function (result) {
parallel.push(function () {
return result.save.apply(result, [{__treeDoNothing: true}, arguments[0]]);
});
change(result);
});
async.parallel(parallel, function (err) {
if (err) return deferred.reject(err);
deferred.resolve();
});
});
return deferred.promise;
}
function destroyChildren(Model, instances, cb) {
var deferred = Q.defer();
var parallel = [];
instances.forEach(function (instance) {
parallel.push(function () {
Model.destroyAll({parentId: instance.id}, arguments[0]);
});
});
async.parallel(parallel, function (err) {
if (err) return deferred.reject(err);
deferred.resolve();
if (cb) cb();
});
return deferred.promise;
}