Skip to content

Commit 704ac65

Browse files
authored
WebUI: migrate away from MooTools each() function
There should be no user visible effect. PR #23819.
1 parent 9ef9c21 commit 704ac65

File tree

7 files changed

+104
-101
lines changed

7 files changed

+104
-101
lines changed

src/webui/www/private/scripts/client.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,14 +1036,14 @@ window.addEventListener("DOMContentLoaded", async (event) => {
10361036
}
10371037
}
10381038
if (responseJSON["torrents_removed"]) {
1039-
responseJSON["torrents_removed"].each((hash) => {
1039+
for (const hash of responseJSON["torrents_removed"]) {
10401040
torrentsTable.removeRow(hash);
10411041
removeTorrentFromCategoryList(hash);
10421042
updateCategories = true; // Always to update All category
10431043
removeTorrentFromTagList(hash);
10441044
updateTags = true; // Always to update All tag
10451045
updateTrackers = true;
1046-
});
1046+
}
10471047
updateTorrents = true;
10481048
updateStatuses = true;
10491049
}

src/webui/www/private/scripts/dynamicTable.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,10 @@ window.qBittorrent.DynamicTable ??= (() => {
262262

263263
resetElementBorderStyle(borderChangeElement, ((changeBorderSide === "right") ? "left" : "right"));
264264

265-
borderChangeElement.getSiblings('[class=""]').each((el) => {
266-
resetElementBorderStyle(el);
267-
});
265+
for (const el of borderChangeElement.parentElement.children) {
266+
if ((el !== borderChangeElement) && (el.classList.length === 0))
267+
resetElementBorderStyle(el);
268+
}
268269
}
269270
this.lastHoverTh = e.target;
270271
this.lastClientX = e.clientX;
@@ -324,9 +325,10 @@ window.qBittorrent.DynamicTable ??= (() => {
324325
}
325326
if (this.currentHeaderAction === "drag") {
326327
resetElementBorderStyle(el);
327-
el.getSiblings('[class=""]').each((el) => {
328-
resetElementBorderStyle(el);
329-
});
328+
for (const e of el.parentElement.children) {
329+
if ((e !== el) && (e.classList.length === 0))
330+
resetElementBorderStyle(e);
331+
}
330332
}
331333
this.currentHeaderAction = "";
332334
}.bind(this);

src/webui/www/private/scripts/mocha-init.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,10 @@ const initializeWindows = () => {
180180
};
181181

182182
const addClickEvent = (el, fn) => {
183-
["Link", "Button"].each((item) => {
183+
for (const item of ["Link", "Button"]) {
184184
if (document.getElementById(el + item))
185185
document.getElementById(el + item).addEventListener("click", fn);
186-
});
186+
}
187187
};
188188

189189
addClickEvent("download", (e) => {
@@ -1285,33 +1285,33 @@ const initializeWindows = () => {
12851285
}
12861286
});
12871287

1288-
["stop", "start", "recheck"].each((item) => {
1288+
for (const item of ["stop", "start", "recheck"]) {
12891289
addClickEvent(item, (e) => {
12901290
e.preventDefault();
12911291
e.stopPropagation();
12921292

12931293
const hashes = torrentsTable.selectedRowsIds();
1294-
if (hashes.length) {
1295-
hashes.each((hash, index) => {
1294+
if (hashes.length > 0) {
1295+
for (const hash of hashes) {
12961296
fetch(`api/v2/torrents/${item}`, {
12971297
method: "POST",
12981298
body: new URLSearchParams({
12991299
hashes: hash
13001300
})
13011301
});
1302-
});
1302+
}
13031303
updateMainData();
13041304
}
13051305
});
1306-
});
1306+
}
13071307

1308-
["decreasePrio", "increasePrio", "topPrio", "bottomPrio"].each((item) => {
1308+
for (const item of ["decreasePrio", "increasePrio", "topPrio", "bottomPrio"]) {
13091309
addClickEvent(item, (e) => {
13101310
e.preventDefault();
13111311
e.stopPropagation();
13121312
setQueuePositionFN(item);
13131313
});
1314-
});
1314+
}
13151315

13161316
setQueuePositionFN = (cmd) => {
13171317
const hashes = torrentsTable.selectedRowsIds();

src/webui/www/private/scripts/prop-peers.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,8 @@ window.qBittorrent.PropPeers ??= (() => {
9696
}
9797
}
9898
if (responseJSON["peers_removed"]) {
99-
responseJSON["peers_removed"].each((hash) => {
99+
for (const hash of responseJSON["peers_removed"])
100100
torrentPeersTable.removeRow(hash);
101-
});
102101
}
103102
torrentPeersTable.updateTable(full_update);
104103

src/webui/www/private/scripts/search.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -584,25 +584,22 @@ window.qBittorrent.Search ??= (() => {
584584

585585
const copySearchTorrentName = () => {
586586
const names = [];
587-
searchResultsTable.selectedRowsIds().each((rowId) => {
587+
for (const rowId of searchResultsTable.selectedRowsIds())
588588
names.push(searchResultsTable.getRow(rowId).full_data.fileName);
589-
});
590589
return names.join("\n");
591590
};
592591

593592
const copySearchTorrentDownloadLink = () => {
594593
const urls = [];
595-
searchResultsTable.selectedRowsIds().each((rowId) => {
594+
for (const rowId of searchResultsTable.selectedRowsIds())
596595
urls.push(searchResultsTable.getRow(rowId).full_data.fileUrl);
597-
});
598596
return urls.join("\n");
599597
};
600598

601599
const copySearchTorrentDescriptionUrl = () => {
602600
const urls = [];
603-
searchResultsTable.selectedRowsIds().each((rowId) => {
601+
for (const rowId of searchResultsTable.selectedRowsIds())
604602
urls.push(searchResultsTable.getRow(rowId).full_data.descrLink);
605-
});
606603
return urls.join("\n");
607604
};
608605

@@ -801,16 +798,16 @@ window.qBittorrent.Search ??= (() => {
801798
document.getElementById("searchResultsNoSearches").classList.remove("invisible");
802799

803800
// sort plugins alphabetically
804-
const allPlugins = searchPlugins.sort((left, right) => {
801+
searchPlugins.sort((left, right) => {
805802
const leftName = left.fullName;
806803
const rightName = right.fullName;
807804
return window.qBittorrent.Misc.naturalSortCollator.compare(leftName, rightName);
808805
});
809806

810-
allPlugins.each((plugin) => {
807+
for (const plugin of searchPlugins) {
811808
if (plugin.enabled === true)
812809
pluginOptions.push(createOption(plugin.fullName, plugin.name));
813-
});
810+
}
814811

815812
if (pluginOptions.length > 2)
816813
pluginOptions.splice(2, 0, createOption("──────────", undefined, true));

src/webui/www/private/views/rss.html

Lines changed: 76 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -449,18 +449,18 @@
449449
}
450450

451451
let rowID = -1;
452-
visibleArticles.sort((e1, e2) => new Date(e2.date) - new Date(e1.date))
453-
.each((torrentEntry) => {
454-
rssArticleTable.updateRowData({
455-
rowId: ++rowID,
456-
name: torrentEntry.title,
457-
link: torrentEntry.link,
458-
torrentURL: torrentEntry.torrentURL,
459-
feedUid: torrentEntry.feedUid,
460-
dataId: torrentEntry.id,
461-
isRead: torrentEntry.isRead
462-
});
452+
visibleArticles.sort((e1, e2) => new Date(e2.date) - new Date(e1.date));
453+
for (const torrentEntry of visibleArticles) {
454+
rssArticleTable.updateRowData({
455+
rowId: ++rowID,
456+
name: torrentEntry.title,
457+
link: torrentEntry.link,
458+
torrentURL: torrentEntry.torrentURL,
459+
feedUid: torrentEntry.feedUid,
460+
dataId: torrentEntry.id,
461+
isRead: torrentEntry.isRead
463462
});
463+
}
464464

465465
clearDetails();
466466
rssArticleTable.updateTable(false);
@@ -647,74 +647,76 @@
647647

648648
// check if list of articles differs
649649
let needsUpdate = false;
650-
flattenedResp.filter((r) => !r.isFolder)
651-
.each((r) => {
652-
let articlesDiffer = true;
653-
if (r.articles.length === feedData[r.uid].length) {
654-
articlesDiffer = false;
655-
for (let i = 0; i < r.articles.length; ++i) {
656-
if (feedData[r.uid][i].id !== r.articles[i].id)
657-
articlesDiffer = true;
658-
}
650+
for (const r of flattenedResp) {
651+
if (r.isFolder)
652+
continue;
653+
654+
let articlesDiffer = true;
655+
if (r.articles.length === feedData[r.uid].length) {
656+
articlesDiffer = false;
657+
for (let i = 0; i < r.articles.length; ++i) {
658+
if (feedData[r.uid][i].id !== r.articles[i].id)
659+
articlesDiffer = true;
659660
}
661+
}
660662

661-
if (articlesDiffer) {
662-
// update unread count
663-
const oldUnread = feedData[r.uid].map((art) => !art.isRead).filter(Boolean).length;
664-
const newUnread = r.articles.map((art) => !art.isRead).filter(Boolean).length;
665-
const unreadDifference = newUnread - oldUnread;
663+
if (articlesDiffer) {
664+
// update unread count
665+
const oldUnread = feedData[r.uid].map((art) => !art.isRead).filter(Boolean).length;
666+
const newUnread = r.articles.map((art) => !art.isRead).filter(Boolean).length;
667+
const unreadDifference = newUnread - oldUnread;
666668

667-
// find all parents (and self) and add unread difference
668-
for (const row of rssFeedTable.getRowValues()) {
669-
if (r.fullName.slice(0, row.full_data.dataPath.length) === row.full_data.dataPath)
670-
row.full_data.unread += unreadDifference;
671-
}
669+
// find all parents (and self) and add unread difference
670+
for (const row of rssFeedTable.getRowValues()) {
671+
if (r.fullName.slice(0, row.full_data.dataPath.length) === row.full_data.dataPath)
672+
row.full_data.unread += unreadDifference;
673+
}
672674

673-
needsUpdate = true;
675+
needsUpdate = true;
674676

675-
// update data
676-
feedData[r.uid] = r.articles;
677+
// update data
678+
feedData[r.uid] = r.articles;
677679

678-
// if feed that is open changed, reload
679-
if ((openedFeedPath !== undefined) && (r.fullName.slice(0, openedFeedPath.length) === openedFeedPath))
680-
showRssFeed(r.fullName);
680+
// if feed that is open changed, reload
681+
if ((openedFeedPath !== undefined) && (r.fullName.slice(0, openedFeedPath.length) === openedFeedPath))
682+
showRssFeed(r.fullName);
683+
}
684+
else {
685+
// calculate read difference and update feed data
686+
let readDifference = 0;
687+
let readChanged = false;
688+
for (let i = 0; i < r.articles.length; ++i) {
689+
const oldRead = feedData[r.uid][i].isRead ? 1 : 0;
690+
const newRead = r.articles[i].isRead ? 1 : 0;
691+
feedData[r.uid][i].isRead = r.articles[i].isRead;
692+
readDifference += oldRead - newRead;
693+
if (readDifference !== 0)
694+
readChanged = true;
681695
}
682-
else {
683-
// calculate read difference and update feed data
684-
let readDifference = 0;
685-
let readChanged = false;
686-
for (let i = 0; i < r.articles.length; ++i) {
687-
const oldRead = feedData[r.uid][i].isRead ? 1 : 0;
688-
const newRead = r.articles[i].isRead ? 1 : 0;
689-
feedData[r.uid][i].isRead = r.articles[i].isRead;
690-
readDifference += oldRead - newRead;
691-
if (readDifference !== 0)
692-
readChanged = true;
693-
}
694696

695-
// if read on article changed
696-
if (readChanged) {
697-
needsUpdate = true;
698-
// find all items that contain this rss feed and add read difference
699-
for (const row of rssFeedTable.getRowValues()) {
700-
if (r.fullName.slice(0, row.full_data.dataPath.length) === row.full_data.dataPath)
701-
row.full_data.unread += readDifference;
702-
}
697+
// if read on article changed
698+
if (readChanged) {
699+
needsUpdate = true;
700+
// find all items that contain this rss feed and add read difference
701+
for (const row of rssFeedTable.getRowValues()) {
702+
if (r.fullName.slice(0, row.full_data.dataPath.length) === row.full_data.dataPath)
703+
row.full_data.unread += readDifference;
704+
}
703705

704-
// if feed that is opened changed update dynamically
705-
if ((openedFeedPath !== undefined) && (r.fullName.slice(0, openedFeedPath.length) === openedFeedPath)) {
706-
for (let i = 0; i < r.articles.length; ++i) {
707-
for (const row of rssArticleTable.getRowValues()) {
708-
if ((row.full_data.feedUid === r.uid) && (row.full_data.dataId === r.articles[i].id)) {
709-
row.full_data.isRead = r.articles[i].isRead;
710-
break;
711-
}
706+
// if feed that is opened changed update dynamically
707+
if ((openedFeedPath !== undefined) && (r.fullName.slice(0, openedFeedPath.length) === openedFeedPath)) {
708+
for (let i = 0; i < r.articles.length; ++i) {
709+
for (const row of rssArticleTable.getRowValues()) {
710+
if ((row.full_data.feedUid === r.uid) && (row.full_data.dataId === r.articles[i].id)) {
711+
row.full_data.isRead = r.articles[i].isRead;
712+
break;
712713
}
713714
}
714715
}
715716
}
716717
}
717-
});
718+
}
719+
}
718720
if (statusDiffers)
719721
rssFeedTable.updateTable(true);
720722
if (needsUpdate) {
@@ -878,8 +880,10 @@
878880
const markItemAsRead = (path) => {
879881
// feed data mark as read
880882
for (const feedID in feedData) {
881-
if (pathByFeedId.get(feedID).slice(0, path.length) === path)
882-
feedData[feedID].each((el) => el.isRead = true);
883+
if (pathByFeedId.get(feedID).slice(0, path.length) === path) {
884+
for (const el of feedData[feedID])
885+
el.isRead = true;
886+
}
883887
}
884888

885889
// mark rows as read
@@ -938,12 +942,12 @@
938942

939943
// update feed data
940944
let prevReadState = true;
941-
feedData[uid].each((article) => {
945+
for (const article of feedData[uid]) {
942946
if (article.id === id) {
943947
prevReadState = article.isRead;
944948
article.isRead = true;
945949
}
946-
});
950+
}
947951

948952
if (!prevReadState) {
949953
// find all items that contain this feed and subtract 1
@@ -976,9 +980,10 @@
976980
.map((sRow) => rssFeedTable.getRow(sRow).full_data.dataPath);
977981
// filter children
978982
const reducedDatapaths = selectedDatapaths.filter((path) =>
979-
selectedDatapaths.filter((innerPath) => path.slice(0, innerPath.length) === innerPath).length === 1
983+
selectedDatapaths.filter(path.startsWith).length === 1
980984
);
981-
reducedDatapaths.each((path) => markItemAsRead(path));
985+
for (const path of reducedDatapaths)
986+
markItemAsRead(path);
982987
};
983988

984989
const openRssDownloader = () => {

src/webui/www/private/views/rssDownloader.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -686,13 +686,13 @@
686686
name: feed,
687687
isFeed: true
688688
});
689-
responseJSON[feed].each((article) => {
689+
for (const article of responseJSON[feed]) {
690690
rssDownloaderArticlesTable.updateRowData({
691691
rowId: ++rowID,
692692
name: article,
693693
isFeed: false
694694
});
695-
});
695+
}
696696
}
697697
rssDownloaderArticlesTable.updateTable(false);
698698
});

0 commit comments

Comments
 (0)