import { gettext } from "django"; import { CSSResult, html, LitElement, property, TemplateResult } from "lit-element"; import { PBResponse } from "../../api/client"; import { COMMON_STYLES } from "../../common/styles"; import { htmlFromString } from "../../utils"; import "./TablePagination"; export abstract class Table extends LitElement { abstract apiEndpoint(page: number): Promise>; abstract columns(): Array; abstract row(item: T): Array; @property({attribute: false}) data?: PBResponse; @property({type: Number}) page = 1; static get styles(): CSSResult[] { return COMMON_STYLES; } constructor() { super(); this.addEventListener("ak-refresh", () => { this.fetch(); }); } public fetch(): void { this.apiEndpoint(this.page).then((r) => { this.data = r; this.page = r.pagination.current; }); } private renderLoading(): TemplateResult { return html`

Loading

`; } private renderRows(): TemplateResult[] | undefined { if (!this.data) { return; } return this.data.results.map((item) => { const fullRow = [""].concat( this.row(item).map((col) => { return `${col}`; }) ); fullRow.push(""); return htmlFromString(...fullRow); }); } renderTable(): TemplateResult { if (!this.data) { this.fetch(); } return html`
{this.page = page; }}>
${this.columns().map((col) => html``)} ${this.data ? this.renderRows() : this.renderLoading()}
${gettext(col)}
{ this.page = page; }}>
`; } render(): TemplateResult { return this.renderTable(); } }