-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
49 lines (43 loc) · 1.46 KB
/
app.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
window.onload = () => {
const addCloud = document.querySelector('.add-clouds');
const clearClouds = document.querySelector('.clear-clouds');
const cloudList = document.querySelector('.clouds');
let clouds = JSON.parse(localStorage.getItem('clouds')) || [];
function addCloudEvent(e) {
e.preventDefault();
const text = (this.querySelector('[name=cloud]')).value;
const cloud = {
text,
done: false
}
clouds.push(cloud);
populateList(clouds, cloudList);
localStorage.setItem('clouds', JSON.stringify(clouds));
this.reset();
}
function populateList(clouds = [], cloudList) {
cloudList.innerHTML = clouds.map((cloud, i) => {
return `<li>
<input type="checkbox" id="item${i}" data-id="${i}" ${cloud.done? 'checked': ''}></input>
<label for="item${i}">${cloud.text}</label>
</li>`
}).join('');
}
function toggle(e) {
if(!e.target.matches('input')) return;
const el = e.target;
const index = el.dataset.id;
clouds[index].done = !clouds[index].done;
localStorage.setItem('clouds', JSON.stringify(clouds));
populateList(clouds, cloudList);
}
function clearCloudsEvent() {
localStorage.clear();
clouds = [];
populateList(clouds, cloudList);
}
addCloud.addEventListener('submit', addCloudEvent);
cloudList.addEventListener('click', toggle);
clearClouds.addEventListener('click', clearCloudsEvent);
populateList(clouds, cloudList);
}