-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstorage.js
67 lines (59 loc) · 2.07 KB
/
storage.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
if(localStorage.deck === undefined){
localStorage.deck = JSON.stringify(Array.from({length: 36}, () => 0));
}
//restore the stocked deck inside the dom
const urlParams = new URLSearchParams(window.location.search);
let code = urlParams.get('code');
let stored_deck = JSON.parse(localStorage.deck);
function restoreDeck(){
if(code !== null){
code.split(':').forEach((v,i) => {
stored_deck[i] = parseInt(v, 16);
})
localStorage.deck = JSON.stringify(stored_deck);
window.history.pushState('Pokédex Benchmark', '', '/');
}
allCell.forEach((cell, index) => {
if(stored_deck[index] !== 0){
cell.innerHTML = '';
getPokemonData(stored_deck[index]).then(data => {
cell.appendChild(generateCard(data));
})
}
})
}
//configuring observation
const config = { attributes: false, childList: true, subtree: false };
//save the entire deck on localstorage & configure cells for stats
let alreadyRefreshing = false;
allCell.forEach((cell, index) => {
function callback(mutationList, observer){
let value = 0;
for (const mutation of mutationList) {
if (mutation.type === 'childList') {
stored_deck = JSON.parse(localStorage.deck);
if(mutation.addedNodes.length > 0){
value = parseInt(cell.querySelector('div').id.split('_')[2]);
}
stored_deck[index] = value;
localStorage.deck = JSON.stringify(stored_deck);
}
}
if(index < 6){
refreshStats(index, value);
}
}
const observer = new MutationObserver(callback)
observer.observe(cell, config);
})
let saveButton = document.querySelector('button#save');
saveButton.addEventListener('click', ()=>{
let deck = JSON.parse(localStorage.deck).slice(0,6);
deck.forEach((value, index) => {
deck[index] = value.toString(16);
})
alert(
"Your deck can be shared with code : "
+deck.toString().toUpperCase().replaceAll(',',':')
)
})