From 33e247f1c1f9b836335d03b1e3a26f0272db637d Mon Sep 17 00:00:00 2001 From: Starbeamrainbowlabs Date: Fri, 11 Mar 2022 01:48:58 +0000 Subject: [PATCH] TableView: comment properly --- src/static/js/ui/TableView.mjs | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/src/static/js/ui/TableView.mjs b/src/static/js/ui/TableView.mjs index e9e82fd..7fab691 100644 --- a/src/static/js/ui/TableView.mjs +++ b/src/static/js/ui/TableView.mjs @@ -4,6 +4,7 @@ import Emel from 'emel'; import fa_emel from './forkawesome_emel.mjs'; import ui_item_index from './ui_item_index.mjs'; +import tabledefs from '../tabledefs/index.mjs'; class TableView { constructor(el) { @@ -13,12 +14,22 @@ class TableView { this.emel = new Emel().emel; } + /** + * Clears the currently displayed table. + * @return {void} + */ clear() { this.el_parts.clear(); - // No need to call this here, as evereything gets replaced instantly at the end of init() + // No need to call this here, as everything gets replaced instantly at the end of init() // this.el.replaceChildren(); } + /** + * Initialises the view to display a new table. + * You probably want to call .switch_table() instead. + * @param {Object} tabledef The table definition to use to render the display for the new table. + * @return {void} + */ init(tabledef) { this.clear(); const parts = this.emel(`h2>(${fa_emel(tabledef.icon)}+{?})^div.data-display`, { @@ -44,14 +55,29 @@ class TableView { this.el.replaceChildren(parts); } + /** + * Switches the view to display a new table. + * Fetches table data from the server, so this may take a moment. + * @param {string} table_name The name of the new table to display. + * @return {Promise} A Promise that resolves when the updating of the UI is complete. + */ async switch_table(table_name) { table_name = table_name.replace(/[^a-z0-9-_]/g, ""); - this.clear(); + if(typeof tabledefs[table_name] === "undefined") + throw new Error(`Error: No definition found for table with the name '${table_name}' (in future, we should render thee raw JSON with a header containing the table name or something).`); + + this.init(tabledefs[table_name]); for await (let tabledef of await globalThis.sysquery.table(table_name)) { this.append_table(tabledef.peer, tabledef.table); } } + /** + * Appends the given peer and table data to the UI. + * @param {{id: string, name: string}} peer The peer information. + * @param {Object} table The table itself to display. + * @return {void} + */ append_table(peer, table) { for(let [ def_name, item_manager ] of this.el_parts) { if(item_manager === null) continue;