Added attribute insertion UI and API
This commit is contained in:
@@ -4,6 +4,7 @@ import schema from '/imports/api/schema.js';
|
||||
import ColorSchema from "/imports/api/creature/subSchemas/ColorSchema.js";
|
||||
import { canEditCreature } from '/imports/api/creature/creaturePermission.js';
|
||||
import { recomputeCreatureById } from '/imports/api/creature/creatureComputation.js'
|
||||
import { getHighestOrder } from '/imports/api/order.js';
|
||||
import pickKeysAsOptional from '/imports/api/pickKeysAsOptional.js';
|
||||
|
||||
let Attributes = new Mongo.Collection("attributes");
|
||||
@@ -11,7 +12,7 @@ let Attributes = new Mongo.Collection("attributes");
|
||||
/*
|
||||
* Attributes are numbered stats of a character
|
||||
*/
|
||||
attributeSchema = schema({
|
||||
let attributeSchema = schema({
|
||||
charId: {
|
||||
type: String,
|
||||
regEx: SimpleSchema.RegEx.Id,
|
||||
@@ -24,6 +25,8 @@ attributeSchema = schema({
|
||||
// The technical, lowercase, single-word name used in formulae
|
||||
variableName: {
|
||||
type: String,
|
||||
// Must contain a letter, and be made of word characters only
|
||||
regEx: /^\w*[a-z]\w*$/i,
|
||||
index: 1,
|
||||
},
|
||||
// Attributes need to store their order to keep the sheet consistent
|
||||
@@ -99,6 +102,35 @@ let updateAttributeSchema = pickKeysAsOptional(attributeSchema, [
|
||||
'color',
|
||||
]);
|
||||
|
||||
const insertAttribute = new ValidatedMethod({
|
||||
|
||||
name: "Attributes.methods.insert",
|
||||
|
||||
validate: schema({
|
||||
attribute: {
|
||||
type: Object,
|
||||
blackbox: true,
|
||||
},
|
||||
}).validator(),
|
||||
|
||||
run({attribute}) {
|
||||
const charId = attribute.charId;
|
||||
if (canEditCreature(charId, this.userId)){
|
||||
attribute.order = getHighestOrder({
|
||||
collection: Attributes,
|
||||
charId,
|
||||
}) + 1;
|
||||
attribute.parent = {
|
||||
id: charId,
|
||||
collection: 'Creatures',
|
||||
};
|
||||
let attId = Attributes.insert(attribute);
|
||||
recomputeCreatureById(charId);
|
||||
return attId;
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
const updateAttribute = new ValidatedMethod({
|
||||
|
||||
name: "Attributes.methods.update",
|
||||
@@ -191,4 +223,4 @@ const adjustAttribute = new ValidatedMethod({
|
||||
});
|
||||
|
||||
export default Attributes;
|
||||
export { updateAttribute, adjustAttribute };
|
||||
export { insertAttribute, updateAttribute, adjustAttribute };
|
||||
|
||||
71
app/imports/api/order.js
Normal file
71
app/imports/api/order.js
Normal file
@@ -0,0 +1,71 @@
|
||||
const getHighestOrder = function({collection, charId}){
|
||||
const highestOrderedDoc = collection.findOne({
|
||||
charId
|
||||
}, {
|
||||
fields: {order: 1},
|
||||
sort: {order: -1},
|
||||
});
|
||||
return (highestOrderedDoc && highestOrderedDoc.order) || 0;
|
||||
}
|
||||
|
||||
const moveDocToOrder = function({collection, doc, order}){
|
||||
const currentOrder = doc.order;
|
||||
if (currentOrder === order){
|
||||
return;
|
||||
} else {
|
||||
// Move the document to its new order
|
||||
collection.update(doc._id, {$set: {order}});
|
||||
let inBetweenSelector, increment;
|
||||
if (order > currentOrder){
|
||||
// Move in-between docs backward
|
||||
inBetweenSelector = [
|
||||
{$gt: currentOrder},
|
||||
{$lte: order},
|
||||
];
|
||||
increment = -1;
|
||||
} else if (order < currentOrder){
|
||||
// Move in-between docs forward
|
||||
inBetweenSelector = [
|
||||
{$lt: currentOrder},
|
||||
{$gte: order},
|
||||
];
|
||||
increment = 1;
|
||||
}
|
||||
collection.update({
|
||||
order: {$and: inBetweenSelector},
|
||||
charId: doc.charId,
|
||||
}, {
|
||||
$inc: {order: increment},
|
||||
}, {
|
||||
multi: true,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
const reorderDocs = function({collection, charId}){
|
||||
let bulkWrite = [];
|
||||
collection.find({
|
||||
charId
|
||||
}, {
|
||||
fields: {order: 1},
|
||||
sort: {order: 1}
|
||||
}).forEach((doc, index) => {
|
||||
if (doc.order !== index){
|
||||
bulkwrite.push({
|
||||
updateOne : {
|
||||
filter: {_id: doc._id},
|
||||
update: {$set: {order: index}},
|
||||
},
|
||||
});
|
||||
}
|
||||
});
|
||||
if (Meteor.isServer){
|
||||
collection.rawCollection().bulkWrite(bulkWrite);
|
||||
} else {
|
||||
bulkWrite.forEach(op => {
|
||||
collection.update(op.filter, op.update);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
export { getHighestOrder, moveDocToOrder, reorderDocs };
|
||||
Reference in New Issue
Block a user