-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenderer.js
226 lines (149 loc) · 5.2 KB
/
renderer.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
const remote = require('electron').remote;
const path = require('path');
const fs = require('fs');
const buttonClose = document.querySelector('#btn-close');
const form = document.querySelector('form');
const files = document.querySelector('#files');
const table = document.querySelector('table');
const operations = document.querySelectorAll('.operations');
const confirmDialog = document.querySelector('#confirm-dialog');
const successDialog = document.querySelector('#success-dialog');
const errorDialog = document.querySelector('#error-dialog');
let filesObjects = [];
let filesObjectsId = 0;
function showError(message) {
errorDialog.querySelector('p').innerHTML = message;
errorDialog.showModal();
}
function getRenderTableRow(file) {
filesObjectsId++;
const tableRowTpl = document.querySelector('#table-row');
const clone = tableRowTpl.content.cloneNode(true);
filesObjects.push({
file,
id: filesObjectsId
});
clone.querySelector('tr').id = 'file-' + filesObjectsId.toString();
clone.querySelector('[data-name="current"]').innerHTML = file.path;
return clone;
}
function getFileObject(id) {
const filtred = filesObjects.filter(f => parseInt(f.id) === parseInt(id));
if (filtred.length) {
return filtred[0].file;
} else {
return false;
}
}
function getRuleApllyed(parsedFile, operation, value) {
switch (operation) {
case 'add':
return `${parsedFile.name}${value}${parsedFile.ext}`;
case 'rem':
if (parsedFile.name.substring(parsedFile.name.length - value.length, parsedFile.name.length) === value) {
return parsedFile.name.substring(0, parsedFile.name.length - value.length) + parsedFile.ext;
} else {
return parsedFile.name + parsedFile.ext;
}
default:
return parsedFile.name + parsedFile.ext;
}
}
function applyOperation() {
const sufix = document.querySelector('[name="sufix"]:checked');
const rows = document.querySelectorAll('tbody tr');
if (sufix) {
const operation = sufix.value.split('|')[0];
const value = sufix.value.split('|')[1];
rows.forEach(row => {
const id = row.id.split('-')[1];
const file = getFileObject(id);
const parsed = path.parse(file.path);
const newName = getRuleApllyed(parsed, operation, value);
row.querySelector('[data-name="new"]').innerHTML = newName;
});
}
}
function clearFilesObjects() {
filesObjects = [];
table.querySelector('tbody').innerHTML = '';
}
function execute() {
const executingDialog = document.querySelector('#executing-dialog');
executingDialog.showModal();
recursiveExecute(0, () => {
form.reset();
clearFilesObjects();
executingDialog.close();
successDialog.showModal();
});
}
function recursiveExecute(index, callback) {
const executingDialog = document.querySelector('#executing-dialog');
const sufix = document.querySelector('[name="sufix"]:checked');
const operation = sufix.value.split('|')[0];
const value = sufix.value.split('|')[1];
const rows = document.querySelectorAll('tbody tr');
const row = rows[index];
const id = row.id.split('-')[1];
const file = getFileObject(id);
const parsed = path.parse(file.path);
const newName = getRuleApllyed(parsed, operation, value);
executingDialog.querySelector('p').innerHTML = `${index+1} de ${rows.length}`;
fs.renameSync(`${parsed.dir}\\${parsed.base}`, `${parsed.dir}\\${newName}`);
if ((index+1) < rows.length) {
recursiveExecute(++index, callback);
} else {
callback();
}
}
buttonClose.addEventListener('click', e => {
e.preventDefault();
remote.getCurrentWindow().close();
});
files.addEventListener('input', e => {
clearFilesObjects();
Array.from(e.target.files).forEach(file => {
const row = getRenderTableRow(file);
table.querySelector('tbody').appendChild(row);
});
applyOperation();
});
operations.forEach(operation => {
operation.addEventListener('input', e => {
applyOperation();
});
});
form.addEventListener('submit', e => {
e.preventDefault();
const sufix = document.querySelector('[name="sufix"]:checked');
const rows = document.querySelectorAll('tbody tr');
if (!sufix) {
showError('Escolha a operação.');
return;
}
if (!rows.length) {
showError('Selecione os arquivos.');
return;
}
if (sufix && rows.length) {
const operation = sufix.value.split('|')[0];
const value = sufix.value.split('|')[1];
if (operation && value) {
confirmDialog.showModal();
}
}
});
confirmDialog.querySelector('#dialog-y').addEventListener('click', e => {
confirmDialog.close();
execute();
});
confirmDialog.querySelector('#dialog-n').addEventListener('click', e => {
confirmDialog.close();
});
successDialog.querySelector('.btn').addEventListener('click', e => {
successDialog.close();
});
errorDialog.querySelector('.btn').addEventListener('click', e => {
errorDialog.close();
});