diff --git a/rpg-docs/client/views/character/spells/spells.js b/rpg-docs/client/views/character/spells/spells.js index d7b7bf2c..2d02b3c5 100644 --- a/rpg-docs/client/views/character/spells/spells.js +++ b/rpg-docs/client/views/character/spells/spells.js @@ -253,8 +253,8 @@ Template.spells.events({ pushDialogStack({ template: "spellLibraryDialog", element: event.currentTarget, - callback: (result) => { - if (!result) return; + callback: (resultArray) => { + if (!resultArray) return; if (!listId){ listId = SpellLists.insert({ name: "New SpellList", @@ -263,53 +263,59 @@ Template.spells.events({ attackBonus: "intelligenceMod + proficiencyBonus", }); } - // Make the library spell into a regular spell - let spell = _.omit(result, "library", "attacks", "effects"); - spell._id = spellId; - spell.charId = charId; - spell.parent = { - id: listId, - collection: "SpellLists", - }; - spell.prepared = "prepared"; - Spells.insert(spell); - // Copy over attacks and effects - _.each(result.attacks, (attack) => { - if (!("attackBonus" in attack)) {attack.attackBonus = "attackBonus"} //if no attack bonus provided, use spell list's - attack.charId = charId; - attack.parent = {id: spellId, collection: "Spells"}; - Attacks.insert(attack); - }); - _.each(result.effects, (effect) => { - effect.charId = charId; - effect.parent = {id: spellId, collection: "Spells"}; - Effects.insert(effect); - }); - /******[UNCOMMENT ONCE BUFFS ARE ADDED]******* - _.each(result.buffs, (buff) => { - buff.charId = charId; - buff.parent = {id: spellId, collection: "Spells"}; - buffId = Buffs.insert(buff); - - _.each(buff.attacks, (attack) => { - if (!(attackBonus in attack)) {attack.attackBonus = "attackBonus"} //if no attack bonus provided, use spell list's + //loop through all returned spells + _.each(resultArray, (rawSpell, index) =>{ + // Make the library spell into a regular spell + let spell = _.omit(rawSpell, "library", "attacks", "effects"); + if (index == 0) { + spell._id = spellId; //only do this for the first spell added + } + spell.charId = charId; + spell.parent = { + id: listId, + collection: "SpellLists", + }; + spell.prepared = "prepared"; + Spells.insert(spell); + // Copy over attacks and effects + _.each(rawSpell.attacks, (attack) => { + if (!("attackBonus" in attack)) {attack.attackBonus = "attackBonus"} //if no attack bonus provided, use spell list's attack.charId = charId; - attack.parent = {id: buffId, collection: "Buffs"}; + attack.parent = {id: spellId, collection: "Spells"}; Attacks.insert(attack); }); - _.each(buff.effects, (effect) => { + _.each(rawSpell.effects, (effect) => { effect.charId = charId; - effect.parent = {id: buffId, collection: "Buffs"}; + effect.parent = {id: spellId, collection: "Spells"}; Effects.insert(effect); }); - _.each(buff.proficiencies, (prof) => { - prof.charId = charId; - prof.parent = {id: buffId, collection: "Buffs"}; - Proficiencies.insert(prof); + + /******[UNCOMMENT ONCE BUFFS ARE ADDED]******* + _.each(rawSpell.buffs, (buff) => { + buff.charId = charId; + buff.parent = {id: spellId, collection: "Spells"}; + buffId = Buffs.insert(buff); + + _.each(buff.attacks, (attack) => { + if (!(attackBonus in attack)) {attack.attackBonus = "attackBonus"} //if no attack bonus provided, use spell list's + attack.charId = charId; + attack.parent = {id: buffId, collection: "Buffs"}; + Attacks.insert(attack); + }); + _.each(buff.effects, (effect) => { + effect.charId = charId; + effect.parent = {id: buffId, collection: "Buffs"}; + Effects.insert(effect); + }); + _.each(buff.proficiencies, (prof) => { + prof.charId = charId; + prof.parent = {id: buffId, collection: "Buffs"}; + Proficiencies.insert(prof); + }); }); + *******[UNCOMMENT ONCE BUFFS ARE ADDED]******/ }); - *******[UNCOMMENT ONCE BUFFS ARE ADDED]******/ }, returnElement: () => $(`[data-id='${spellId}']`).get(0), }) diff --git a/rpg-docs/client/views/character/spells/spellsLibraryDialog/spellLibraryDialog.js b/rpg-docs/client/views/character/spells/spellsLibraryDialog/spellLibraryDialog.js index 6b8ce8e3..9f9cab15 100644 --- a/rpg-docs/client/views/character/spells/spellsLibraryDialog/spellLibraryDialog.js +++ b/rpg-docs/client/views/character/spells/spellsLibraryDialog/spellLibraryDialog.js @@ -14,7 +14,7 @@ const categories = [ ]; Template.spellLibraryDialog.onCreated(function(){ - this.selectedSpell = new ReactiveVar(); + this.selectedSpells = new ReactiveVar([]); //this holds an array of the selected spells by ID this.searchTerm = new ReactiveVar(); this.categoriesOpen = new ReactiveVar([]); this.readyDict = new ReactiveDict(); @@ -59,8 +59,8 @@ Template.spellLibraryDialog.helpers({ }); }, isSelected(spell){ - const selected = Template.instance().selectedSpell.get(); - return selected && selected._id === spell._id; + const selected = Template.instance().selectedSpells.get(); + return _.contains(selected, spell._id); }, isOpen(key){ const cats = Template.instance().categoriesOpen.get(); @@ -89,10 +89,30 @@ Template.spellLibraryDialog.events({ popDialogStack(); }, "click .okButton": function(event, template){ - popDialogStack(template.selectedSpell.get()); + const selectedIds = template.selectedSpells.get(); + var returnSpells = []; + _.each(selectedIds, (id) => { + let spell = LibrarySpells.findOne(id); + if (spell) { + returnSpells.push(spell) + } + }); + popDialogStack(returnSpells); }, "click .library-spell": function(event, template){ - template.selectedSpell.set(this.spell); + let selected = template.selectedSpells.get(); + const spellId = this.spell._id; + // Toggle whether this spellId is in the array or not + if (_.contains(selected, spellId)){ + selected = _.without(selected, spellId); + } else { + selected.push(spellId); + } + template.selectedSpells.set(selected); + + + console.log("selectedSpells", Template.instance().selectedSpells.get()); + console.log("spellId", this.spell._id); }, "click #backButton": function(event, template){ popDialogStack();