Huge rewrite

Auto-refresh and sort are disabled for now
This commit is contained in:
2025-10-09 15:42:31 +02:00
parent b2f155c0b4
commit cabc0fbaf1
7 changed files with 350 additions and 168 deletions

View File

@@ -1,25 +1,98 @@
"use strict";
class trdom_torrentcontrol {
class trdom_torrentcontrol extends EventTarget {
#logcb;
#log(loglevel, msg) {
if (this.#logcb !== null) {
this.#logcb('trdom_servermanager', loglevel, msg);
this.#logcb(`server:${this.#server.name}:${this.#hash}`, loglevel, msg);
}
}
#element = document.createElement('div');
getElement() {
get element() {
return this.#element;
}
#hash;
#server;
//TODO: remove
#getKnownTorrents;
#data;
get exists() {
return this.#data != null;
}
updateDOM() {
let control_element = this.#element;
let status = control_element.element_status;
control_element.classList.remove(
'trweb_status_asdf',
'trweb_status_offline',
'trweb_status_nonexistent',
'trweb_status_paused',
'trweb_status_verifqueued',
'trweb_status_verifying',
'trweb_status_downloading',
'trweb_status_seeding'
);
nukeChildren(status);
let statustext = 'Nothing to see here';
let statusclass = 'trweb_status_asdf'
let barwidth = 50;
if (!this.#server.isOnline()) {
statustext = "Server offline";
statusclass = 'trweb_status_offline';
barwidth = 0;
}
else {
if (!this.exists) {
//this.#log(5, JSON.stringify(this.#data));
statustext = "Not available";
statusclass = 'trweb_status_nonexistent';
barwidth = 0;
}
else {
let server_status = this.#data;
statustext = `${server_status.status}`;
barwidth = server_status.percentDone;
switch (server_status.status) {
case 0:
statustext = 'Paused';
statusclass = 'trweb_status_paused';
break;
case 1:
statustext = 'Queued for verification';
statusclass = 'trweb_status_verifqueued';
break;
case 2:
statustext = 'Verifying';
statusclass = 'trweb_status_verifying';
break;
case 3:
statustext = 'Queued';
break;
case 4:
statustext = 'Downloading';
statusclass = 'trweb_status_downloading';
break;
case 6:
statustext = 'Seeding';
statusclass = 'trweb_status_seeding';
break;
}
statustext = `[${this.#server.name}]: ${statustext} - ${server_status.percentDone}%`;
}
}
control_element.element_statusbar.style.width = `${barwidth}%`;
control_element.classList.add(statusclass);
status.appendChild(document.createTextNode(statustext));
}
handleEvent(e) {
this.#log(6, `Handling event of type ${e.type}`);
switch (e.type) {
case 'click':
if (e.currentTarget.hasAttributeNS('trweb', 'torrentlistfield')) {
@@ -33,7 +106,7 @@ class trdom_torrentcontrol {
e.stopPropagation();
break;
case 'magnet':
let magneturl;
/*let magneturl;
for (const [srv, data] of Object.entries(this.#getKnownTorrents()[this.#hash].servers)) {
if (data.magnetLink != null) {
magneturl = data.magnetLink;
@@ -45,8 +118,9 @@ class trdom_torrentcontrol {
}
else {
this.#log(3, `Couldn't find magnet link! ${JSON.stringify(this.#getKnownTorrents()[this.#hash].servers)}`);
}
}*/
e.stopPropagation();
window.alert('not implemented');
break;
case 'download':
e.stopPropagation();
@@ -54,14 +128,27 @@ class trdom_torrentcontrol {
break;
}
}
break;
case 'torrent-deleted':
this.#log(6, 'we be delet');
this.#data = null;
this.updateDOM();
break;
case 'torrent-updated':
if (e.detail.torrentHash == this.#hash) {
this.#log(6, `Got data from server: ${JSON.stringify(e.detail.torrentInfo)}`);
this.#data = e.detail.torrentInfo;
this.updateDOM();
}
break;
}
}
constructor(hash, server, logcb, getKnownTorrents) {
constructor(hash, server, initdata = null, logcb = null) {
super();
this.#hash = hash;
this.#server = server;
this.#logcb = logcb;
this.#getKnownTorrents = getKnownTorrents;
let srv = server.name;
@@ -130,6 +217,16 @@ class trdom_torrentcontrol {
this.#element.element_pause = element_pause;
this.#element.element_magnet = element_magnet;
this.#element.element_download = element_download;
this.#data = initdata;
this.updateDOM();
this.addEventListener('torrent-updated', this);
this.addEventListener('torrent-deleted', this);
server.addControl(hash, this);
}
}