@universal-ember/table 3.0.0 → 3.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/-private/-type-tests/plugin-properties.test.js +27 -0
- package/dist/-private/-type-tests/plugin-properties.test.js.map +1 -0
- package/dist/-private/-type-tests/plugin-with.test.js +20 -0
- package/dist/-private/-type-tests/plugin-with.test.js.map +1 -0
- package/dist/-private/-type-tests/plugins-accessors.test.js +36 -0
- package/dist/-private/-type-tests/plugins-accessors.test.js.map +1 -0
- package/dist/-private/-type-tests/plugins-signature-from.test.js +15 -0
- package/dist/-private/-type-tests/plugins-signature-from.test.js.map +1 -0
- package/dist/-private/-type-tests/plugins-signature-utils.test.js +36 -0
- package/dist/-private/-type-tests/plugins-signature-utils.test.js.map +1 -0
- package/dist/-private/-type-tests/table-api.test.js +17 -0
- package/dist/-private/-type-tests/table-api.test.js.map +1 -0
- package/dist/-private/-type-tests/table-config.test.js +55 -0
- package/dist/-private/-type-tests/table-config.test.js.map +1 -0
- package/dist/-private/column.js +62 -0
- package/dist/-private/column.js.map +1 -0
- package/dist/-private/ember-compat.js +17 -0
- package/dist/-private/ember-compat.js.map +1 -0
- package/dist/-private/interfaces/column.js +2 -0
- package/dist/-private/interfaces/column.js.map +1 -0
- package/dist/-private/interfaces/index.js +2 -0
- package/dist/-private/interfaces/index.js.map +1 -0
- package/dist/-private/interfaces/modifier.js +2 -0
- package/dist/-private/interfaces/modifier.js.map +1 -0
- package/dist/-private/interfaces/pagination.js +2 -0
- package/dist/-private/interfaces/pagination.js.map +1 -0
- package/dist/-private/interfaces/plugins.js +2 -0
- package/dist/-private/interfaces/plugins.js.map +1 -0
- package/dist/-private/interfaces/preferences.js +2 -0
- package/dist/-private/interfaces/preferences.js.map +1 -0
- package/dist/-private/interfaces/selection.js +2 -0
- package/dist/-private/interfaces/selection.js.map +1 -0
- package/dist/-private/interfaces/table.js +2 -0
- package/dist/-private/interfaces/table.js.map +1 -0
- package/dist/-private/js-helper.js +55 -0
- package/dist/-private/js-helper.js.map +1 -0
- package/dist/-private/preferences.js +143 -0
- package/dist/-private/preferences.js.map +1 -0
- package/dist/-private/private-types.js +2 -0
- package/dist/-private/private-types.js.map +1 -0
- package/dist/-private/row.js +51 -0
- package/dist/-private/row.js.map +1 -0
- package/dist/-private/table.js +273 -0
- package/dist/-private/table.js.map +1 -0
- package/dist/-private/utils.js +15 -0
- package/dist/-private/utils.js.map +1 -0
- package/dist/_rollupPluginBabelHelpers-BpiaYhlf.js +63 -0
- package/dist/_rollupPluginBabelHelpers-BpiaYhlf.js.map +1 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -0
- package/dist/plugins/-private/base.js +524 -0
- package/dist/plugins/-private/base.js.map +1 -0
- package/dist/plugins/-private/utils.js +103 -0
- package/dist/plugins/-private/utils.js.map +1 -0
- package/dist/plugins/column-reordering/helpers.js +44 -0
- package/dist/plugins/column-reordering/helpers.js.map +1 -0
- package/dist/plugins/column-reordering/index.js +3 -0
- package/dist/plugins/column-reordering/index.js.map +1 -0
- package/dist/plugins/column-reordering/plugin.js +359 -0
- package/dist/plugins/column-reordering/plugin.js.map +1 -0
- package/dist/plugins/column-reordering/utils.js +34 -0
- package/dist/plugins/column-reordering/utils.js.map +1 -0
- package/dist/plugins/column-resizing/handle.js +241 -0
- package/dist/plugins/column-resizing/handle.js.map +1 -0
- package/dist/plugins/column-resizing/helpers.js +71 -0
- package/dist/plugins/column-resizing/helpers.js.map +1 -0
- package/dist/plugins/column-resizing/index.js +4 -0
- package/dist/plugins/column-resizing/index.js.map +1 -0
- package/dist/plugins/column-resizing/plugin.js +328 -0
- package/dist/plugins/column-resizing/plugin.js.map +1 -0
- package/dist/plugins/column-resizing/resize-observer.js +44 -0
- package/dist/plugins/column-resizing/resize-observer.js.map +1 -0
- package/dist/plugins/column-resizing/utils.js +44 -0
- package/dist/plugins/column-resizing/utils.js.map +1 -0
- package/dist/plugins/column-visibility/helpers.js +25 -0
- package/dist/plugins/column-visibility/helpers.js.map +1 -0
- package/dist/plugins/column-visibility/index.js +3 -0
- package/dist/plugins/column-visibility/index.js.map +1 -0
- package/dist/plugins/column-visibility/plugin.js +92 -0
- package/dist/plugins/column-visibility/plugin.js.map +1 -0
- package/dist/plugins/data-sorting/helpers.js +49 -0
- package/dist/plugins/data-sorting/helpers.js.map +1 -0
- package/dist/plugins/data-sorting/index.js +4 -0
- package/dist/plugins/data-sorting/index.js.map +1 -0
- package/dist/plugins/data-sorting/plugin.js +132 -0
- package/dist/plugins/data-sorting/plugin.js.map +1 -0
- package/dist/plugins/data-sorting/types.js +14 -0
- package/dist/plugins/data-sorting/types.js.map +1 -0
- package/dist/plugins/index.js +3 -0
- package/dist/plugins/index.js.map +1 -0
- package/dist/plugins/metadata/helpers.js +12 -0
- package/dist/plugins/metadata/helpers.js.map +1 -0
- package/dist/plugins/metadata/index.js +3 -0
- package/dist/plugins/metadata/index.js.map +1 -0
- package/dist/plugins/metadata/plugin.js +25 -0
- package/dist/plugins/metadata/plugin.js.map +1 -0
- package/dist/plugins/row-selection/helpers.js +10 -0
- package/dist/plugins/row-selection/helpers.js.map +1 -0
- package/dist/plugins/row-selection/index.js +3 -0
- package/dist/plugins/row-selection/index.js.map +1 -0
- package/dist/plugins/row-selection/plugin.js +118 -0
- package/dist/plugins/row-selection/plugin.js.map +1 -0
- package/dist/plugins/sticky-columns/helpers.js +49 -0
- package/dist/plugins/sticky-columns/helpers.js.map +1 -0
- package/dist/plugins/sticky-columns/index.js +3 -0
- package/dist/plugins/sticky-columns/index.js.map +1 -0
- package/dist/plugins/sticky-columns/plugin.js +139 -0
- package/dist/plugins/sticky-columns/plugin.js.map +1 -0
- package/dist/test-support/index.js +62 -0
- package/dist/test-support/index.js.map +1 -0
- package/dist/utils.js +77 -0
- package/dist/utils.js.map +1 -0
- package/package.json +3 -2
- package/src/-private/-type-tests/plugin-properties.test.ts +38 -0
- package/src/-private/-type-tests/plugin-with.test.ts +23 -0
- package/src/-private/-type-tests/plugins-accessors.test.ts +86 -0
- package/src/-private/-type-tests/plugins-signature-from.test.ts +66 -0
- package/src/-private/-type-tests/plugins-signature-utils.test.ts +154 -0
- package/src/-private/-type-tests/table-api.test.ts +20 -0
- package/src/-private/-type-tests/table-config.test.ts +70 -0
- package/src/-private/column.ts +67 -0
- package/src/-private/ember-compat.ts +26 -0
- package/src/-private/interfaces/column.ts +73 -0
- package/src/-private/interfaces/index.ts +7 -0
- package/src/-private/interfaces/modifier.ts +7 -0
- package/src/-private/interfaces/pagination.ts +13 -0
- package/src/-private/interfaces/plugins.ts +349 -0
- package/src/-private/interfaces/preferences.ts +82 -0
- package/src/-private/interfaces/selection.ts +38 -0
- package/src/-private/interfaces/table.ts +121 -0
- package/src/-private/js-helper.ts +65 -0
- package/src/-private/preferences.ts +176 -0
- package/src/-private/private-types.ts +8 -0
- package/src/-private/row.ts +66 -0
- package/src/-private/table.ts +310 -0
- package/src/-private/utils.ts +21 -0
- package/src/index.ts +25 -0
- package/src/plugins/-private/base.ts +836 -0
- package/src/plugins/-private/utils.ts +166 -0
- package/src/plugins/column-reordering/helpers.ts +50 -0
- package/src/plugins/column-reordering/index.ts +6 -0
- package/src/plugins/column-reordering/plugin.ts +489 -0
- package/src/plugins/column-reordering/utils.ts +48 -0
- package/src/plugins/column-resizing/handle.ts +280 -0
- package/src/plugins/column-resizing/helpers.ts +79 -0
- package/src/plugins/column-resizing/index.ts +7 -0
- package/src/plugins/column-resizing/plugin.ts +490 -0
- package/src/plugins/column-resizing/resize-observer.ts +48 -0
- package/src/plugins/column-resizing/utils.ts +54 -0
- package/src/plugins/column-visibility/helpers.ts +28 -0
- package/src/plugins/column-visibility/index.ts +6 -0
- package/src/plugins/column-visibility/plugin.ts +155 -0
- package/src/plugins/data-sorting/helpers.ts +56 -0
- package/src/plugins/data-sorting/index.ts +8 -0
- package/src/plugins/data-sorting/plugin.ts +222 -0
- package/src/plugins/data-sorting/types.ts +26 -0
- package/src/plugins/index.ts +20 -0
- package/src/plugins/metadata/helpers.ts +12 -0
- package/src/plugins/metadata/index.ts +7 -0
- package/src/plugins/metadata/plugin.ts +26 -0
- package/src/plugins/row-selection/helpers.ts +13 -0
- package/src/plugins/row-selection/index.ts +7 -0
- package/src/plugins/row-selection/plugin.ts +218 -0
- package/src/plugins/sticky-columns/helpers.ts +59 -0
- package/src/plugins/sticky-columns/index.ts +7 -0
- package/src/plugins/sticky-columns/plugin.ts +201 -0
- package/src/test-support/index.ts +76 -0
- package/src/utils.ts +85 -0
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { cached } from '@glimmer/tracking';
|
|
2
|
+
import { action } from '@ember/object';
|
|
3
|
+
|
|
4
|
+
import { BasePlugin, meta, options, preferences } from '../-private/base.ts';
|
|
5
|
+
|
|
6
|
+
import type { Plugin, PluginPreferences } from '../../plugins/index.ts';
|
|
7
|
+
import type { Column, Table } from '../../index.ts';
|
|
8
|
+
|
|
9
|
+
interface ColumnVisibilityPreferences extends PluginPreferences {
|
|
10
|
+
columns: {
|
|
11
|
+
[columnKey: string]: {
|
|
12
|
+
isVisible?: boolean;
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
declare module '@universal-ember/table/plugins' {
|
|
18
|
+
interface Registry {
|
|
19
|
+
ColumnVisibility?: ColumnVisibilityPreferences;
|
|
20
|
+
'column-visibility'?: ColumnVisibilityPreferences;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface Signature {
|
|
25
|
+
Meta: {
|
|
26
|
+
Table: TableMeta;
|
|
27
|
+
Column: ColumnMeta;
|
|
28
|
+
};
|
|
29
|
+
Options: {
|
|
30
|
+
Plugin: {
|
|
31
|
+
enabled?: boolean;
|
|
32
|
+
};
|
|
33
|
+
Column: {
|
|
34
|
+
/**
|
|
35
|
+
* The default visibilty of the column, when rendered.
|
|
36
|
+
* The column can still be toggled on and off.
|
|
37
|
+
*
|
|
38
|
+
* When interacting with preferences, the value stored in preferenced
|
|
39
|
+
* will be the inverse of this value (to save space in storage).
|
|
40
|
+
*/
|
|
41
|
+
isVisible?: boolean;
|
|
42
|
+
};
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export class ColumnVisibility
|
|
47
|
+
extends BasePlugin<Signature>
|
|
48
|
+
implements Plugin<Signature>
|
|
49
|
+
{
|
|
50
|
+
name = 'column-visibility';
|
|
51
|
+
static features = ['columnVisibility'];
|
|
52
|
+
|
|
53
|
+
meta = {
|
|
54
|
+
column: ColumnMeta,
|
|
55
|
+
table: TableMeta,
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
reset() {
|
|
59
|
+
preferences.forAllColumns(this.table, ColumnVisibility).delete('isVisible');
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
get columns() {
|
|
63
|
+
return meta.forTable(this.table, ColumnVisibility).visibleColumns;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export class ColumnMeta<Data = unknown> {
|
|
68
|
+
constructor(private column: Column<Data>) {}
|
|
69
|
+
|
|
70
|
+
get isVisible(): boolean {
|
|
71
|
+
const columnPreferences = preferences.forColumn(
|
|
72
|
+
this.column,
|
|
73
|
+
ColumnVisibility,
|
|
74
|
+
);
|
|
75
|
+
const columnOptions = options.forColumn(this.column, ColumnVisibility);
|
|
76
|
+
|
|
77
|
+
return Boolean(
|
|
78
|
+
columnPreferences.get('isVisible') ?? columnOptions?.isVisible ?? true,
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
get isHidden(): boolean {
|
|
83
|
+
return !this.isVisible;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
hide = () => {
|
|
87
|
+
if (!this.isVisible) return;
|
|
88
|
+
|
|
89
|
+
const myPreferences = preferences.forColumn(this.column, ColumnVisibility);
|
|
90
|
+
const myOptions = options.forColumn(this.column, ColumnVisibility);
|
|
91
|
+
const currentSaved = myPreferences.get('isVisible');
|
|
92
|
+
const willBeDefault = Boolean(currentSaved) === !myOptions?.isVisible;
|
|
93
|
+
|
|
94
|
+
if (willBeDefault) {
|
|
95
|
+
myPreferences.set('isVisible', false);
|
|
96
|
+
// TODO: open an issue about tracked-built-ins' delete not being reactive
|
|
97
|
+
// myPreferences.delete('isVisible');
|
|
98
|
+
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
myPreferences.set('isVisible', false);
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
show = () => {
|
|
106
|
+
if (this.isVisible) return;
|
|
107
|
+
|
|
108
|
+
const myPreferences = preferences.forColumn(this.column, ColumnVisibility);
|
|
109
|
+
const myOptions = options.forColumn(this.column, ColumnVisibility);
|
|
110
|
+
const currentSaved = myPreferences.get('isVisible');
|
|
111
|
+
const willBeDefault = currentSaved === !myOptions?.isVisible;
|
|
112
|
+
|
|
113
|
+
if (willBeDefault) {
|
|
114
|
+
myPreferences.set('isVisible', true);
|
|
115
|
+
// TODO: open an issue about tracked-built-ins' delete not being reactive
|
|
116
|
+
// myPreferences.delete('isVisible');
|
|
117
|
+
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
myPreferences.set('isVisible', true);
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
toggle = () => {
|
|
125
|
+
if (this.isVisible) {
|
|
126
|
+
this.hide();
|
|
127
|
+
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
this.show();
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export class TableMeta<Data = unknown> {
|
|
136
|
+
constructor(private table: Table<Data>) {}
|
|
137
|
+
|
|
138
|
+
@cached
|
|
139
|
+
get visibleColumns(): Column<Data>[] {
|
|
140
|
+
const allColumns = this.table.columns.values();
|
|
141
|
+
|
|
142
|
+
return allColumns.filter((column) => {
|
|
143
|
+
const columnMeta = meta.forColumn(column, ColumnVisibility);
|
|
144
|
+
|
|
145
|
+
return columnMeta.isVisible;
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
@action
|
|
150
|
+
toggleColumnVisibility(column: Column<Data>) {
|
|
151
|
+
const columnMeta = meta.forColumn(column, ColumnVisibility);
|
|
152
|
+
|
|
153
|
+
columnMeta.toggle();
|
|
154
|
+
}
|
|
155
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { meta } from '../-private/base.ts';
|
|
2
|
+
import { Sorting } from './plugin.ts';
|
|
3
|
+
|
|
4
|
+
import type { Column } from '../../index.ts';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Query a specific column's current sort direction
|
|
8
|
+
*/
|
|
9
|
+
export const sortDirection = (column: Column) =>
|
|
10
|
+
meta.forColumn(column, Sorting).sortDirection;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Ask if a column is sortable
|
|
14
|
+
*/
|
|
15
|
+
export const isSortable = (column: Column) =>
|
|
16
|
+
meta.forColumn(column, Sorting).isSortable;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Ask if a column is ascending
|
|
20
|
+
*/
|
|
21
|
+
export const isAscending = (column: Column) =>
|
|
22
|
+
meta.forColumn(column, Sorting).isAscending;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Ask if a column is sorted descending
|
|
26
|
+
*/
|
|
27
|
+
export const isDescending = (column: Column) =>
|
|
28
|
+
meta.forColumn(column, Sorting).isDescending;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Ask if a column is not sorted
|
|
32
|
+
*/
|
|
33
|
+
export const isUnsorted = (column: Column) =>
|
|
34
|
+
meta.forColumn(column, Sorting).isUnsorted;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Sort the specified column's data using a tri-toggle.
|
|
38
|
+
*
|
|
39
|
+
* States go in this order:
|
|
40
|
+
* Ascending => None => Descending
|
|
41
|
+
* ⬑ ---------- <= ---------- ↲
|
|
42
|
+
*/
|
|
43
|
+
export const sort = (column: Column) =>
|
|
44
|
+
meta.forTable(column.table, Sorting).handleSort(column);
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Toggle a column between descending and not unsorted states
|
|
48
|
+
*/
|
|
49
|
+
export const sortDescending = (column: Column) =>
|
|
50
|
+
meta.forTable(column.table, Sorting).toggleDescending(column);
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Toggle a column between ascending and not unsorted states
|
|
54
|
+
*/
|
|
55
|
+
export const sortAscending = (column: Column) =>
|
|
56
|
+
meta.forTable(column.table, Sorting).toggleAscending(column);
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export * from './helpers.ts';
|
|
2
|
+
export { Sorting as Plugin } from './plugin.ts';
|
|
3
|
+
export { Sorting as DataSorting } from './plugin.ts';
|
|
4
|
+
export { SortDirection } from './types.ts';
|
|
5
|
+
|
|
6
|
+
// Public Types
|
|
7
|
+
export type { Signature } from './plugin.ts';
|
|
8
|
+
export type { Sort, SortItem } from './types.ts';
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
import { cached } from '@glimmer/tracking';
|
|
2
|
+
import { action } from '@ember/object';
|
|
3
|
+
|
|
4
|
+
import { BasePlugin, meta, options } from '../-private/base.ts';
|
|
5
|
+
import { SortDirection } from './types.ts';
|
|
6
|
+
|
|
7
|
+
import type { Sort, SortItem } from './types.ts';
|
|
8
|
+
import type { ColumnApi, PluginPreferences } from '../../plugins/index.ts';
|
|
9
|
+
import type { Column, Table } from '../../index.ts';
|
|
10
|
+
|
|
11
|
+
interface SortingPreferences extends PluginPreferences {
|
|
12
|
+
table: {
|
|
13
|
+
sorts?: Sort[];
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
declare module '@universal-ember/table/plugins' {
|
|
18
|
+
interface Registry {
|
|
19
|
+
Sorting?: SortingPreferences;
|
|
20
|
+
sorting?: SortingPreferences;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface Signature<DataType = unknown> {
|
|
25
|
+
Meta: {
|
|
26
|
+
Column: ColumnMeta;
|
|
27
|
+
Table: TableMeta;
|
|
28
|
+
};
|
|
29
|
+
Options: {
|
|
30
|
+
Plugin: Options<DataType>;
|
|
31
|
+
Column: ColumnOptions;
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
interface Options<DataType = unknown> {
|
|
36
|
+
/**
|
|
37
|
+
* Handler for interpreting sort requests from the headless table.
|
|
38
|
+
*
|
|
39
|
+
* Does not actually do the sorting.
|
|
40
|
+
*
|
|
41
|
+
* if omitted, the DataSorting plugin will disable itself
|
|
42
|
+
*/
|
|
43
|
+
onSort?: (sorts: SortItem<DataType>[]) => void;
|
|
44
|
+
/**
|
|
45
|
+
* Provided sorts that the table should assume are true.
|
|
46
|
+
*
|
|
47
|
+
* if omitted, the DataSorting plugin will disable itself
|
|
48
|
+
*/
|
|
49
|
+
sorts?: SortItem<DataType>[];
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
interface ColumnOptions {
|
|
53
|
+
/**
|
|
54
|
+
* Opt in or out of sorting for a particular column.
|
|
55
|
+
* Default is true.
|
|
56
|
+
*/
|
|
57
|
+
isSortable?: boolean;
|
|
58
|
+
/**
|
|
59
|
+
* Use this key instead of the `key` on the column config.
|
|
60
|
+
* This has no bearing on any behavior in the plugin, *other than*,
|
|
61
|
+
* swapping out the `property`'s value on the `SortItem`s passed to
|
|
62
|
+
* the table plugin config's `onSort` callback
|
|
63
|
+
*/
|
|
64
|
+
sortProperty?: string;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Manages basic data-sorting behaviors. Ascending -> Descending -> None
|
|
69
|
+
*
|
|
70
|
+
* This plugin requires a table plugin configuration, `onSort` for handling *how* sorting happens.
|
|
71
|
+
* communicating back to the table that sorting has succeeded can be done by setting the `sorts`
|
|
72
|
+
* property in the table plugin configuration.
|
|
73
|
+
*
|
|
74
|
+
* Note that this plugin doesn't actually sort the data, as data management is not the responsibility
|
|
75
|
+
* of the table, but of the surrounding context providing the data to the table. So sorting can happen
|
|
76
|
+
* client-side still, just in a component -- much the same way you'd handel sorting via API requests.
|
|
77
|
+
*
|
|
78
|
+
* This plugin is for *conveying* what the current sorts are, rather than _doing_ the sorting.
|
|
79
|
+
*/
|
|
80
|
+
export class Sorting<DataType = unknown> extends BasePlugin<
|
|
81
|
+
Signature<DataType>
|
|
82
|
+
> {
|
|
83
|
+
name = 'data-sorting';
|
|
84
|
+
|
|
85
|
+
meta = {
|
|
86
|
+
column: ColumnMeta,
|
|
87
|
+
table: TableMeta,
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
headerCellModifier = (element: HTMLElement, { column }: ColumnApi) => {
|
|
91
|
+
const columnMeta = meta.forColumn(column, Sorting);
|
|
92
|
+
|
|
93
|
+
element.setAttribute('data-test-is-sortable', `${columnMeta.isSortable}`);
|
|
94
|
+
element.setAttribute('aria-sort', `${columnMeta.sortDirection}`);
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export class ColumnMeta {
|
|
99
|
+
constructor(private column: Column) {}
|
|
100
|
+
|
|
101
|
+
@cached
|
|
102
|
+
get options() {
|
|
103
|
+
return options.forColumn(this.column, Sorting);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
get isSortable() {
|
|
107
|
+
return this.options?.isSortable ?? this.tableMeta.isSortable;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
get tableMeta() {
|
|
111
|
+
return meta.forTable(this.column.table, Sorting);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
get sortDirection() {
|
|
115
|
+
const sort = this.tableMeta.sorts.find(
|
|
116
|
+
(sort) => sort.property === this.sortProperty,
|
|
117
|
+
);
|
|
118
|
+
|
|
119
|
+
return sort?.direction ?? SortDirection.None;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
get isAscending() {
|
|
123
|
+
return this.sortDirection === SortDirection.Ascending;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
get isDescending() {
|
|
127
|
+
return this.sortDirection === SortDirection.Descending;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
get isUnsorted() {
|
|
131
|
+
return this.sortDirection === SortDirection.None;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
get sortProperty() {
|
|
135
|
+
return this.options?.sortProperty ?? this.column.config.key;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export class TableMeta {
|
|
140
|
+
constructor(private table: Table) {}
|
|
141
|
+
|
|
142
|
+
@cached
|
|
143
|
+
get options() {
|
|
144
|
+
return options.forTable(this.table, Sorting);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
get sorts() {
|
|
148
|
+
return this.options?.sorts ?? [];
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
get isSortable() {
|
|
152
|
+
return Boolean(this.options?.onSort) && Boolean(this.options?.sorts);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
get onSort() {
|
|
156
|
+
return this.options?.onSort;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
@action
|
|
160
|
+
handleSort(column: Column) {
|
|
161
|
+
const columnMeta = meta.forColumn(column, Sorting);
|
|
162
|
+
|
|
163
|
+
if (!columnMeta.sortProperty) {
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
if (columnMeta.sortDirection === SortDirection.Ascending) {
|
|
168
|
+
this.onSort?.([]);
|
|
169
|
+
} else if (columnMeta.sortDirection === SortDirection.Descending) {
|
|
170
|
+
this.onSort?.([
|
|
171
|
+
{
|
|
172
|
+
direction: SortDirection.Ascending,
|
|
173
|
+
property: columnMeta.sortProperty,
|
|
174
|
+
},
|
|
175
|
+
]);
|
|
176
|
+
} else {
|
|
177
|
+
this.onSort?.([
|
|
178
|
+
{
|
|
179
|
+
direction: SortDirection.Descending,
|
|
180
|
+
property: columnMeta.sortProperty,
|
|
181
|
+
},
|
|
182
|
+
]);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
@action
|
|
187
|
+
toggleAscending(column: Column) {
|
|
188
|
+
const columnMeta = meta.forColumn(column, Sorting);
|
|
189
|
+
|
|
190
|
+
if (!columnMeta.sortProperty) {
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
if (columnMeta.sortDirection === SortDirection.Ascending) {
|
|
195
|
+
return this.onSort?.([]);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
this.onSort?.([
|
|
199
|
+
{ direction: SortDirection.Ascending, property: columnMeta.sortProperty },
|
|
200
|
+
]);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
@action
|
|
204
|
+
toggleDescending(column: Column) {
|
|
205
|
+
const columnMeta = meta.forColumn(column, Sorting);
|
|
206
|
+
|
|
207
|
+
if (!columnMeta.sortProperty) {
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
if (columnMeta.sortDirection === SortDirection.Descending) {
|
|
212
|
+
return this.onSort?.([]);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
this.onSort?.([
|
|
216
|
+
{
|
|
217
|
+
direction: SortDirection.Descending,
|
|
218
|
+
property: columnMeta.sortProperty,
|
|
219
|
+
},
|
|
220
|
+
]);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { ColumnKey } from '../../-private/interfaces';
|
|
2
|
+
|
|
3
|
+
export enum SortDirection {
|
|
4
|
+
Ascending = 'ascending',
|
|
5
|
+
Descending = 'descending',
|
|
6
|
+
None = 'none',
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface Sort {
|
|
10
|
+
property: string;
|
|
11
|
+
direction: SortDirection;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export enum SortTransform {
|
|
15
|
+
Underscore = 'underscore',
|
|
16
|
+
Camelize = 'camelize',
|
|
17
|
+
}
|
|
18
|
+
export interface SortsOptions {
|
|
19
|
+
separator: string;
|
|
20
|
+
transform: string | null;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface SortItem<T> {
|
|
24
|
+
direction: SortDirection;
|
|
25
|
+
property: ColumnKey<T>;
|
|
26
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// Public API
|
|
2
|
+
export {
|
|
3
|
+
BasePlugin,
|
|
4
|
+
columns,
|
|
5
|
+
hasPlugin,
|
|
6
|
+
meta,
|
|
7
|
+
options,
|
|
8
|
+
preferences,
|
|
9
|
+
} from './-private/base.ts';
|
|
10
|
+
export { applyStyles, removeStyles } from './-private/utils.ts';
|
|
11
|
+
|
|
12
|
+
// Public Types
|
|
13
|
+
export type { ColumnFeatures, TableFeatures } from './-private/base.ts';
|
|
14
|
+
export type {
|
|
15
|
+
ColumnApi,
|
|
16
|
+
Plugin,
|
|
17
|
+
PluginPreferences,
|
|
18
|
+
Registry,
|
|
19
|
+
} from '../-private/interfaces';
|
|
20
|
+
export type { PluginSignature } from '../-private/interfaces/plugins';
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { options } from '../-private/base.ts';
|
|
2
|
+
import { Metadata } from './plugin.ts';
|
|
3
|
+
|
|
4
|
+
import type { Column, Table } from '../../index.ts';
|
|
5
|
+
|
|
6
|
+
export const forColumn = (column: Column<any>, key: string) => {
|
|
7
|
+
return options.forColumn(column, Metadata)[key];
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export const forTable = (table: Table<any>, key: string) => {
|
|
11
|
+
return options.forTable(table, Metadata)[key];
|
|
12
|
+
};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { BasePlugin } from '../-private/base.ts';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Data stored per column or table can be arbitrary
|
|
5
|
+
*
|
|
6
|
+
*/
|
|
7
|
+
type ArbitraryData = Record<string, any>;
|
|
8
|
+
|
|
9
|
+
export interface Signature<Data = ArbitraryData> {
|
|
10
|
+
Options: {
|
|
11
|
+
Column: Data;
|
|
12
|
+
Plugin: Data;
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* This plugin does noting,
|
|
18
|
+
* but gives consumer of it a safe way to store and associate "any" data with columns
|
|
19
|
+
* (and have a generic top-level bucket of data as well)
|
|
20
|
+
*
|
|
21
|
+
* This "metadata" stored per column per table is managed via the "options" part of the Signature, as
|
|
22
|
+
* "meta" is a term used for plugins for plugin authors.
|
|
23
|
+
*/
|
|
24
|
+
export class Metadata<S extends Signature> extends BasePlugin<S> {
|
|
25
|
+
name = 'metadata';
|
|
26
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { meta } from '../-private/base.ts';
|
|
2
|
+
import { RowSelection } from './plugin.ts';
|
|
3
|
+
|
|
4
|
+
import type { Row } from '../../-private/row.ts';
|
|
5
|
+
|
|
6
|
+
export const isSelected = (row: Row<any>) =>
|
|
7
|
+
meta.forRow(row, RowSelection).isSelected;
|
|
8
|
+
export const select = (row: Row<any>) =>
|
|
9
|
+
meta.forRow(row, RowSelection).select();
|
|
10
|
+
export const deselect = (row: Row<any>) =>
|
|
11
|
+
meta.forRow(row, RowSelection).deselect();
|
|
12
|
+
export const toggle = (row: Row<any>) =>
|
|
13
|
+
meta.forRow(row, RowSelection).toggle();
|