"use strict"; class trdom_torrentmanager extends EventTarget { #logcb; #log(loglevel, msg) { if (this.#logcb !== null) { this.#logcb('trdom_torrentmanager', loglevel, msg); } } #element = document.createElement('div'); get element() { return this.#element; } #torrentDB = {}; get torrents() { return this.#torrentDB; } constructor(logcb = null) { super(); this.#logcb = logcb; this.#element.classList.add('trweb_torrentlistview', 'container-fluid'); } #putToPlace(torrent) { let sortedTorrents = Object.values(this.#torrentDB).sort((a, b) => a.name.localeCompare(b.name)); let previous = sortedTorrents.find((val, ind, arr) => val.name.localeCompare(torrent.name) > 0); if (previous == undefined) { this.element.appendChild(torrent.element); } else { this.element.insertBefore(torrent.element, previous.element); } } handleEvent(e) { this.#log(6, `Handling event of type ${e.type}`); switch (e.type) { case 'torrent-created': let initHash = e.detail.torrentHash; if (this.#torrentDB[initHash] == null) { let torrentCreated = new trtorrent(initHash); this.#torrentDB[initHash] = torrentCreated; torrentCreated.addEventListener('torrent-renamed', this); this.#putToPlace(torrentCreated); //this.#element.appendChild(torrentCreated.element); for (const [srv, control] of Object.entries(e.detail.torrentControls)) { torrentCreated.addControl(srv, control) } } else { this.#log(1, 'This event should never ever fire for a torrent that already exists'); window.alert('We be ded, check console'); } break; case 'torrent-renamed': this.#putToPlace(e.target); break; case 'torrentserver-added': let addedServer = e.detail.serverObject; for (const [hash, torrent] of Object.entries(this.#torrentDB)) { torrent.addControl(addedServer.name, new trdom_torrentcontrol(hash, addedServer, null, this.#logcb)) } break; default: this.#log(5, `Event type ${e.type} not supported`); break; } } }