WebUI: migrate to JS native class

This commit is contained in:
Chocobo1 2025-05-07 14:08:35 +08:00
commit c0d8e5bca3
No known key found for this signature in database
GPG key ID: 210D9C873253A68C

View file

@ -15,41 +15,41 @@ window.qBittorrent.MultiRename ??= (() => {
Extension: "Extension" Extension: "Extension"
}; };
const RenameFiles = new Class({ class RenameFiles {
hash: "", hash = "";
selectedFiles: [], selectedFiles = [];
matchedFiles: [], matchedFiles = [];
// Search Options // Search Options
_inner_search: "", #inner_search = "";
setSearch: function(val) { setSearch(val) {
this._inner_search = val; this.#inner_search = val;
this._inner_update(); this.#inner_update();
this.onChanged(this.matchedFiles); this.onChanged(this.matchedFiles);
}, }
useRegex: false, useRegex = false;
matchAllOccurrences: false, matchAllOccurrences = false;
caseSensitive: false, caseSensitive = false;
// Replacement Options // Replacement Options
_inner_replacement: "", #inner_replacement = "";
setReplacement: function(val) { setReplacement(val) {
this._inner_replacement = val; this.#inner_replacement = val;
this._inner_update(); this.#inner_update();
this.onChanged(this.matchedFiles); this.onChanged(this.matchedFiles);
}, }
appliesTo: AppliesTo.FilenameExtension, appliesTo = AppliesTo.FilenameExtension;
includeFiles: true, includeFiles = true;
includeFolders: false, includeFolders = false;
replaceAll: false, replaceAll = false;
fileEnumerationStart: 0, fileEnumerationStart = 0;
onChanged: (rows) => {}, onChanged(rows) {}
onInvalidRegex: (err) => {}, onInvalidRegex(err) {}
onRenamed: (rows) => {}, onRenamed(rows) {}
onRenameError: (response) => {}, onRenameError(response) {}
_inner_update: function() { #inner_update() {
const findMatches = (regex, str) => { const findMatches = (regex, str) => {
let result; let result;
let count = 0; let count = 0;
@ -119,7 +119,7 @@ window.qBittorrent.MultiRename ??= (() => {
this.matchedFiles = []; this.matchedFiles = [];
// Ignore empty searches // Ignore empty searches
if (!this._inner_search) if (!this.#inner_search)
return; return;
// Setup regex flags // Setup regex flags
@ -131,10 +131,10 @@ window.qBittorrent.MultiRename ??= (() => {
// Setup regex search // Setup regex search
const regexEscapeExp = /[/\-\\^$*+?.()|[\]{}]/g; const regexEscapeExp = /[/\-\\^$*+?.()|[\]{}]/g;
const standardSearch = new RegExp(this._inner_search.replace(regexEscapeExp, "\\$&"), regexFlags); const standardSearch = new RegExp(this.#inner_search.replace(regexEscapeExp, "\\$&"), regexFlags);
let regexSearch; let regexSearch;
try { try {
regexSearch = new RegExp(this._inner_search, regexFlags); regexSearch = new RegExp(this.#inner_search, regexFlags);
} }
catch (err) { catch (err) {
if (this.useRegex) { if (this.useRegex) {
@ -185,7 +185,7 @@ window.qBittorrent.MultiRename ??= (() => {
let renamed = row.original; let renamed = row.original;
for (let i = matches.length - 1; i >= 0; --i) { for (let i = matches.length - 1; i >= 0; --i) {
const match = matches[i]; const match = matches[i];
let replacement = this._inner_replacement; let replacement = this.#inner_replacement;
// Replace numerical groups // Replace numerical groups
for (let g = 0; g < match.length; ++g) { for (let g = 0; g < match.length; ++g) {
const group = match[g]; const group = match[g];
@ -215,9 +215,9 @@ window.qBittorrent.MultiRename ??= (() => {
++fileEnumeration; ++fileEnumeration;
this.matchedFiles.push(row); this.matchedFiles.push(row);
} }
}, }
rename: async function() { async rename() {
if (!this.matchedFiles || (this.matchedFiles.length === 0) || !this.hash) { if (!this.matchedFiles || (this.matchedFiles.length === 0) || !this.hash) {
this.onRenamed([]); this.onRenamed([]);
return; return;
@ -269,12 +269,13 @@ window.qBittorrent.MultiRename ??= (() => {
await _inner_rename(0); await _inner_rename(0);
} }
this.onRenamed(replaced); this.onRenamed(replaced);
}, }
update: function() {
this._inner_update(); update() {
this.#inner_update();
this.onChanged(this.matchedFiles); this.onChanged(this.matchedFiles);
} }
}); }
return exports(); return exports();
})(); })();