import { ListResult } from '../lib/client'; const translations: Record> = { pl_PL: { Loading: 'Wczytywanie', 'Previous page': 'Poprzednia strona', 'Next page': 'Następna strona', }, }; const t = (s: string) => { return translations[window.game_data.locale]?.[s] ?? s; }; export type DialogTableColumn = { header: string; accessor: (row: T) => string; }; export class DialogTable { constructor( private readonly id: string, private readonly columns: DialogTableColumn[], private readonly limit: number, private readonly loadData: ( page: number, limit: number ) => Promise> ) {} public async render() { await this.renderPage(1); } private async renderPage(page: number) { window.Dialog.show(`${this.id}_loading`, `

${t('Loading')}...

`); const { data, total } = await this.loadData(page, this.limit); const maxPage = Math.ceil(total / this.limit); const pageOpts = []; for (let i = 1; i <= (page > maxPage ? page : maxPage); i++) { pageOpts.push( `${i}` ); } const prevPageId = `${this.id}_page_prev`; const selectId = `${this.id}_page_select`; const nextPageId = `${this.id}_page_next`; window.Dialog.show( this.id, `
${this.columns.map((col) => ``).join('')} ${data .map( (r) => ` ${this.columns .map((col) => ``) .join('')} ` ) .join('')}
${col.header}
${col.accessor(r)}
` ); document .querySelector('#' + prevPageId) ?.addEventListener('click', (e: Event) => { this.renderPage(page - 1); }); document .querySelector('#' + selectId) ?.addEventListener('change', (e: Event) => { if (!(e.currentTarget instanceof HTMLSelectElement)) { return; } this.renderPage(parseInt(e.currentTarget.value)); }); document .querySelector('#' + nextPageId) ?.addEventListener('click', (e: Event) => { this.renderPage(page + 1); }); } }