2020-12-01 08:46:59 +00:00
|
|
|
import { gettext } from "django";
|
2020-11-29 21:14:48 +00:00
|
|
|
import { html, LitElement, property, TemplateResult } from "lit-element";
|
|
|
|
import { PBResponse } from "../../api/client";
|
|
|
|
import { COMMON_STYLES } from "../../common/styles";
|
|
|
|
|
2020-11-30 11:33:09 +00:00
|
|
|
export abstract class Table<T> extends LitElement {
|
|
|
|
abstract apiEndpoint(page: number): Promise<PBResponse<T>>;
|
2020-11-29 21:14:48 +00:00
|
|
|
abstract columns(): Array<string>;
|
2020-11-30 22:49:33 +00:00
|
|
|
abstract row(item: T): Array<string>;
|
2020-11-29 21:14:48 +00:00
|
|
|
|
|
|
|
@property()
|
2020-11-30 11:33:09 +00:00
|
|
|
data?: PBResponse<T>;
|
2020-11-29 21:14:48 +00:00
|
|
|
|
|
|
|
@property()
|
2020-12-01 08:15:41 +00:00
|
|
|
page = 1;
|
2020-11-29 21:14:48 +00:00
|
|
|
|
|
|
|
static get styles() {
|
|
|
|
return [COMMON_STYLES];
|
|
|
|
}
|
|
|
|
|
|
|
|
public fetch() {
|
|
|
|
this.apiEndpoint(this.page).then((r) => {
|
|
|
|
this.data = r;
|
|
|
|
this.page = r.pagination.current;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-11-30 11:33:09 +00:00
|
|
|
private renderLoading(): TemplateResult {
|
|
|
|
return html`<tr role="row">
|
|
|
|
<td role="cell" colspan="25">
|
|
|
|
<div class="pf-l-bullseye">
|
|
|
|
<div class="pf-c-empty-state pf-m-sm">
|
|
|
|
<div class="pf-c-empty-state__content">
|
|
|
|
<div class="pf-c-empty-state__icon">
|
|
|
|
<span class="pf-c-spinner" role="progressbar">
|
|
|
|
<span class="pf-c-spinner__clipper"></span>
|
|
|
|
<span class="pf-c-spinner__lead-ball"></span>
|
|
|
|
<span class="pf-c-spinner__tail-ball"></span>
|
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
<h2 class="pf-c-title pf-m-lg">Loading</h2>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</td>
|
|
|
|
</tr>`;
|
|
|
|
}
|
|
|
|
|
2020-11-29 21:14:48 +00:00
|
|
|
private renderRows(): TemplateResult[] | undefined {
|
|
|
|
if (!this.data) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return this.data.results.map((item) => {
|
2020-12-01 08:15:41 +00:00
|
|
|
const fullRow = ["<tr role=\"row\">"].concat(
|
2020-11-29 21:14:48 +00:00
|
|
|
this.row(item).map((col) => {
|
|
|
|
return `<td role="cell">${col}</td>`;
|
|
|
|
})
|
|
|
|
);
|
2020-12-01 08:15:41 +00:00
|
|
|
fullRow.push("</tr>");
|
2020-11-29 21:14:48 +00:00
|
|
|
return html(<any>fullRow);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-11-30 22:49:33 +00:00
|
|
|
renderTable() {
|
2020-11-29 21:14:48 +00:00
|
|
|
if (!this.data) {
|
|
|
|
this.fetch();
|
|
|
|
}
|
|
|
|
return html`<div class="pf-c-toolbar">
|
|
|
|
<div class="pf-c-toolbar__content">
|
|
|
|
<div class="pf-c-toolbar__bulk-select">
|
|
|
|
<slot name="create-button"></slot>
|
|
|
|
<button
|
2020-12-01 08:46:59 +00:00
|
|
|
@click=${() => {this.fetch();}}
|
2020-11-29 21:14:48 +00:00
|
|
|
class="pf-c-button pf-m-primary"
|
|
|
|
>
|
2020-12-01 08:46:59 +00:00
|
|
|
${gettext("Refresh")}
|
2020-11-29 21:14:48 +00:00
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
<pb-table-pagination
|
|
|
|
class="pf-c-toolbar__item pf-m-pagination"
|
|
|
|
.table=${this}
|
|
|
|
></pb-table-pagination>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<table class="pf-c-table pf-m-compact pf-m-grid-md">
|
|
|
|
<thead>
|
|
|
|
<tr role="row">
|
|
|
|
${this.columns().map(
|
2020-12-01 09:21:04 +00:00
|
|
|
(col) => html`<th role="columnheader" scope="col">${gettext(col)}</th>`
|
|
|
|
)}
|
2020-11-29 21:14:48 +00:00
|
|
|
</tr>
|
|
|
|
</thead>
|
|
|
|
<tbody role="rowgroup">
|
2020-11-30 11:33:09 +00:00
|
|
|
${this.data ? this.renderRows() : this.renderLoading()}
|
2020-11-29 21:14:48 +00:00
|
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
<div class="pf-c-pagination pf-m-bottom">
|
|
|
|
<pb-table-pagination
|
|
|
|
class="pf-c-toolbar__item pf-m-pagination"
|
|
|
|
.table=${this}
|
|
|
|
></pb-table-pagination>
|
|
|
|
</div>`;
|
|
|
|
}
|
2020-11-30 22:49:33 +00:00
|
|
|
|
|
|
|
render() {
|
|
|
|
return this.renderTable();
|
|
|
|
}
|
2020-11-29 21:14:48 +00:00
|
|
|
}
|