Skip to content

Commit 28d9327

Browse files
authored
Merge pull request #3 from pouyio/gh-pages
day 15
2 parents 98f3b38 + c2addf3 commit 28d9327

File tree

5 files changed

+130
-2
lines changed

5 files changed

+130
-2
lines changed

15-localStorage/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Object and Arrays - Reference VS Copy
2+
Watch this challenge live in [codepen](https://codepen.io/pouyio/pen/wegbpP/).

15-localStorage/app.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
window.onload = () => {
2+
3+
const addCloud = document.querySelector('.add-clouds');
4+
const clearClouds = document.querySelector('.clear-clouds');
5+
const cloudList = document.querySelector('.clouds');
6+
let clouds = JSON.parse(localStorage.getItem('clouds')) || [];
7+
8+
function addCloudEvent(e) {
9+
e.preventDefault();
10+
const text = (this.querySelector('[name=cloud]')).value;
11+
const cloud = {
12+
text,
13+
done: false
14+
}
15+
clouds.push(cloud);
16+
populateList(clouds, cloudList);
17+
localStorage.setItem('clouds', JSON.stringify(clouds));
18+
this.reset();
19+
}
20+
21+
function populateList(clouds = [], cloudList) {
22+
cloudList.innerHTML = clouds.map((cloud, i) => {
23+
return `<li>
24+
<input type="checkbox" id="item${i}" data-id="${i}" ${cloud.done? 'checked': ''}></input>
25+
<label for="item${i}">${cloud.text}</label>
26+
</li>`
27+
}).join('');
28+
}
29+
30+
function toggle(e) {
31+
if(!e.target.matches('input')) return;
32+
const el = e.target;
33+
const index = el.dataset.id;
34+
clouds[index].done = !clouds[index].done;
35+
localStorage.setItem('clouds', JSON.stringify(clouds));
36+
populateList(clouds, cloudList);
37+
}
38+
39+
function clearCloudsEvent() {
40+
localStorage.clear();
41+
clouds = [];
42+
populateList(clouds, cloudList);
43+
}
44+
45+
addCloud.addEventListener('submit', addCloudEvent);
46+
cloudList.addEventListener('click', toggle);
47+
clearClouds.addEventListener('click', clearCloudsEvent);
48+
populateList(clouds, cloudList);
49+
}

15-localStorage/index.html

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>LocalStorage and Event delegation</title>
6+
<style media="screen">
7+
body{
8+
font-family: sans-serif;
9+
font-size: 1.8em;
10+
color: #383838;
11+
min-height: 100vh;
12+
margin: 0;
13+
padding: 1em;
14+
box-sizing: border-box;
15+
background-color: #e7e4e4;
16+
display: flex;
17+
flex-direction: column;
18+
justify-content: space-around;
19+
align-items: center;
20+
}
21+
</style>
22+
</head>
23+
<body>
24+
<ul class="clouds">
25+
<li>Loading...</li>
26+
</ul>
27+
<form class="add-clouds">
28+
<input type="text" name="cloud" placeholder="Cloud name">
29+
<input type="submit" value="Add cloud">
30+
<input class="clear-clouds" type="button" value="Clear clouds">
31+
</form>
32+
<code>And reload the page!</code>
33+
<script type="text/javascript" src="./app.js">
34+
</script>
35+
</body>
36+
</html>

15-localStorage/style.css

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
body {
2+
font-family: sans-serif;
3+
color: #383838;
4+
font-size: 1.5rem;
5+
min-height: 100vh;
6+
padding: 1em;
7+
box-sizing: border-box;
8+
background-color: #e7e4e4;
9+
display: flex;
10+
flex-direction: column;
11+
justify-content: center;
12+
align-items: center;
13+
max-width: 600px;
14+
margin: 0 auto;
15+
}
16+
17+
.content {
18+
text-align: justify;
19+
}
20+
21+
img {
22+
width: 220px;
23+
height: auto;
24+
padding: .8em;
25+
opacity: 0;
26+
transition: all .5s;
27+
}
28+
29+
.al-right {
30+
float: right;
31+
padding-right: 0;
32+
}
33+
34+
.al-left {
35+
float: left;
36+
padding-left: 0;
37+
}
38+
39+
.active {
40+
opacity: 1;
41+
}

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ My goal is simply to learn the basics by doing many simple things rather than st
1818
11. [x] [Custom Video Player](./11-custom-html5-video-player) - [Codepend](http://codepen.io/pouyio/full/bByVLB/)
1919
12. [x] [Key Sequence Detection](./12-key-sequence-detection)- [Codepen](http://codepen.io/pouyio/full/vywmwQ/)
2020
13. [x] [Slide in on Scroll](./13-slide-in-on-scroll) - [Codepen](http://codepen.io/pouyio/full/ENzdzv/)
21-
14. [x] [JavaScript References vs. Copying](./14-reference-copying)- [Codepen](https://codepen.io/pouyio/pen/wegbpP)
22-
15. [ ] LocalStorage
21+
14. [x] [JavaScript References vs. Copying](./14-reference-copying) - [Codepen](https://codepen.io/pouyio/pen/wegbpP)
22+
15. [x] [LocalStorage](./15-localStorage) - [Codepen](https://codepen.io/pouyio/full/XgRpOd/)
2323
16. [ ] Mouse Move Shadow
2424
17. [ ] Sort Without Articles
2525
18. [ ] Adding Up Times with Reduce

0 commit comments

Comments
 (0)