-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy path22 - Getting Real Data with fetch.html
287 lines (238 loc) Β· 7.05 KB
/
22 - Getting Real Data with fetch.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Getting Started with JavaScript</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
<style>
body,
.jumbotron {
padding: 30px;
}
body {
background-image: url('img/meadow.jpeg');
background-size: cover;
}
.content {
font-size: 19px;
line-height: 1.5;
max-width: 500px;
margin: 0 auto;
position: relative;
border-radius: 5px;
}
.floater {
background: #FFF;
transition: 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.4) all;
margin-bottom: 30px;
z-index: 11;
}
.floater input[type=text] {
padding: 30px 20px;
border: none;
box-shadow: none;
font-size: 14px;
transition: 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.4) all;
}
.floater-bottom {
padding: 6px 10px 8px;
text-align: right;
border-top: 1px solid #DDD;
}
.floater-bottom button {
padding: 4px 8px;
font-size: 10px;
}
.overlay {
position: fixed;
top: 0;
left: 0;
background: rgba(0, 0, 0, 0.80);
width: 100%;
height: 100%;
opacity: 0;
transition: 0.3s ease opacity;
z-index: 10;
pointer-events: none;
}
body.show-floater .floater {
transform: scale(1.1);
}
body.show-floater .floater input[type=text] {
padding: 50px 20px;
font-size: 18px;
}
body.show-floater .overlay {
opacity: 1;
pointer-events: all;
}
/* ==================== */
.bookmarks-list {
position: relative;
z-index: 9;
}
.bookmark {
display: flex;
background: #FFF;
position: relative;
color: #999;
padding: 20px;
transition: 0.3s ease all;
border-bottom: 1px solid #DDD;
font-size: 16px;
}
.bookmark:hover {
color: #1CE;
text-decoration: none;
}
.bookmark:first-child {
border-top-left-radius: 5px;
border-top-right-radius: 5px;
}
.bookmark:last-child {
border-bottom-left-radius: 5px;
border-bottom-right-radius: 5px;
border-bottom: 0;
}
.bookmark .img {
background-color: #DDD;
border-radius: 3px;
background-image: url('https://pbs.twimg.com/profile_images/804421640465580032/aG7EyewO_400x400.jpg');
background-size: cover;
background-position: top center;
width: 120px;
height: 60px;
margin-right: 20px;
}
.bookmark .title {
display: flex;
align-items: center;
}
.bookmark .glyphicon-remove {
position: absolute;
top: 10px;
right: 10px;
color: #EEE;
font-size: 12px;
transition: 0.3s ease color;
cursor: pointer;
}
.bookmark:hover .glyphicon-remove {
color: #AAA;
}
</style>
</head>
<body>
<div class="overlay"></div>
<div class="floater content">
<form class="bookmark-form">
<div class="floater-top">
<input type="text" class="form-control" placeholder="What's on your mind?">
</div>
<div class="floater-bottom">
<button type="submit" class="btn btn-primary btn-sm">Add</button>
</div>
</form>
</div>
<div class="bookmarks-list content"></div>
<!-- π₯π₯π₯π₯ start javascript π₯π₯π₯π₯ -->
<script>
const body = document.body;
const input = document.querySelector('input[type=text]');
const overlay = document.querySelector('.overlay');
function showFloater() {
body.classList.add('show-floater');
}
function closeFloater() {
if (body.classList.contains('show-floater')) {
body.classList.remove('show-floater');
}
}
input.addEventListener('focusin', showFloater);
// input.addEventListener('focusout', closeFloater);
overlay.addEventListener('click', closeFloater);
// =========================
const bookmarksList = document.querySelector('.bookmarks-list');
const bookmarkForm = document.querySelector('.bookmark-form');
const bookmarkInput = bookmarkForm.querySelector('input[type=text]');
const bookmarks = JSON.parse(localStorage.getItem('bookmarks')) || [];
const apiUrl = 'https://opengraph.io/api/1.0/site';
const appId = '58858c7bcf07b61e64257391';
fillBookmarksList(bookmarks);
function createBookmark(e) {
e.preventDefault();
if (!bookmarkInput.value) {
alert('We need info!');
return;
}
const url = encodeURIComponent(bookmarkInput.value);
// add a new bookmark to the bookmarks
fetch(`${apiUrl}/${url}?app_id=${appId}`)
// fetch(apiUrl + '/' + url + '?app_id=' + appId)
.then(response => response.json())
.then(data => {
console.log(data);
const bookmark = {
title: data.hybridGraph.title,
image: data.hybridGraph.image,
link: data.hybridGraph.url
};
console.log(bookmark);
bookmarks.push(bookmark);
fillBookmarksList(bookmarks);
storeBookmarks(bookmarks);
bookmarkForm.reset();
})
.catch(error => {
alert('There was a problem getting info!');
});
// console.table(bookmarks);
// save that bookmarks list to localStorage
// const title = bookmarkInput.value;
// const bookmark = document.createElement('a');
// bookmark.className = 'bookmark';
// bookmark.innerText = title;
// bookmark.href = '#';
// bookmark.target = '_blank';
// bookmarksList.appendChild(bookmark);
}
function fillBookmarksList(bookmarks = []) {
const bookmarksHtml = bookmarks.map((bookmark, i) => {
return `
<a href="${bookmark.link}" class="bookmark" data-id="${i}">
<div class="img" style="background-image:url('${bookmark.image}')"></div>
<div class="title">${bookmark.title}</div>
<span class="glyphicon glyphicon-remove"></span>
</a>
`;
}).join('');
bookmarksList.innerHTML = bookmarksHtml;
// let bookmarksHtml = '';
// for (let i = 0; i < bookmarks.length; i++) {
// bookmarksHtml += `
// <a href="#" class="bookmark">
// ${bookmarks[i].title}
// </a>
// `;
// }
// console.log(bookmarksHtml);
}
function removeBookmark(e) {
if (!e.target.matches('.glyphicon-remove')) return;
// find the index
// remove from the bookmarks using splice
// fill the list
// store back to localStorage
const index = e.target.parentNode.dataset.id;
bookmarks.splice(index, 1);
fillBookmarksList(bookmarks);
storeBookmarks(bookmarks);
}
function storeBookmarks(bookmarks = []) {
localStorage.setItem('bookmarks', JSON.stringify(bookmarks));
}
bookmarkForm.addEventListener('submit', createBookmark);
bookmarksList.addEventListener('click', removeBookmark);
</script>
</body>
</html>