forked from rancher/dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResourceTable.vue
More file actions
630 lines (523 loc) · 16.4 KB
/
ResourceTable.vue
File metadata and controls
630 lines (523 loc) · 16.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
<script>
import { mapGetters } from 'vuex';
import { get } from '@shell/utils/object';
import { mapPref, GROUP_RESOURCES } from '@shell/store/prefs';
import ButtonGroup from '@shell/components/ButtonGroup';
import SortableTable from '@shell/components/SortableTable';
import { NAMESPACE, AGE } from '@shell/config/table-headers';
import { findBy } from '@shell/utils/array';
import { ExtensionPoint, TableColumnLocation } from '@shell/core/types';
import { getApplicableExtensionEnhancements } from '@shell/core/plugin-helpers';
// Default group-by in the case the group stored in the preference does not apply
const DEFAULT_GROUP = 'namespace';
export const defaultTableSortGenerationFn = (schema, $store) => {
if ( !schema ) {
return null;
}
const resource = schema.id;
let sortKey = resource;
const inStore = $store.getters['currentStore'](resource);
const generation = $store.getters[`${ inStore }/currentGeneration`]?.(resource);
if ( generation ) {
sortKey += `/${ generation }`;
}
const nsFilterKey = $store.getters['activeNamespaceCacheKey'];
if ( nsFilterKey ) {
return `${ sortKey }/${ nsFilterKey }`;
}
// covers case where we have no current cluster's ns cache
return sortKey;
};
export default {
name: 'ResourceTable',
components: { ButtonGroup, SortableTable },
props: {
schema: {
type: Object,
default: null,
},
rows: {
type: Array,
required: true
},
loading: {
type: Boolean,
required: false
},
altLoading: {
type: Boolean,
required: false
},
keyField: {
// Field that is unique for each row.
type: String,
default: '_key',
},
headers: {
type: Array,
default: null,
},
groupBy: {
type: String,
default: null
},
namespaced: {
type: Boolean,
default: null, // Automatic from schema
},
search: {
// Show search input to filter rows
type: Boolean,
default: true
},
tableActions: {
// Show bulk table actions
type: [Boolean, null],
default: null
},
pagingLabel: {
type: String,
default: 'sortableTable.paging.resource',
},
/**
* Additional params to pass to the pagingLabel translation
*/
pagingParams: {
type: Object,
default: null,
},
rowActions: {
type: Boolean,
default: true,
},
groupable: {
type: Boolean,
default: null, // Null: auto based on namespaced and type custom groupings
},
groupTooltip: {
type: String,
default: 'resourceTable.groupBy.namespace',
},
overflowX: {
type: Boolean,
default: false
},
overflowY: {
type: Boolean,
default: false
},
sortGenerationFn: {
type: Function,
default: null,
},
getCustomDetailLink: {
type: Function,
default: null
},
ignoreFilter: {
type: Boolean,
default: false
},
hasAdvancedFiltering: {
type: Boolean,
default: false
},
advFilterHideLabelsAsCols: {
type: Boolean,
default: false
},
advFilterPreventFilteringLabels: {
type: Boolean,
default: false
},
/**
* Allows for the usage of a query param to work for simple filtering (q)
*/
useQueryParamsForSimpleFiltering: {
type: Boolean,
default: false
},
/**
* Manaul force the update of live and delayed cells. Change this number to kick off the update
*/
forceUpdateLiveAndDelayed: {
type: Number,
default: 0
},
externalPaginationEnabled: {
type: Boolean,
default: false
},
externalPaginationResult: {
type: Object,
default: null
}
},
mounted() {
/**
* v-shortkey prevents the event's propagation:
* https://github.com/fgr-araujo/vue-shortkey/blob/55d802ea305cadcc2ea970b55a3b8b86c7b44c05/src/index.js#L156-L157
*
* 'Enter' key press is handled via event listener in order to allow the event propagation
*/
window.addEventListener('keyup', this.handleEnterKeyPress);
},
beforeDestroy() {
window.removeEventListener('keyup', this.handleEnterKeyPress);
},
data() {
// Confirm which store we're in, if schema isn't available we're probably showing a list with different types
const inStore = this.schema?.id ? this.$store.getters['currentStore'](this.schema.id) : undefined;
return { inStore };
},
computed: {
options() {
return this.$store.getters[`type-map/optionsFor`](this.schema, this.externalPaginationEnabled);
},
_listGroupMapped() {
return this.options?.listGroups?.reduce((acc, grp) => {
acc[grp.value] = grp;
return acc;
}, {});
},
_mandatorySort() {
return this.options?.listMandatorySort;
},
...mapGetters(['currentProduct']),
isNamespaced() {
if ( this.namespaced !== null ) {
return this.namespaced;
}
return !!get( this.schema, 'attributes.namespaced');
},
showNamespaceColumn() {
const groupNamespaces = this.group === 'namespace';
const out = !this.showGrouping || !groupNamespaces;
return out;
},
_showBulkActions() {
if (this.tableActions !== null) {
return this.tableActions;
} else if (this.schema) {
const hideTableActions = this.$store.getters['type-map/hideBulkActionsFor'](this.schema);
return !hideTableActions;
}
return false;
},
_headers() {
let headers;
const showNamespace = this.showNamespaceColumn;
if ( this.headers ) {
headers = this.headers.slice();
} else {
headers = this.$store.getters['type-map/headersFor'](this.schema, this.externalPaginationEnabled);
}
// add custom table columns provided by the extensions ExtensionPoint.TABLE_COL hook
// gate it so that we prevent errors on older versions of dashboard
if (this.$store.$plugin?.getUIConfig) {
const extensionCols = getApplicableExtensionEnhancements(this, ExtensionPoint.TABLE_COL, TableColumnLocation.RESOURCE, this.$route);
// Try and insert the columns before the Age column
let insertPosition = headers.length;
if (headers.length > 0) {
const ageColIndex = headers.findIndex((h) => h.name === AGE.name);
if (ageColIndex >= 0) {
insertPosition = ageColIndex;
} else {
// we've found some labels with ' ', which isn't necessarily empty (explore action/button)
// if we are to add cols, let's push them before these so that the UI doesn't look weird
const lastViableColIndex = headers.findIndex((h) => (!h.label || !h.label?.trim()) && (!h.labelKey || !h.labelKey?.trim()));
if (lastViableColIndex >= 0) {
insertPosition = lastViableColIndex;
}
}
}
// adding extension defined cols to the correct header config
extensionCols.forEach((col) => {
// we need the 'value' prop to be populated in order for the rows to show the values
if (!col.value && col.getValue) {
col.value = col.getValue;
}
headers.splice(insertPosition, 0, col);
});
}
// If only one namespace is selected, hide the namespace column
if ( !showNamespace ) {
const idx = headers.findIndex((header) => header.name === NAMESPACE.name);
if ( idx >= 0 ) {
headers.splice(idx, 1);
}
}
// If we are grouping by a custom group, it may specify that we hide a specific column
const custom = this._listGroupMapped?.[this.group];
if (custom?.hideColumn) {
const idx = headers.findIndex((header) => header.name === custom.hideColumn);
if ( idx >= 0 ) {
headers.splice(idx, 1);
}
}
return headers;
},
/**
* Take rows and filter out entries given the namespace filter
*/
filteredRows() {
const isAll = this.$store.getters['isAllNamespaces'];
// Do we need to filter by namespace like things?
if (
!this.isNamespaced || // Resource type isn't namespaced
this.ignoreFilter || // Component owner strictly states no filtering
this.externalPaginationEnabled ||
(isAll && !this.currentProduct?.hideSystemResources) || // Need all
(this.inStore ? this.$store.getters[`${ this.inStore }/haveNamespace`](this.schema.id)?.length : false)// Store reports type has namespace filter, so rows already contain the correctly filtered resources
) {
return this.rows || [];
}
const includedNamespaces = this.$store.getters['namespaces']();
// Shouldn't happen, but does for resources like management.cattle.io.preference
if (!this.rows) {
return [];
}
const haveAllNamespace = this.$store.getters['haveAllNamespace'];
return this.rows.filter((row) => {
if (this.currentProduct?.hideSystemResources && this.isNamespaced) {
return !!includedNamespaces[row.metadata.namespace] && !row.isSystemResource;
} else if (!this.isNamespaced) {
return true;
} else if (haveAllNamespace) {
// `rows` only contains resource from a single namespace
return true;
} else {
return !!includedNamespaces[row.metadata.namespace];
}
});
},
_group: mapPref(GROUP_RESOURCES),
// The group stored in the preference (above) might not be valid for this resource table - so ensure we
// choose a group that is applicable (the default)
// This saves us from having to store a group preference per resource type - given that custom groupings aer not used much
// and it feels like a good UX to be able to keep the namespace/flat grouping across tables
group: {
get() {
// Check group is valid
const exists = this.groupOptions.find((g) => g.value === this._group);
if (!exists) {
// Attempt to find the default option in available options...
// if not use the first value in the options collection...
// and if not that just fall back to the default
if (this.groupOptions.find((g) => g.value === DEFAULT_GROUP)) {
return DEFAULT_GROUP;
}
return this.groupOptions[0]?.value || DEFAULT_GROUP;
}
return this._group;
},
set(value) {
this._group = value;
}
},
showGrouping() {
if ( this.groupable === null ) {
const namespaceGroupable = this.$store.getters['isMultipleNamespaces'] && this.isNamespaced;
const customGroupable = !!this.options?.listGroups?.length;
return namespaceGroupable || customGroupable;
}
return this.groupable || false;
},
computedGroupBy() {
if ( this.groupBy ) {
// This probably comes from the type-map config for the resource (see ResourceList)
return this.groupBy;
}
if ( this.group === 'namespace' && this.showGrouping ) {
// This switches to group rows by a key which is the label for the group (??)
return 'groupByLabel';
}
const custom = this._listGroupMapped?.[this.group];
if (custom?.field) {
// Override the normal filtering
return custom.field;
}
return null;
},
groupOptions() {
// Ignore the defaults below, we have an override set of groups
// REPLACE (instead of SUPPLEMENT) defaults with listGroups (given listGroupsWillOverride is true)
if (this.options?.listGroupsWillOverride && !!this.options?.listGroups?.length) {
return this.options?.listGroups;
}
const standard = [
{
tooltipKey: 'resourceTable.groupBy.none',
icon: 'icon-list-flat',
value: 'none',
},
{
tooltipKey: this.groupTooltip,
icon: 'icon-folder',
value: 'namespace',
},
];
// SUPPLEMENT (instead of REPLACE) defaults with listGroups (given listGroupsWillOverride is false)
if (!!this.options?.listGroups?.length) {
return standard.concat(this.options.listGroups);
}
return standard;
},
parsedPagingParams() {
if (this.pagingParams) {
return this.pagingParams;
}
if ( !this.schema ) {
return {
singularLabel: '',
pluralLabel: ''
};
}
return {
singularLabel: this.$store.getters['type-map/labelFor'](this.schema),
pluralLabel: this.$store.getters['type-map/labelFor'](this.schema, 99),
};
},
},
methods: {
keyAction(action) {
const table = this.$refs.table;
if ( !table ) {
return;
}
const selection = table.selectedRows;
if ( action === 'remove' ) {
const act = findBy(table.availableActions, 'action', 'promptRemove');
if ( act ) {
table.setBulkActionOfInterest(act);
table.applyTableAction(act);
}
return;
}
if ( selection.length !== 1 ) {
return;
}
switch ( action ) {
case 'detail':
selection[0].goToDetail();
break;
case 'edit':
selection[0].goToEdit();
break;
case 'yaml':
selection[0].goToViewYaml();
break;
}
},
clearSelection() {
this.$refs.table.clearSelection();
},
safeSortGenerationFn() {
if (this.sortGenerationFn) {
return this.sortGenerationFn(this.schema, this.$store);
}
return defaultTableSortGenerationFn(this.schema, this.$store);
},
handleActionButtonClick(event) {
this.$emit('clickedActionButton', event);
},
handleEnterKeyPress(event) {
if (event.key === 'Enter') {
this.keyAction('detail');
}
}
},
};
</script>
<template>
<SortableTable
ref="table"
v-bind="$attrs"
:headers="_headers"
:rows="filteredRows"
:loading="loading"
:alt-loading="altLoading"
:group-by="computedGroupBy"
:group="group"
:group-options="groupOptions"
:search="search"
:paging="true"
:paging-params="parsedPagingParams"
:paging-label="pagingLabel"
:row-actions="rowActions"
:table-actions="_showBulkActions"
:overflow-x="overflowX"
:overflow-y="overflowY"
:get-custom-detail-link="getCustomDetailLink"
:has-advanced-filtering="hasAdvancedFiltering"
:adv-filter-hide-labels-as-cols="advFilterHideLabelsAsCols"
:adv-filter-prevent-filtering-labels="advFilterPreventFilteringLabels"
:key-field="keyField"
:sort-generation-fn="safeSortGenerationFn"
:use-query-params-for-simple-filtering="useQueryParamsForSimpleFiltering"
:force-update-live-and-delayed="forceUpdateLiveAndDelayed"
:external-pagination-enabled="externalPaginationEnabled"
:external-pagination-result="externalPaginationResult"
:mandatory-sort="_mandatorySort"
@clickedActionButton="handleActionButtonClick"
@group-value-change="group = $event"
v-on="$listeners"
>
<template
v-if="showGrouping"
#header-middle
>
<slot name="more-header-middle" />
<ButtonGroup
v-model="group"
:options="groupOptions"
/>
</template>
<template
v-if="showGrouping"
#header-right
>
<slot name="header-right" />
</template>
<template #group-by="{group: thisGroup}">
<div
v-clean-html="thisGroup.ref"
class="group-tab"
/>
</template>
<!-- Pass down templates provided by the caller -->
<template
v-for="(_, slot) of $scopedSlots"
v-slot:[slot]="scope"
>
<slot
:name="slot"
v-bind="scope"
/>
</template>
<template #shortkeys>
<button
v-shortkey.once="['e']"
class="hide"
@shortkey="keyAction('edit')"
/>
<button
v-shortkey.once="['y']"
class="hide"
@shortkey="keyAction('yaml')"
/>
<button
v-if="_showBulkActions"
v-shortkey.once="['del']"
class="hide"
@shortkey="keyAction('remove')"
/>
<button
v-if="_showBulkActions"
v-shortkey.once="['backspace']"
class="hide"
@shortkey="keyAction('remove')"
/>
</template>
</SortableTable>
</template>