-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
132 lines (115 loc) · 3.93 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
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
const loginForm = document.querySelector("#login-form");
const greeting = document.querySelector("#greeting");
const clock = document.querySelector("#clock");
const goalForm = document.querySelector("#goal-form");
const goalContainer = goalForm.querySelector(".goal-container");
const goalBtn = goalContainer.querySelector(".btn");
const goalMenu = goalContainer.querySelector(".menu-box");
const todoContainer = document.querySelector(".todo");
const HIDDEN_CLASSNAME = "hidden";
const CLEAR_CLASSNAME = "clear";
let username = localStorage.getItem("username") || "";
let goal = localStorage.getItem("goal") || "";
function getClock() {
const date = new Date();
const hours = String(
date.getHours() > 12 ? date.getHours() - 12 : date.getHours()
).padStart(2, "0");
const minutes = String(date.getMinutes()).padStart(2, "0");
clock.textContent = `${hours}:${minutes}`;
}
function getGreeting() {
const ampm = document.querySelector(".ampm");
const hour = new Date().getHours();
let greetingText = "좋은 하루 보내세요,";
if (hour < 8) {
greetingText = "좋은 새벽이에요,";
} else if (hour < 12) {
greetingText = "좋은 아침이에요,";
} else if (hour < 18) {
greetingText = "좋은 오후에요,";
} else if (hour < 22) {
greetingText = "좋은 저녁이에요,";
} else {
greetingText = "좋은 밤 보내세요,";
}
greetingText += ` ${username}님.`;
greeting.innerText = greetingText;
if (hour > 12) {
ampm.innerText = "PM";
}
}
function editGoal(val) {
const input = goalForm.querySelector("input");
const asking = goalForm.querySelector("h5");
const goalText = goalForm.querySelector("h3");
asking.innerText = "새로운 목표는 어떤 것인가요?";
input.value = val;
input.classList.remove(HIDDEN_CLASSNAME);
goalText.classList.add(HIDDEN_CLASSNAME);
}
function setGoal(val) {
const input = goalForm.querySelector("input");
const asking = goalForm.querySelector("h5");
const goalText = goalForm.querySelector("h3");
asking.innerText = "TODAY";
goalText.innerText = `${val}`;
input.classList.add(HIDDEN_CLASSNAME);
goalText.classList.remove(HIDDEN_CLASSNAME);
}
function checkUsernameStatus() {
if (username) {
loginForm.classList.add(HIDDEN_CLASSNAME);
goalForm.classList.remove(HIDDEN_CLASSNAME);
getGreeting();
greeting.classList.remove(HIDDEN_CLASSNAME);
todoContainer.classList.remove(CLEAR_CLASSNAME);
}
if (goal) {
setGoal(goal);
}
}
getClock();
checkUsernameStatus();
setInterval(getClock, 1000);
setInterval(getGreeting, 3600000);
//eventListner에서 어떤 함수를 실행하든 간에, event에 대한 정보를 전달한다. preventDefault는 default인 행동을 막는 것이다.
loginForm.addEventListener("submit", (e) => {
e.preventDefault();
const loginInput = loginForm.querySelector("input");
username = loginInput.value;
loginForm.classList.add(HIDDEN_CLASSNAME);
greeting.classList.remove(HIDDEN_CLASSNAME);
localStorage.setItem("username", username);
checkUsernameStatus();
});
goalForm.addEventListener("submit", (e) => {
e.preventDefault();
const input = goalForm.querySelector("input");
setGoal(input.value);
localStorage.setItem("goal", input.value);
});
goalContainer.addEventListener("mouseenter", () => {
goalBtn.classList.remove(CLEAR_CLASSNAME);
});
goalContainer.addEventListener("mouseleave", () => {
goalBtn.classList.add(CLEAR_CLASSNAME);
goalMenu.classList.add(CLEAR_CLASSNAME);
});
goalBtn.addEventListener("click", (e) => {
e.preventDefault();
goalMenu.classList.remove(CLEAR_CLASSNAME);
});
goalMenu.addEventListener("click", (e) => {
console.log(e.target.classList);
if (e.target.classList.contains("edit_btn")) {
console.log("수정버튼");
editGoal(goal);
} else if (e.target.classList.contains("del_btn")) {
editGoal("");
localStorage.removeItem("goal");
}
});
goalMenu.addEventListener("mouseleave", () => {
goalMenu.classList.add(CLEAR_CLASSNAME);
});