-
Notifications
You must be signed in to change notification settings - Fork 0
/
design-objects-in-context-es6.html
408 lines (308 loc) · 10.1 KB
/
design-objects-in-context-es6.html
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
<!doctype html>
<html lang="en">
<head>
<style>
table {
border-collapse: collapse;
}
table, td, tr {
border: 1px solid black;
}
#td-id {
min-width: 30px;
}
#td-delete {
min-width: 60px;
}
#td-title {
min-width: 150px;
}
#td-songs {
min-width: 300px;
}
</style>
</head>
<body>
<h2>Create Artist</h2>
<form id="artist-form">
<strong>Artist name:</strong> <input type="text" id="artist-name"
placeholder="(ex. Janis Joplin)">
<button type="button" id="create-artist">Create artist</button>
</form>
<h2>Create Song</h2>
<form id="song-form">
<strong>Song title:</strong> <input type="text" id="song-name"
placeholder="(ex. Piece of My Heart)">
<select id="select-artist">
</select>
<button type="button" id="create-song">Create song</button>
</form>
<h2>Create Playlist</h2>
<form id="playlist-form">
<strong>Playlist title:</strong> <input type="text" id="playlist-name"
placeholder="(ex. Favorite Songs)">
<button type="button" id="create-playlist">Show playlist</button>
<br>
<span id="select-song">
</span>
</form>
<br>
<br>
<table id="playlist-table">
<thead>
<td id="td-id">
<strong>ID</strong>
</td>
<td id="td-title">
<strong>Title</strong>
</td>
<td id="td-songs">
<strong>Songs</strong>
</td>
<td id="td-delete">
<strong>Delete</strong>
</td>
</thead>
<tbody id="tbody">
</tbody>
</table>
<script>
// create Artist class
class Artist {
constructor(name) {
this.name = name;
this.id = Artist.incrementId();
}
static _id = 0;
static incrementId() {
return ++Artist._id;
}
}
// create Song class
class Song {
constructor(name, artist) {
this.name = name;
this.artist = artist;
this.id = Song.incrementId();
}
static _id = 0;
static incrementId() {
return ++Song._id;
}
}
// create Playlist class
class Playlist {
constructor(name, songs) {
this.name = name;
this.songs = songs;
this.id = Playlist.incrementId();
}
static _id = 0;
static incrementId() {
return ++Playlist._id;
}
}
// add event listener on Create artist, song and playlist buttons
document.getElementById("create-artist").
addEventListener("click", createArtistAction);
document.getElementById("create-song").
addEventListener("click", createSongAction);
document.getElementById("create-playlist").
addEventListener("click", createPlaylistAction);
// create items on Enter
document.getElementById("artist-name").
addEventListener("keydown", function(event) {
if (event.keyCode === 13) {
event.preventDefault();
createArtistAction();
}
});
document.getElementById("song-name").
addEventListener("keydown", function(event) {
if (event.keyCode === 13) {
event.preventDefault();
createSongAction();
}
});
document.getElementById("playlist-name").
addEventListener("keydown", function(event) {
if (event.keyCode === 13) {
event.preventDefault();
createPlaylistAction();
}
});
// create arrays for artist, songs and playlists created
let artists = [];
let songs = [];
let playlists = [];
// error handling
function createArtistAction() {
const artistName = document.getElementById("artist-name").value;
let artist;
try {
artist = createArtist(artistName);
} catch (e) {
alert(e);
return;
}
artists.push(artist);
createArtistList(artist);
console.log(artists);
}
function createSongAction() {
const songName = document.getElementById("song-name").value;
const artistId = document.getElementById("select-artist").value;
let song;
try {
song = createSong(songName, artistId);
} catch (e) {
alert(e);
return;
}
songs.push(song);
createSongList(song);
console.log(songs);
}
function createPlaylistAction() {
const playlistName = document.getElementById("playlist-name").value;
const checkedSongs = document.querySelectorAll('input[type=checkbox]:checked');
let playlist;
try {
playlist = createPlaylist(playlistName, checkedSongs);
} catch (e) {
alert(e);
return;
}
playlists.push(playlist);
const tbody = document.getElementById("tbody");
createPlaylistList(tbody, playlist);
console.log(playlists);
}
// push created artist in array and add it as an option for song creation
function createArtist(artistName) {
artistName = artistName.trim();
if (artistName.length < 2) {
throw "Artist name must be at least 2 characters long";
}
for (const artist of artists) {
if (artist.name === artistName) {
throw "Artist already exists";
}
}
return new Artist(artistName);
}
// create artist dropdown list
function createArtistList(artist) {
const artistAdded = document.createElement("option");
artistAdded.value = artist.id;
const artistOption = document.createTextNode(artist.name);
const artistList = document.getElementById("select-artist");
artistAdded.appendChild(artistOption);
artistList.appendChild(artistAdded);
}
// push created song in array
function createSong(songName, artistId) {
songName = songName.trim();
if (songName === "") {
throw "Song title must be at least 1 character long";
}
if (artistId === "") {
throw "Please select/insert an artist first";
}
// get selected artist
let artist;
for (let i = 0; i < artists.length; i++) {
if (artists[i].id === Number(artistId)) {
artist = artists[i];
break;
}
}
if (artist === undefined) {
throw "Artist not found";
}
// verify duplicate songs
for (const song of songs) {
if (song.name === songName && song.artist.id === artist.id) {
throw "Song already exists";
}
}
return new Song(songName, artist);
}
// add song as checkbox & label in the playlist form
function createSongList(song) {
const songCheckbox = document.createElement("input");
songCheckbox.type = "checkbox";
songCheckbox.value = song.id;
songCheckbox.name = song.artist.name + " - " + song.name;
songCheckbox.id = "song-" + song.id;
const songLabel = document.createElement("label");
songLabel.setAttribute("for", songCheckbox.id);
songLabel.innerHTML = song.artist.name + " - " + song.name + "<br>";
const songList = document.getElementById("select-song");
songList.appendChild(songCheckbox);
songList.appendChild(songLabel);
}
// push created playlist in array
function createPlaylist(playlistName, checkedSongs) {
playlistName = playlistName.trim();
if (playlistName.length < 2) {
throw "Playlist title must be at least 2 characters long";
}
if (checkedSongs.length === 0) {
throw "You must select/insert at least 1 song";
}
let selectedSongs = [];
for (let i = 0; i < checkedSongs.length; i++) {
for (song of songs) {
if (song.id === Number(checkedSongs[i].value)) {
selectedSongs.push(song);
}
}
}
return new Playlist(playlistName, selectedSongs);
}
// create table with playlists
function createPlaylistList(tbody, playlist) {
// insert playlist in table
const row = tbody.insertRow();
const cId = row.insertCell();
const cName = row.insertCell();
const cSongs = row.insertCell();
const cDelete = row.insertCell();
const ctextId = document.createTextNode(playlist.id);
const ctextName = document.createTextNode(playlist.name);
const cButtonDelete = document.createElement("button");
cId.appendChild(ctextId);
cName.appendChild(ctextName);
// create song artist and title
for (let i = 0; i < playlist.songs.length; i++) {
const ctextSongs = document.createTextNode(playlist.songs[i].artist.name +
" - " + playlist.songs[i].name);
const br = document.createElement("br");
cSongs.appendChild(ctextSongs);
cSongs.appendChild(br);
}
// create delete button
cButtonDelete.innerHTML = "X";
cButtonDelete.setAttribute("attr-playlist-id", playlist.id);
cButtonDelete.addEventListener("click", deletePlaylist);
cDelete.appendChild(cButtonDelete);
}
// delete playlist
function deletePlaylist(button) {
// delete matching row
const row = button.toElement.parentElement.parentElement;
row.parentElement.removeChild(row);
// delete playlist from playlists
const playlistId = Number(button.target.getAttribute("attr-playlist-id"));
for (let index in playlists) {
if (playlistId === playlists[index].id) {
playlists.splice(index, 1);
break;
}
}
console.log(playlists);
}
</script>
</body>
</html>