2022-02-27 02:06:33 +00:00
|
|
|
"use strict";
|
|
|
|
|
2022-03-06 02:39:52 +00:00
|
|
|
import Emel from 'emel';
|
2022-03-10 02:53:30 +00:00
|
|
|
import { NightInk as nightink } from 'nightink';
|
2022-03-06 02:39:52 +00:00
|
|
|
|
|
|
|
import AbstractUIItem from './AbstractUIItem.mjs';
|
2022-03-10 02:53:30 +00:00
|
|
|
import peer_name from './peer_name.mjs';
|
2022-03-06 02:39:52 +00:00
|
|
|
|
|
|
|
class UITable extends AbstractUIItem {
|
|
|
|
constructor(el, def) {
|
|
|
|
super(el, def);
|
|
|
|
|
|
|
|
this.emel = new Emel().emel;
|
|
|
|
|
2022-03-10 02:53:30 +00:00
|
|
|
this.el.replaceChildren(this.emel(`table>(thead>(tr>th{Peer}+${
|
|
|
|
Object.keys(def.content).map(header => `th{${header}}`).join("+")
|
|
|
|
})+tbody`));
|
2022-03-06 02:39:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
clear() {
|
|
|
|
super.clear();
|
2022-03-10 02:53:30 +00:00
|
|
|
this.el.querySelector(`tbody`).replaceChildren();
|
2022-03-06 02:39:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
append(peer, table) {
|
|
|
|
super.append(peer, table);
|
|
|
|
|
2022-03-10 02:53:30 +00:00
|
|
|
const el_tbody = this.el.querySelector("tbody");
|
|
|
|
|
2022-03-06 02:39:52 +00:00
|
|
|
let new_row = document.createElement("tr");
|
2022-03-10 02:53:30 +00:00
|
|
|
// TODO: Insert a <td /> for the name of the peer here
|
|
|
|
new_row.appendChild(peer_name(peer));
|
|
|
|
|
|
|
|
// Note here that the comma is *important*. This is a destructuring assignment where we ignore the first value.
|
|
|
|
for(let [ , template ] of Object.entries(this.def.content)) {
|
2022-03-06 02:39:52 +00:00
|
|
|
let table_cell = document.createElement("td");
|
2022-03-10 02:53:30 +00:00
|
|
|
table_cell.appendChild(document.createTextNode(nightink(template, table)));
|
|
|
|
new_row.appendChild(table_cell);
|
|
|
|
}
|
|
|
|
|
|
|
|
new_row.dataset.peer_name = peer.name;
|
|
|
|
new_row.dataset.peer_id = peer.id;
|
|
|
|
|
|
|
|
const comparer = new Intl.Collator(navigator.language);
|
|
|
|
|
|
|
|
// If the peer id exactly matches, replace this row instead
|
|
|
|
for(let row of el_tbody.childNodes) {
|
|
|
|
if(peer.id === row.dataset.peer_id) {
|
|
|
|
row.before(new_row);
|
|
|
|
el_tbody.removeChild(row);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, insert it such that the list is sorted
|
|
|
|
// FUTURE: Use a fanceh thingy that sorts table data?
|
|
|
|
for(let row of el_tbody.childNodes) {
|
|
|
|
const comp_name = comparer.compare(
|
|
|
|
peer.name.toLowerCase(),
|
|
|
|
row.dataset.peer_name.toLowerCase()
|
|
|
|
);
|
|
|
|
const comp_id = comparer.compare(peer.id, row.dataset.peer_id);
|
|
|
|
|
|
|
|
let insert_before = false;
|
|
|
|
if(comp_name < 0) insert_before = true;
|
|
|
|
if(!insert_before && comp_name === 0 && comp_id < 0) insert_before = true;
|
|
|
|
|
|
|
|
if(insert_before) {
|
|
|
|
row.before(new_row);
|
|
|
|
return;
|
|
|
|
}
|
2022-03-06 02:39:52 +00:00
|
|
|
}
|
2022-02-27 02:06:33 +00:00
|
|
|
|
2022-03-10 02:53:30 +00:00
|
|
|
// In the unlikely event we failed to insert it somewhere, make sure it's inserted
|
|
|
|
// e.g. if this is the first row, or sorts after all existing rows
|
|
|
|
el_tbody.appendChild(new_row);
|
2022-02-27 02:06:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default UITable;
|