Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions table/src/-private/column.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { action, get } from '@ember/object';
import { get } from '@ember/object';
import { isEmpty } from '@ember/utils';

import type { CellComponent } from './cell-component.ts';
Expand Down Expand Up @@ -52,8 +52,7 @@ export class Column<
public config: ColumnConfig<T, unknown, Meta>,
) {}

@action
getValueForRow(row: Row<T>): ContentValue {
getValueForRow = (row: Row<T>): ContentValue => {
if (this.config.value) {
return this.config.value(this.#contextFor(row));
}
Expand All @@ -71,7 +70,7 @@ export class Column<
* properly constrained the type of value, (isEmpty doesn't narrow types either)
*/
return value as ContentValue;
}
};

private getDefaultValue(row: Row<T>) {
return this.getOptionsForRow(row)[DEFAULT_VALUE_KEY];
Expand All @@ -84,10 +83,9 @@ export class Column<
* The type also has the `@options` the table's Cells ask for.
* The column's `options` must return them: this is not checked.
*/
@action
getOptionsForRow(
getOptionsForRow = (
row: Row<T>,
): { defaultValue: string } & CellOptionsOf<CellArgs> {
): { defaultValue: string } & CellOptionsOf<CellArgs> => {
const configuredDefault = this.table.config.defaultCellValue;
const defaults = {
[DEFAULT_VALUE_KEY]:
Expand All @@ -98,7 +96,7 @@ export class Column<
...defaults,
...this.config.options?.(this.#contextFor(row)),
} as { defaultValue: string } & CellOptionsOf<CellArgs>;
}
};

#contextFor(row: Row<T>): CellContext<T, unknown, Meta> {
// The row is a row of this column's table, so its table has this table's types.
Expand Down
6 changes: 2 additions & 4 deletions table/src/-private/row.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { assert } from '@ember/debug';
import { action } from '@ember/object';

import type { Table } from './table';

Expand Down Expand Up @@ -35,8 +34,7 @@ export class Row<DataType = Record<string, unknown>> {
this.table = table;
}

@action
handleClick(event: MouseEvent): void {
handleClick = (event: MouseEvent): void => {
assert(
`expected event.target to be an instance of HTMLElement`,
event.target instanceof HTMLElement || event.target instanceof SVGElement,
Expand All @@ -62,5 +60,5 @@ export class Row<DataType = Record<string, unknown>> {
if (inputParent) {
return;
}
}
};
}
11 changes: 4 additions & 7 deletions table/src/-private/table.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { cached, tracked } from '@glimmer/tracking';
import { assert } from '@ember/debug';
import { action } from '@ember/object';
import { guidFor } from '@ember/object/internals';

import { isDevelopingApp, macroCondition } from '@embroider/macros';
Expand Down Expand Up @@ -357,15 +356,13 @@ export class Table<
/**
* @private
*/
@action
resetScrollContainer(): void {
resetScrollContainer = (): void => {
if (!this.scrollContainerElement) return;

this.scrollContainerElement.scrollTop = 0;
}
};

@action
resetToDefaults(): void {
resetToDefaults = (): void => {
this.plugins.forEach((plugin) => plugin.reset?.());
}
};
}
50 changes: 20 additions & 30 deletions table/src/plugins/column-reordering/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { cached, tracked } from '@glimmer/tracking';
import { assert } from '@ember/debug';
import { action } from '@ember/object';

import { TrackedMap } from 'tracked-built-ins';

Expand Down Expand Up @@ -122,28 +121,26 @@ export class TableMeta<DataType = unknown> {
columnOrder: ColumnOrder<DataType> = new ColumnOrder<DataType>({
columns: () => this.allColumns,
visibleColumns: () => this.visibleColumns,
save: this.save,
save: (map) => this.save(map),
read: () => this.read(),
});

/**
* Get the curret order/position of a column
*/
@action
getPosition(column: Column<DataType>): number {
getPosition = (column: Column<DataType>): number => {
return this.columnOrder.get(column.key);
}
};

/**
* Swap the column with the column at `newPosition`
*/
@action
setPosition(
setPosition = (
column: Column<DataType>,
newPosition: number,
): false | undefined {
): false | undefined => {
return this.columnOrder.swapWith(column.key, newPosition);
}
};

/**
* Using a `ColumnOrder` instance, set the order of all columns
Expand All @@ -156,43 +153,40 @@ export class TableMeta<DataType = unknown> {
* Revert to default config, delete preferences,
* and clear the columnOrder
*/
@action
reset(): void {
reset = (): void => {
preferences.forTable(this.table, ColumnReordering).delete('order');
this.columnOrder = new ColumnOrder<DataType>({
columns: () => this.allColumns,
visibleColumns: () => this.visibleColumns,
save: this.save,
});
}
};

/**
* @private
*/
@action
save(map: Map<string, number>): void {
save = (map: Map<string, number>): void => {
const order: Record<string, number> = {};

for (const [key, position] of map.entries()) {
order[key] = position;
}

preferences.forTable(this.table, ColumnReordering).set('order', order);
}
};

/**
* @private
*/
@action
private read() {
private read = () => {
const order = preferences
.forTable(this.table, ColumnReordering)
.get('order');

if (!order) return;

return new Map<string, number>(Object.entries(order));
}
};

get columns(): Column<DataType>[] {
return this.columnOrder.orderedColumns.filter(
Expand Down Expand Up @@ -316,8 +310,7 @@ export class ColumnOrder<DataType = unknown> {
* - skip over non-visible columns when determining the previous "index"
* - set the position to whatever that is.
*/
@action
moveLeft(key: string): void {
moveLeft = (key: string): void => {
const orderedColumns = this.orderedColumns;
if (this.map.get(key) === 0) {
return;
Expand Down Expand Up @@ -353,7 +346,7 @@ export class ColumnOrder<DataType = unknown> {
}

this.args.save?.(this.map);
}
};

setAll = (map: Map<string, number>): void => {
let allColumns = this.args.columns();
Expand All @@ -377,8 +370,7 @@ export class ColumnOrder<DataType = unknown> {
* - skip over non-visible columns when determining the next "index"
* - set the position to whatever that is.
*/
@action
moveRight(key: string): void {
moveRight = (key: string): void => {
const orderedColumns = this.orderedColumns;
let found = false;

Expand Down Expand Up @@ -410,13 +402,12 @@ export class ColumnOrder<DataType = unknown> {
}

this.args.save?.(this.map);
}
};

/**
* Performs a swap of the column's position with the column at position
*/
@action
swapWith(key: string, position: number): false | undefined {
swapWith = (key: string, position: number): false | undefined => {
const validPositions = [...this.orderedMap.values()];

/**
Expand Down Expand Up @@ -487,10 +478,9 @@ export class ColumnOrder<DataType = unknown> {
}

this.args.save?.(this.map);
}
};

@action
get(key: string): number {
get = (key: string): number => {
const result = this.orderedMap.get(key);

assert(
Expand All @@ -500,7 +490,7 @@ export class ColumnOrder<DataType = unknown> {
);

return result;
}
};

/**
* The same as this.map, but with all the columns' information.
Expand Down
33 changes: 13 additions & 20 deletions table/src/plugins/column-resizing/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { cached, tracked } from '@glimmer/tracking';
import { assert } from '@ember/debug';
import { isDestroyed, isDestroying } from '@ember/destroyable';
import { action } from '@ember/object';

import { preferences } from '../../plugins/index.ts';

Expand Down Expand Up @@ -258,15 +257,13 @@ export class ColumnMeta {
return styles;
}

@action
resize(delta: number): void {
resize = (delta: number): void => {
this.tableMeta.resizeColumn(this.column, delta);
}
};

@action
save(): void {
save = (): void => {
this.tableMeta.saveColWidths(this.tableMeta.visibleColumnMetas);
}
};
}

/**
Expand Down Expand Up @@ -357,8 +354,7 @@ export class TableMeta {
);
}

@action
saveColWidths(visibleColumnMetas: ColumnMeta[]): void {
saveColWidths = (visibleColumnMetas: ColumnMeta[]): void => {
const tablePrefs = this.table.preferences;

for (const column of visibleColumnMetas) {
Expand All @@ -369,19 +365,17 @@ export class TableMeta {
}

tablePrefs.persist();
}
};

@action
reset(): void {
reset = (): void => {
if (!this.scrollContainerWidth) return;

for (const column of this.visibleColumnMetas) {
column._width = undefined;
}
}
};

@action
onTableResize(entry: ResizeObserverEntry): void {
onTableResize = (entry: ResizeObserverEntry): void => {
assert(
'scroll container element must be an HTMLElement',
entry.target instanceof HTMLElement,
Expand All @@ -403,13 +397,12 @@ export class TableMeta {
this.scrollContainerWidth - this.totalVisibleColumnsWidth - totalGap;

distributeDelta(diff, this.#availableColumns);
}
};

@action
resizeColumn<DataType = unknown>(
resizeColumn = <DataType = unknown>(
column: Column<DataType>,
delta: number,
): void {
): void => {
if (delta === 0) return;

const tableLayout = this.options?.tableLayout ?? 'auto';
Expand All @@ -419,7 +412,7 @@ export class TableMeta {
} else {
this.#resizeColumnAuto(column, delta);
}
}
};

/**
* Simple column resizing for table-layout: fixed
Expand Down
6 changes: 2 additions & 4 deletions table/src/plugins/column-visibility/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { cached } from '@glimmer/tracking';
import { action } from '@ember/object';

import { BasePlugin, meta, options, preferences } from '../-private/base.ts';

Expand Down Expand Up @@ -149,10 +148,9 @@ export class TableMeta<Data = unknown> {
});
}

@action
toggleColumnVisibility(column: Column<Data>): void {
toggleColumnVisibility = (column: Column<Data>): void => {
const columnMeta = meta.forColumn(column, ColumnVisibility);

columnMeta.toggle();
}
};
}
Loading
Loading