Skip to content

Commit 65a844f

Browse files
authored
Merge pull request #14 from fullerhkz/fix/add-dialog-close-after-add
fix(dialogs): close the Add Torrent dialog after a confirmed add
2 parents a6ed6ac + 7648820 commit 65a844f

2 files changed

Lines changed: 53 additions & 2 deletions

File tree

‎init.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
/* Cache key for every theme asset URL. The build tool rewrites the value
2020
* whenever a theme file changes, so a changed file always resolves to a new
2121
* URL and no stale copy is served from the browser cache. */
22-
var CQB_REV = "b82dd246e2";
22+
var CQB_REV = "1fbd68bee5";
2323
/* The released theme version (major.minor.patch). The release tool rewrites
2424
* this value; it is surfaced on window.cqb.version for About/diagnostics. */
2525
var CQB_VERSION = "2.6.1";

‎js/dialogs.js‎

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@
321321
formFile.classList.add("cqb-file-tucked");
322322

323323
/* Drag/drop feeds the native FileList; chips reflect it; Add re-syncs. */
324-
wireDropzone(zone, chips, fileInput, { multiple: true, onChange: function () { syncAdd(); } });
324+
var dz = wireDropzone(zone, chips, fileInput, { multiple: true, onChange: function () { syncAdd(); } });
325325

326326
/* Segmented toggle: show one panel at a time, keeping the other's value. */
327327
function setSrc(mode) {
@@ -373,6 +373,57 @@
373373
}
374374
urlArea && urlArea.addEventListener("input", syncAdd);
375375
syncAdd();
376+
377+
/* Core empties #torrent_file and #url with .val(""), which fires no
378+
* event: on every open (beforeShow) and after a confirmed add
379+
* (addTorrentFinished). Re-read both so the chips and Add match. */
380+
var opened = 0;
381+
function resync() {
382+
dz.renderChips();
383+
syncAdd();
384+
}
385+
if (window.theDialogManager && theDialogManager.addHandler) {
386+
theDialogManager.addHandler("tadd", "afterShow", function () { opened++; resync(); });
387+
}
388+
389+
/* Close the dialog once an add is confirmed. Core answers an upload by
390+
* redirecting the hidden frame to addtorrent.php?result[]=<status>&...,
391+
* one final status per item (Success, Duplicate, Failed...), so the frame
392+
* URL says whether anything was added. The source field cannot: ruTorrent
393+
* 5.3.18 and earlier empty it after every answer, refusals included. Only
394+
* a load that follows a real submit counts (the frame also loads once,
395+
* blank, when core inserts it). Add stays disabled while the upload is in
396+
* flight, and an answer that lands after the dialog was closed or reopened
397+
* leaves the dialog alone. */
398+
function addedAny(frame) {
399+
try {
400+
return new URLSearchParams(frame.contentWindow.location.search).getAll("result[]").indexOf("Success") >= 0;
401+
} catch (e) {
402+
return false;
403+
}
404+
}
405+
function closeAfterAdd(form, frameName) {
406+
var frame = document.querySelector('iframe[name="' + frameName + '"]');
407+
if (!form || !frame) return;
408+
var pending = false, sentIn = -1;
409+
form.addEventListener("submit", function (e) {
410+
if (e.defaultPrevented) return;
411+
pending = true;
412+
sentIn = opened;
413+
add.disabled = true;
414+
});
415+
frame.addEventListener("load", function () {
416+
if (!pending) return;
417+
pending = false;
418+
resync();
419+
var dm = window.theDialogManager;
420+
if (addedAny(frame) && sentIn === opened && dm && dm.visible && dm.visible.indexOf("tadd") >= 0) {
421+
dm.hide("tadd");
422+
}
423+
});
424+
}
425+
closeAfterAdd(formFile, "uploadfrm");
426+
closeAfterAdd(formUrl, "uploadfrmurl");
376427
}
377428

378429
/* Parse MediaInfo "Key : Value" output into {name, rows:[{k,v}]} sections. */

0 commit comments

Comments
 (0)