Began making generic tree re-arranging methods, still buggy
This commit is contained in:
@@ -30,3 +30,4 @@ let ChildSchema = new SimpleSchema({
|
||||
});
|
||||
|
||||
export default ChildSchema;
|
||||
export { RefSchema };
|
||||
|
||||
@@ -72,30 +72,31 @@ export function updateOrder({docRef, order}){
|
||||
return;
|
||||
} else {
|
||||
// Move the document to its new order
|
||||
docRef.collection.update(doc._id, {$set: {order}});
|
||||
collection.update(doc._id, {$set: {order}}, {selector: {type: 'any'}});
|
||||
let inBetweenSelector, increment;
|
||||
if (order > currentOrder){
|
||||
// Move in-between docs backward
|
||||
inBetweenSelector = [
|
||||
{$gt: currentOrder},
|
||||
{$lte: order},
|
||||
];
|
||||
inBetweenSelector = {
|
||||
$gt: currentOrder,
|
||||
$lte: order
|
||||
};
|
||||
increment = -1;
|
||||
} else if (order < currentOrder){
|
||||
// Move in-between docs forward
|
||||
inBetweenSelector = [
|
||||
{$lt: currentOrder},
|
||||
{$gte: order},
|
||||
];
|
||||
inBetweenSelector = {
|
||||
$lt: currentOrder,
|
||||
$gte: order
|
||||
};
|
||||
increment = 1;
|
||||
}
|
||||
collection.update({
|
||||
'parent.id': doc.parent.id,
|
||||
order: {$and: inBetweenSelector},
|
||||
order: inBetweenSelector,
|
||||
}, {
|
||||
$inc: {order: increment},
|
||||
}, {
|
||||
multi: true,
|
||||
selector: {type: 'any'},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
import { updateParent } from '/imports/api/parenting/parenting.js';
|
||||
|
||||
export default function organizeDoc({docRef, parentRef, order}){
|
||||
updateParent({docRef, parentRef});
|
||||
updateOrder({docRef, order})
|
||||
};
|
||||
47
app/imports/api/parenting/organizeMethods.js
Normal file
47
app/imports/api/parenting/organizeMethods.js
Normal file
@@ -0,0 +1,47 @@
|
||||
import SimpleSchema from 'simpl-schema';
|
||||
import { updateParent } from '/imports/api/parenting/parenting.js';
|
||||
import { updateOrder } from '/imports/api/parenting/order.js';
|
||||
import { RefSchema } from '/imports/api/parenting/ChildSchema.js';
|
||||
import { assertDocEditPermission } from '/imports/api/sharing/sharingPermissions.js';
|
||||
import fetchDocByRef from '/imports/api/parenting/fetchDocByRef.js';
|
||||
|
||||
const organizeDoc = new ValidatedMethod({
|
||||
name: 'organize.methods.organizeDoc',
|
||||
validate: new SimpleSchema({
|
||||
docRef: RefSchema,
|
||||
parentRef: RefSchema,
|
||||
order: {
|
||||
type: Number,
|
||||
min: 0,
|
||||
},
|
||||
}).validator(),
|
||||
run({docRef, parentRef, order}) {
|
||||
let doc = fetchDocByRef(docRef);
|
||||
|
||||
// The user must be able to edit both the doc and its parent to move it
|
||||
// successfully
|
||||
assertDocEditPermission(doc, this.userId);
|
||||
let parent = fetchDocByRef(parentRef);
|
||||
assertDocEditPermission(parent, this.userId);
|
||||
updateParent({docRef, parentRef});
|
||||
updateOrder({docRef, order})
|
||||
},
|
||||
});
|
||||
|
||||
const reorderDoc = new ValidatedMethod({
|
||||
name: 'organize.methods.reorderDoc',
|
||||
validate: new SimpleSchema({
|
||||
docRef: RefSchema,
|
||||
order: {
|
||||
type: Number,
|
||||
min: 0,
|
||||
},
|
||||
}).validator(),
|
||||
run({docRef, order}) {
|
||||
let doc = fetchDocByRef(docRef);
|
||||
assertDocEditPermission(doc, this.userId);
|
||||
updateOrder({docRef, order})
|
||||
},
|
||||
});
|
||||
|
||||
export { organizeDoc, reorderDoc };
|
||||
@@ -1,6 +1,5 @@
|
||||
import fetchDocByRef from '/imports/api/parenting/fetchDocByRef.js';
|
||||
import getCollectionByName from '/imports/api/parenting/getCollectionByName.js';
|
||||
import SimpleSchema from 'simpl-schema';
|
||||
|
||||
export function fetchParent({id, collection}){
|
||||
return fetchDocByRef({id, collection});
|
||||
@@ -23,20 +22,20 @@ export function updateChildren({collection, parentId, filter = {}, modifier, opt
|
||||
collection.update(filter, modifier, options);
|
||||
}
|
||||
|
||||
export function fetchDecendents({ collection, ancestorId, filter = {}, options}){
|
||||
export function fetchDescendants({ collection, ancestorId, filter = {}, options}){
|
||||
filter["ancestors.id"] = ancestorId;
|
||||
let decendents = [];
|
||||
decendents.push(...collection.find(filter, options).fetch());
|
||||
return decendents;
|
||||
let descendants = [];
|
||||
descendants.push(...collection.find(filter, options).fetch());
|
||||
return descendants;
|
||||
}
|
||||
|
||||
export function updateDecendents({collection, ancestorId, filter = {}, modifier, options={}}){
|
||||
export function updateDescendants({collection, ancestorId, filter = {}, modifier, options={}}){
|
||||
filter["ancestors.id"] = ancestorId;
|
||||
options.multi = true;
|
||||
collection.update(filter, modifier, options);
|
||||
}
|
||||
|
||||
export function forEachDecendent({collection, ancestorId, filter = {}, options}, callback){
|
||||
export function forEachDescendant({collection, ancestorId, filter = {}, options}, callback){
|
||||
filter["ancestors.id"] = ancestorId;
|
||||
collection.find(filter, options).forEach(callback);
|
||||
}
|
||||
@@ -75,16 +74,16 @@ export function updateParent({docRef, parentRef}){
|
||||
let {parent, ancestors} = getAncestry({parentRef});
|
||||
collection.update(docRef.id, {$set: {parent, ancestors}});
|
||||
|
||||
// Remove the old ancestors from the decendents
|
||||
updateDecendents({
|
||||
// Remove the old ancestors from the descendants
|
||||
updateDescendants({
|
||||
ancestorId: docRef.id,
|
||||
modifier: {$pullAll: {
|
||||
ancestors: oldDoc.ancestors,
|
||||
}},
|
||||
});
|
||||
|
||||
// Add the new ancestors to the decendents
|
||||
updateDecendents({
|
||||
// Add the new ancestors to the descendants
|
||||
updateDescendants({
|
||||
ancestorId: docRef.id,
|
||||
modifier: {$push: {
|
||||
ancestors: {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import getCollectionByName from '/imports/api/parenting/getCollectionByName.js';
|
||||
import updateDecendents from '/imports/api/parenting/parenting.js';
|
||||
import updateDescendants from '/imports/api/parenting/parenting.js';
|
||||
|
||||
// 1 + n database hits
|
||||
export function softRemove({_id, collection}){
|
||||
@@ -12,9 +12,9 @@ export function softRemove({_id, collection}){
|
||||
}, $unset: {
|
||||
removedWith: 1,
|
||||
}});
|
||||
// Remove all the decendents that have not yet been removed, and set them to be
|
||||
// Remove all the descendants that have not yet been removed, and set them to be
|
||||
// removed with this document
|
||||
updateDecendents({
|
||||
updateDescendants({
|
||||
ancestorId: _id,
|
||||
filter: {removed: {$ne: true}},
|
||||
modifier: {$set: {
|
||||
@@ -41,7 +41,7 @@ export function restore({_id, collection}){
|
||||
removedAt: 1,
|
||||
}});
|
||||
if (numUpdated === 0) restoreError();
|
||||
updateDecendents({
|
||||
updateDescendants({
|
||||
ancestorId: _id,
|
||||
filter: {
|
||||
removedWith: _id,
|
||||
|
||||
Reference in New Issue
Block a user