From 53afaa4f37fc3d4cea7ae879ef91a9b69f3e94b1 Mon Sep 17 00:00:00 2001 From: Jacob Date: Wed, 26 Jul 2017 19:54:18 +0100 Subject: [PATCH] CTRL-dragging now copies spells --- rpg-docs/Model/Character/Spells.js | 77 +++++++++++++------ .../client/views/character/spells/spells.js | 6 +- 2 files changed, 59 insertions(+), 24 deletions(-) diff --git a/rpg-docs/Model/Character/Spells.js b/rpg-docs/Model/Character/Spells.js index c6036ee4..ae85cf5f 100644 --- a/rpg-docs/Model/Character/Spells.js +++ b/rpg-docs/Model/Character/Spells.js @@ -87,16 +87,20 @@ Spells.deny(CHARACTER_SUBSCHEMA_DENY); -var checkMovePermission = function(spellId, parent) { +var checkMovePermission = function(spellId, parent, destinationOnly) { 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"); + var permission; + + if (!destinationOnly) { //if we're not modifying the origin, only the destination + 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); @@ -126,33 +130,55 @@ var checkMovePermission = function(spellId, parent) { } }; -var moveSpell = function(spellId, parentCollection, parentId) { +var moveSpell = function(spellId, parentCollection, parentId) { //moving spells between characters is NOT YET SUPPORTED :O 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}); + checkMovePermission(spellId, {collection: parentCollection, id: parentId}, false); + } + + //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, + }} + ); + } +}; + +var copySpell = 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}, true); //we're only reading from the source character } - 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, - }} - ); - } + if (spell.parentCollection == "Characters") { //then we are copying the spell to a different character. + //TODO: handle this + } else { //else we are copying the spell within the same character + newSpell = _.clone(spell); + delete newSpell._id; + newSpellId = Spells.insert(newSpell); //add a new copy of the spell + Spells.update( + newSpellId, + {$set: { + "parent.collection": parentCollection, + "parent.id": parentId, + }} + ); } }; @@ -163,4 +189,9 @@ Meteor.methods({ check(spellListId, String); moveSpell(spellId, "SpellLists", spellListId); }, + copySpellToList: function(spellId, spellListId) { + check(spellId, String); + check(spellListId, String); + copySpell(spellId, "SpellLists", spellListId); + }, }); \ No newline at end of file diff --git a/rpg-docs/client/views/character/spells/spells.js b/rpg-docs/client/views/character/spells/spells.js index 45b1a54b..7b5bf170 100644 --- a/rpg-docs/client/views/character/spells/spells.js +++ b/rpg-docs/client/views/character/spells/spells.js @@ -352,7 +352,11 @@ Template.layout.events({ "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); + if (event.ctrlKey){ + Meteor.call("copySpellToList", spellId, this._id); + } else { + Meteor.call("moveSpellToList", spellId, this._id); + } Session.set("inventory.dragSpellId", null); }, }); \ No newline at end of file