-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
282 lines (242 loc) · 12.3 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Bulk File Renamer</title>
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'unsafe-inline'; style-src 'self' https://fonts.googleapis.com; font-src https://fonts.googleapis.com https://fonts.gstatic.com;">
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
<main class="content-wrapper">
<div class="options">
<h1>Bulk File Renamer</h1>
<section>
<label>Select the directory containing the files you wish to rename:</label>
<button id="select-dir">Select a directory</button>
<p id="directory-selected-notification">All files in <span id="lbl-selected-directory" class="selected-value"></span> <strong>and files inside any subdirectories</strong> will be renamed as per the options you select below:</p>
</section>
<h2>Options</h2>
<section id="option-set1" class="options-group">
<h3>Case</h3>
<div>
<input type="radio" name="case" id="case-nochange" value="0" checked/>
<label for="case-nochange">No change</label>
<input type="radio" name="case" id="case-lower" value="lower"/>
<label for="case-lower">All lowercase</label>
<input type="radio" name="case" id="case-upper" value="upper"/>
<label for="case-upper">All uppercase</label>
</div>
<h3>Join words</h3>
<input type="radio" name="join-words" id="join-words-nochange" value="0" checked/>
<label for="join-words-nochange">No change</label>
<input type="radio" name="join-words" id="join-words-underscore" value="underscore"/>
<label for="join-words-underscore">Underscores</label>
<input type="radio" name="join-words" id="join-words-hyphens" value="hyphen"/>
<label for="join-words-hyphens">Hyphens</label>
</section>
<hr>
<section id="option-set2" class="options-group">
<h3>Full rename</h3>
<p>All files will be named as below, with an increasing number appended to the end.</p>
<input type="text" name="rename" id="rename"/>
</section>
<hr>
<button id="go">Rename all files</button>
</div>
<div class="preview">
<h2>Preview</h2>
<p id="no-files-selected-notification">You need to select a directory that contains files to be able to preview the changes.</p>
<ul id="files-list">
</ul>
</div>
</main>
<script>
const ipcRenderer = require('electron').ipcRenderer;
const dirBtn = document.getElementById('select-dir');
const goBtn = document.getElementById('go');
var filesFound = false;
var caseRadios = document.querySelectorAll('input[name="case"]');
var joinWordsRadios = document.querySelectorAll('input[name="join-words"]');
var renameInput = document.getElementById('rename');
var directorySelected = '';
var filesList = []; // Holds the original file names.
var newFilesList = []; // Holds the new file names.
var newFilesChanges = {}; // Holds what will change for each file (e.g. spaces to hyphens).
// Set the defaults
var renamingDefaults = {};
newFilesChanges.case = 0;
newFilesChanges.joinWords = 0;
newFilesChanges.rename = '';
/* Select a directory. */
dirBtn.addEventListener('click', function () {
ipcRenderer.send('selectDir', {});
// Reset any selections.
resetOptions();
renameInput.value = '';
});
/* Add an event listener to each radio for 'Case'. */
caseRadios.forEach(function(element) {
element.addEventListener('change', function(event, value) {
// Swap the styling of the option sections, as the full rename is not available if selecting this option.
document.getElementById('option-set1').classList.add("active-options");
document.getElementById('option-set2').classList.remove("active-options");
// Reset the full rename value, as it's only applicable when other options aren't set.
renameInput.value = '';
updateFilesListModification('rename', '');
// Store the case modifier.
updateFilesListModification('case', event.target.value);
previewRenames();
});
});
/* Add an event listener to each radio for how words are joined. */
joinWordsRadios.forEach(function(element) {
element.addEventListener('change', function(event, value) {
// Swap the styling of the option sections, as the full rename is not available if selecting this option.
document.getElementById('option-set1').classList.add("active-options");
document.getElementById('option-set2').classList.remove("active-options");
// Reset the full rename value, as it's only applicable when other options aren't set.
renameInput.value = '';
updateFilesListModification('rename', '');
// Store the word join modifier.
updateFilesListModification('joinWords', event.target.value);
previewRenames();
});
});
/* Look for changes if doing a full rename. */
renameInput.addEventListener("keyup", function(event) {
// Swap the styling of the option sections, as others are no longer available when doing a full rename.
document.getElementById('option-set2').classList.add("active-options");
document.getElementById('option-set1').classList.remove("active-options");
newFilesChanges.rename = event.target.value;
// Reset the other options, as they aren't applicable when doing a full rename.
resetOptions();
previewRenames();
});
/* Perform the renaming. */
goBtn.addEventListener('click', function() {
if (!directorySelected) {
alert('You must select a directory.');
return;
}
if (confirm('Are you sure you wish to rename all files in this directory?')) {
textCase = document.querySelector('input[name="case"]:checked').value;
joinWords = document.querySelector('input[name="join-words"]:checked').value;
rename = document.querySelector('input[name="rename"]').value;
ipcRenderer.send('go', {
'case': textCase,
'joinWords': joinWords,
'rename': rename,
});
directorySelected = '';
}
});
/* Preview the results */
function previewRenames() {
if (filesFound !== true) {
return;
}
let fileCount = 0;
filesList.forEach(function(file, index) {
if (newFilesChanges['joinWords'] != 0) {
if (newFilesChanges['joinWords'] == 'underscore') {
file = file.replace(/\s|\-/g, "_");
} else if (newFilesChanges['joinWords'] == 'hyphen') {
file = file.replace(/\s|\_/g, "-");
}
}
if (newFilesChanges['case'] != 0) {
if (newFilesChanges['case'] == 'upper') {
file = file.toUpperCase();
} else if (newFilesChanges['case'] == 'lower') {
file = file.toLowerCase();
}
}
if (newFilesChanges['rename'] != '') {
fileCount++;
// Keep the extension
let extension = file.split('.')[1];
file = newFilesChanges['rename'] + fileCount + '.' + extension;
}
newFilesList[index] = file;
});
// Go through every file in our new files list.
newFilesList.forEach(function(item) {
let filesListNodes = document.getElementById('files-list').childNodes;
let x = 0;
if (filesList.length > 0) {
filesListNodes.forEach(function(item) {
item.innerText = filesList[x] + '-> ' + newFilesList[x];
x++;
});
}
});
}
function updateFilesListModification(element, change) {
newFilesChanges[element] = change;
}
/* Reset any selected options. */
function resetOptions() {
updateFilesListModification('joinWords', null);
updateFilesListModification('case', null);
caseRadios.forEach(function(element, index) {
if (element.value == 0) {
element.checked = true;
} else {
element.checked = false;
}
});
joinWordsRadios.forEach(function(element, index) {
if (element.value == 0) {
element.checked = true;
} else {
element.checked = false;
}
});
}
/* Reset all values/options */
function resetAll() {
resetOptions();
updateFilesListModification('rename', '');
updateFilesListModification('joinWords', null);
updateFilesListModification('case', null);
filesList = [];
newFilesList = [];
}
/* Receives files in selected directory or the result of a succesful renaming */
ipcRenderer.on('asynchronous-message', (event, message) => {
document.getElementById('lbl-selected-directory').innerText = message.directorySelected;
document.getElementById('files-list').textContent = "";
// If the renaming is complete
if (message.renameSuccessful == true) {
filesList = [];
// Reset the preview window
//document.getElementById('files-list').innerHTML = "";
document.getElementById('directory-selected-notification').style.display = "none";
document.getElementById('no-files-selected-notification').style.display = "block";
document.getElementById('option-set1').classList.add("active-options");
document.getElementById('option-set2').classList.remove("active-options");
renameInput.value = '';
updateFilesListModification('rename', '');
resetAll();
} else { // A directory should have been selected
resetAll();
if (message.filesInDir.length > 0) {
directorySelected = message.directorySelected;
document.getElementById('directory-selected-notification').style.display = "block";
document.getElementById('no-files-selected-notification').style.display = "none";
filesFound = true;
message.filesInDir.forEach(function(item) {
let filename = item.substring(item.lastIndexOf("/") + 1, item.length);
filesList.push(filename);
document.getElementById('files-list').insertAdjacentHTML('beforeend', '<li>'+filename+'</li>');
});
newFilesList = filesList.slice();
} else {
document.getElementById('directory-selected-notification').style.display = "none";
document.getElementById('no-files-selected-notification').style.display = "block";
}
}
});
</script>
</body>
</html>