-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
246 lines (211 loc) · 6.96 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
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
let filesData = [];
let selectedFiles = [];
let filesBody;
let fileUpload;
let downloadButton;
let checkBoxHead;
let fileInput;
const numerizeFiles = () => {
Object.keys(filesData).forEach((fileName, index) => {
const fileNoId = `${fileName}-file-no`;
const fileNo = document.getElementById(fileNoId);
fileNo.textContent = index + 1;
});
};
const triggerCheckBoxAndDownloadButton = () => {
downloadButton.disabled = selectedFiles.length === 0;
checkBoxHead.checked =
selectedFiles.length === Object.keys(filesData).length &&
selectedFiles.length !== 0;
};
const isUnique = (fileName) => {
return !Object.keys(filesData).includes(fileName);
};
const handleUpload = (file) => {
const reader = new FileReader();
reader.onload = () => {
const buffer = new Uint8Array(reader.result);
const array = Array.from(buffer);
const fileNameData = file.name;
if (!isUnique(fileNameData)) {
generateToast({
message: `⚠️ ${fileNameData} already exists! ⚠️`,
background: "hsl(51 97.8% 65.1%)",
color: "hsl(51 97.8% 12.1%)",
length: "2000ms",
});
return;
}
if (!XBOX360Retitler.checkFile(array)) {
generateToast({
message: `⚠️ ${fileNameData} is not a valid file. ⚠️`,
background: "hsl(350 100% 66.5%)",
color: "hsl(350 100% 13.5%)",
length: "2000ms",
});
return;
}
const oldTitleData = XBOX360Retitler.readTitle(array);
filesData[fileNameData] = {
fileName: fileNameData,
oldTitle: oldTitleData,
newTitle: oldTitleData,
data: array.slice(),
};
const fileContainer = document.createElement("div");
fileContainer.id = fileNameData;
fileContainer.classList.add("file-container", "grid-container");
const fileNo = document.createElement("span");
fileNo.id = `${fileNameData}-file-no`;
fileNo.textContent = Object.keys(filesData).length;
const checkBox = document.createElement("input");
checkBox.type = "checkbox";
checkBox.id = `${fileNameData}-checkbox`;
const fileName = document.createElement("span");
fileName.textContent = fileNameData;
fileName.classList.add("file-name");
fileName.id = `${fileNameData}-file-name`;
const oldTitle = document.createElement("input");
oldTitle.type = "text";
oldTitle.classList.add("title");
oldTitle.value = oldTitleData;
oldTitle.readOnly = true;
oldTitle.id = `${fileNameData}-old-title`;
const newTitle = document.createElement("input");
newTitle.type = "text";
newTitle.classList.add("title");
newTitle.value = oldTitleData;
newTitle.id = `${fileNameData}-new-title`;
const deleteButton = document.createElement("button");
deleteButton.type = "button";
deleteButton.textContent = "✕";
deleteButton.classList.add("delete-button");
deleteButton.id = `${fileNameData}-delete-button`;
newTitle.addEventListener("input", (event) => {
const newValue = event.target.value;
const fileName = event.target.parentElement.id;
if (newValue.length > 0) {
filesData[fileName].newTitle = newValue;
} else {
filesData[fileName].newTitle = "";
}
});
deleteButton.addEventListener("click", (event) => {
const fileName = event.target.parentElement.id;
selectedFiles = selectedFiles.filter((e) => e !== fileName);
delete filesData[fileName];
filesBody.removeChild(fileContainer);
numerizeFiles();
triggerCheckBoxAndDownloadButton();
});
fileContainer.appendChild(fileNo);
fileContainer.appendChild(checkBox);
fileContainer.appendChild(fileName);
fileContainer.appendChild(oldTitle);
fileContainer.appendChild(newTitle);
fileContainer.appendChild(deleteButton);
filesBody.insertBefore(fileContainer, fileUpload);
triggerCheckBoxAndDownloadButton();
};
reader.readAsArrayBuffer(file);
};
const handleUploadFiles = (files) => {
[...files].forEach((file) => {
handleUpload(file);
});
numerizeFiles();
fileInput.value = [];
};
const handleDragAndDrop = () => {
const dropArea = fileUpload;
const preventDefaults = (e) => {
e.preventDefault();
e.stopPropagation();
};
const highlight = (e) => {
dropArea.classList.add("highlight");
};
const unhighlight = (e) => {
dropArea.classList.remove("highlight");
};
const handleDrop = (e) => {
let dt = e.dataTransfer;
let files = dt.files;
handleFiles(files);
numerizeFiles();
};
const handleFiles = (files) => {
[...files].forEach(handleUpload);
};
["dragenter", "dragover", "dragleave", "drop"].forEach((eventName) => {
dropArea.addEventListener(eventName, preventDefaults, false);
});
["dragenter", "dragover"].forEach((eventName) => {
dropArea.addEventListener(eventName, highlight, false);
});
["dragleave", "drop"].forEach((eventName) => {
dropArea.addEventListener(eventName, unhighlight, false);
});
dropArea.addEventListener("drop", handleDrop, false);
};
const handleDownload = () => {
selectedFiles.forEach((fileName) => {
const file = filesData[fileName];
XBOX360Retitler.changeTitle(file.data, file.newTitle);
XBOX360Retitler.writeHash(file.data);
const blob = new Blob([new Uint8Array(file.data)], {
type: "application/octet-stream",
});
const link = document.createElement("a");
link.href = window.URL.createObjectURL(blob);
link.download = file.fileName;
link.click();
});
};
const handleCheckboxes = () => {
checkBoxHead.addEventListener("change", (event) => {
const isChecked = event.target.checked;
if (isChecked) {
Object.values(filesData).forEach((file) => {
const fileName = file.fileName;
if (!selectedFiles.includes(fileName)) {
const checkBox = document.getElementById(`${fileName}-checkbox`);
selectedFiles.push(fileName);
checkBox.checked = true;
}
});
} else {
selectedFiles.forEach((fileName) => {
const checkBox = document.getElementById(`${fileName}-checkbox`);
checkBox.checked = false;
});
selectedFiles = [];
}
triggerCheckBoxAndDownloadButton();
});
filesBody.addEventListener("change", (event) => {
if (event.target.type === "checkbox") {
const isChecked = event.target.checked;
const fileName = event.target.parentElement.id;
if (isChecked) {
selectedFiles.push(fileName);
} else {
selectedFiles = selectedFiles.filter((e) => e !== fileName);
}
triggerCheckBoxAndDownloadButton();
}
});
};
const main = () => {
filesBody = document.getElementById("files-body");
fileUpload = document.getElementById("file-upload");
checkBoxHead = document.getElementById("checkbox-head");
downloadButton = document.getElementById("download-button");
fileInput = document.getElementById("file-input");
handleCheckboxes();
handleDragAndDrop();
initToast();
};
document.addEventListener("DOMContentLoaded", () => {
main();
});