-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
423 lines (386 loc) · 22.8 KB
/
index.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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<title>Football Apps</title>
<meta name="description" content="Football Apps"/>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="manifest" href="/manifest.json">
<meta name="theme-color" content="#00897B"/>
<link rel="stylesheet" href="/assets/css/materialize.min.css">
<link rel="favicon" href="/assets/image/icon.png">
</head>
<body>
<!-- COMPONENT SECTION -->
<div id="nav"></div>
<!-- BODY SECTION -->
<div id="content"></div>
<script src="/assets/js/materialize.min.js"></script>
<script src="/assets/js/idb.js"></script>
<script src="/assets/js/component.js"></script>
<script>
// REGISTER SERVICE WORKER
if ("serviceWorker" in navigator) {
window.addEventListener("load", function() {
navigator.serviceWorker
.register("/service-worker.js")
.then(function() {
console.log("Pendaftaran ServiceWorker berhasil");
})
.catch(function() {
console.log("Pendaftaran ServiceWorker gagal");
});
});
// NOTIFICATION
if ('Notification' in window) {
Notification.requestPermission().then(function (result) {
if (result === "denied") {
console.log("Fitur notifikasi tidak diijinkan.");
return;
} else if (result === "default") {
console.error("Pengguna menutup kotak dialog permintaan ijin.");
return;
}
if (('PushManager' in window)) {
navigator.serviceWorker.getRegistration().then(function(reg) {
reg.pushManager.subscribe({
userVisibleOnly: true
}).then(function(sub) {
console.log('Berhasil melakukan subscribe dengan endpoint: ', sub.endpoint);
}).catch(function(e) {
console.error('Tidak dapat melakukan subscribe ', e);
});
});
}
});
}
} else {
console.log("ServiceWorker belum didukung browser ini.");
}
let url = window.location.search;
var dbIndex = idb.open("footballApps", 1, function(upgradeDB) {
if (!upgradeDB.objectStoreNames.contains("footballFavorite")) {
var footballFavorite = upgradeDB.createObjectStore("footballFavorite", {keyPath: "id"});
// footballFavorite.createIndex("name", "name", {unique: false});
// footballFavorite.createIndex("description", "description", {unique: false});
// footballFavorite.createIndex("logo", "logo", {unique: false});
}
if (!upgradeDB.objectStoreNames.contains("instalasiFavorite")) {
var footballFavorite = upgradeDB.createObjectStore("instalasiFavorite", {keyPath: "id"});
// footballFavorite.createIndex("name", "name", {unique: false});
// footballFavorite.createIndex("description", "description", {unique: false});
// footballFavorite.createIndex("logo", "logo", {unique: false});
}
})
document.addEventListener("DOMContentLoaded", function() {
// Call Sidebar Component
let nav = document.querySelector("#nav"),
body = document.querySelector("#content")
ajax.get("/component/navigation.html", function(res) {
nav.innerHTML = res
// Set Item Menu
document.querySelectorAll(".topnav, .sidenav").forEach(function(elm, key) {
db.nav.data.forEach(function(item) {
elm.appendChild(db.nav.template(item))
})
// Set Mobile Navbar Effect
if(key == 1) {
M.Sidenav.init(elm);
}
});
// Default Pages
if (url == "") {
ajax.get("/pages/klasemen.html", function(resPage) {
body.innerHTML = resPage
if (navigator.onLine) {
klasemenHandler()
}
else {
instalasiFavorite()
showFavoriteTeam()
}
})
}
else {
let pages = url.substr(1);
pages = pages.split("&");
pages = pages[0].split("=");
pages = pages[1];
// Call Page
ajax.get("/pages/" + pages + ".html", function(resPage) {
body.innerHTML = resPage
if (navigator.onLine) {
if (pages == "klasemen") {
klasemenHandler()
}
else if(pages == "team") {
showTeam()
}
else if(pages == "detail") {
showDetail()
}
else if(pages == "favorite") {
instalasiFavorite()
showFavoriteTeam();
}
}
else {
if(pages == "detail") {
showDetail()
}
else {
instalasiFavorite()
showFavoriteTeam();
}
}
})
}
// Set Action For Item Menu
document.querySelectorAll(".sidenav a, .topnav a").forEach(function(elm) {
elm.addEventListener("click", function(event) {
// Close Navbar
var sidenav = document.querySelector(".sidenav");
M.Sidenav.getInstance(sidenav).close();
});
});
})
})
function klasemenHandler() {
ajax.getData({
url: "https://www.thesportsdb.com/api/v1/json/1/all_leagues.php",
headers: []
}, function(res) {
try {
res = JSON.parse(res);
res = res.leagues;
document.querySelector("#items").innerHTML = `${res.map((item) =>
`<div class="col s12 m6" style="cursor: pointer" onclick='location.assign("?p=team&l=${item.strLeague}")'>
<div class="card-panel teal">
<span class="white-text" style="font-size: 18px;">${item.strLeague}</span>
</div>
</div>`
).join('')}`
} catch (error) {
console.log("Error", error);
}
});
}
function showTeam() {
let name = url.substr(1);
name = name.split("&");
name = name[1].split("=");
name = name[1].split("%20").join(" ");
document.querySelector("#items-title").innerHTML = name;
ajax.getData({
url: "https://www.thesportsdb.com/api/v1/json/1/search_all_teams.php?l=" + name,
headers: []
}, function(res) {
try {
res = JSON.parse(res);
res = res.teams;
document.querySelector("#items").innerHTML = `${res.map((item) =>
`<div class="col s12 m6">
<div class="card">
<div class="card-image" style="height: 180px">
<img src="${item.strTeamLogo}">
<span class="card-title">${item.strTeam}</span>
</div>
<div class="card-content">
<p>${item.strDescriptionEN.length > 100 ? item.strDescriptionEN.substring(0, 100): item.strDescriptionEN}...</p>
</div>
<div class="card-action">
<a href="?p=detail&id=${item.idTeam}">Selengkapnya</a>
</div>
</div>
</div>`
).join('')}`
} catch(error) {
console.log("Error", error);
}
})
}
function showDetail() {
let name = url.substr(1);
name = name.split("&");
name = name[1].split("=");
name = name[1];
dbIndex.then(function(database) {
var tx = database.transaction(['footballFavorite'], 'readonly');
var store = tx.objectStore('footballFavorite');
return store.get(parseInt(name))
}).then(function(val) {
if (typeof val == "undefined") {
ajax.getData({
url: "https://www.thesportsdb.com/api/v1/json/1/lookupteam.php?id=" + name,
headers: []
}, function(res) {
try {
res = JSON.parse(res);
res = res.teams;
document.querySelector("#items-title").innerHTML = res[0].strTeam;
document.querySelector("#items").innerHTML = `${res.map((item) =>
`<div class="col s12 m5">
<div class="card">
<div class="card-image" style="height: 180px">
<img src="${item.strTeamLogo}">
<span class="card-title">${item.strTeam}</span>
</div>
<div class="card-action">
<button type="button" onclick="addToFavorite(${item.idTeam})" id="favorite">Add To Favorite</a>
</div>
</div>
</div>
<div class="col s12 m7">
<div class="card">
<div class="card-content">
<span class="card-title">${item.strTeam}</span>
<p>${item.strDescriptionEN}.</p>
</div>
</div>
</div>`
).join('')}`
} catch(error) {
console.log("Error", error);
}
})
}
else {
document.querySelector("#items-title").innerHTML = val.nama;
document.querySelector("#items").innerHTML = `
<div class="col s12 m5">
<div class="card">
<div class="card-image" style="height: 180px">
<img src="${val.logo}">
<span class="card-title">${val.nama}</span>
</div>
<div class="card-action">
<button type="button" onclick="removeFromFavorite(${val.id})" id="favorite">Remove From Favorite</a>
</div>
</div>
</div>
<div class="col s12 m7">
<div class="card">
<div class="card-content">
<span class="card-title">${val.nama}</span>
<p>${val.description}.</p>
</div>
</div>
</div>`
}
})
}
function instalasiFavorite() {
dbIndex.then(function(database) {
var tx = database.transaction(['instalasiFavorite'], 'readwrite');
var store = tx.objectStore('instalasiFavorite');
return store.get(parseInt(0))
}).then(function(val) {
if (typeof val == "undefined") {
dbIndex.then(function(database) {
var tx = database.transaction(['instalasiFavorite'], 'readwrite');
var store = tx.objectStore('instalasiFavorite');
var data = {
id: 0
}
store.add(data)
return store.complete;
}).then(function() {
var data = [{
'id': 133601,
'nama': "Aston Villa",
'description': `Aston Villa Football Club (/ˈæstən ˈvɪllə/; also known as Villa, The Villa, The Villans, The Lions) are a professional football club based in Witton, Birmingham, who play in the Premier League, the highest level of English football. Founded in 1874, they have played at their current home ground, Villa Park, since 1897. Aston Villa were founder members of The Football League in 1888. They were also founder members of the Premier League in 1992, and have remained there ever since. The club were floated by the previous owner and chairman Doug Ellis, but in 2006 full control was acquired by American businessman Randy Lerner. Lerner announced on 12 May 2014 that he is selling the club. Aston Villa are among the oldest and the most successful football clubs in the history of English football. Villa won the 1981–82 European Cup, and are thus one of five English clubs to win what is now the UEFA Champions League. They have the fourth highest total of major honours won by an English club, having won the First Division Championship seven times (most recently in the 1980–81 season), the FA Cup seven times (last won in 1957), the Football League Cup five times (last won in 1996) and the UEFA Super Cup in 1982. The club have also produced more England national team players than any other side, currently having produced 72. They have a fierce local rivalry with Birmingham City. The Second City derby between Aston Villa and Birmingham City has been played since 1879. The club's traditional kit colours are claret shirts with sky blue sleeves, white shorts and sky blue socks. Their traditional badge is of a rampant gold lion on a light blue background with the club's motto "Prepared" underneath; a modified version of this was adopted in 2007.`,
'logo': "https://www.thesportsdb.com/images/media/team/logo/wtsxus1424375522.png"
},{
'id': 133604,
'nama': "Arsenal",
'description': `"Arsenal Football Club is a professional football club based in Holloway, London which currently plays in the Premier League, the highest level of English football. One of the most successful clubs in English football, they have won 13 First Division and Premier League titles and a joint record 11 FA Cups. Arsenal's success has been particularly consistent: the club has accumulated the second most points in English top-flight football, hold the ongoing record for the longest uninterrupted period in the top flight, and would be placed first in an aggregated league of the entire 20th century. Arsenal is the second side to complete an English top-flight season unbeaten (in the 2003–04 season), playing almost twice as many matches as the previous invincibles Preston North End in the 1888–89 season. Arsenal was founded in 1886 in Woolwich and in 1893 became the first club from the south of England to join the Football League. In 1913, they moved north across the city to Arsenal Stadium in Highbury. In the 1930s, they won five League Championship titles and two FA Cups. After a lean period in the post-war years they won the League and FA Cup Double, in the 1970–71 season, and in the 1990s and first decade of the 21st century, won two more Doubles and reached the 2006 UEFA Champions League Final. Since neighbouring Tottenham Hotspur, the two clubs have had a fierce rivalry, the North London derby. Arsenal have one of the highest incomes and largest fanbases in the world. The club was named the fifth most valuable association football club in the world, valued at £1.3 billion in 2014."`,
'logo': "https://www.thesportsdb.com/images/media/team/logo/q2mxlz1512644512.png"
}]
data.forEach(function(item, key){
dbIndex.then(function(database) {
var tx = database.transaction(['footballFavorite'], 'readwrite');
var store = tx.objectStore('footballFavorite');
let id = parseInt(item.id);
store.add(item)
return tx.complete;
}).then(function() {
if (key == 1) {
location.assign(location.href)
}
})
})
})
}
}).catch(function() {
console.log('Tim gagal disimpan.')
})
}
function showFavoriteTeam() {
document.querySelector("#items-title").innerHTML = "Favorite Team";
dbIndex.then(function(database) {
var tx = database.transaction(['footballFavorite'], 'readonly');
var store = tx.objectStore('footballFavorite');
return store.getAll()
}).then(function(val) {
console.log(val)
document.querySelector("#items").innerHTML = `${val.map((item) =>
`<div class="col s12 m6">
<div class="card">
<div class="card-image" style="height: 180px">
<img src="${item.logo}">
<span class="card-title">${item.nama}</span>
</div>
<div class="card-content">
<p>${item.description.length > 100 ? item.description.substring(0, 100): item.description}...</p>
</div>
<div class="card-action">
<a href="?p=detail&id=${item.id}">Selengkapnya</a>
</div>
</div>
</div>`
).join('')}`
})
}
function removeFromFavorite(item) {
dbIndex.then(function(database) {
var tx = database.transaction(['footballFavorite'], 'readwrite');
var store = tx.objectStore('footballFavorite');
store.delete(item)
return store.complete
}).then(function() {
location.assign(location.href)
});
}
function addToFavorite(item) {
ajax.getData({
url: "https://www.thesportsdb.com/api/v1/json/1/lookupteam.php?id=" + item,
headers: []
}, function(res) {
try {
res = JSON.parse(res);
res = res.teams[0];
dbIndex.then(function(database) {
var tx = database.transaction(['footballFavorite'], 'readwrite');
var store = tx.objectStore('footballFavorite');
let id = parseInt(res.idTeam);
var data = {
'id': id,
'nama': res.strTeam,
'description': res.strDescriptionEN,
'logo': res.strTeamLogo
}
store.add(data)
return tx.complete;
}).then(function() {
location.assign(location.href)
}).catch(function() {
console.log('Tim gagal disimpan.')
})
}catch(error) {
console.log("Error", error);
}
})
}
</script>
</body>
</html>