| 
									
										
										
										
											2020-04-22 11:17:05 +02:00
										 |  |  | import {sortByString} from '@theme/util/filterHelpers' | 
					
						
							| 
									
										
										
										
											2020-05-24 13:32:31 +02:00
										 |  |  | import { getResourceIndexInLibrary } from '@theme/util' | 
					
						
							| 
									
										
										
										
											2020-04-22 11:17:05 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | export default { | 
					
						
							|  |  |  |   namespaced: true, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   state: { | 
					
						
							|  |  |  |     spells: [], | 
					
						
							| 
									
										
										
										
											2020-05-04 11:25:26 +02:00
										 |  |  |     spellSlots: [], | 
					
						
							| 
									
										
										
										
											2021-01-07 10:10:18 +01:00
										 |  |  |     spellCast: [], | 
					
						
							| 
									
										
										
										
											2020-05-04 11:25:26 +02:00
										 |  |  |     notPrintedSpells: [] | 
					
						
							| 
									
										
										
										
											2020-04-22 11:17:05 +02:00
										 |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   getters: { | 
					
						
							|  |  |  |     spells: state => state.spells, | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   actions: { | 
					
						
							|  |  |  |     reset: ({ commit }) => { | 
					
						
							|  |  |  |       commit('resetSpells') | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     updateSpells: ({ commit }, payload) => { | 
					
						
							|  |  |  |       commit('setSpells', payload) | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     addSpell: ({ commit }, payload) => { | 
					
						
							|  |  |  |       commit('addSpell', payload) | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     removeSpell: ({ commit }, payload) => { | 
					
						
							|  |  |  |       commit('removeSpell', payload) | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   mutations: { | 
					
						
							|  |  |  |     initialiseStore (state) { | 
					
						
							|  |  |  | 			// Récupération des données utilisateurs depuis le navigateur
 | 
					
						
							|  |  |  | 			if(localStorage.getItem('mySpells') && localStorage.getItem('mySpells') !== undefined) { | 
					
						
							|  |  |  |         let localStorageData = JSON.parse(localStorage.getItem('mySpells')) | 
					
						
							| 
									
										
										
										
											2020-05-04 12:01:58 +02:00
										 |  |  |         if (localStorageData.spells) { | 
					
						
							|  |  |  |           state.spells = localStorageData.spells | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (localStorageData.spellSlots) { | 
					
						
							|  |  |  |           state.spellSlots = localStorageData.spellSlots | 
					
						
							| 
									
										
										
										
											2021-01-07 10:10:18 +01:00
										 |  |  |           for (let i = 0; i < state.spellSlots.length; i++) { | 
					
						
							|  |  |  |             if (state.spellSlots[i] > 0) { | 
					
						
							|  |  |  |               if (!state.spellCast[i]) { | 
					
						
							|  |  |  |                 state.spellCast[i] = new Array(Number(state.spellSlots[i])) | 
					
						
							|  |  |  |               } | 
					
						
							|  |  |  |               for (let j = 0; j < state.spellSlots[i]; j++) { | 
					
						
							|  |  |  |                 if (!state.spellCast[i][j]) { | 
					
						
							|  |  |  |                   state.spellCast[i][j] = false | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |               } | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |               state.spellCast[i] = null | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |           } | 
					
						
							| 
									
										
										
										
											2020-05-04 12:01:58 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |         if (localStorageData.notPrintedSpells) { | 
					
						
							|  |  |  |           state.notPrintedSpells = localStorageData.notPrintedSpells | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-04-22 11:17:05 +02:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  |     setSpells: (state, payload) => { | 
					
						
							|  |  |  |       state.spells = payload | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     resetSpells: (state) => { | 
					
						
							|  |  |  |       state.spells = [] | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     addSpell: (state, payload) => { | 
					
						
							|  |  |  |       state.spells.push(payload) | 
					
						
							|  |  |  |       state.spells.sort((a, b) => { return sortByString(a.title, b.title) }) | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     updateSpell: (state, payload) => { | 
					
						
							| 
									
										
										
										
											2020-05-24 13:32:31 +02:00
										 |  |  |       let spellIndex = getResourceIndexInLibrary(payload, state.spells) | 
					
						
							|  |  |  |       if (spellIndex >= 0) { | 
					
						
							|  |  |  |         state.spells[spellIndex] = payload | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2020-04-22 11:17:05 +02:00
										 |  |  |     }, | 
					
						
							|  |  |  |     removeSpell: (state, payload) => { | 
					
						
							| 
									
										
										
										
											2020-05-24 13:32:31 +02:00
										 |  |  |       let spellIndex = getResourceIndexInLibrary(payload, state.spells) | 
					
						
							|  |  |  |       if (spellIndex >= 0) { | 
					
						
							|  |  |  |         state.spells.splice(spellIndex, 1) | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2020-04-22 11:17:05 +02:00
										 |  |  |     }, | 
					
						
							| 
									
										
										
										
											2020-05-03 19:35:37 +02:00
										 |  |  |     setSpellSlots: (state, payload) => { | 
					
						
							|  |  |  |       state.spellSlots = payload | 
					
						
							| 
									
										
										
										
											2021-01-07 10:10:18 +01:00
										 |  |  |       for (let i = 0; i < state.spellSlots.length; i++) { | 
					
						
							|  |  |  |         if (state.spellSlots[i] > 0) { | 
					
						
							|  |  |  |           if (!state.spellCast[i]) { | 
					
						
							|  |  |  |             state.spellCast[i] = new Array(Number(state.spellSlots[i])) | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |           for (let j = 0; j < state.spellSlots[i]; j++) { | 
					
						
							|  |  |  |             if (!state.spellCast[i][j]) { | 
					
						
							|  |  |  |               state.spellCast[i][j] = false | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |           state.spellCast[i] = null | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     setSpellCast: (state, payload) => { | 
					
						
							|  |  |  |       state.spellCast = payload | 
					
						
							| 
									
										
										
										
											2020-05-03 19:35:37 +02:00
										 |  |  |     }, | 
					
						
							| 
									
										
										
										
											2020-05-04 12:01:58 +02:00
										 |  |  |     setNotPrintedSpells: (state, payload) => { | 
					
						
							|  |  |  |       state.notPrintedSpells = payload | 
					
						
							|  |  |  |     }, | 
					
						
							| 
									
										
										
										
											2020-05-04 11:25:26 +02:00
										 |  |  |     addNotPrintedSpell: (state, payload) => { | 
					
						
							| 
									
										
										
										
											2020-05-24 13:32:31 +02:00
										 |  |  |       let spellIndex = getResourceIndexInLibrary(payload, state.notPrintedSpells) | 
					
						
							| 
									
										
										
										
											2020-05-04 11:25:26 +02:00
										 |  |  |       if (!spellIndex >= 0) { | 
					
						
							|  |  |  |         state.notPrintedSpells.push(payload) | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     removeNotPrintedSpell: (state, payload) => { | 
					
						
							| 
									
										
										
										
											2020-05-24 13:32:31 +02:00
										 |  |  |       let spellIndex = getResourceIndexInLibrary(payload, state.notPrintedSpells) | 
					
						
							| 
									
										
										
										
											2020-05-04 11:25:26 +02:00
										 |  |  |       if (spellIndex >= 0) { | 
					
						
							|  |  |  |         state.notPrintedSpells.splice(spellIndex, 1) | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-04-22 11:17:05 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |