@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,166 @@
|
|
|
1
|
+
import { assert } from '@ember/debug';
|
|
2
|
+
|
|
3
|
+
import type { BasePlugin } from './base';
|
|
4
|
+
import type { Constructor } from '../../-private/private-types';
|
|
5
|
+
import type { Plugin } from '../../plugins';
|
|
6
|
+
|
|
7
|
+
type PluginOption =
|
|
8
|
+
| Constructor<Plugin<any>>
|
|
9
|
+
| [Constructor<Plugin<any>>, () => any];
|
|
10
|
+
|
|
11
|
+
type ExpandedPluginOption = [Constructor<Plugin<any>>, () => any];
|
|
12
|
+
|
|
13
|
+
export type Plugins = PluginOption[];
|
|
14
|
+
|
|
15
|
+
export function normalizePluginsConfig(
|
|
16
|
+
plugins?: Plugins,
|
|
17
|
+
): ExpandedPluginOption[] {
|
|
18
|
+
if (!plugins) return [];
|
|
19
|
+
|
|
20
|
+
const result: ExpandedPluginOption[] = [];
|
|
21
|
+
|
|
22
|
+
for (const plugin of plugins) {
|
|
23
|
+
if (!Array.isArray(plugin)) {
|
|
24
|
+
result.push([plugin, () => ({})]);
|
|
25
|
+
|
|
26
|
+
continue;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
if (plugin.length === 2) {
|
|
30
|
+
result.push([plugin[0], plugin[1]]);
|
|
31
|
+
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
result.push([plugin[0], () => ({})]);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
assert(
|
|
39
|
+
`Every entry in the plugins config must be invokable`,
|
|
40
|
+
result.every(
|
|
41
|
+
(tuple) =>
|
|
42
|
+
typeof tuple[0] === 'function' && typeof tuple[1] === 'function',
|
|
43
|
+
),
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
return result;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Creates a map of featureName => [plugins providing said feature name]
|
|
51
|
+
*/
|
|
52
|
+
function collectFeatures(plugins: ExpandedPluginOption[]) {
|
|
53
|
+
const result: Record<string, { name: string }[]> = {};
|
|
54
|
+
|
|
55
|
+
for (const [plugin] of plugins) {
|
|
56
|
+
if ('features' in plugin) {
|
|
57
|
+
for (const feature of (plugin as unknown as typeof BasePlugin).features ||
|
|
58
|
+
[]) {
|
|
59
|
+
result[feature] = [...(result[feature] || []), plugin];
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return result;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Creates a map of requirement => [plugins requesting the feature / requirement]
|
|
69
|
+
*/
|
|
70
|
+
function collectRequirements(plugins: ExpandedPluginOption[]) {
|
|
71
|
+
const result: Record<string, { name: string }[]> = {};
|
|
72
|
+
|
|
73
|
+
for (const [plugin] of plugins) {
|
|
74
|
+
if ('requires' in plugin) {
|
|
75
|
+
for (const requirement of (plugin as unknown as typeof BasePlugin)
|
|
76
|
+
.requires || []) {
|
|
77
|
+
result[requirement] = [...(result[requirement] || []), plugin];
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return result;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export function verifyPlugins(plugins: ExpandedPluginOption[]) {
|
|
86
|
+
const features = collectFeatures(plugins);
|
|
87
|
+
const requirements = collectRequirements(plugins);
|
|
88
|
+
const allFeatures = Object.keys(features);
|
|
89
|
+
const errors: string[] = [];
|
|
90
|
+
|
|
91
|
+
// Only one plugin can provide each feature
|
|
92
|
+
for (const [feature, providingPlugins] of Object.entries(features)) {
|
|
93
|
+
if (providingPlugins.length > 1) {
|
|
94
|
+
errors.push(
|
|
95
|
+
`More than one plugin is providing the feature: ${feature}. ` +
|
|
96
|
+
`Please remove one of ${providingPlugins.map((p) => p.name).join(', ')}`,
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
for (const [requirement, requestingPlugins] of Object.entries(requirements)) {
|
|
102
|
+
if (!allFeatures.includes(requirement)) {
|
|
103
|
+
errors.push(
|
|
104
|
+
`Configuration is missing requirement: ${requirement}, ` +
|
|
105
|
+
`And is requested by ${requestingPlugins.map((p) => p.name).join(', ')}. ` +
|
|
106
|
+
`Please add a plugin with the ${requirement} feature`,
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if (errors.length > 0) {
|
|
112
|
+
throw new Error(errors.join('\n'));
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
type AssignableStyles = Omit<CSSStyleDeclaration, 'length' | 'parentRule'>;
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* @public
|
|
120
|
+
*
|
|
121
|
+
* Utility that helps safely apply styles to an element
|
|
122
|
+
*/
|
|
123
|
+
export function applyStyles(
|
|
124
|
+
element: HTMLElement | SVGElement,
|
|
125
|
+
styles: Partial<AssignableStyles>,
|
|
126
|
+
) {
|
|
127
|
+
for (const [name, value] of Object.entries(styles)) {
|
|
128
|
+
if (name in element.style) {
|
|
129
|
+
assignStyle(
|
|
130
|
+
element,
|
|
131
|
+
name as keyof CSSStyleDeclaration,
|
|
132
|
+
value as CSSStyleDeclaration[keyof CSSStyleDeclaration],
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
type StyleDeclarationFor<MaybeStyle> =
|
|
139
|
+
MaybeStyle extends keyof CSSStyleDeclaration ? MaybeStyle : never;
|
|
140
|
+
|
|
141
|
+
function assignStyle<StyleName>(
|
|
142
|
+
element: HTMLElement | SVGElement,
|
|
143
|
+
styleName: StyleDeclarationFor<StyleName>,
|
|
144
|
+
value: CSSStyleDeclaration[StyleDeclarationFor<StyleName>],
|
|
145
|
+
) {
|
|
146
|
+
element.style[styleName] = value;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function removeStyle(element: HTMLElement | SVGElement, styleName: string) {
|
|
150
|
+
element.style.removeProperty(styleName);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* @public
|
|
155
|
+
*
|
|
156
|
+
* Utility that helps safely remove styles from an element
|
|
157
|
+
*/
|
|
158
|
+
export function removeStyles(
|
|
159
|
+
element: HTMLElement | SVGElement,
|
|
160
|
+
styles: Array<keyof AssignableStyles>,
|
|
161
|
+
) {
|
|
162
|
+
for (const name of styles) {
|
|
163
|
+
if (typeof name !== 'string') continue;
|
|
164
|
+
removeStyle(element, name);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { meta } from '../-private/base.ts';
|
|
2
|
+
import { ColumnReordering } from './plugin.ts';
|
|
3
|
+
|
|
4
|
+
import type { ColumnOrder } from './plugin.ts';
|
|
5
|
+
import type { Column, Table } from '../../index.ts';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Move the column one position to the left.
|
|
9
|
+
* If the column is first, nothing will happen.
|
|
10
|
+
*/
|
|
11
|
+
export const moveLeft = (column: Column) =>
|
|
12
|
+
meta.forColumn(column, ColumnReordering).moveLeft();
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Move the column one position to the right.
|
|
16
|
+
* If the column is last, nothing will happen.
|
|
17
|
+
*/
|
|
18
|
+
export const moveRight = (column: Column) =>
|
|
19
|
+
meta.forColumn(column, ColumnReordering).moveRight();
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Override all column positions at once.
|
|
23
|
+
*/
|
|
24
|
+
export const setColumnOrder = (table: Table, order: ColumnOrder) => {
|
|
25
|
+
return meta.forTable(table, ColumnReordering).setOrder(order);
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Ask if the column cannot move to the left
|
|
30
|
+
*/
|
|
31
|
+
export const cannotMoveLeft = (column: Column) =>
|
|
32
|
+
meta.forColumn(column, ColumnReordering).cannotMoveLeft;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Ask if the column cannot move to the right
|
|
36
|
+
*/
|
|
37
|
+
export const cannotMoveRight = (column: Column) =>
|
|
38
|
+
meta.forColumn(column, ColumnReordering).cannotMoveRight;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Ask if the column can move to the left
|
|
42
|
+
*/
|
|
43
|
+
export const canMoveLeft = (column: Column) =>
|
|
44
|
+
meta.forColumn(column, ColumnReordering).cannotMoveLeft;
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Ask if the column can move to the right
|
|
48
|
+
*/
|
|
49
|
+
export const canMoveRight = (column: Column) =>
|
|
50
|
+
meta.forColumn(column, ColumnReordering).cannotMoveRight;
|
|
@@ -0,0 +1,489 @@
|
|
|
1
|
+
import { cached, tracked } from '@glimmer/tracking';
|
|
2
|
+
import { assert } from '@ember/debug';
|
|
3
|
+
import { action } from '@ember/object';
|
|
4
|
+
|
|
5
|
+
import { TrackedMap } from 'tracked-built-ins';
|
|
6
|
+
|
|
7
|
+
import { preferences } from '../../plugins/index.ts';
|
|
8
|
+
|
|
9
|
+
import { BasePlugin, columns, meta } from '../-private/base.ts';
|
|
10
|
+
|
|
11
|
+
import type { PluginPreferences } from '../../plugins/index.ts';
|
|
12
|
+
import type { Column, Table } from '../../index.ts';
|
|
13
|
+
|
|
14
|
+
interface ColumnReorderingPreferences extends PluginPreferences {
|
|
15
|
+
table: {
|
|
16
|
+
order?: Record<string, number>;
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
declare module '@universal-ember/table/plugins' {
|
|
21
|
+
interface Registry {
|
|
22
|
+
ColumnReordering?: ColumnReorderingPreferences;
|
|
23
|
+
'column-reordering'?: ColumnReorderingPreferences;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface Signature {
|
|
28
|
+
Meta: {
|
|
29
|
+
Column: ColumnMeta;
|
|
30
|
+
Table: TableMeta;
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export class ColumnReordering extends BasePlugin<Signature> {
|
|
35
|
+
name = 'column-reordering';
|
|
36
|
+
static features = ['columnOrder'];
|
|
37
|
+
|
|
38
|
+
meta = {
|
|
39
|
+
column: ColumnMeta,
|
|
40
|
+
table: TableMeta,
|
|
41
|
+
} as const;
|
|
42
|
+
|
|
43
|
+
reset() {
|
|
44
|
+
const tableMeta = meta.forTable(this.table, ColumnReordering);
|
|
45
|
+
|
|
46
|
+
tableMeta.reset();
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
get columns() {
|
|
50
|
+
return meta.forTable(this.table, ColumnReordering).columns;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export class ColumnMeta {
|
|
55
|
+
constructor(private column: Column) {}
|
|
56
|
+
|
|
57
|
+
get #tableMeta() {
|
|
58
|
+
return meta.forTable(this.column.table, ColumnReordering);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
get position() {
|
|
62
|
+
return this.#tableMeta.getPosition(this.column);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
set position(value: number) {
|
|
66
|
+
this.#tableMeta.setPosition(this.column, value);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
get canMoveLeft() {
|
|
70
|
+
return this.#tableMeta.getPosition(this.column) !== 0;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
get canMoveRight() {
|
|
74
|
+
return (
|
|
75
|
+
this.#tableMeta.getPosition(this.column) !==
|
|
76
|
+
this.#tableMeta.columns.length - 1
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
get cannotMoveLeft() {
|
|
81
|
+
return !this.canMoveLeft;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
get cannotMoveRight() {
|
|
85
|
+
return !this.canMoveRight;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Move the column one spot to the left
|
|
90
|
+
*/
|
|
91
|
+
moveLeft = () => {
|
|
92
|
+
this.#tableMeta.columnOrder.moveLeft(this.column.key);
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Move the column one spot to the right
|
|
97
|
+
*/
|
|
98
|
+
moveRight = () => {
|
|
99
|
+
this.#tableMeta.columnOrder.moveRight(this.column.key);
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export class TableMeta {
|
|
104
|
+
constructor(private table: Table) {}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* @private
|
|
108
|
+
*
|
|
109
|
+
* We want to maintain the instance of this ColumnOrder class because
|
|
110
|
+
* we allow the consumer of the table to swap out columns at any time.
|
|
111
|
+
* When they do this, we want to maintain the order of the table, best we can.
|
|
112
|
+
* This is also why the order of the columns is maintained via column key
|
|
113
|
+
*/
|
|
114
|
+
@tracked
|
|
115
|
+
columnOrder = new ColumnOrder({
|
|
116
|
+
columns: () => this.availableColumns,
|
|
117
|
+
save: this.save,
|
|
118
|
+
existingOrder: this.read(),
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Get the curret order/position of a column
|
|
123
|
+
*/
|
|
124
|
+
@action
|
|
125
|
+
getPosition(column: Column) {
|
|
126
|
+
return this.columnOrder.get(column.key);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Swap the column with the column at `newPosition`
|
|
131
|
+
*/
|
|
132
|
+
@action
|
|
133
|
+
setPosition(column: Column, newPosition: number) {
|
|
134
|
+
return this.columnOrder.swapWith(column.key, newPosition);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Using a `ColumnOrder` instance, set the order of all columns
|
|
139
|
+
*/
|
|
140
|
+
setOrder = (order: ColumnOrder) => {
|
|
141
|
+
this.columnOrder.setAll(order.map);
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Revert to default config, delete preferences,
|
|
146
|
+
* and clear the columnOrder
|
|
147
|
+
*/
|
|
148
|
+
@action
|
|
149
|
+
reset() {
|
|
150
|
+
preferences.forTable(this.table, ColumnReordering).delete('order');
|
|
151
|
+
this.columnOrder = new ColumnOrder({
|
|
152
|
+
columns: () => this.availableColumns,
|
|
153
|
+
save: this.save,
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* @private
|
|
159
|
+
*/
|
|
160
|
+
@action
|
|
161
|
+
save(map: Map<string, number>) {
|
|
162
|
+
const order: Record<string, number> = {};
|
|
163
|
+
|
|
164
|
+
for (const [key, position] of map.entries()) {
|
|
165
|
+
order[key] = position;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
preferences.forTable(this.table, ColumnReordering).set('order', order);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* @private
|
|
173
|
+
*/
|
|
174
|
+
@action
|
|
175
|
+
private read() {
|
|
176
|
+
const order = preferences
|
|
177
|
+
.forTable(this.table, ColumnReordering)
|
|
178
|
+
.get('order');
|
|
179
|
+
|
|
180
|
+
if (!order) return;
|
|
181
|
+
|
|
182
|
+
return new Map<string, number>(Object.entries(order));
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
get columns() {
|
|
186
|
+
return this.columnOrder.orderedColumns;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* @private
|
|
191
|
+
* This isn't our data to expose, but it is useful to alias
|
|
192
|
+
*/
|
|
193
|
+
private get availableColumns() {
|
|
194
|
+
return columns.for(this.table, ColumnReordering);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* @private
|
|
200
|
+
* Used for keeping track of and updating column order
|
|
201
|
+
*/
|
|
202
|
+
export class ColumnOrder {
|
|
203
|
+
/**
|
|
204
|
+
* This map will be empty until we re-order something.
|
|
205
|
+
*/
|
|
206
|
+
map = new TrackedMap<string, number>();
|
|
207
|
+
|
|
208
|
+
constructor(
|
|
209
|
+
private args: {
|
|
210
|
+
columns: () => Column[];
|
|
211
|
+
save?: (order: Map<string, number>) => void;
|
|
212
|
+
existingOrder?: Map<string, number>;
|
|
213
|
+
},
|
|
214
|
+
) {
|
|
215
|
+
if (args.existingOrder) {
|
|
216
|
+
this.map = new TrackedMap(args.existingOrder);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* To account for columnVisibilty, we need to:
|
|
222
|
+
* - get the list of visible columns
|
|
223
|
+
* - get the column order (which preserves the order of hidden columns)
|
|
224
|
+
* - skip over non-visible columns when determining the previous "index"
|
|
225
|
+
* - set the position to whatever that is.
|
|
226
|
+
*/
|
|
227
|
+
@action
|
|
228
|
+
moveLeft(key: string) {
|
|
229
|
+
const orderedColumns = this.orderedColumns;
|
|
230
|
+
|
|
231
|
+
let found = false;
|
|
232
|
+
let nextColumn: { key: string } | undefined;
|
|
233
|
+
|
|
234
|
+
for (const column of orderedColumns.reverse()) {
|
|
235
|
+
if (found) {
|
|
236
|
+
nextColumn = column;
|
|
237
|
+
|
|
238
|
+
break;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
if (column.key === key) {
|
|
242
|
+
found = true;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
if (!nextColumn) return;
|
|
247
|
+
|
|
248
|
+
const nextPosition = this.get(nextColumn.key);
|
|
249
|
+
|
|
250
|
+
this.swapWith(key, nextPosition);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
setAll = (map: Map<string, number>) => {
|
|
254
|
+
this.map.clear();
|
|
255
|
+
|
|
256
|
+
for (const [key, value] of map.entries()) {
|
|
257
|
+
this.map.set(key, value);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
this.args.save?.(map);
|
|
261
|
+
};
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* To account for columnVisibilty, we need to:
|
|
265
|
+
* - get the list of visible columns
|
|
266
|
+
* - get the column order (which preserves the order of hidden columns)
|
|
267
|
+
* - skip over non-visible columns when determining the next "index"
|
|
268
|
+
* - set the position to whatever that is.
|
|
269
|
+
*/
|
|
270
|
+
@action
|
|
271
|
+
moveRight(key: string) {
|
|
272
|
+
const orderedColumns = this.orderedColumns;
|
|
273
|
+
|
|
274
|
+
let found = false;
|
|
275
|
+
let nextColumn: { key: string } | undefined;
|
|
276
|
+
|
|
277
|
+
for (const column of orderedColumns) {
|
|
278
|
+
if (found) {
|
|
279
|
+
nextColumn = column;
|
|
280
|
+
|
|
281
|
+
break;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
if (column.key === key) {
|
|
285
|
+
found = true;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
if (!nextColumn) return;
|
|
290
|
+
|
|
291
|
+
const nextPosition = this.get(nextColumn.key);
|
|
292
|
+
|
|
293
|
+
this.swapWith(key, nextPosition);
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* Performs a swap of the column's position with the column at position
|
|
298
|
+
*/
|
|
299
|
+
@action
|
|
300
|
+
swapWith(key: string, position: number) {
|
|
301
|
+
const validPositions = [...this.orderedMap.values()];
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* Position to swap to must exist
|
|
305
|
+
*/
|
|
306
|
+
if (!validPositions.includes(position)) {
|
|
307
|
+
return;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* Where did this column `key` come from? we can find out
|
|
312
|
+
* by reading orderedMap
|
|
313
|
+
*/
|
|
314
|
+
const currentPosition = this.orderedMap.get(key);
|
|
315
|
+
|
|
316
|
+
assert(
|
|
317
|
+
`Pre-existing position for ${key} could not be found. Does the column exist? ` +
|
|
318
|
+
`The current positions are: ` +
|
|
319
|
+
[...this.orderedMap.entries()]
|
|
320
|
+
.map((entry) => entry.join(' => '))
|
|
321
|
+
.join(', ') +
|
|
322
|
+
` and the availableColumns are: ` +
|
|
323
|
+
this.args
|
|
324
|
+
.columns()
|
|
325
|
+
.map((column) => column.key)
|
|
326
|
+
.join(', ') +
|
|
327
|
+
` and current "map" (${this.map.size}) is: ` +
|
|
328
|
+
[...this.map.entries()].map((entry) => entry.join(' => ')).join(', '),
|
|
329
|
+
undefined !== currentPosition,
|
|
330
|
+
);
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* No need to change anything if the position is the same
|
|
334
|
+
* This helps reduce @tracked invalidations, which in turn reduces DOM thrashing.
|
|
335
|
+
*/
|
|
336
|
+
if (currentPosition === position) {
|
|
337
|
+
return false;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
const keyByPosition = new Map<number, string>(
|
|
341
|
+
[...this.orderedMap.entries()].map(
|
|
342
|
+
(entry) => entry.reverse() as [number, string],
|
|
343
|
+
),
|
|
344
|
+
);
|
|
345
|
+
|
|
346
|
+
for (const [existingPosition, key] of keyByPosition.entries()) {
|
|
347
|
+
if (existingPosition === position) {
|
|
348
|
+
/**
|
|
349
|
+
* We swap positions because the positions are not incremental
|
|
350
|
+
* meaning we can have gaps, intentionally, due to hidden columns
|
|
351
|
+
*/
|
|
352
|
+
this.map.set(key, currentPosition);
|
|
353
|
+
|
|
354
|
+
break;
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
/**
|
|
359
|
+
* Finally, set the position for the requested column
|
|
360
|
+
*/
|
|
361
|
+
this.map.set(key, position);
|
|
362
|
+
|
|
363
|
+
/**
|
|
364
|
+
* Now that we've set the value for one column,
|
|
365
|
+
* we need to make sure that all columns have a recorded position.
|
|
366
|
+
*/
|
|
367
|
+
for (const [key, position] of this.orderedMap.entries()) {
|
|
368
|
+
if (this.map.has(key)) continue;
|
|
369
|
+
|
|
370
|
+
this.map.set(key, position);
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
this.args.save?.(this.map);
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
@action
|
|
377
|
+
get(key: string) {
|
|
378
|
+
const result = this.orderedMap.get(key);
|
|
379
|
+
|
|
380
|
+
assert(
|
|
381
|
+
`No position found for ${key}. Is the column used within this table?`,
|
|
382
|
+
/* 0 is falsey, but it's a valid value for position */
|
|
383
|
+
undefined !== result,
|
|
384
|
+
);
|
|
385
|
+
|
|
386
|
+
return result;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
/**
|
|
390
|
+
* The same as this.map, but with all the columns' information
|
|
391
|
+
*/
|
|
392
|
+
@cached
|
|
393
|
+
get orderedMap(): ReadonlyMap<string, number> {
|
|
394
|
+
return orderOf(this.args.columns(), this.map);
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
@cached
|
|
398
|
+
get orderedColumns(): Column[] {
|
|
399
|
+
const availableColumns = this.args.columns();
|
|
400
|
+
const availableByKey = availableColumns.reduce(
|
|
401
|
+
(keyMap, column) => {
|
|
402
|
+
keyMap[column.key] = column;
|
|
403
|
+
|
|
404
|
+
return keyMap;
|
|
405
|
+
},
|
|
406
|
+
{} as Record<string, Column>,
|
|
407
|
+
);
|
|
408
|
+
const mergedOrder = orderOf(availableColumns, this.map);
|
|
409
|
+
|
|
410
|
+
const result: Column[] = Array.from({ length: availableColumns.length });
|
|
411
|
+
|
|
412
|
+
for (const [key, position] of mergedOrder.entries()) {
|
|
413
|
+
const column = availableByKey[key];
|
|
414
|
+
|
|
415
|
+
assert(`Could not find column for pair: ${key} @ @{position}`, column);
|
|
416
|
+
result[position] = column;
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
assert(
|
|
420
|
+
`Generated orderedColumns' length (${result.filter(Boolean).length}) ` +
|
|
421
|
+
`does not match the length of available columns (${availableColumns.length}). ` +
|
|
422
|
+
`orderedColumns: ${result
|
|
423
|
+
.filter(Boolean)
|
|
424
|
+
.map((c) => c.key)
|
|
425
|
+
.join(', ')} -- ` +
|
|
426
|
+
`available columns: ${availableColumns.map((c) => c.key).join(', ')}`,
|
|
427
|
+
result.filter(Boolean).length === availableColumns.length,
|
|
428
|
+
);
|
|
429
|
+
|
|
430
|
+
return result.filter(Boolean);
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
/**
|
|
435
|
+
* @private
|
|
436
|
+
*
|
|
437
|
+
* Utility for helping determine the percieved order of a set of columns
|
|
438
|
+
* given the original (default) ordering, and then user-configurations
|
|
439
|
+
*/
|
|
440
|
+
export function orderOf(
|
|
441
|
+
columns: { key: string }[],
|
|
442
|
+
currentOrder: Map<string, number>,
|
|
443
|
+
): Map<string, number> {
|
|
444
|
+
const result = new Map<string, number>();
|
|
445
|
+
const availableColumns = columns.map((column) => column.key);
|
|
446
|
+
const availableSet = new Set(availableColumns);
|
|
447
|
+
const current = new Map<number, string>(
|
|
448
|
+
[...currentOrder.entries()].map(([key, position]) => [position, key]),
|
|
449
|
+
);
|
|
450
|
+
|
|
451
|
+
/**
|
|
452
|
+
* O(n * log(n)) ?
|
|
453
|
+
*/
|
|
454
|
+
for (let i = 0; i < Math.max(columns.length, current.size); i++) {
|
|
455
|
+
const orderedKey = current.get(i);
|
|
456
|
+
|
|
457
|
+
if (orderedKey) {
|
|
458
|
+
/**
|
|
459
|
+
* If the currentOrder specifies columns not presently available,
|
|
460
|
+
* ignore them
|
|
461
|
+
*/
|
|
462
|
+
if (availableSet.has(orderedKey)) {
|
|
463
|
+
result.set(orderedKey, i);
|
|
464
|
+
continue;
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
let availableKey: string | undefined;
|
|
469
|
+
|
|
470
|
+
while ((availableKey = availableColumns.shift())) {
|
|
471
|
+
if (result.has(availableKey) || currentOrder.has(availableKey)) {
|
|
472
|
+
continue;
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
break;
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
if (!availableKey) {
|
|
479
|
+
/**
|
|
480
|
+
* The rest of our columns likely have their order set
|
|
481
|
+
*/
|
|
482
|
+
continue;
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
result.set(availableKey, i);
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
return result;
|
|
489
|
+
}
|