|
321 | 321 | formFile.classList.add("cqb-file-tucked"); |
322 | 322 |
|
323 | 323 | /* 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(); } }); |
325 | 325 |
|
326 | 326 | /* Segmented toggle: show one panel at a time, keeping the other's value. */ |
327 | 327 | function setSrc(mode) { |
|
373 | 373 | } |
374 | 374 | urlArea && urlArea.addEventListener("input", syncAdd); |
375 | 375 | 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"); |
376 | 427 | } |
377 | 428 |
|
378 | 429 | /* Parse MediaInfo "Key : Value" output into {name, rows:[{k,v}]} sections. */ |
|
0 commit comments