-
Notifications
You must be signed in to change notification settings - Fork 0
/
webview.js
265 lines (239 loc) · 6.35 KB
/
webview.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
const vscode = acquireVsCodeApi()
const add_btn = document.getElementById("add_btn")
const title_input = document.getElementById("title")
const textarea_input = document.getElementById("textarea")
const tableBodyWrapper = document.getElementById('table_body')
const data = {
_canBeChanged: false,
//////
ele: null,
id: '',
title: '',
desc: '',
//////
}
Object.defineProperty(data, 'canbeChanged', {
get() {
return this['_canBeChanged']
},
/**
*
* @param {boolean} val
*/
set(val) {
const text = val ? '修改' : '添加'
add_btn.innerText = text
if (val) {
let t = title_input.value.trim()
let a = textarea_input.value.trim()
data.title = t
data.desc = a
}
this['_canBeChanged'] = val
}
})
add_btn.addEventListener("click", function () {
let t = title_input.value.trim()
let a = textarea_input.value.trim()
if (t.length <= 0) return
if (data.canbeChanged) {
vscode.postMessage({
command: 'update',
text: JSON.stringify({
id: data.id,
title: t,
desc: a,
})
})
trigerEditAction(data.title, data.desc)
if (!!data.ele) {
data.ele.querySelectorAll('td')[0].innerText = t
data.ele.querySelectorAll('td')[1].innerText = a
data.ele.setAttribute('data-title', t)
data.ele.setAttribute('data-desc', a)
}
data.canbeChanged = false
return
}
vscode.postMessage({
command: "add",
text: JSON.stringify({
t,
a,
})
})
title_input.value = ""
textarea_input.value = ""
})
const renderHTML = (data) => {
const body = document.getElementById("table_body")
let h = ``
data.forEach(item => {
let x = +item.task_status
let ops = ""
let p = [
"已完成",
"未完成",
"未开始"
]
for (let index = 0; index < 3; index++) {
if (x == index) {
ops += `<option value="${index}" selected>${p[index]}</option>`
} else {
ops += `<option value="${index}">${p[index]}</option>`
}
}
h += `
<tr>
<td>${item.title}</td>
<td>${item.desc}</td>
<td style="width: 40px">${item.create_at}</td>
<td style="width: 40px">${item.task_status}</td>
<td style="width: 40px">
<button onclick="delTask('${item.id}')">删除</button>
<div>
<select value="${item.task_status}" onchange="changeStatus(this, '${item.id}')">
${ops}
</select>
</div>
</td>
</tr>
`
});
body.innerHTML = h
}
window.changeStatus = (ele, id) => {
const value = ele.value
vscode.postMessage({
command: "updateStatus",
text: JSON.stringify({
id,
status: +value
})
})
try {
const eles = ele.getElementsByTagName("option")
for (let index = 0; index < eles.length; index++) {
const element = eles[index];
element.selected = false;
}
eles[+value].selected = true;
const beforeEle = ele.parentElement.parentElement.previousElementSibling
beforeEle.innerText = value
} catch (error) {
throw new Error(error)
}
}
window.delTask = function (id) {
vscode.postMessage({
command: 'del',
text: { id },
})
}
const trigerEditAction = (title, desc)=> {
title_input.value = title
textarea_input.value = desc
}
window.editTask = function(ele, id) {
const _ = ele.parentElement.parentElement
const title = _.getAttribute('data-title')
const desc = _.getAttribute('data-desc')
const t = title_input.value.trim()
const a = textarea_input.value.trim()
data.ele = _
data.title = t
data.desc = a
setTimeout(()=>{
trigerEditAction(title, desc)
}, 200)
data.canbeChanged = true
data.id = id
}
window.addEventListener('message', event => {
const message = event.data;
if (message.command == undefined || !message.command) return
const action = message.command;
let { text: payload } = message
if (typeof text == 'string') return
switch (action) {
case "add":
updateUI(updateAction.Add, payload)
break
case 'del':
updateUI(updateAction.Remove, payload)
break
case "reload":
const x = message.text
const data = JSON.parse(x)
renderHTML(data)
break;
default:
break;
}
})
const timeDiyFormat = (timestamp) => {
var mistiming = Math.round(new Date() / 1000) - timestamp;
var postfix = mistiming > 0 ? '前' : '后'
mistiming = Math.abs(mistiming)
var arrr = ['年', '个月', '星期', '天', '小时', '分钟', '秒'];
var arrn = [31536000, 2592000, 604800, 86400, 3600, 60, 1];
for (var i = 0; i < 7; i++) {
var inm = Math.floor(mistiming / arrn[i])
if (inm != 0) {
if (isNaN(inm)) return '刚刚'
const _ = inm + arrr[i] + postfix
return _
}
}
}
const updateAction = {
Add: 0,
Remove: 1,
}
const updateUI = (action, payload)=> {
switch (action) {
case updateAction.Add:
const create_at = timeDiyFormat(payload['create_at'])
const id = payload['id']
const htmlTemplate = `
<td>${ payload.title }</td>
<td>${ payload.desc }</td>
<td width="60px">${ create_at }</td>
<td width="30px">${ payload.task_status }</td>
<td width="60px">
<div>
<select id="select" value="${ payload.task_status }" onchange="changeStatus(this, '${ id }')">
<option value="0">已完成</option>
<option value="1">未完成</option>
<option value="2" selected="">未开始</option>
</select>
</div>
<button class="action-btn" onclick="editTask('${ id }')">编辑</button>
<button class="action-btn" onclick="delTask('${ id }')">删除</button>
</td>`
const tr = document.createElement('tr')
tr.setAttribute('data-id', id)
tr.innerHTML = htmlTemplate
toggleTable(true)
tableBodyWrapper.insertBefore(tr, tableBodyWrapper.childNodes[0])
break
case updateAction.Remove:
const { id: tid } = payload
const ele = document.querySelector(`tr[data-id="${ tid }"]`)
ele.remove()
toggleTable(false)
break
}
}
/**
*
* @param {boolean} flag
*/
const toggleTable = (flag)=>{
if (!tableBodyWrapper.innerHTML) {
const action = flag ? 'add' : 'remove'
if (!tableBodyWrapper.innerHTML) {
document.getElementsByClassName('styled-table')[0].classList[action]('active')
}
}
}