-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.js
More file actions
260 lines (202 loc) · 6.68 KB
/
main.js
File metadata and controls
260 lines (202 loc) · 6.68 KB
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
// changeJSON
let csvJson = "";
let dataCSVJson = {};
let outputCSVJson = {};
// 所有人數
let allMemberCount = 0;
// 未填寫表單成員
let notFillOutArray = [];
// 輸出項目(你的學號?)
let outPutKeyText = "";
// 對輸出項目分組
let groupByKeyAry = [];
window.onload = (event) => {
new ClipboardJS('#CarTextBtn');
// new ClipboardJS('#SelfTextBtn');
new Sortable(document.getElementById("LeaveCampList"), {
filter: "#ListTitle",
ghostClass: 'blue-background-class',
animation: 150,
});
new Sortable(document.getElementById("BackCampList"), {
ghostClass: 'blue-background-class',
animation: 150,
});
main();
}
function main() {
const inputFile = document.querySelector("input[type='file']");
setLocalStorage("#battalion");
setLocalStorage("#AllMemberCountInput");
setLocalStorage("#IdKeyInput");
setLocalStorage("#LeaveTitleInput");
setLocalStorage("#BackTitleInput");
inputFile.onchange = (event) => {
document.getElementById("LeaveCampList").innerText = "";
document.getElementById("BackCampList").innerText = "";
// 取得要輸出的key
outPutKeyText = document.getElementById("IdKeyInput").value;
// 取得輸出分組順序的key
groupByKeyAry = [];
for (let key of document.getElementsByClassName("GroupByKeyInput")) {
groupByKeyAry.push(key.value);
}
// 取得總人數
allMemberCount = parseInt(document.getElementById("AllMemberCountInput").value);
// 建立總人數名單
notFillOutArray = [];
for (let i = 1; i <= allMemberCount; i += 1) {
notFillOutArray.push(i.toString().padStart(3, '0'));
}
const file = inputFile.files[0];
const fileReader = new FileReader();
fileReader.readAsText(file, "utf-8");
if (!outPutKeyText) {
fileReader.abort();
}
fileReader.onload = (event) => {
// console.log(event, fileReader);
document.getElementById("file-name").innerHTML = file.name;
csvJson = csvJSON(fileReader.result);
// 檢查名單是否重複
dataCSVJson = checkAllMemberInArray(csvJson);
console.log(dataCSVJson);
// 篩選需要輸出的項目與資料分組
outputCSVJson = outputArrayFunc(dataCSVJson);
console.log(outputCSVJson);
outputJSON(outputCSVJson);
}
}
}
// 檢查名單是否重複
function checkAllMemberInArray(tempJson) {
let repeatAry = [];
for (key in tempJson) {
// 若輸入的數字前面沒補0,則自動補0
if (tempJson[key][outPutKeyText].length < 3) {
tempJson[key][outPutKeyText] = tempJson[key][outPutKeyText].padStart(3, '0');
}
if (notFillOutArray.indexOf(tempJson[key][outPutKeyText]) !== -1) {
notFillOutArray.splice(notFillOutArray.indexOf(tempJson[key][outPutKeyText]), 1);
}
else {
if (parseInt(tempJson[key][outPutKeyText]) <= allMemberCount) {
console.log(`重複號碼 : ${tempJson[key][outPutKeyText]}`);
repeatAry.push(tempJson[key][outPutKeyText]);
}
}
}
// 刪除重複名單中最早的填寫資料
for (let i = 0; i <= repeatAry.length; i += 1) {
for (key in tempJson) {
if (repeatAry[i] === tempJson[key][outPutKeyText]) {
tempJson.splice(key, 1);
break;
}
}
}
return tempJson;
}
function outputJSON(data) {
let startDate = document.getElementById("startDate").value.slice(5).replace('-', '/');
let endDate = document.getElementById("endDate").value.slice(5).replace('-', '/');
let carText =
`${document.getElementById("battalion").value}:
●${startDate} 休假\n\n`;
document.getElementById("LeaveListTitle").innerText = carText;
// data[0]對應出營方式
data[0].forEach(data => {
carText =
`${data["place"]}: ${data["id"].length}員
學號如下:${data["id"].sort().join("、")}\n\n`;
createDivList(carText, "LeaveCamp");
});
carText = `\n●${endDate} 收假\n\n`;
document.getElementById("BackListTitle").innerText = carText;
// data[1]對應回營方式
data[1].forEach(data => {
carText =
`${data["place"]}: ${data["id"].length}員
學號如下:${data["id"].sort().join("、")}\n\n`;
createDivList(carText, "BackCamp");
});
document.getElementById("notInList").innerText = notFillOutArray.join("、");
}
//建立新的可拉動 div 區塊
function createDivList(content, addType) {
let typeText = "";
switch (addType) {
case 'LeaveCamp': {
typeText = "LeaveCampList";
break;
}
case 'BackCamp': {
typeText = "BackCampList";
break;
}
}
const li = document.createElement("li");
li.className = ("list-item");
li.style = "white-space: pre-wrap;";
li.append(content);
document.getElementById(typeText).appendChild(li);
}
function outputArrayFunc(data) {
let campAry = [];
let tempObj = {};
// 分別對不同的輸出分組順序進行分組
for (key in groupByKeyAry) {
tempObj = (data.map(data => ({ place: data[groupByKeyAry[key]], id: data[outPutKeyText] }))
.reduce((groupByPlace, data) => {
const index = groupByPlace.findIndex(AryItem => AryItem.place === data.place);
if (index === -1) {
groupByPlace.push({
place: data.place,
id: [data.id],
});
}
else {
groupByPlace[index].id.push(data.id);
}
return groupByPlace;
}, []));
campAry.push(tempObj);
}
return campAry;
}
function setLocalStorage(str) {
if (localStorage.getItem(str.slice(1)) !== null) {
document.querySelector(str).value = localStorage.getItem(str.slice(1));
}
document.querySelector(str).onchange = (event) => {
localStorage.setItem(str.slice(1), document.querySelector(str).value);
}
}
function csvJSON(csv) {
// Split data into lines and separate headers from actual data
// using Array spread operator
const [headerLine, ...lines] = csv.split(/\r?\n(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)/);
for (let i in lines) {
lines[i] = JSON.parse(JSON.stringify(lines[i].replace(/\r\n/g, '').replace(/\r/g, '').replace(/\n/g, '')));
}
// Use common line separator, which parses each line as the contents of a JSON array
const parseLine = (line) => JSON.parse(`[${line}]`);
// Split headers line into an array
const headers = parseLine(headerLine);
// Create objects from parsing lines
// There will be as much objects as lines
const result = lines
.map((line, index) =>
// Split line with JSON
parseLine(line)
// Reduce values array into an object like: { [header]: value }
.reduce(
(object, value, index) => ({
...object,
[headers[index]]: value,
}),
{}
)
);
return result;
}