-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
73 lines (59 loc) · 2.48 KB
/
script.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
68
69
70
71
72
73
const container = document.querySelector(".container"); // if there were multiple containers it would only select the first one
const seats = document.querySelectorAll(".row .seat:not(.occupied)"); // puts things into a node list which is similar to an array and we can run methods on it as if it were an array
const count = document.getElementById("count");
const total = document.getElementById("total");
const movieSelect = document.getElementById("movie");
populateUI();
let ticketPrice = +movieSelect.value; // + has similar function to parseInt
// Save selected movie index and price
function setMovieData(movieIndex, moviePrice) {
localStorage.setItem("selectedMovieIndex", movieIndex);
localStorage.setItem("selectedMoviePrice", moviePrice);
}
// Update total and count
function updateSelectedCount() {
const selectedSeats = document.querySelectorAll(".row .seat.selected");
// Copy selected seats into array
// Map through array
// Return a new array of indexes
// spread operator copies the elements/values of an array rather than copying the actual array
const seatsIndex = [...selectedSeats].map(seat => [...seats].indexOf(seat));
localStorage.setItem("selectedSeats", JSON.stringify(seatsIndex));
const selectedSeatsCount = selectedSeats.length; // length is a property that gets the number of elements in an array, or node list!
console.log(selectedSeatsCount);
count.innerText = selectedSeatsCount;
total.innerText = selectedSeatsCount * ticketPrice;
}
// Get data from local storage and populate UI
function populateUI() {
const selectedSeats = JSON.parse(localStorage.getItem("selectedSeats"));
if (selectedSeats !== null && selectedSeats.length > 0) {
seats.forEach((seat, index) => {
if (selectedSeats.indexOf(index) > -1) {
seat.classList.add("selected");
}
});
}
const selectedMovieIndex = localStorage.getItem("selectedMovieIndex");
if (selectedMovieIndex !== null) {
movieSelect.selectedIndex = selectedMovieIndex;
}
}
// Movie select event
movieSelect.addEventListener("change", e => {
ticketPrice = +e.target.value;
setMovieData(e.target.selectedIndex, e.target.value);
updateSelectedCount();
});
// registers 'click' only on seats that are unoccupied
container.addEventListener("click", e => {
if (
e.target.classList.contains("seat") &&
!e.target.classList.contains("occupied")
) {
e.target.classList.toggle("selected");
updateSelectedCount();
}
});
// Initial count and total set
updateSelectedCount();