-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
119 lines (96 loc) · 2.97 KB
/
index.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
class App {
constructor() {
const form = document.querySelector('#movieform')
form.addEventListener('submit', (event) => {
event.preventDefault()
this.handleSubmit(event)
})
this.list = document.querySelector('#movies')
this.movies = []
this.load()
}
renderSpan(className, value) {
const span = document.createElement('span')
span.classList.add(className)
span.setAttribute('contenteditable', 'true')
span.textContent = value
return span
}
save() {
localStorage.setItem('movies', JSON.stringify(this.movies))
}
load() {
const movies = JSON.parse(localStorage.getItem('movies')) || []
if(movies)
movies.forEach(movie => this.addMovie(movie))
}
renderButton(className) {
const button = document.createElement('button')
button.classList.add(className)
if(className == 'trash')
button.innerHTML = '<i class="far fa-trash-alt" title="trash movie"></i>'
else
button.innerHTML = '<i class="fas fa-star" title="favorite movie"></i>'
return button
}
renderListItem(movie) {
const listItem = document.createElement('li')
listItem.classList.add('movie')
const movieProps = Object.keys(movie)
movieProps.forEach((prop) => {
if(prop == 'favorite')
return
const span = this.renderSpan(prop, movie[prop])
listItem.appendChild(span)
})
//add and bind listeners to favorite and trash button for each list item
const trashButton = this.renderButton('trash')
const faveButton = this.renderButton('fave')
trashButton.addEventListener('click', (event) => {
this.handleTrash(event, movie)
})
faveButton.addEventListener('click', (event) => {
this.handleFavorite(event, movie)
})
listItem.appendChild(trashButton)
listItem.appendChild(faveButton)
return listItem
}
addMovie(movie) {
//add the movie to the movies array
this.movies.push(movie)
const listItem = this.renderListItem(movie)
if (movie.favorite) {
listItem.classList.add('favorite')
}
this.list.appendChild(listItem)
}
handleTrash(event, movie) {
//better way to handle deletion: pass the listItem as an argument to this function
//also pass the movie as an argument so it can easily be popped out
const list = document.querySelector('#movies')
const listItem = event.target.closest('.movie')
list.removeChild(listItem)
this.movies.splice(this.movies.indexOf(movie), 1)
this.save()
}
handleFavorite(event, movie) {
const listItem = event.target.closest('.movie')
movie.favorite = listItem.classList.toggle('favorite')
this.save()
}
handleSubmit(event) {
//create new movie object
const movie = {
title: event.target.moviename.value,
year: ' (' + event.target.releaseyear.value + ')',
}
this.addMovie(movie)
this.save()
//clears text box
const f = event.target
f.reset()
f.moviename.focus()
}
}
const app = new App()