Added basic drag-and-drop functionality between spell lists

Closes #105
This commit is contained in:
Jacob
2017-07-26 19:23:41 +01:00
parent 73d1419ee9
commit 3599b5fbc4
3 changed files with 107 additions and 1 deletions

View File

@@ -83,3 +83,84 @@ Spells.after.update(function (userId, spell, fieldNames) {
Spells.allow(CHARACTER_SUBSCHEMA_ALLOW);
Spells.deny(CHARACTER_SUBSCHEMA_DENY);
var checkMovePermission = function(spellId, parent) {
var spell = Spells.findOne(spellId);
if (!spell)
throw new Meteor.Error("No such spell",
"An spell could not be found to move");
//handle permissions
var permission = Meteor.call("canWriteCharacter", spell.charId);
if (!permission){
throw new Meteor.Error("Access denied",
"Not permitted to move spells from this character");
}
if (parent.collection === "Characters"){
permission = Meteor.call("canWriteCharacter", parent.id);
if (!permission){
throw new Meteor.Error("Access denied",
"Not permitted to move spells to this character");
}
} else {
var parentCollectionObject = global[parent.collection];
var parentObject = null;
if (parentCollectionObject)
parentObject = parentCollectionObject.findOne(
parent.id, {fields: {_id: 1, charId: 1}}
);
if (!parentObject) throw new Meteor.Error(
"Invalid parent",
"The destination parent " + parent.id +
" does not exist in the collection " + parent.collection
);
if (parentObject.charId){
permission = Meteor.call("canWriteCharacter", parentObject.charId);
if (!permission){
throw new Meteor.Error("Access denied",
"Not permitted to move spells to this character");
}
}
}
};
var moveSpell = function(spellId, parentCollection, parentId) {
var spell = Spells.findOne(spellId);
if (!spell) return;
parentCollection = parentCollection || spell.parent.collection;
parentId = parentId || spell.parent.id;
if (Meteor.isServer) {
checkMovePermission(spellId, {collection: parentCollection, id: parentId});
}
if (spell.parentCollection == "Characters") { //then we are moving the spell to a different character.
} else { //else we are moving the spell within the same character
//update the spell provided the update will actually change something
if (
spell.parent.collection !== parentCollection ||
spell.parent.id !== parentId
){
Spells.update(
spellId,
{$set: {
"parent.collection": parentCollection,
"parent.id": parentId,
}}
);
}
}
};
Meteor.methods({
moveSpellToList: function(spellId, spellListId) {
check(spellId, String);
check(spellListId, String);
moveSpell(spellId, "SpellLists", spellListId);
},
});

View File

@@ -83,7 +83,9 @@
{{#each spells ../_id ../../_id}}
{{#if showSpell ../../_id}}
<div class="item-slot">
<div class="tall spell item layout horizontal center" data-id={{_id}}>
<div class="tall spell item layout horizontal center spellItem"
data-id={{_id}}
draggable={{canEditCharacter charId}}>
<iron-icon icon="social:whatshot"
style="color: {{hexColor color}};
margin-right: 16px;"

View File

@@ -333,3 +333,26 @@ Template.spells.events({
event.stopPropagation();
},
});
Template.layout.events({
"dragstart .spellItem": function(event, instance){
event.originalEvent.dataTransfer.setData("dicecloud-id/spells", this._id);
Session.set("inventory.dragSpellId", this._id);
},
"dragend .spellItem": function(event, instance){
Session.set("inventory.dragSpellId", null);
},
"dragover .spellList, dragenter .spellList":
function(event, instance){
if (_.contains(event.originalEvent.dataTransfer.types, "dicecloud-id/spells")){
event.preventDefault();
}
},
"drop .spellList": function(event, instance){
var spellId = event.originalEvent.dataTransfer.getData("dicecloud-id/spells");
//move spell to new list
Meteor.call("moveSpellToList", spellId, this._id);
Session.set("inventory.dragSpellId", null);
},
});