@3mo/moddable-data-grid
Version:
A moddable variant of @3mo/fetchable-data-grid
139 lines (138 loc) • 5.38 kB
JavaScript
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) {
continue;
}
if (value instanceof DateTimeRange) {
this.parameters[key] = new DateTimeRange(value.start, value.end);
}
if (typeof value === 'object' && ('start' in value || 'end' in value)) {
this.parameters[key] = new DateTimeRange(value.start, value.end);
}
if (typeof value === 'string' && DateTime.isoRegularExpression.test(value)) {
this.parameters[key] = new DateTime(value);
}
if (typeof value === 'string'
&& value.includes('~')
&& value.split('~').map(p => p.trim()).filter(Boolean).every(p => DateTime.isoRegularExpression.test(p))) {
this.parameters[key] = value.toDateTimeRange();
}
}
}
this.id ??= crypto.randomUUID();
}
clone() {
return new ModdableDataGridMode(this);
}
with(mode) {
return new ModdableDataGridMode({ ...this, ...mode });
}
copy(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();
const orderedColumns = clone.columns?.map(c1 => {
const extractedCol = dataGrid.extractedColumns.find(c2 => c2.dataSelector === c1.dataSelector);
return extractedCol ? c1.apply(extractedCol) : undefined;
}).filter(c => c !== undefined) ?? dataGrid.extractedColumns;
dataGrid.setColumns(orderedColumns);
dataGrid.sort(clone.sorting ?? []);
dataGrid.setPagination(clone.pagination);
dataGrid.setParameters(clone.parameters ?? {});
}
}