2022-02-26 21:58:51 +00:00
"use strict" ;
2022-02-27 02:05:45 +00:00
import Emel from 'emel' ;
import fa _emel from './forkawesome_emel.mjs' ;
2022-03-11 01:58:43 +00:00
import AbstractUIItem from './AbstractUIItem.mjs' ;
2022-03-10 23:09:22 +00:00
import ui _item _index from './ui_item_index.mjs' ;
2022-03-11 01:48:58 +00:00
import tabledefs from '../tabledefs/index.mjs' ;
2022-03-06 02:39:52 +00:00
2022-02-26 21:58:51 +00:00
class TableView {
constructor ( el ) {
2022-03-06 02:39:52 +00:00
if ( el === null || typeof el === "undefined" ) throw new Error ( ` Expected HTML element as the first argument, but got null or undefined. ` ) ;
2022-02-26 21:58:51 +00:00
this . el = el ;
this . el _parts = new Map ( ) ;
2022-02-27 02:05:45 +00:00
this . emel = new Emel ( ) . emel ;
2022-02-26 21:58:51 +00:00
}
2022-03-11 01:48:58 +00:00
/ * *
* Clears the currently displayed table .
* @ return { void }
* /
2022-02-26 21:58:51 +00:00
clear ( ) {
this . el _parts . clear ( ) ;
2022-03-11 01:48:58 +00:00
// No need to call this here, as everything gets replaced instantly at the end of init()
2022-03-06 02:39:52 +00:00
// this.el.replaceChildren();
2022-02-26 21:58:51 +00:00
}
2022-03-11 01:48:58 +00:00
/ * *
* 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 }
* /
2022-02-27 02:05:45 +00:00
init ( tabledef ) {
this . clear ( ) ;
2022-03-06 02:39:52 +00:00
const parts = this . emel ( ` h2>( ${ fa _emel ( tabledef . icon ) } +{?})^div.data-display ` , {
2022-02-27 02:05:45 +00:00
placeholders : [
tabledef . name
]
} ) ;
2022-03-06 02:39:52 +00:00
const el _dataitems = parts . querySelector ( ".data-display" ) ;
2022-02-27 02:05:45 +00:00
for ( let def of tabledef . items ) {
2022-03-06 02:39:52 +00:00
let el = document . createElement ( "div" ) ;
el . classList . add ( "data-item" ) ;
2022-03-14 02:40:34 +00:00
console . log ( ui _item _index [ def . type ] ) ;
if ( typeof ui _item _index [ def . type ] !== "undefined" ) {
2022-03-10 23:09:22 +00:00
let item _manager = new ui _item _index [ def . type ] ( el , def ) ;
this . el _parts . set ( def . name , item _manager ) ;
2022-03-14 02:40:34 +00:00
console . log ( ` TableView:UIItem create ` , item _manager ) ;
2022-03-10 23:09:22 +00:00
}
else {
2022-03-14 02:40:34 +00:00
console . warn ( ` Unknown item definition type ' ${ def . type } ', ignoring. This is probably a bug. ` , def ) ;
continue ;
2022-03-10 02:53:30 +00:00
}
2022-03-14 02:40:34 +00:00
// Only append the item to the display if we actually found a manager for it
el _dataitems . appendChild ( el ) ;
2022-02-27 02:05:45 +00:00
}
this . el . replaceChildren ( parts ) ;
}
2022-03-11 01:48:58 +00:00
/ * *
* 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 .
* /
2022-02-26 21:58:51 +00:00
async switch _table ( table _name ) {
2022-02-27 02:05:45 +00:00
table _name = table _name . replace ( /[^a-z0-9-_]/g , "" ) ;
2022-03-11 01:48:58 +00:00
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 ] ) ;
2022-02-26 21:58:51 +00:00
for await ( let tabledef of await globalThis . sysquery . table ( table _name ) ) {
this . append _table ( tabledef . peer , tabledef . table ) ;
}
}
2022-03-11 01:48:58 +00:00
/ * *
* 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 }
* /
2022-02-26 21:58:51 +00:00
append _table ( peer , table ) {
2022-03-14 02:40:34 +00:00
console . log ( ` TableView:append PEER ` , peer , ` TABLE ` , table ) ;
2022-03-10 03:04:50 +00:00
for ( let [ def _name , item _manager ] of this . el _parts ) {
if ( item _manager === null ) continue ;
item _manager . append ( peer , table ) ;
}
2022-02-26 21:58:51 +00:00
}
}
export default TableView ;