-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
192 lines (153 loc) · 4.94 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
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
console.log(window.bacefook);
// ロード時
window.addEventListener("load", () => {
let username = localStorage.getItem("username");
if(!username){
username = window.prompt("What is your name?");
localStorage.setItem("username", username);
if(username.length === 0 || username === null) {
localStorage.clear();
const userEl = document.querySelector("h3");
userEl.innerText = `ユーザー名: unknown`;
logButtonEl.innerText="ログイン"
}
}
if(username.length !== 0 && username !== null){
const userEl = document.querySelector("h3");
userEl.innerText = `ユーザー名: ${username}`;
logButtonEl.innerText="ログアウト"
}
getNews();
});
// 投稿表示
const containerEl = document.querySelector("#newsfeed");
let opentopic = 0;
function getNews() {
opentopic = bacefook.newsfeed.length;
for (let index = bacefook.newsfeed.length - 1; index >= 0; index--) {
const post = bacefook.newsfeed[index];
const postingEl = document.createElement("div");
postingEl.className = "posting";
const friendEl = document.createElement("div");
friendEl.className = "friend";
friendEl.innerText = post.friend + index;
const timeStampEl = document.createElement("div");
timeStampEl.className = "timeStamp";
timeStampEl.innerText = post.timestamp;
const momentEl = document.createElement("div");
momentEl.className = "moment";
momentEl.innerText = moment(post.timestamp).fromNow();
const feelingEl = document.createElement("div");
feelingEl.className = "feeling";
feelingEl.innerText = post.feeling;
const imgEl = document.createElement("img");
imgEl.className = "img";
imgEl.src = post.image;
const postEl = document.createElement("div");
postEl.innerText = post.text;
postingEl.append(friendEl);
postingEl.append(timeStampEl);
postingEl.append(feelingEl);
postingEl.append(imgEl);
postingEl.append(momentEl);
postingEl.append(postEl);
containerEl.append(postingEl);
}
}
// 更新ボタン
const buttonEl = document.querySelector(".button");
console.log(buttonEl);
buttonEl.addEventListener("click", () => {
const postingEl = document.querySelectorAll(".posting");
for (let i = 0; i < postingEl.length; i++) {
postingEl[i].remove()
}
const newTopicEl = document.getElementsByClassName("topic")[0];
newTopicEl.remove();
count = 0;
getNews();
});
// 投稿ボタン
const addButtonEl = document.getElementById("addButton");
console.log(addButtonEl);
addButtonEl.addEventListener("click", () => {
let textEl = document.getElementById("newTopic");
const feelings = [
"happy",
"smug",
"lovestruck",
"gross",
"scared",
"tired",
"angry",
"frustrated",
"excited",
""
];
const images = [
"./images/1.jpg",
"./images/2.png",
"./images/3.png",
];
const mypost = {
feeling: feelings[Math.floor(Math.random() * feelings.length)],
friend: localStorage.getItem("username"),
image: images[Math.floor(Math.random() * images.length)],
text: textEl.value,
timestamp: new Date(),
}
window.bacefook.newsfeed.push(mypost);
const postingEl = document.querySelectorAll(".posting");
for (let i = 0; i < postingEl.length; i++) {
postingEl[i].remove()
}
const newTopicEl = document.getElementsByClassName("topic")[0];
newTopicEl.remove();
count = 0;
getNews();
textEl.value = "";
});
// ログイン・ログアウト
const logButtonEl = document.getElementsByClassName("logButton")[0];
console.log("log", logButtonEl);
logButtonEl.addEventListener("click", () => {
let username = localStorage.getItem("username");
if (!username) {
username = window.prompt("What is your name?");
if(username.length === 0 || username === null) {
localStorage.clear();
userEl.innerText = `ユーザー名: unknown`;
logButtonEl.innerText="ログイン"
} else {
localStorage.setItem("username", username);
const userEl = document.querySelector("h3");
userEl.innerText = `ユーザー名: ${username}`;
logButtonEl.innerText="ログアウト"
getNews();
}
} else {
localStorage.clear();
const userEl = document.querySelector("h3");
userEl.innerText = `ユーザー名: unknown`;
logButtonEl.innerText="ログイン"
}
});
// 新着表示
let count = 0;
const intervalId = setInterval(() => {
let newTopic = bacefook.newsfeed.length;
if (opentopic !== newTopic){
const topicEl = document.createElement("div");
topicEl.className = "topic";
topicEl.innerText = "新着"+(newTopic - opentopic) + "件あります。";
if (count === 0) {
console.log("count",count);
// containerEl.prepend(topicEl);
containerEl.before(topicEl);
count = 1;
} else {
const newTopicEl = document.getElementsByClassName("topic")[0];
newTopicEl.innerText = "新着"+(newTopic - opentopic) + "件あります。";
}
}
}, 100);