@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,176 @@
|
|
|
1
|
+
import { TrackedMap } from 'tracked-built-ins';
|
|
2
|
+
|
|
3
|
+
import type {
|
|
4
|
+
PluginPreferenceFor,
|
|
5
|
+
PluginPreferences,
|
|
6
|
+
PreferencesAdapter as Adapter,
|
|
7
|
+
PreferencesTableValues,
|
|
8
|
+
TablePreferencesData,
|
|
9
|
+
} from './interfaces';
|
|
10
|
+
|
|
11
|
+
export class TablePreferences {
|
|
12
|
+
storage = new TrackedPreferences();
|
|
13
|
+
|
|
14
|
+
constructor(
|
|
15
|
+
private key: string,
|
|
16
|
+
private adapter?: Adapter,
|
|
17
|
+
) {
|
|
18
|
+
if (this.adapter) {
|
|
19
|
+
this.restore(this.adapter);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
hasAdapter() {
|
|
24
|
+
return this.adapter !== undefined;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
getIsAtDefault() {
|
|
28
|
+
return this.storage.isAtDefault;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Passes a JSON-compatible structure to `adapter.persist`
|
|
33
|
+
*
|
|
34
|
+
* This structure could be stored in a remote database or
|
|
35
|
+
* local storage. The `adpater.restore` method can be used to restore
|
|
36
|
+
* this structure back in to the {@link TrackedPreferences }
|
|
37
|
+
*/
|
|
38
|
+
persist() {
|
|
39
|
+
return this.adapter?.persist?.(this.key, {
|
|
40
|
+
...this.storage.serialize(),
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Using the `adapter.restore` method, convert the JSON structure
|
|
46
|
+
* to {@link TrackedPreferences }
|
|
47
|
+
*/
|
|
48
|
+
restore(adapter: Adapter) {
|
|
49
|
+
const data = adapter?.restore?.(this.key);
|
|
50
|
+
|
|
51
|
+
if (!data) return;
|
|
52
|
+
|
|
53
|
+
return this.storage.restore(data);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* @public
|
|
59
|
+
*
|
|
60
|
+
* The API for reactively interacting with preferences
|
|
61
|
+
*/
|
|
62
|
+
class TrackedPreferences {
|
|
63
|
+
plugins = new Map<string, TrackedPluginPrefs>();
|
|
64
|
+
|
|
65
|
+
get isAtDefault(): boolean {
|
|
66
|
+
return [...this.plugins.values()].every(
|
|
67
|
+
(pluginPrefs) => pluginPrefs.isAtDefault,
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
forPlugin(name: string) {
|
|
72
|
+
let existing = this.plugins.get(name);
|
|
73
|
+
|
|
74
|
+
if (!existing) {
|
|
75
|
+
existing = new TrackedPluginPrefs();
|
|
76
|
+
this.plugins.set(name, existing);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return existing;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
serialize(): TablePreferencesData {
|
|
83
|
+
const plugins: TablePreferencesData['plugins'] = {};
|
|
84
|
+
|
|
85
|
+
for (const [pluginName, preferences] of this.plugins.entries()) {
|
|
86
|
+
/**
|
|
87
|
+
* This cast is dirty, and should be fixed eventually.
|
|
88
|
+
* We should be able to, knowing that pluginName
|
|
89
|
+
* will either be in the registry, or be a default PluginPreferences
|
|
90
|
+
* object, that we can assign the serialized structure to plugins.
|
|
91
|
+
*/
|
|
92
|
+
(plugins as any)[pluginName] = preferences.serialize();
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return {
|
|
96
|
+
plugins,
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
restore(data: TablePreferencesData): void {
|
|
101
|
+
const { plugins } = data;
|
|
102
|
+
|
|
103
|
+
for (const [pluginName, preferences] of Object.entries(plugins || {})) {
|
|
104
|
+
const trackedPluginPrefs = new TrackedPluginPrefs();
|
|
105
|
+
|
|
106
|
+
trackedPluginPrefs.restore(preferences);
|
|
107
|
+
|
|
108
|
+
this.plugins.set(pluginName, trackedPluginPrefs);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
class TrackedPluginPrefs<PluginName = unknown> {
|
|
114
|
+
table = new TrackedMap<string, unknown>();
|
|
115
|
+
columns = new Map<string, TrackedMap<string, unknown>>();
|
|
116
|
+
|
|
117
|
+
get isAtDefault(): boolean {
|
|
118
|
+
return (
|
|
119
|
+
this.table.size === 0 &&
|
|
120
|
+
[...this.columns.values()].every((x) => x.size === 0)
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
forColumn = (key: string): TrackedMap<string, unknown> => {
|
|
125
|
+
let existing = this.columns.get(key);
|
|
126
|
+
|
|
127
|
+
if (!existing) {
|
|
128
|
+
existing = new TrackedMap();
|
|
129
|
+
this.columns.set(key, existing);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
return existing;
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
serialize(): PluginPreferenceFor<PluginName> {
|
|
136
|
+
const columnsPrefs: PluginPreferences['columns'] = {};
|
|
137
|
+
const tablePrefs: PluginPreferences['table'] = {};
|
|
138
|
+
|
|
139
|
+
for (const [columnKey, preferences] of this.columns.entries()) {
|
|
140
|
+
const serializedPreferences: Record<string, unknown> = {};
|
|
141
|
+
|
|
142
|
+
for (const [key, preference] of preferences.entries()) {
|
|
143
|
+
serializedPreferences[key] = preference;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
columnsPrefs[columnKey] = serializedPreferences;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
for (const [key, preference] of this.table.entries()) {
|
|
150
|
+
tablePrefs[key] = preference;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
return {
|
|
154
|
+
table: tablePrefs,
|
|
155
|
+
columns: columnsPrefs,
|
|
156
|
+
} as PluginPreferenceFor<PluginName>;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
restore(data: PluginPreferences): void {
|
|
160
|
+
const { table, columns } = data;
|
|
161
|
+
|
|
162
|
+
for (const [key, preferences] of Object.entries(columns)) {
|
|
163
|
+
const trackedPluginPrefs = new TrackedMap(Object.entries(preferences));
|
|
164
|
+
|
|
165
|
+
this.columns.set(key, trackedPluginPrefs);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* TODO: fix the inference here...
|
|
170
|
+
* each time there is a cast, there is a greater risk of runtime error.
|
|
171
|
+
*/
|
|
172
|
+
this.table = new TrackedMap<string, PreferencesTableValues<PluginName>>(
|
|
173
|
+
Object.entries(table) as [string, PreferencesTableValues<PluginName>][],
|
|
174
|
+
);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { assert } from '@ember/debug';
|
|
2
|
+
import { action } from '@ember/object';
|
|
3
|
+
|
|
4
|
+
import type { Table } from './table';
|
|
5
|
+
|
|
6
|
+
export class Row<DataType = Record<string, unknown>> {
|
|
7
|
+
data: DataType;
|
|
8
|
+
table: Table<DataType>;
|
|
9
|
+
|
|
10
|
+
get index(): number {
|
|
11
|
+
const i = this.table.rows.values().indexOf(this);
|
|
12
|
+
|
|
13
|
+
assert(
|
|
14
|
+
`Row is no longer a part of the table, something has gone wrong`,
|
|
15
|
+
i >= 0,
|
|
16
|
+
);
|
|
17
|
+
|
|
18
|
+
return i;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
get isOdd() {
|
|
22
|
+
return this.index % 2 !== 0;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
get next(): Row<DataType> | undefined {
|
|
26
|
+
return this.table.rows[this.index + 1];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
get prev(): Row<DataType> | undefined {
|
|
30
|
+
return this.table.rows[this.index - 1];
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
constructor(table: Table<DataType>, data: DataType) {
|
|
34
|
+
this.data = data;
|
|
35
|
+
this.table = table;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
@action
|
|
39
|
+
handleClick(event: MouseEvent) {
|
|
40
|
+
assert(
|
|
41
|
+
`expected event.target to be an instance of HTMLElement`,
|
|
42
|
+
event.target instanceof HTMLElement || event.target instanceof SVGElement,
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
const selection = document.getSelection();
|
|
46
|
+
|
|
47
|
+
if (selection) {
|
|
48
|
+
const { type, anchorNode } = selection;
|
|
49
|
+
const isSelectingText =
|
|
50
|
+
type === 'Range' && event.target?.contains(anchorNode);
|
|
51
|
+
|
|
52
|
+
if (isSelectingText) {
|
|
53
|
+
event.stopPropagation();
|
|
54
|
+
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// Ignore clicks on interactive elements within the row
|
|
60
|
+
const inputParent = event.target.closest('input, button, label, a, select');
|
|
61
|
+
|
|
62
|
+
if (inputParent) {
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
import { cached, tracked } from '@glimmer/tracking';
|
|
2
|
+
import { assert } from '@ember/debug';
|
|
3
|
+
import { action } from '@ember/object';
|
|
4
|
+
import { guidFor } from '@ember/object/internals';
|
|
5
|
+
|
|
6
|
+
import { isDevelopingApp, macroCondition } from '@embroider/macros';
|
|
7
|
+
import { modifier } from 'ember-modifier';
|
|
8
|
+
import { Resource } from 'ember-modify-based-class-resource';
|
|
9
|
+
import { map } from 'reactiveweb/map';
|
|
10
|
+
|
|
11
|
+
import {
|
|
12
|
+
normalizePluginsConfig,
|
|
13
|
+
verifyPlugins,
|
|
14
|
+
} from '../plugins/-private/utils.ts';
|
|
15
|
+
import { Column } from './column.ts';
|
|
16
|
+
import { TablePreferences } from './preferences.ts';
|
|
17
|
+
import { Row } from './row.ts';
|
|
18
|
+
import { composeFunctionModifiers } from './utils.ts';
|
|
19
|
+
|
|
20
|
+
import type { BasePlugin, Plugin } from '../plugins/index.ts';
|
|
21
|
+
import type { Class } from './private-types.ts';
|
|
22
|
+
import type { Destructor, TableConfig } from './interfaces';
|
|
23
|
+
import { compatOwner } from './ember-compat.ts';
|
|
24
|
+
|
|
25
|
+
const getOwner = compatOwner.getOwner;
|
|
26
|
+
const setOwner = compatOwner.setOwner;
|
|
27
|
+
|
|
28
|
+
const DEFAULT_COLUMN_CONFIG = {
|
|
29
|
+
isVisible: true,
|
|
30
|
+
minWidth: 128,
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
interface Signature<DataType> {
|
|
34
|
+
Named: TableConfig<DataType>;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Because the table is our entry-point object to all the table behaviors,
|
|
39
|
+
* we need a stable way to know which table we have.
|
|
40
|
+
* Normally, this could be done with referential integrity / identity.
|
|
41
|
+
* However, due to how resources are implemented, if the consumer opts to
|
|
42
|
+
* not use the `@use` decorator, then proxies get involved.
|
|
43
|
+
* The proxies don't maintain instanceof checks, which may be a bug in
|
|
44
|
+
* ember-resources.
|
|
45
|
+
*/
|
|
46
|
+
export const TABLE_KEY = Symbol('__TABLE_KEY__');
|
|
47
|
+
export const TABLE_META_KEY = Symbol('__TABLE_META__');
|
|
48
|
+
export const COLUMN_META_KEY = Symbol('__COLUMN_META__');
|
|
49
|
+
export const ROW_META_KEY = Symbol('__ROW_META__');
|
|
50
|
+
|
|
51
|
+
const attachContainer = (element: Element, table: Table) => {
|
|
52
|
+
assert('Must be installed on an HTMLElement', element instanceof HTMLElement);
|
|
53
|
+
|
|
54
|
+
table.scrollContainerElement = element;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
export class Table<DataType = unknown> extends Resource<Signature<DataType>> {
|
|
58
|
+
/**
|
|
59
|
+
* @private
|
|
60
|
+
*/
|
|
61
|
+
[TABLE_KEY] = guidFor(this);
|
|
62
|
+
/**
|
|
63
|
+
* @private
|
|
64
|
+
*/
|
|
65
|
+
[TABLE_META_KEY] = new Map<Class<unknown>, any>();
|
|
66
|
+
/**
|
|
67
|
+
* @private
|
|
68
|
+
*/
|
|
69
|
+
[COLUMN_META_KEY] = new WeakMap<Column, Map<Class<unknown>, any>>();
|
|
70
|
+
/**
|
|
71
|
+
* @private
|
|
72
|
+
*/
|
|
73
|
+
[ROW_META_KEY] = new WeakMap<Row, Map<Class<unknown>, any>>();
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* @private
|
|
77
|
+
*
|
|
78
|
+
* Unused for now, may be used in the future.
|
|
79
|
+
* This data is collected along with the scrollContainerWidth, (which is currently in use)
|
|
80
|
+
*/
|
|
81
|
+
@tracked scrollContainerHeight?: number;
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* @private
|
|
85
|
+
*
|
|
86
|
+
* Used to help determine how much space we can give to columns.
|
|
87
|
+
* As we generate widths for columns, the columns' widths must
|
|
88
|
+
* add up to about this number.
|
|
89
|
+
*/
|
|
90
|
+
@tracked scrollContainerWidth?: number;
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* @private
|
|
94
|
+
*
|
|
95
|
+
* Lazy way to delay consuming arguments until they are needed.
|
|
96
|
+
*/
|
|
97
|
+
@tracked declare args: { named: Signature<DataType>['Named'] };
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* @private
|
|
101
|
+
*/
|
|
102
|
+
scrollContainerElement?: HTMLElement;
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Interact with, save, modify, etc the preferences for the table,
|
|
106
|
+
* plugins, columns, etc
|
|
107
|
+
*/
|
|
108
|
+
declare preferences: TablePreferences;
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* @private
|
|
112
|
+
*/
|
|
113
|
+
modify(_: [] | undefined, named: Signature<DataType>['Named']) {
|
|
114
|
+
this.args = { named };
|
|
115
|
+
|
|
116
|
+
// only set the preferences once
|
|
117
|
+
if (!this.preferences) {
|
|
118
|
+
const { key = guidFor(this), adapter } = named?.preferences ?? {};
|
|
119
|
+
|
|
120
|
+
// TODO: when no key is present,
|
|
121
|
+
// use "local-storage" preferences.
|
|
122
|
+
// it does not make sense to use a guid in a user's preferences
|
|
123
|
+
this.preferences = new TablePreferences(key, adapter);
|
|
124
|
+
} else {
|
|
125
|
+
// subsequent updates to args
|
|
126
|
+
this.resetScrollContainer();
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Collection of utility modifiers that are the result of composing modifiers
|
|
132
|
+
* from plugins.
|
|
133
|
+
*
|
|
134
|
+
* Using this is optional, and you can "just" use modifiers from specific plugins
|
|
135
|
+
* in specific places if you wish -- but these exists as a "convenience".
|
|
136
|
+
*
|
|
137
|
+
* These are all no-use, no-cost utilities
|
|
138
|
+
*/
|
|
139
|
+
modifiers = {
|
|
140
|
+
container: modifier((element: HTMLElement): Destructor => {
|
|
141
|
+
const modifiers = this.plugins.map((plugin) => plugin.containerModifier);
|
|
142
|
+
const composed = composeFunctionModifiers([
|
|
143
|
+
attachContainer,
|
|
144
|
+
...modifiers,
|
|
145
|
+
]);
|
|
146
|
+
|
|
147
|
+
return composed(element, this as Table<unknown>);
|
|
148
|
+
}),
|
|
149
|
+
|
|
150
|
+
// resize: ResizeModifier,
|
|
151
|
+
// TODO: switch to composing real modifiers once "curry" and "compose"
|
|
152
|
+
// RFCs are accepted and implemented
|
|
153
|
+
//
|
|
154
|
+
// Atm the moment, if _any_ header modifier's tracked data changes,
|
|
155
|
+
// all the functions for all of the plugins run again.
|
|
156
|
+
//
|
|
157
|
+
// With curried+composed modifiers, only the plugin's headerModifier
|
|
158
|
+
// that has tracked changes would run, leaving the other modifiers alone
|
|
159
|
+
columnHeader: modifier(
|
|
160
|
+
(element: HTMLElement, [column]: [Column<DataType>]): Destructor => {
|
|
161
|
+
const modifiers = this.plugins.map(
|
|
162
|
+
(plugin) => plugin.headerCellModifier,
|
|
163
|
+
);
|
|
164
|
+
const composed = composeFunctionModifiers(modifiers);
|
|
165
|
+
|
|
166
|
+
return composed(element, { column, table: this });
|
|
167
|
+
},
|
|
168
|
+
),
|
|
169
|
+
|
|
170
|
+
row: modifier(
|
|
171
|
+
(element: HTMLElement, [row]: [Row<DataType>]): Destructor => {
|
|
172
|
+
const modifiers = this.plugins.map((plugin) => plugin.rowModifier);
|
|
173
|
+
const composed = composeFunctionModifiers(modifiers);
|
|
174
|
+
|
|
175
|
+
return composed(element, { row, table: this });
|
|
176
|
+
},
|
|
177
|
+
),
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* @private
|
|
182
|
+
*
|
|
183
|
+
* For all configured plugins, instantiates each one.
|
|
184
|
+
* If the plugins argument changes to the Table (either directly or through
|
|
185
|
+
* headlessTable, all state is lost and re-created)
|
|
186
|
+
*/
|
|
187
|
+
@cached
|
|
188
|
+
get plugins(): Plugin[] {
|
|
189
|
+
const plugins = normalizePluginsConfig(this.args.named?.plugins);
|
|
190
|
+
|
|
191
|
+
verifyPlugins(plugins);
|
|
192
|
+
|
|
193
|
+
return plugins.map((tuple) => {
|
|
194
|
+
// We don't need the options here
|
|
195
|
+
const [PluginClass] = tuple;
|
|
196
|
+
|
|
197
|
+
if (typeof PluginClass === 'function') {
|
|
198
|
+
const plugin = new PluginClass(this);
|
|
199
|
+
|
|
200
|
+
const owner = getOwner(this);
|
|
201
|
+
|
|
202
|
+
assert(
|
|
203
|
+
`The Table does not have an owner. cannot create a plugin without an owner`,
|
|
204
|
+
owner,
|
|
205
|
+
);
|
|
206
|
+
setOwner(plugin, owner);
|
|
207
|
+
|
|
208
|
+
return plugin;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
// This is a plugin object, rather than a class
|
|
212
|
+
// TODO: add test coverage around using classless plugins
|
|
213
|
+
return PluginClass;
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Get the active plugin instance for the given plugin class
|
|
219
|
+
*/
|
|
220
|
+
pluginOf<Instance extends BasePlugin<any>>(
|
|
221
|
+
klass: Class<Instance>,
|
|
222
|
+
): Instance | undefined {
|
|
223
|
+
const result = this.plugins.find((plugin) => plugin instanceof klass);
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* This is an unsafe cast, because Instance could be unrelated to any of the types
|
|
227
|
+
* that matches Plugin[]
|
|
228
|
+
*
|
|
229
|
+
* For example, `table.pluginOf(MyCustomPlugin)`, where MyCustomPlugin isn't in the
|
|
230
|
+
* `plugins` list. This partially a problem with how Array.prototype.find doesn't
|
|
231
|
+
* effectively narrow for what we want (combined with TS being clunky around
|
|
232
|
+
* comparing Instance and Class types).
|
|
233
|
+
*/
|
|
234
|
+
return result as unknown as Instance | undefined;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* @private
|
|
239
|
+
*
|
|
240
|
+
* used by other private APIs
|
|
241
|
+
*/
|
|
242
|
+
get config() {
|
|
243
|
+
return this.args.named;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
rows = map(this, {
|
|
247
|
+
data: () => {
|
|
248
|
+
const dataFn = this.args.named?.data;
|
|
249
|
+
|
|
250
|
+
if (!dataFn) return [];
|
|
251
|
+
|
|
252
|
+
return dataFn() ?? [];
|
|
253
|
+
},
|
|
254
|
+
map: (datum) => new Row(this, datum),
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
columns = map(this, {
|
|
258
|
+
data: () => {
|
|
259
|
+
const configFn = this.args.named?.columns;
|
|
260
|
+
|
|
261
|
+
if (!configFn) return [];
|
|
262
|
+
|
|
263
|
+
const result = configFn() ?? [];
|
|
264
|
+
|
|
265
|
+
if (macroCondition(isDevelopingApp())) {
|
|
266
|
+
/**
|
|
267
|
+
* Assertions for a column config to be valid:
|
|
268
|
+
* - every key must be unique
|
|
269
|
+
*/
|
|
270
|
+
const keys = new Set();
|
|
271
|
+
const allKeys = result.map((columnConfig) => columnConfig.key);
|
|
272
|
+
|
|
273
|
+
result.forEach((columnConfig) => {
|
|
274
|
+
if (keys.has(columnConfig.key)) {
|
|
275
|
+
throw new Error(
|
|
276
|
+
`Every column key in the table's column config must be unique. ` +
|
|
277
|
+
`Found duplicate entry: ${columnConfig.key}. ` +
|
|
278
|
+
`All keys used: ${allKeys}`,
|
|
279
|
+
);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
keys.add(columnConfig.key);
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
return result;
|
|
287
|
+
},
|
|
288
|
+
map: (config) => {
|
|
289
|
+
return new Column<DataType>(this, {
|
|
290
|
+
...DEFAULT_COLUMN_CONFIG,
|
|
291
|
+
...config,
|
|
292
|
+
});
|
|
293
|
+
},
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* @private
|
|
298
|
+
*/
|
|
299
|
+
@action
|
|
300
|
+
resetScrollContainer() {
|
|
301
|
+
if (!this.scrollContainerElement) return;
|
|
302
|
+
|
|
303
|
+
this.scrollContainerElement.scrollTop = 0;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
@action
|
|
307
|
+
resetToDefaults() {
|
|
308
|
+
this.plugins.forEach((plugin) => plugin.reset?.());
|
|
309
|
+
}
|
|
310
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { Destructor, FunctionModifier } from './interfaces';
|
|
2
|
+
|
|
3
|
+
export function composeFunctionModifiers<Args extends unknown[]>(
|
|
4
|
+
modifiers: Array<FunctionModifier<Args> | undefined>,
|
|
5
|
+
) {
|
|
6
|
+
const setup = modifiers.filter(Boolean) as FunctionModifier<Args>[];
|
|
7
|
+
|
|
8
|
+
const composed = (element: HTMLElement, ...args: Args) => {
|
|
9
|
+
const destructors = setup
|
|
10
|
+
.map((fn) => fn(element, ...args))
|
|
11
|
+
.filter(Boolean) as Destructor[];
|
|
12
|
+
|
|
13
|
+
return () => {
|
|
14
|
+
for (const destructor of destructors) {
|
|
15
|
+
destructor();
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
return composed;
|
|
21
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/********************************
|
|
2
|
+
* Public API
|
|
3
|
+
*******************************/
|
|
4
|
+
export { headlessTable, headlessTable as table } from './-private/js-helper.ts';
|
|
5
|
+
|
|
6
|
+
// Utilities
|
|
7
|
+
export { TablePreferences } from './-private/preferences.ts';
|
|
8
|
+
export { deserializeSorts, serializeSorts } from './utils.ts';
|
|
9
|
+
|
|
10
|
+
/********************************
|
|
11
|
+
* Public Types
|
|
12
|
+
*******************************/
|
|
13
|
+
export type { Column } from './-private/column.ts';
|
|
14
|
+
export type {
|
|
15
|
+
ColumnConfig,
|
|
16
|
+
ColumnKey,
|
|
17
|
+
Pagination,
|
|
18
|
+
PreferencesAdapter,
|
|
19
|
+
TablePreferencesData as PreferencesData,
|
|
20
|
+
Selection,
|
|
21
|
+
TableConfig,
|
|
22
|
+
TableMeta,
|
|
23
|
+
} from './-private/interfaces/index.ts';
|
|
24
|
+
export type { Row } from './-private/row.ts';
|
|
25
|
+
export type { Table } from './-private/table.ts';
|