-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
83 lines (70 loc) · 2.77 KB
/
script.js
File metadata and controls
83 lines (70 loc) · 2.77 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// let button = document.getElementsByClassName("text")[0];
let btnCurrentSelected = false;
let firstPlayer, firstIndex;
let secondPlayer, secondIndex;
let nameArr = [{name: "Jason Kendall", img: "images/kendall.png"},
{name: "Willie Stargell", img: "images/stargell.png"},
{name: "Bill Mazeroski", img: "images/mazeroski.png"},
{name: "Ke'Bryan Hayes", img: "images/hayes.png"},
{name: "Oneil Cruz", img: "images/cruz.png"},
{name: "Dave Parker", img: "images/parker.png"},
{name: "Bryan Reynolds", img: "images/reynolds.png"},
{name: "Honus Wagner", img: "images/wagner.png"},
{name: "Rich Gossage", img: "images/gossage.png"},
];
//initialize cards and names into lineup
let images = document.getElementsByClassName("player-image");
let nodes = document.getElementsByClassName('text');
function updateImageOrder() {
for (let i = 0; i < images.length; i++) {
images[i].setAttribute("src", nameArr[i].img);
}
}
function updateNameOrder(){
for (let i = 0; i < nodes.length; i++) {
nodes[i].textContent = nameArr[i].name;
}
}
//initialize cards and names into lineup
updateImageOrder();
updateNameOrder();
//add/remove click styling
let addOutline = (docObj, index, className) => docObj[index].classList.add(className);
let removeOutline = (docObj, index, className) => docObj[index].classList.remove(className);
for (let i = 0; i < nodes.length; i++) {
nodes[i].addEventListener('click', function(i) {
console.log('You clicked element #' + i);
if (btnCurrentSelected) {
//set second player to second button clicked
secondPlayer = nodes[i].textContent;
addOutline(nodes, i, "clicked");
addOutline(images, i, "clicked-shadow");
secondIndex = i;
if(firstPlayer === secondPlayer){ //if same button clicked twice
removeOutline(nodes, firstIndex, "clicked");
removeOutline(images, firstIndex, "clicked-shadow");
} else { //swap name content
nodes[firstIndex].textContent = secondPlayer;
nodes[secondIndex].textContent = firstPlayer;
removeOutline(nodes, firstIndex, "clicked");
removeOutline(nodes, secondIndex, "clicked");
removeOutline(images, firstIndex, "clicked-shadow");
removeOutline(images, secondIndex, "clicked-shadow");
//swap images, swap names in db array
let temp = nameArr[firstIndex].img;
nameArr[firstIndex].img = nameArr[secondIndex].img;
nameArr[secondIndex].img = temp;
updateImageOrder();
nameArr[firstIndex].name = secondPlayer;
nameArr[secondIndex].name = firstPlayer;
}
btnCurrentSelected = false;
} else {
firstPlayer = nodes[i].textContent;
addOutline(nodes, i, "clicked");
addOutline(images, i, "clicked-shadow");
firstIndex = i;
btnCurrentSelected = true;
}
}.bind(null, i));
}