UNPKG

@3mo/moddable-data-grid

Version:

A moddable variant of @3mo/fetchable-data-grid

127 lines (126 loc) 4.83 kB
import { NotificationComponent } from '@a11d/lit-application'; import { equals } from '@a11d/equals'; import { Localizer } from '@3mo/localization'; Localizer.dictionaries.add({ en: { 'ModdableDataGridMode.Copy': 'Copy', }, de: { 'View "${name:string}" moved to archive': 'Ansicht "${name}" ins Archiv verschoben', 'ModdableDataGridMode.Copy': 'Kopie', } }); export class ModdableDataGridModeColumn { static fromColumn(column) { return new ModdableDataGridModeColumn({ dataSelector: column.dataSelector, width: column.width, hidden: column.hidden, sticky: column.sticky, }); } constructor(init) { Object.assign(this, structuredClone(init)); } [equals](other) { return other.dataSelector === this.dataSelector && other.width === this.width && other.hidden === this.hidden && other.sticky === this.sticky; } apply(column) { column.width = this.width ?? column.width; column.hidden = this.hidden ?? column.hidden; column.sticky = this.sticky ?? column.sticky; return column; } } export class ModdableDataGridMode { /** * Extracts the mode from the current point in time of the data grid * @param dataGrid The data grid to extract the mode from * @returns The extracted mode */ static fromDataGrid(dataGrid) { return new ModdableDataGridMode({ // Non situational properties id: dataGrid.mode?.id, name: dataGrid.mode?.name, archived: dataGrid.mode?.archived, // Situational properties columns: dataGrid.columns.map(c => ModdableDataGridModeColumn.fromColumn(c)), pagination: dataGrid.pagination, parameters: structuredClone(dataGrid.parameters) ?? {}, sorting: structuredClone(dataGrid.sorting) ?? [], }); } constructor(init) { this.archived = false; Object.assign(this, structuredClone(init)); if (this.columns) { this.columns = init?.columns?.map(c => new ModdableDataGridModeColumn(c)); } if (this.parameters) { for (const [key, value] of Object.entries(this.parameters)) { if (value && typeof value === 'string' && DateTime.isoRegularExpression.test(value)) { this.parameters[key] = new Date(value); } if (value && typeof value === 'object' && ('start' in value || 'end' in value)) { this.parameters[key] = new DateTimeRange(value.start, value.end); } if (value instanceof DateTimeRange) { this.parameters[key] = new DateTimeRange(value.start, value.end); } } } this.id ?? (this.id = crypto.randomUUID()); } clone() { return new ModdableDataGridMode(this); } with(mode) { return new ModdableDataGridMode({ ...this, ...mode }); } copy(name) { name ?? (name = `${this.name} - ${t('ModdableDataGridMode.Copy')}`); return this.with({ id: undefined, name }); } [equals](other) { return other.name === this.name && other.id === this.id && other.archived === this.archived && Object[equals](other.columns, this.columns) && Object[equals](other.sorting, this.sorting) && Object[equals](other.pagination, this.pagination) && Object[equals](other.definedParameters, this.definedParameters); } get definedParameters() { return Object.fromEntries(Object.entries(this.parameters ?? {}) .filter(([, value]) => value !== undefined && value !== null && value !== '' && (Array.isArray(value) ? value.length > 0 : true))); } save(dataGrid) { return dataGrid.modesController.save(this); } select(dataGrid) { return dataGrid.modesController.set(this); } async archive(dataGrid) { this.archived = true; await this.save(dataGrid); NotificationComponent.notifySuccess(t('View "${name:string}" moved to archive', { name: this.name })); } async unarchive(dataGrid) { this.archived = false; await this.save(dataGrid); } delete(dataGrid) { return dataGrid.modesController.delete(this); } apply(dataGrid) { const clone = this.clone(); dataGrid.setColumns(dataGrid.extractedColumns.map(c1 => clone.columns?.find(c2 => c1.dataSelector === c2.dataSelector)?.apply(c1) ?? c1)); dataGrid.sort(clone.sorting ?? []); dataGrid.setPagination(clone.pagination); dataGrid.setParameters(clone.parameters ?? {}); } }