@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.
Files changed (168) hide show
  1. package/dist/-private/-type-tests/plugin-properties.test.js +27 -0
  2. package/dist/-private/-type-tests/plugin-properties.test.js.map +1 -0
  3. package/dist/-private/-type-tests/plugin-with.test.js +20 -0
  4. package/dist/-private/-type-tests/plugin-with.test.js.map +1 -0
  5. package/dist/-private/-type-tests/plugins-accessors.test.js +36 -0
  6. package/dist/-private/-type-tests/plugins-accessors.test.js.map +1 -0
  7. package/dist/-private/-type-tests/plugins-signature-from.test.js +15 -0
  8. package/dist/-private/-type-tests/plugins-signature-from.test.js.map +1 -0
  9. package/dist/-private/-type-tests/plugins-signature-utils.test.js +36 -0
  10. package/dist/-private/-type-tests/plugins-signature-utils.test.js.map +1 -0
  11. package/dist/-private/-type-tests/table-api.test.js +17 -0
  12. package/dist/-private/-type-tests/table-api.test.js.map +1 -0
  13. package/dist/-private/-type-tests/table-config.test.js +55 -0
  14. package/dist/-private/-type-tests/table-config.test.js.map +1 -0
  15. package/dist/-private/column.js +62 -0
  16. package/dist/-private/column.js.map +1 -0
  17. package/dist/-private/ember-compat.js +17 -0
  18. package/dist/-private/ember-compat.js.map +1 -0
  19. package/dist/-private/interfaces/column.js +2 -0
  20. package/dist/-private/interfaces/column.js.map +1 -0
  21. package/dist/-private/interfaces/index.js +2 -0
  22. package/dist/-private/interfaces/index.js.map +1 -0
  23. package/dist/-private/interfaces/modifier.js +2 -0
  24. package/dist/-private/interfaces/modifier.js.map +1 -0
  25. package/dist/-private/interfaces/pagination.js +2 -0
  26. package/dist/-private/interfaces/pagination.js.map +1 -0
  27. package/dist/-private/interfaces/plugins.js +2 -0
  28. package/dist/-private/interfaces/plugins.js.map +1 -0
  29. package/dist/-private/interfaces/preferences.js +2 -0
  30. package/dist/-private/interfaces/preferences.js.map +1 -0
  31. package/dist/-private/interfaces/selection.js +2 -0
  32. package/dist/-private/interfaces/selection.js.map +1 -0
  33. package/dist/-private/interfaces/table.js +2 -0
  34. package/dist/-private/interfaces/table.js.map +1 -0
  35. package/dist/-private/js-helper.js +55 -0
  36. package/dist/-private/js-helper.js.map +1 -0
  37. package/dist/-private/preferences.js +143 -0
  38. package/dist/-private/preferences.js.map +1 -0
  39. package/dist/-private/private-types.js +2 -0
  40. package/dist/-private/private-types.js.map +1 -0
  41. package/dist/-private/row.js +51 -0
  42. package/dist/-private/row.js.map +1 -0
  43. package/dist/-private/table.js +273 -0
  44. package/dist/-private/table.js.map +1 -0
  45. package/dist/-private/utils.js +15 -0
  46. package/dist/-private/utils.js.map +1 -0
  47. package/dist/_rollupPluginBabelHelpers-BpiaYhlf.js +63 -0
  48. package/dist/_rollupPluginBabelHelpers-BpiaYhlf.js.map +1 -0
  49. package/dist/index.js +4 -0
  50. package/dist/index.js.map +1 -0
  51. package/dist/plugins/-private/base.js +524 -0
  52. package/dist/plugins/-private/base.js.map +1 -0
  53. package/dist/plugins/-private/utils.js +103 -0
  54. package/dist/plugins/-private/utils.js.map +1 -0
  55. package/dist/plugins/column-reordering/helpers.js +44 -0
  56. package/dist/plugins/column-reordering/helpers.js.map +1 -0
  57. package/dist/plugins/column-reordering/index.js +3 -0
  58. package/dist/plugins/column-reordering/index.js.map +1 -0
  59. package/dist/plugins/column-reordering/plugin.js +359 -0
  60. package/dist/plugins/column-reordering/plugin.js.map +1 -0
  61. package/dist/plugins/column-reordering/utils.js +34 -0
  62. package/dist/plugins/column-reordering/utils.js.map +1 -0
  63. package/dist/plugins/column-resizing/handle.js +241 -0
  64. package/dist/plugins/column-resizing/handle.js.map +1 -0
  65. package/dist/plugins/column-resizing/helpers.js +71 -0
  66. package/dist/plugins/column-resizing/helpers.js.map +1 -0
  67. package/dist/plugins/column-resizing/index.js +4 -0
  68. package/dist/plugins/column-resizing/index.js.map +1 -0
  69. package/dist/plugins/column-resizing/plugin.js +328 -0
  70. package/dist/plugins/column-resizing/plugin.js.map +1 -0
  71. package/dist/plugins/column-resizing/resize-observer.js +44 -0
  72. package/dist/plugins/column-resizing/resize-observer.js.map +1 -0
  73. package/dist/plugins/column-resizing/utils.js +44 -0
  74. package/dist/plugins/column-resizing/utils.js.map +1 -0
  75. package/dist/plugins/column-visibility/helpers.js +25 -0
  76. package/dist/plugins/column-visibility/helpers.js.map +1 -0
  77. package/dist/plugins/column-visibility/index.js +3 -0
  78. package/dist/plugins/column-visibility/index.js.map +1 -0
  79. package/dist/plugins/column-visibility/plugin.js +92 -0
  80. package/dist/plugins/column-visibility/plugin.js.map +1 -0
  81. package/dist/plugins/data-sorting/helpers.js +49 -0
  82. package/dist/plugins/data-sorting/helpers.js.map +1 -0
  83. package/dist/plugins/data-sorting/index.js +4 -0
  84. package/dist/plugins/data-sorting/index.js.map +1 -0
  85. package/dist/plugins/data-sorting/plugin.js +132 -0
  86. package/dist/plugins/data-sorting/plugin.js.map +1 -0
  87. package/dist/plugins/data-sorting/types.js +14 -0
  88. package/dist/plugins/data-sorting/types.js.map +1 -0
  89. package/dist/plugins/index.js +3 -0
  90. package/dist/plugins/index.js.map +1 -0
  91. package/dist/plugins/metadata/helpers.js +12 -0
  92. package/dist/plugins/metadata/helpers.js.map +1 -0
  93. package/dist/plugins/metadata/index.js +3 -0
  94. package/dist/plugins/metadata/index.js.map +1 -0
  95. package/dist/plugins/metadata/plugin.js +25 -0
  96. package/dist/plugins/metadata/plugin.js.map +1 -0
  97. package/dist/plugins/row-selection/helpers.js +10 -0
  98. package/dist/plugins/row-selection/helpers.js.map +1 -0
  99. package/dist/plugins/row-selection/index.js +3 -0
  100. package/dist/plugins/row-selection/index.js.map +1 -0
  101. package/dist/plugins/row-selection/plugin.js +118 -0
  102. package/dist/plugins/row-selection/plugin.js.map +1 -0
  103. package/dist/plugins/sticky-columns/helpers.js +49 -0
  104. package/dist/plugins/sticky-columns/helpers.js.map +1 -0
  105. package/dist/plugins/sticky-columns/index.js +3 -0
  106. package/dist/plugins/sticky-columns/index.js.map +1 -0
  107. package/dist/plugins/sticky-columns/plugin.js +139 -0
  108. package/dist/plugins/sticky-columns/plugin.js.map +1 -0
  109. package/dist/test-support/index.js +62 -0
  110. package/dist/test-support/index.js.map +1 -0
  111. package/dist/utils.js +77 -0
  112. package/dist/utils.js.map +1 -0
  113. package/package.json +3 -2
  114. package/src/-private/-type-tests/plugin-properties.test.ts +38 -0
  115. package/src/-private/-type-tests/plugin-with.test.ts +23 -0
  116. package/src/-private/-type-tests/plugins-accessors.test.ts +86 -0
  117. package/src/-private/-type-tests/plugins-signature-from.test.ts +66 -0
  118. package/src/-private/-type-tests/plugins-signature-utils.test.ts +154 -0
  119. package/src/-private/-type-tests/table-api.test.ts +20 -0
  120. package/src/-private/-type-tests/table-config.test.ts +70 -0
  121. package/src/-private/column.ts +67 -0
  122. package/src/-private/ember-compat.ts +26 -0
  123. package/src/-private/interfaces/column.ts +73 -0
  124. package/src/-private/interfaces/index.ts +7 -0
  125. package/src/-private/interfaces/modifier.ts +7 -0
  126. package/src/-private/interfaces/pagination.ts +13 -0
  127. package/src/-private/interfaces/plugins.ts +349 -0
  128. package/src/-private/interfaces/preferences.ts +82 -0
  129. package/src/-private/interfaces/selection.ts +38 -0
  130. package/src/-private/interfaces/table.ts +121 -0
  131. package/src/-private/js-helper.ts +65 -0
  132. package/src/-private/preferences.ts +176 -0
  133. package/src/-private/private-types.ts +8 -0
  134. package/src/-private/row.ts +66 -0
  135. package/src/-private/table.ts +310 -0
  136. package/src/-private/utils.ts +21 -0
  137. package/src/index.ts +25 -0
  138. package/src/plugins/-private/base.ts +836 -0
  139. package/src/plugins/-private/utils.ts +166 -0
  140. package/src/plugins/column-reordering/helpers.ts +50 -0
  141. package/src/plugins/column-reordering/index.ts +6 -0
  142. package/src/plugins/column-reordering/plugin.ts +489 -0
  143. package/src/plugins/column-reordering/utils.ts +48 -0
  144. package/src/plugins/column-resizing/handle.ts +280 -0
  145. package/src/plugins/column-resizing/helpers.ts +79 -0
  146. package/src/plugins/column-resizing/index.ts +7 -0
  147. package/src/plugins/column-resizing/plugin.ts +490 -0
  148. package/src/plugins/column-resizing/resize-observer.ts +48 -0
  149. package/src/plugins/column-resizing/utils.ts +54 -0
  150. package/src/plugins/column-visibility/helpers.ts +28 -0
  151. package/src/plugins/column-visibility/index.ts +6 -0
  152. package/src/plugins/column-visibility/plugin.ts +155 -0
  153. package/src/plugins/data-sorting/helpers.ts +56 -0
  154. package/src/plugins/data-sorting/index.ts +8 -0
  155. package/src/plugins/data-sorting/plugin.ts +222 -0
  156. package/src/plugins/data-sorting/types.ts +26 -0
  157. package/src/plugins/index.ts +20 -0
  158. package/src/plugins/metadata/helpers.ts +12 -0
  159. package/src/plugins/metadata/index.ts +7 -0
  160. package/src/plugins/metadata/plugin.ts +26 -0
  161. package/src/plugins/row-selection/helpers.ts +13 -0
  162. package/src/plugins/row-selection/index.ts +7 -0
  163. package/src/plugins/row-selection/plugin.ts +218 -0
  164. package/src/plugins/sticky-columns/helpers.ts +59 -0
  165. package/src/plugins/sticky-columns/index.ts +7 -0
  166. package/src/plugins/sticky-columns/plugin.ts +201 -0
  167. package/src/test-support/index.ts +76 -0
  168. package/src/utils.ts +85 -0
@@ -0,0 +1,836 @@
1
+ import { assert } from '@ember/debug';
2
+
3
+ import {
4
+ COLUMN_META_KEY,
5
+ ROW_META_KEY,
6
+ TABLE_KEY,
7
+ TABLE_META_KEY,
8
+ } from '../../-private/table.ts';
9
+ import { normalizePluginsConfig } from './utils.ts';
10
+
11
+ import type { Table } from '../../-private/table.ts';
12
+ import type { ColumnReordering } from '../column-reordering';
13
+ import type { ColumnVisibility } from '../column-visibility';
14
+ import type { Class, Constructor } from '../../-private/private-types.ts';
15
+ import type { Column, Row } from '../../index.ts';
16
+ import type {
17
+ ColumnMetaFor,
18
+ ColumnOptionsFor,
19
+ OptionsFor,
20
+ Plugin,
21
+ RowMetaFor,
22
+ TableMetaFor,
23
+ } from '../../-private/interfaces';
24
+
25
+ type InstanceOf<T> = T extends Class<infer Instance> ? Instance : T;
26
+
27
+ /**
28
+ * @public
29
+ *
30
+ * list of interfaces by feature name that consumers may provide alternative
31
+ * implementation for
32
+ */
33
+ export interface TableFeatures extends Record<string, unknown | undefined> {
34
+ /**
35
+ * @public
36
+ *
37
+ * interface for the table meta of a "column visibility plugin"
38
+ */
39
+ columnVisibility: InstanceOf<ColumnVisibility['meta']['table']>;
40
+ /**
41
+ * @public
42
+ *
43
+ * interface for the table meta of a "column order plugin"
44
+ */
45
+ columnOrder: InstanceOf<ColumnReordering['meta']['table']>;
46
+ }
47
+
48
+ /**
49
+ * @public
50
+ *
51
+ * list of interfaces by feature name that consumers may provide alternative
52
+ * implementation for
53
+ */
54
+ export interface ColumnFeatures extends Record<string, unknown | undefined> {
55
+ /**
56
+ * @public
57
+ *
58
+ * interface for the column meta of a "column visibility plugin"
59
+ */
60
+ columnVisibility: InstanceOf<ColumnVisibility['meta']['column']>;
61
+ /**
62
+ * @public
63
+ *
64
+ * interface for the column meta of a "column order plugin"
65
+ */
66
+ columnOrder: InstanceOf<ColumnReordering['meta']['column']>;
67
+ }
68
+
69
+ /**
70
+ * @private utility type
71
+ *
72
+ */
73
+ export type SignatureFrom<Klass extends BasePlugin<any>> =
74
+ Klass extends BasePlugin<infer Signature> ? Signature : never;
75
+
76
+ declare const __Signature__: unique symbol;
77
+
78
+ /**
79
+ * @public
80
+ *
81
+ * If your table plugin is a class, you may extend from BasePlugin, which provides
82
+ * small utility methods and properties for getting the metadata for your plugin
83
+ * for the table and each column
84
+ *
85
+ * One instance of a plugin exists per table
86
+ */
87
+ export abstract class BasePlugin<Signature = unknown>
88
+ implements Plugin<Signature>
89
+ {
90
+ constructor(protected table: Table) {}
91
+
92
+ /**
93
+ * @private (secret)
94
+ *
95
+ * Because classes are kind of like interfaces,
96
+ * we need "something" to help TS know what a Resource is.
97
+ *
98
+ * This isn't a real API, but does help with type inference
99
+ * with the SignatureFrom utility above
100
+ */
101
+ declare [__Signature__]: Signature;
102
+
103
+ /**
104
+ * Helper for specifying plugins on `headlessTable` with the plugin-level options
105
+ */
106
+ static with<T extends BasePlugin<any>>(
107
+ this: Constructor<T>,
108
+ configFn: () => OptionsFor<SignatureFrom<T>>,
109
+ ): [Constructor<T>, () => OptionsFor<SignatureFrom<T>>] {
110
+ return [this, configFn];
111
+ }
112
+
113
+ /**
114
+ * Helper for specifying column-level configurations for a plugin on `headlessTable`'s
115
+ * columns option
116
+ */
117
+ static forColumn<T extends BasePlugin<any>>(
118
+ this: Constructor<T>,
119
+ configFn: () => ColumnOptionsFor<SignatureFrom<T>>,
120
+ ): [Constructor<T>, () => ColumnOptionsFor<SignatureFrom<T>>] {
121
+ return [this, configFn];
122
+ }
123
+
124
+ declare meta?: {
125
+ column?: Constructor<ColumnMetaFor<Signature>>;
126
+ table?: Constructor<TableMetaFor<Signature>>;
127
+ row?: Constructor<RowMetaFor<Signature>>;
128
+ };
129
+
130
+ abstract name: string;
131
+ static features?: string[];
132
+ static requires?: string[];
133
+ }
134
+
135
+ /**
136
+ * @public
137
+ *
138
+ * returns boolean if the passed table has an instance of the configured passed plugin class.
139
+ * This can be used to help guard against accessing public-specific APIs if those plugins
140
+ * are not configured for a particular table instance
141
+ */
142
+ export function hasPlugin<P extends BasePlugin<any>, Data = unknown>(
143
+ table: Table<Data>,
144
+ klass: Class<P>,
145
+ ) {
146
+ return Boolean(table.pluginOf(klass));
147
+ }
148
+
149
+ export const preferences = {
150
+ /**
151
+ * @public
152
+ *
153
+ * returns an object for getting and setting preferences data
154
+ * based on the column (scoped to key)
155
+ *
156
+ * Only the provided plugin will have access to these preferences
157
+ * (though, if other plugins can guess how the underlying plugin access
158
+ * works, they can access this data, too. No security guaranteed)
159
+ */
160
+ forColumn<P extends BasePlugin<any>, Data = unknown>(
161
+ column: Column<Data>,
162
+ klass: Class<P>,
163
+ ) {
164
+ return {
165
+ /**
166
+ * delete an entry on the underlying `Map` used for this column-plugin pair
167
+ */
168
+ delete(key: string) {
169
+ const prefs = column.table.preferences;
170
+ const existing = prefs.storage.forPlugin(klass.name);
171
+ const columnPrefs = existing.forColumn(column.key);
172
+
173
+ columnPrefs.delete(key);
174
+
175
+ return prefs.persist();
176
+ },
177
+ /**
178
+ * get an entry on the underlying `Map` used for this column-plugin pair
179
+ */
180
+ get(key: string) {
181
+ const prefs = column.table.preferences;
182
+ const existing = prefs.storage.forPlugin(klass.name);
183
+ const columnPrefs = existing.forColumn(column.key);
184
+
185
+ return columnPrefs.get(key);
186
+ },
187
+ /**
188
+ * set an entry on the underlying `Map` used for this column-plugin pair
189
+ */
190
+ set(key: string, value: unknown) {
191
+ const prefs = column.table.preferences;
192
+ const existing = prefs.storage.forPlugin(klass.name);
193
+ const columnPrefs = existing.forColumn(column.key);
194
+
195
+ columnPrefs.set(key, value);
196
+
197
+ prefs.persist();
198
+ },
199
+ };
200
+ },
201
+
202
+ /**
203
+ * @public
204
+ *
205
+ * returns an object for bulk updating preferences data
206
+ * for all columns (scoped to key and table)
207
+ */
208
+ forAllColumns<P extends BasePlugin<any>, Data = unknown>(
209
+ table: Table<Data>,
210
+ klass: Class<P>,
211
+ ) {
212
+ return {
213
+ /**
214
+ * delete an entry on every column in the underlying column `Map` for this table-plugin pair
215
+ */
216
+ delete(key: string) {
217
+ const tablePrefs = table.preferences;
218
+
219
+ for (const column of table.columns) {
220
+ const prefs = column.table.preferences;
221
+ const existing = prefs.storage.forPlugin(klass.name);
222
+ const columnPrefs = existing.forColumn(column.key);
223
+
224
+ columnPrefs.delete(key);
225
+ }
226
+
227
+ return tablePrefs.persist();
228
+ },
229
+ };
230
+ },
231
+
232
+ /**
233
+ * @public
234
+ *
235
+ * returns an object for getting and setting preferences data
236
+ * based on the table (scoped to the key: "table")
237
+ *
238
+ * Only the provided plugin will have access to these preferences
239
+ * (though, if other plugins can guess how the underlying plugin access
240
+ * works, they can access this data, too. No security guaranteed)
241
+ */
242
+ forTable<P extends BasePlugin<any>, Data = unknown>(
243
+ table: Table<Data>,
244
+ klass: Class<P>,
245
+ ) {
246
+ return {
247
+ /**
248
+ * delete an entry on the underlying `Map` used for this table-plugin pair
249
+ */
250
+ delete(key: string) {
251
+ const prefs = table.preferences;
252
+ const existing = prefs.storage.forPlugin(klass.name);
253
+
254
+ existing.table.delete(key);
255
+
256
+ return prefs.persist();
257
+ },
258
+ /**
259
+ * get an entry on the underlying `Map` used for this table-plugin pair
260
+ */
261
+ get(key: string) {
262
+ const prefs = table.preferences;
263
+ const existing = prefs.storage.forPlugin(klass.name);
264
+
265
+ return existing.table.get(key);
266
+ },
267
+ /**
268
+ * set an entry on the underlying `Map` used for this table-plugin pair
269
+ */
270
+ set(key: string, value: unknown) {
271
+ const prefs = table.preferences;
272
+ const existing = prefs.storage.forPlugin(klass.name);
273
+
274
+ existing.table.set(key, value);
275
+
276
+ return prefs.persist();
277
+ },
278
+ };
279
+ },
280
+ };
281
+
282
+ /**
283
+ * if a `requester` is not provided,
284
+ * Get the columns for the table, considering any and all plugins that could modify columns.
285
+ *
286
+ * If you are an end-consumer of @universal-ember/table, this is the function to use.
287
+ * If you are a plugin-author, you'll want to pass your plugin class as the second parameter.
288
+ *
289
+ * For a given plugin, `requester`, determine what columns should be returned.
290
+ * Since multiple plugins could be used in a table, there is an implicit hierarchy of
291
+ * column modifications that can occur from each of those plugins.
292
+ *
293
+ * If a plugin defines other plugins as either *requirements* or *optional requirements*,
294
+ * and that upstream plugin defines a `columns` property, then those columns will be returned here.
295
+ *
296
+ * This works recursively up the plugin tree up until a plugin has no requirements, and then
297
+ * all columns from the table are returned.
298
+ */
299
+ function columnsFor<DataType = any>(
300
+ table: Table<DataType>,
301
+ requester?: Plugin<any>,
302
+ ): Column<DataType>[] {
303
+ assert(
304
+ `First argument passed to columns.for must be an instance of Table`,
305
+ table[TABLE_KEY],
306
+ );
307
+
308
+ const visibility = findPlugin(table.plugins, 'columnVisibility');
309
+ const reordering = findPlugin(table.plugins, 'columnOrder');
310
+ const sizing = findPlugin(table.plugins, 'columnResizing');
311
+
312
+ // TODO: actually resolve the graph, rather than use the hardcoded feature names
313
+ // atm, this only "happens" to work based on expectations of
314
+ // of the currently implemented plugins' capabilities and implied hierarchy.
315
+
316
+ if (requester) {
317
+ assert(
318
+ `[${requester.name}] requested columns from the table, but the plugin, ${requester.name}, ` +
319
+ `is not used in this table`,
320
+ table.plugins.some(
321
+ (plugin) => plugin instanceof (requester as Class<Plugin>),
322
+ ),
323
+ );
324
+
325
+ if (sizing && sizing.constructor === requester) {
326
+ return table.columns.values();
327
+ }
328
+
329
+ if (visibility && visibility.constructor === requester) {
330
+ return table.columns.values();
331
+ }
332
+
333
+ if (reordering && reordering.constructor === requester) {
334
+ if (visibility) {
335
+ assert(
336
+ `<#${visibility.name}> defined a 'columns' property, but did not return valid data.`,
337
+ visibility.columns && Array.isArray(visibility.columns),
338
+ );
339
+
340
+ return visibility.columns;
341
+ }
342
+
343
+ return table.columns.values();
344
+ }
345
+
346
+ if (reordering) {
347
+ assert(
348
+ `<#${reordering.name}> defined a 'columns' property, but did not return valid data.`,
349
+ reordering.columns && Array.isArray(reordering.columns),
350
+ );
351
+
352
+ return reordering.columns;
353
+ }
354
+
355
+ if (visibility) {
356
+ assert(
357
+ `<#${visibility.name}> defined a 'columns' property, but did not return valid data.`,
358
+ visibility.columns && Array.isArray(visibility.columns),
359
+ );
360
+
361
+ return visibility.columns;
362
+ }
363
+
364
+ if (sizing) {
365
+ assert(
366
+ `<#${sizing.name}> defined a 'columns' property, but did not return valid data.`,
367
+ sizing.columns && Array.isArray(sizing.columns),
368
+ );
369
+
370
+ return sizing.columns;
371
+ }
372
+
373
+ return table.columns.values();
374
+ }
375
+
376
+ /**
377
+ * This flow is the inverse of when we have a requester
378
+ */
379
+
380
+ if (reordering) {
381
+ assert(
382
+ `<#${reordering.name}> defined a 'columns' property, but did not return valid data.`,
383
+ reordering.columns && Array.isArray(reordering.columns),
384
+ );
385
+
386
+ return reordering.columns;
387
+ }
388
+
389
+ if (visibility) {
390
+ assert(
391
+ `<#${visibility.name}> defined a 'columns' property, but did not return valid data.`,
392
+ visibility.columns && Array.isArray(visibility.columns),
393
+ );
394
+
395
+ return visibility.columns;
396
+ }
397
+
398
+ if (sizing) {
399
+ assert(
400
+ `<#${sizing.name}> defined a 'columns' property, but did not return valid data.`,
401
+ sizing.columns && Array.isArray(sizing.columns),
402
+ );
403
+
404
+ return sizing.columns;
405
+ }
406
+
407
+ return table.columns.values();
408
+ }
409
+
410
+ export const columns = {
411
+ for: columnsFor,
412
+
413
+ /**
414
+ * for a given current or reference column, return the column that
415
+ * is immediately next, or to the right of that column.
416
+ *
417
+ * If a plugin class is provided, the hierarchy of column list modifications
418
+ * will be respected.
419
+ */
420
+ next: <Data = unknown>(
421
+ current: Column<Data>,
422
+ requester?: Plugin<any>,
423
+ ): Column<Data> | undefined => {
424
+ const columns = requester
425
+ ? columnsFor(current.table, requester)
426
+ : columnsFor(current.table);
427
+
428
+ const referenceIndex = columns.indexOf(current);
429
+
430
+ assert(
431
+ `index of reference column must be >= 0. column likely not a part of the table`,
432
+ referenceIndex >= 0,
433
+ );
434
+
435
+ /**
436
+ * There can be nothing after the last column
437
+ */
438
+ if (referenceIndex >= columns.length - 1) {
439
+ return undefined;
440
+ }
441
+
442
+ return columns[referenceIndex + 1];
443
+ },
444
+
445
+ /**
446
+ * for a given current or reference column, return the column that
447
+ * is immediately previous, or to the left of that column.
448
+ *
449
+ * If a plugin class is provided, the hierarchy of column list modifications
450
+ * will be respected.
451
+ */
452
+ previous: <Data = unknown>(
453
+ current: Column<Data>,
454
+ requester?: Plugin<any>,
455
+ ): Column<Data> | undefined => {
456
+ const columns = requester
457
+ ? columnsFor(current.table, requester)
458
+ : columnsFor(current.table);
459
+ const referenceIndex = columns.indexOf(current);
460
+
461
+ assert(
462
+ `index of reference column must be >= 0. column likely not a part of the table`,
463
+ referenceIndex >= 0,
464
+ );
465
+
466
+ /**
467
+ * There can be nothing before the first column
468
+ */
469
+ if (referenceIndex === 0) {
470
+ return undefined;
471
+ }
472
+
473
+ return columns[referenceIndex - 1];
474
+ },
475
+ /**
476
+ * for a given current or reference column, return the columns that
477
+ * should appear before, or to the left of that column.
478
+ *
479
+ * if a plugin class is provided, the hierarchy of column list modifications
480
+ * will be respected.
481
+ */
482
+ before: <Data = unknown>(
483
+ current: Column<Data>,
484
+ requester?: Plugin<any>,
485
+ ): Column<Data>[] => {
486
+ const columns = requester
487
+ ? columnsFor(current.table, requester)
488
+ : columnsFor(current.table);
489
+
490
+ const referenceIndex = columns.indexOf(current);
491
+
492
+ return columns.slice(0, referenceIndex);
493
+ },
494
+ /**
495
+ * for a given current or reference column, return the columns that
496
+ * should appear after, or to the right of that column.
497
+ *
498
+ * if a plugin class is provided, the hierarchy of column list modifications
499
+ * will be respected.
500
+ */
501
+ after: <Data = unknown>(
502
+ current: Column<Data>,
503
+ requester?: Plugin<any>,
504
+ ): Column<Data>[] => {
505
+ const columns = requester
506
+ ? columnsFor(current.table, requester)
507
+ : columnsFor(current.table);
508
+
509
+ const referenceIndex = columns.indexOf(current);
510
+
511
+ return columns.slice(referenceIndex + 1);
512
+ },
513
+ };
514
+
515
+ export const meta = {
516
+ /**
517
+ * @public
518
+ *
519
+ * For a given column and plugin, return the meta / state bucket for the
520
+ * plugin<->column instance pair.
521
+ *
522
+ * Note that this requires the column instance to exist on the table.
523
+ */
524
+ forColumn<P extends BasePlugin<any>, Data = unknown>(
525
+ column: Column<Data>,
526
+ klass: Class<P>,
527
+ ): ColumnMetaFor<SignatureFrom<P>> {
528
+ const columnMeta = column.table[COLUMN_META_KEY];
529
+
530
+ return getPluginInstance(columnMeta, column, klass, () => {
531
+ const plugin = column.table.pluginOf(klass);
532
+
533
+ assert(
534
+ `[${klass.name}] cannot get plugin instance of unregistered plugin class`,
535
+ plugin,
536
+ );
537
+ assert(
538
+ `<#${plugin.name}> plugin does not have meta specified`,
539
+ plugin.meta,
540
+ );
541
+ assert(
542
+ `<#${plugin.name}> plugin does not specify column meta`,
543
+ plugin.meta.column,
544
+ );
545
+
546
+ return new plugin.meta.column(column);
547
+ });
548
+ },
549
+
550
+ /**
551
+ * @public
552
+ *
553
+ * For a given row and plugin, return the meta / state bucket for the
554
+ * plugin<->row instance pair.
555
+ *
556
+ * Note that this requires the row instance to exist on the table.
557
+ */
558
+ forRow<P extends BasePlugin<any>, Data = unknown>(
559
+ row: Row<Data>,
560
+ klass: Class<P>,
561
+ ): RowMetaFor<SignatureFrom<P>> {
562
+ const rowMeta = row.table[ROW_META_KEY];
563
+
564
+ return getPluginInstance(rowMeta, row, klass, () => {
565
+ const plugin = row.table.pluginOf(klass);
566
+
567
+ assert(
568
+ `[${klass.name}] cannot get plugin instance of unregistered plugin class`,
569
+ plugin,
570
+ );
571
+ assert(
572
+ `<#${plugin.name}> plugin does not have meta specified`,
573
+ plugin.meta,
574
+ );
575
+ assert(
576
+ `<#${plugin.name}> plugin does not specify row meta`,
577
+ plugin.meta.row,
578
+ );
579
+
580
+ return new plugin.meta.row(row);
581
+ });
582
+ },
583
+
584
+ /**
585
+ * @public
586
+ *
587
+ * For a given table and plugin, return the meta / state bucket for the
588
+ * plugin<->table instance pair.
589
+ */
590
+ forTable<P extends BasePlugin<any>, Data = unknown>(
591
+ table: Table<Data>,
592
+ klass: Class<P>,
593
+ ): TableMetaFor<SignatureFrom<P>> {
594
+ const tableMeta = table[TABLE_META_KEY];
595
+
596
+ return getPluginInstance(tableMeta, klass, () => {
597
+ const plugin = table.pluginOf(klass);
598
+
599
+ assert(
600
+ `[${klass.name}] cannot get plugin instance of unregistered plugin class`,
601
+ plugin,
602
+ );
603
+ assert(
604
+ `<#${plugin.name}> plugin does not have meta specified`,
605
+ plugin.meta,
606
+ );
607
+ assert(
608
+ `<#${plugin.name}> plugin does not specify table meta`,
609
+ plugin.meta.table,
610
+ );
611
+ assert(
612
+ `<#${plugin.name}> plugin already exists for the table. ` +
613
+ `A plugin may only be instantiated once per table.`,
614
+ ![...tableMeta.keys()].includes(klass),
615
+ );
616
+
617
+ return new plugin.meta.table(table);
618
+ });
619
+ },
620
+
621
+ /**
622
+ * Instead of finding meta based on column or table instances,
623
+ * you can search for meta based on feature strings, such as `columnWidth`
624
+ */
625
+ withFeature: {
626
+ /**
627
+ * @public
628
+ *
629
+ * for a given column and feature name, return the "ColumnMeta" for that feature.
630
+ * This is useful when plugins may depend on one another but may not necessarily care which
631
+ * plugin is providing what behavior.
632
+ *
633
+ * For example, multiple column-focused plugins may care about width or visibility
634
+ */
635
+ forColumn<FeatureName extends string, Data = unknown>(
636
+ column: Column<Data>,
637
+ featureName: FeatureName,
638
+ ): ColumnFeatures[FeatureName] {
639
+ const { plugins } = column.table;
640
+
641
+ const provider = findPlugin(plugins, featureName);
642
+
643
+ assert(
644
+ `Could not find plugin with feature: ${featureName}. ` +
645
+ `Available features: ${availableFeatures(plugins)}`,
646
+ provider,
647
+ );
648
+
649
+ // TS doesn't believe in the constructor property?
650
+ return meta.forColumn(column, (provider as any).constructor);
651
+ },
652
+
653
+ /**
654
+ * @public
655
+ *
656
+ * for a given table and feature name, return the "TableMeta" for that feature.
657
+ * This is useful when plugins may depend on one another but may not necessarily care
658
+ * which plugin is providing that behavior.
659
+ *
660
+ * For example, multiple column-focused plugins may care about width or visibility.
661
+ */
662
+ forTable<FeatureName extends string, Data = unknown>(
663
+ table: Table<Data>,
664
+ featureName: FeatureName,
665
+ ): TableFeatures[FeatureName] {
666
+ const { plugins } = table;
667
+
668
+ const provider = findPlugin(plugins, featureName);
669
+
670
+ assert(
671
+ `Could not find plugin with feature: ${featureName}. ` +
672
+ `Available features: ${availableFeatures(plugins)}`,
673
+ provider,
674
+ );
675
+
676
+ // TS doesn't believe in the constructor property?
677
+ return meta.forTable(table, (provider as any).constructor);
678
+ },
679
+ },
680
+ };
681
+
682
+ function findPlugin(plugins: Plugin[], featureName: string) {
683
+ const provider = plugins.find((plugin) => {
684
+ /*
685
+ * have to cast in order to get static properties, but we may not have a base plugin
686
+ * so we must rely on nullish coalesting to protect from throwing exceptions
687
+ *
688
+ * (Plugin || BasePlugin).features)
689
+ */
690
+ const features =
691
+ plugin.features || (plugin.constructor as typeof BasePlugin).features;
692
+
693
+ return features?.includes(featureName);
694
+ });
695
+
696
+ return provider;
697
+ }
698
+
699
+ function availableFeatures(plugins: Plugin[]): string {
700
+ const allFeatures = plugins
701
+ .map((plugin) => {
702
+ /*
703
+ * have to cast in order to get static properties, but we may not have a base plugin
704
+ * so we must rely on nullish coalesting to protect from throwing exceptions
705
+ *
706
+ * (Plugin || BasePlugin).features)
707
+ */
708
+ const features =
709
+ plugin.features || (plugin.constructor as typeof BasePlugin).features;
710
+
711
+ return features;
712
+ })
713
+ .flat()
714
+ .filter(Boolean);
715
+
716
+ return allFeatures.length > 0 ? allFeatures.join(', ') : '[none]';
717
+ }
718
+
719
+ export const options = {
720
+ /**
721
+ * @public
722
+ *
723
+ * For a given table and plugin, return the options, if any were given from the user
724
+ * during construction of the table.
725
+ */
726
+ forTable<P extends BasePlugin<any>, Data = unknown>(
727
+ table: Table<Data>,
728
+ klass: Class<P>,
729
+ ): Partial<OptionsFor<SignatureFrom<P>>> {
730
+ const normalized = normalizePluginsConfig(table?.config?.plugins);
731
+ const tuple = normalized?.find((option) => option[0] === klass);
732
+ const t = tuple as [Class<P>, () => OptionsFor<SignatureFrom<P>>];
733
+
734
+ // Plugin not provided, likely
735
+ if (!t) return {};
736
+
737
+ const fn = t[1];
738
+
739
+ return fn() ?? {};
740
+ },
741
+
742
+ forColumn<P extends BasePlugin<any>, Data = unknown>(
743
+ column: Column<Data>,
744
+ klass: Class<P>,
745
+ ): Partial<ColumnOptionsFor<SignatureFrom<P>>> {
746
+ const tuple = column.config.pluginOptions?.find(
747
+ (option) => option[0] === klass,
748
+ );
749
+ const t = tuple as [unknown, () => ColumnOptionsFor<SignatureFrom<P>>];
750
+
751
+ const fn = t?.[1];
752
+
753
+ if (!fn) return {};
754
+
755
+ return fn() ?? {};
756
+ },
757
+ };
758
+
759
+ type FactoryMap<Instance> = Map<Class<Instance>, Instance>;
760
+
761
+ /**
762
+ * @private
763
+ */
764
+ function getPluginInstance<Instance>(
765
+ map: Map<Class<Instance>, Instance>,
766
+ mapKey: Class<Instance>,
767
+ factory: () => Instance,
768
+ ): Instance;
769
+ function getPluginInstance<RootKey extends Column<any> | Row<any>, Instance>(
770
+ map: WeakMap<Column | Row, Map<Class<Instance>, Instance>>,
771
+ rootKey: RootKey,
772
+ mapKey: Class<Instance>,
773
+ factory: () => Instance,
774
+ ): Instance;
775
+ function getPluginInstance<RootKey extends Column<any> | Row<any>, Instance>(
776
+ ...args:
777
+ | [FactoryMap<Instance>, Class<Instance>, () => Instance]
778
+ | [
779
+ WeakMap<Column | Row, FactoryMap<Instance>>,
780
+ RootKey,
781
+ Class<Instance>,
782
+ () => Instance,
783
+ ]
784
+ ): Instance {
785
+ let map: WeakMap<Column | Row, FactoryMap<Instance>> | FactoryMap<Instance>;
786
+ let mapKey: Class<Instance>;
787
+ let rootKey: RootKey | undefined;
788
+ let factory: () => Instance;
789
+
790
+ if (args.length === 3) {
791
+ map = args[0];
792
+ mapKey = args[1];
793
+ factory = args[2];
794
+ } else if (args.length === 4) {
795
+ map = args[0];
796
+ rootKey = args[1];
797
+ mapKey = args[2];
798
+ factory = args[3];
799
+ } else {
800
+ throw new Error(
801
+ // TS says args is of type "never", but TS can't protect against general misuse
802
+ // (esp without TS)
803
+ `Incorrect arity passed to getPluginInstance. Expected 3 or 4, received ${
804
+ (args as any).length
805
+ }`,
806
+ );
807
+ }
808
+
809
+ let bucket: FactoryMap<Instance> | undefined;
810
+
811
+ if (map instanceof WeakMap) {
812
+ assert(`rootKey is missing`, rootKey);
813
+
814
+ bucket = map.get(rootKey);
815
+
816
+ if (!bucket) {
817
+ bucket = new Map();
818
+
819
+ map.set(rootKey, bucket);
820
+ }
821
+ } else {
822
+ bucket = map;
823
+ }
824
+
825
+ let instance = bucket.get(mapKey);
826
+
827
+ if (instance) {
828
+ return instance;
829
+ }
830
+
831
+ instance = factory();
832
+
833
+ bucket.set(mapKey, instance);
834
+
835
+ return instance;
836
+ }