-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
543 lines (436 loc) · 16.4 KB
/
main.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
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
/* -------------------------------------------------------------------------- */
/* Dependency */
/* -------------------------------------------------------------------------- */
// import { LoremIpsum } from "lorem-ipsum";
// import fs from 'fs';
// const fs = require('fs')
// import marked from "marked";
/* -------------------------------------------------------------------------- */
/* define an object structure */
/* -------------------------------------------------------------------------- */
// Define html element references
// `{}` => Object Initializer로 여기서는 PAGES라는 객체를 생성함.
// 여기서 각 오브젝트 프로퍼티는 해당 콘텐츠를 insert할 공간(요소)의 id를 담는다.
PAGES = {};
PAGES.loading = {};
PAGES.loading.page = document.querySelector(".loader");
// Main page - Readme
PAGES.readme = {};
PAGES.readme.page = document.querySelector("#readme");
PAGES.readme.content = document.querySelector("#readme-page");
PAGES.readme.title = document.querySelector("#readme-head");
PAGES.readme.article = document.querySelector("#readme-content");
// Main page (Grid of articles)
PAGES.articles = {};
PAGES.articles.page = document.querySelector("#articleList");
PAGES.articles.grid = document.querySelector("#grid-blog");
PAGES.articles.content = document.querySelector("#grid-content-page");
PAGES.articles.title = document.querySelector("#article-head");
PAGES.articles.article = document.querySelector("#article-content");
PAGES.articles.cards = document.getElementsByClassName("card");
PAGES.articles.fetched;
PAGES.articles.images = document.querySelectorAll('img') ;
// Some other page
PAGES.portfolio = {};
PAGES.portfolio.page = document.querySelector("#portfolioPage");
PAGES.portfolio.content = document.querySelector("#tollstations");
PAGES.travel = {};
PAGES.travel.page = document.querySelector("#travel");;
PAGES.travel.cards = document.querySelector("#travel-cards");;
PAGES.tasks = {};
PAGES.tasks.page = document.querySelector("#tasks");
PAGES.tasks.cards = document.querySelector("#tasks-cards");
PAGES.about = {};
PAGES.about.page = document.querySelector("#about");
PAGES.about.profile = document.querySelector("#profile");
PAGES.about.picname = document.querySelector("#pic-name");
PAGES.about.bio = document.querySelector("#biography");
// 404 error page
PAGES.page404 = {};
PAGES.page404.page = document.querySelector("#page404");
PAGES.page404.error = document.querySelector("#page404-error");
// practice
PAGES.practice = {};
PAGES.practice.page = document.querySelector("#practice");
PAGES.practice.practice = document.querySelector("#markdownprac");
// PAGES.practice.content = document.querySelector("#");
/* -------------------------------------------------------------------------- */
/* Custom code for each page */
/* -------------------------------------------------------------------------- */
// Code to run for each page -> 각 페이지(PAGES 프로퍼티) 마다 고유의 추가 컨텐츠를 가지며, 이를 로딩하는 '코드'들을 담는 오브젝트를 선언한다.
pageFunctions = {};
// Custom code to run when showing the 404 page
pageFunctions.page404 = function () {
PAGES.page404.error.innerHTML = `Page ${location.hash.substr(1)} not found!`;
};
/* ------------------------- Adding dynamic content ------------------------- */
// To add some dynamic content to the page, we create a new custom function for the page "toll" and add a REST call.
// fetch를 이용해 데이터를 불러온 후,tollInfo()를 실행한다.
pageFunctions.readme = function () {
// fetch("https://602f9901a1e9d20017af097d.mockapi.io/WADD/v1/articles")
fetch(`./src/md/readme.md`).then(function (response) {
// The API call was successful!
return response.text();
}).then(function (html) {
// This is the HTML from our response as a text string
// console.log(content.title);
PAGES.readme.title.innerHTML = marked(html);
}).catch(function (err) {
// There was an error
console.warn('Something went wrong.', err);
});
// hideLoader();
}
pageFunctions.articles = function () {
// fetch("https://602f9901a1e9d20017af097d.mockapi.io/WADD/v1/articles")
// document.querySelector('#backBtArticle').classList.add('blur');
fetch("./src/articles.json")
.then(response => response.json())
.then(json => {
console.log(json);
PAGES.articles.fetched = json;
PAGES.articles.grid.innerHTML = json.reduce((acc, articles) => {
return (acc += createArticles(articles));
}, "");
// const article_list = json.map((article) => {
// return (PAGES.articles.grid.appendChild(createArticles(articles)));
// }, "");
// PAGES.articles.grid.appendChild(article_list)
// hideLoader();
});
}
function createArticles(article) { // HTML을 생성하는 함수
let rand = getRandomInt(6);
let rand2 = getRandomInt(100000);
if (rand < 4) {
return `
<div class="card" style="
background-image: url('https://source.unsplash.com/random?${article.about}?sig=${rand2}');
" id="${article.id}" onclick="open_article(this.id)">
<span class="card-title">${article.title}</span>
</div>
`;
} else if (rand == 4) {
return `
<div class="card card-tall" style="
background-image: url('https://source.unsplash.com/random?${article.about}?sig=${rand2}');
" id="${article.id}" onclick="open_article(this.id)">
<span class="card-title">${article.title}</span>
</div>
`;
} else {
return `
<div class="card card-tall card-wide" style="
background-image: url('https://source.unsplash.com/random?${article.about}?sig=${rand2}');
" id="${article.id}" onclick="open_article(this.id)">
<span class="card-title"">${article.title}</span>
</div>
`;
}
}
function backToArticles() {
console.log("Back to Article List...");
document.querySelector('#articles-title').classList.remove('blur');
document.querySelector('#backBtArticle').classList.add('blur');
PAGES.articles.grid.classList.remove("blur");
PAGES.articles.title.classList.add("blur");
PAGES.articles.article.classList.add("blur");
document.body.scrollTop = 0;
document.documentElement.scrollTop = 0;
// window.scrollTo(0,0);
}
function open_article(id) {
console.log("Fetching Article...");
PAGES.articles.title.classList.remove("blur");
PAGES.articles.article.classList.remove("blur");
document.querySelector('#backBtArticle').classList.remove('blur');
document.querySelector('#articles-title').classList.add('blur');
PAGES.articles.grid.classList.add("blur");
let content = PAGES.articles.fetched[id - 1];
PAGES.articles.title.innerHTML = content.title;
// PAGES.artic
fetch(`./src/md/${content.title}.md`).then(function (response) {
// The API call was successful!
return response.text();
}).then(function (html) {
// This is the HTML from our response as a text string
console.log(content.title);
PAGES.articles.title.innerHTML =
marked(html);
}).catch(function (err) {
// There was an error
console.warn('Something went wrong.', err);
});
document.body.scrollTop = 0;
document.documentElement.scrollTop = 0;
}
function getRandomInt(max) {
return Math.floor(Math.random() * Math.floor(max));
}
pageFunctions.travel = function () {
console.log("travel page..")
fetch("https://602f9901a1e9d20017af097d.mockapi.io/WADD/v1/travel")
.then(response => response.json())
.then(json => {
PAGES.travel.cards.innerHTML = json.reduce((acc, place) => {
return (acc += createTravelCard(place));
}, "");
// hideLoader();
});
}
function createTravelCard(location) {
return `<div class="travelcard travelcardAnimation">
<div class="card-inner">
<div class="card-front">
<img src="https://source.unsplash.com/random?${location.place}" alt="" />
</div>
<div class="card-back">
<h1>${location.place}</h1>
<ul>
<li>
<strong>Date:</strong> ${location.date}
</li>
<li>
<strong>Accompanided with:</strong> ${location.accompany}
</li>
<li>
<strong>Comment:</strong> ${location.review}
</li>
</ul>
</div>
</div>
</div>
`
}
/* ----------------------------- Task Management ---------------------------- */
let taskInput = document.getElementById("new-task");
let addButton = document.getElementsByTagName("button")[0];
let incompleteTasksHolder = document.getElementById("incomplete-tasks");
let completedTasksHolder = document.getElementById("completed-tasks");
pageFunctions.tasks = function () {
console.log("task page..")
let count_done = 0;
let count_undone = 0;
var values = {},
keys = Object.keys(localStorage),
i = keys.length;
while (i--) {
values[keys[i]] = localStorage.getItem(keys);
console.log(values[keys[i]]);
(values[keys[i]] ? count_done++ : count_undone++)
}
// console.log(count_done);
// console.log(count_undone);
// console.log(values);
for (const value in values) {
// if (Object.hasOwnProperty.call(object, value)) {
// const element = object[value];
// }
console.log(localStorage.getItem(value));
const newTask = createNewTask(value, localStorage.getItem(value));
if (localStorage.getItem(value) == 0) {
incompleteTasksHolder.innerHTML += newTask;
} else {
completedTasksHolder.innerHTML += newTask;
}
}
// hideLoader();
}
//New Task List Item
function createNewTask(value, flag) {
if (flag == 0) {
return `<li class="travelcard">
<div class="task-head">
<input type="checkbox" onchange="checkBoxHandler(this)">
<label>${value}</label>
</div>
<input type="text">
<div class="task-option">
<button class="edit" onclick="editTask(this)">Edit</button>
<button class="delete" onclick="deleteTask(this)">Delete</button>
</div>
</li>`
} else {
return `<li class="travelcard">
<div class="task-head">
<input type="checkbox" onchange="checkBoxHandler(this)" checked>
<label>${value}</label>
</div>
<input type="text">
<div class="task-option">
<button class="edit" onclick="editTask(this)">Edit</button>
<button class="delete" onclick="deleteTask(this)">Delete</button>
</div>
</li>`
}
}
// function addTask
addTask = () => {
console.log("Add task...");
//Create a new list item with the text from #new-task:
const newTask = createNewTask(taskInput.value, 0);
//Append listItem to incompleteTasksHolder
incompleteTasksHolder.innerHTML += newTask;
// bindTaskEvents(listItem, taskCompleted);
// let temp = JSON.stringify(newTask.innerHTML);
// arr = [taskInput.value, 0];
// console.log(arr);
localStorage.setItem(`${taskInput.value}`, 0);
// console.log( localStorage.getItem(`${taskInput.value}`));
taskInput.value = "";
}
// Edit an existing task
editTask = (obj) => {
console.log("Edit Task...");
let listItem = obj.parentNode.parentNode;
let editInput = listItem.querySelector("input[type=text]")
let label = listItem.querySelector("label");
//if the class of the parent is .editMode
if (listItem.classList.contains("editMode")) {
//switch from .editMode
//Make label text become the input's value
label.innerText = editInput.value;
} else {
//Switch to .editMode
//input value becomes the label's text
editInput.value = label.innerText;
}
// Toggle .editMode on the parent
listItem.classList.toggle("editMode");
}
// Delete an existing task
deleteTask = (obj) => {
console.log("Delete task...");
let listItem = obj.parentNode.parentNode;
let ul = listItem.parentNode;
//Remove the parent list item from the ul
ul.removeChild(listItem);
let temp = listItem.querySelector("label").innerHTML;
// console.log(temp);
console.log(localStorage.getItem(`${temp}`));
localStorage.removeItem(`${temp}`);
}
// Mark a task as complete
taskCompleted = (obj) => {
console.log("Task complete...");
//Append the task list item to the #completed-tasks
let listItem = obj.parentNode.parentNode;
completedTasksHolder.appendChild(listItem);
listItem.classList.add("checked");
let temp = listItem.querySelector("label").innerHTML;
console.log("complete: ", temp);
// console.log("complete: ", localStorage.getItem(`${temp}`));
// arr = [temp, 1];
// console.log(arr);
localStorage.setItem(`${temp}`, JSON.stringify(1));
// console.log( localStorage.getItem(`${taskInput.value}`));
}
// Mark a task as incomplete
taskIncomplete = (obj) => {
console.log("Task Incomplete...");
// When checkbox is unchecked
// Append the task list item #incomplete-tasks
let listItem = obj.parentNode.parentNode;
let temp = listItem.querySelector("label").innerHTML;
// arr = [temp, 0];
localStorage.setItem(`${temp}`, JSON.stringify(0));
incompleteTasksHolder.appendChild(listItem);
listItem.classList.remove("checked");
}
checkBoxHandler = (obj) => {
if (obj.checked) {
taskCompleted(obj);
} else {
taskIncomplete(obj);
}
}
/* -------------------------------- practice -------------------------------- */
pageFunctions.portfolio = function () {
fetch("./src/projects.json")
.then(response => response.json())
.then(json => {
console.log(json);
PAGES.portfolio.content.innerHTML = json.reduce((acc, toll) => {
return (acc += tollInfo(toll));
}, "");
});
};
function tollInfo(toll) { // HTML을 생성하는 함수
return `
<div class="toll tollanimation">
<h2>${toll.title}</h2>
<h4 id="">About: ${toll.about}</h4>
<div id="p_a">
<p>${toll.text}</p>
<a href="${toll.link}">Link</a>
</div>
</div>
`;
}
function delayElems(item, index) {
console.log(item);
item.style.animationDelay = 0.31*(1+index) +'s'
// item.sheet.insertRule('# {color: darkseagreen}');
}
document.querySelectorAll("#tollStations.toll").forEach(delayElems)
console.log(document.querySelectorAll("#tollStations.toll"));
// document.querySelectorAll() fruits.forEach(myFunction);
/* -------------------------------------------------------------------------- */
/* Navigation */
/* -------------------------------------------------------------------------- */
var path;
function unhideLoader() {
document.querySelector('.loader-wrapper').classList.remove('blur')
// document.querySelector('.loader-wrapper').style.zIndex = 5
}
function hideLoader() {
document.querySelector('.loader-wrapper').classList.add('blur')
document.querySelector('.loader-wrapper').style.zIndex = 0
}
function name(params) {
}
// Navigation
function navigate() {
path = location.hash
.substr(1)
.toLowerCase()
.split("/");
// Find what page to show
var currentPage = path[0];
if (!PAGES.hasOwnProperty(currentPage)) {
if (path[0] === "") {
currentPage = "readme";
} else {
currentPage = "page404";
}
}
// Hide the previous active page
for (var page in PAGES) {
if (PAGES.hasOwnProperty(page)) {
PAGES[page].page.classList.remove("active");
}
}
// Show the active page and run its custom script
PAGES[currentPage].page.classList.add("active");
// currentPage와 동일한 이름을 가지는 코드가 pageFunctions에 있는 경우 이를 실행한다.
if (pageFunctions.hasOwnProperty(currentPage)) {
pageFunctions[currentPage]();
}
hideLoader();
}
/* -------------------------------------------------------------------------- */
/* Main Execution */
/* -------------------------------------------------------------------------- */
window.setTimeout(function () {
hideLoader()
}, 1500);
// First time loading the page
navigate();
// By using the function window.onhashchange, we can detect when the hash in the URL changes. Combining this with anchors, and we have a way detect when we should changing the content of the SPA.
window.addEventListener("hashchange", function () {
// unhideLoader();
navigate();
});
window.onbeforeunload = function() { window.scrollTo(0,0); }
// 페이지가 로딩될때 생성된 PAGES를 출력한다.
console.log(PAGES);