-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
295 lines (234 loc) · 8.62 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
class Post {
constructor({ postId, content, publishTime }) {
this.postId = postId
this.content = content
this.publishTime = publishTime
}
renderElement() {
const postElement = document.createElement("div")
postElement.classList.add("post")
const contentElement = document.createElement("p")
contentElement.classList.add("content")
contentElement.textContent = this.content
postElement.append(contentElement)
const bottom = document.createElement("div")
bottom.classList.add("bottom")
postElement.append(bottom)
const time = document.createElement("p")
time.classList.add("time")
time.textContent = this.formatTime(this.publishTime)
bottom.append(time)
const buttons = document.createElement("div")
buttons.classList.add("buttons")
bottom.append(buttons)
const editElement = document.createElement("div")
editElement.classList.add("button")
editElement.classList.add("edit")
// 创建输入框
const inputElement = document.createElement("textarea")
inputElement.classList.add("input-content")
inputElement.value = this.content
// 创建确认按钮
const confirmElement = document.createElement("div")
confirmElement.classList.add("button")
confirmElement.classList.add("confirm")
confirmElement.textContent = "确认"
// 创建取消按钮
const cancelElement = document.createElement("div")
cancelElement.classList.add("button")
cancelElement.classList.add("cancel")
cancelElement.textContent = "取消"
editElement.onclick = () => {
// 点击编辑
contentElement.replaceWith(inputElement)
// 编辑按钮替换成确认按钮
editElement.replaceWith(confirmElement)
// 6. 删除按钮替换成取消按钮
deleteElement.replaceWith(cancelElement)
}
confirmElement.onclick = async () => {
// 1. 更新消息
await api.updatePost(this.postId, inputElement.value)
myAlert.alertSuccess("消息更新成功")
// 2. 输入框替换回原来的文本元素
contentElement.textContent = inputElement.value
inputElement.replaceWith(contentElement)
confirmElement.replaceWith(editElement)
cancelElement.replaceWith(deleteElement)
}
cancelElement.onclick = function () {
inputElement.replaceWith(contentElement)
confirmElement.replaceWith(editElement)
cancelElement.replaceWith(deleteElement)
}
editElement.textContent = "编辑"
buttons.append(editElement)
const deleteElement = document.createElement("div")
deleteElement.classList.add("button")
deleteElement.classList.add("delete")
deleteElement.textContent = "删除"
deleteElement.onclick = async () => {
if (confirm("确定要删除吗?")) {
// 1. 调用删除接口
await api.deletePost(this.postId)
myAlert.alertSuccess("删除成功")
// 2. 删掉消息元素自身
postElement.remove()
}
}
buttons.append(deleteElement)
return postElement
}
formatTime(timestamp) {
// 时间戳转成时间
const time = new Date(timestamp * 1000)
return `${time.getFullYear()}-${this.fillZero(
time.getMonth() + 1
)}-${time.getDate()} ${this.fillZero(time.getHours())}:${this.fillZero(
time.getMinutes()
)}`
}
fillZero(n) {
return n < 10 ? `0${n}` : n
}
}
class Manager {
limit = 5
page = 1
// 是否加载中
isLoading = false
// 是否获取完成
isFinished = false
// 搜索的文本
search = ""
constructor() {
window.addEventListener("load", () => {
// 显示发布框
document.querySelector("#container .publish").onclick = () =>
this.publishClick()
// 三种方法:
// 1. 将 publishClick 修改为箭头函数
// publishClick = () => {}
// 2. bind 绑定 this
// this.publishClick.bind(this)
// 3. 放到箭头函数中
// () => this.publishClick()
// 取消按钮,隐藏发布框
document.querySelector("#container .cancel").onclick = () =>
this.cancelClick()
// 发布按钮,发布消息
document.querySelector("#container .confirm").onclick = () =>
this.confirmClick()
document.querySelector("#container .search").onclick = () =>
this.searchClick()
document.querySelector("#container .clear").onclick = () =>
this.clearClick()
window.addEventListener("scroll", () => {
// 滚动距离页面底部的距离
// HTML 元素内容的整体高度 - (HTML 元素顶部滚动出的高度 + )
// 参考 https://3yya.com/courseware/chapter/211
const bottomOfWindowHeight =
document.documentElement.offsetHeight -
(document.documentElement.scrollTop +
document.documentElement.clientHeight)
if (bottomOfWindowHeight < 100) {
// 页面距离底部小于 100 像素
// 加载数据
this.getPosts()
}
})
// 读取帖子
this.getPosts()
})
}
searchClick() {
this.search = document.querySelector("#container .search-input").value
this.getPosts(true)
}
clearClick() {
this.search = ""
this.getPosts(true)
}
publishClick() {
document.querySelector("#container .input-dialog").hidden = false
// 平滑滚到页头
window.scrollTo({ top: 0, behavior: "smooth" })
}
cancelClick() {
document.querySelector("#container .input-dialog").hidden = true
}
async confirmClick() {
const content = document.querySelector(
"#container .input-content"
).value
if (content.length < 5) {
myAlert.alertWarning("至少 5 个字符。")
return
}
if (content.length > 100) {
myAlert.alertWarning("最多 100 个字符。")
return
}
await api.publishPost(content)
myAlert.alertSuccess("发布成功")
document.querySelector("#container .input-content").value = ""
document.querySelector("#container .input-dialog").hidden = true
this.getPosts(true)
}
async getPosts(refresh = false) {
if (refresh) {
// 刷新页面
this.isFinished = false
this.page = 1
document.querySelector("#container .empty").hidden = true
document.querySelector("#container .posts").innerHTML = ""
}
if (this.isLoading || this.isFinished) {
// 如果在加载中,或没有数据了,则直接退出
return
}
this.isLoading = true
const { results } = await api.getPosts({
page: this.page,
limit: this.limit,
search: this.search,
})
this.isLoading = false
if (results.length === 0) {
// 已经没有更多了
this.isFinished = true
document.querySelector("#container .empty").hidden = false
return
}
const postsElement = document.querySelector("#container .posts")
results.forEach((data) => {
const post = new Post({
postId: data.post_id,
content: data.content,
publishTime: data.publish_time,
})
postsElement.append(post.renderElement())
})
this.page++
if (!this.isScroll()) {
// 如果没出现滚动条
// 则继续拿数据
return this.getPosts()
}
}
isScroll() {
// 是否出现了滚动条
return (
document.documentElement.clientHeight !=
document.documentElement.scrollHeight
)
}
}
// api 实例
const api = new Api("https://3yya.com/u/d8cf630cf5f367cc/rest/app")
// const api = new Api(
// "http://127.0.0.1:9000/u/d8cf630cf5f367cc/rest/app"
// )
// alert 实例
const myAlert = new Alert(3500)
new Manager()