-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathEntityList.ts
More file actions
166 lines (141 loc) · 5.5 KB
/
Copy pathEntityList.ts
File metadata and controls
166 lines (141 loc) · 5.5 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
import {
CommandData,
ConfigCommandsData,
EntityListData,
EntityListFieldData,
EntityStateValueData,
FilterData,
} from "@/types";
import { EntityListInstance, InstanceId } from "./types";
export class EntityList implements EntityListData {
authorizations: EntityListData['authorizations'];
config: EntityListData['config'];
data: EntityListData['data'];
fields: EntityListData['fields'];
subEntities: EntityListData['subEntities'];
meta: EntityListData['meta'];
pageAlert: EntityListData['pageAlert'];
query: EntityListData['query'];
filterValues: EntityListData['filterValues'];
title: EntityListData['title'];
entityKey: string;
hiddenFilters?: Record<string, FilterData['value']>;
hiddenCommands: { entity: string[], instance: string[] };
constructor(
data: EntityListData,
entityKey: string,
hiddenFilters?: Record<string, FilterData['value']>,
hiddenCommands?: { entity: string[], instance: string[] }
) {
Object.assign(this, data);
this.entityKey = entityKey;
this.hiddenFilters = hiddenFilters;
this.hiddenCommands = hiddenCommands;
}
get count() {
return this.meta?.total ?? this.data.length;
}
get currentSort() {
return this.query?.sort ?? this.config.defaultSort;
}
get currentSortDir() {
return this.query?.dir ?? this.config.defaultSortDir;
}
get visibleFilters(): Array<FilterData>|null {
return this.hiddenFilters
? this.config.filters?._root.filter(filter => !(filter.key in this.hiddenFilters))
: this.config.filters?._root;
}
get visibleCommands(): ConfigCommandsData {
return {
instance: this.config.commands?.instance?.map(group => group.filter(command => {
return !this.hiddenCommands?.instance?.includes(command.key);
})),
entity: this.config.commands?.entity?.map(group => group.filter(command => {
return !this.hiddenCommands?.entity?.includes(command.key);
})),
}
}
get allowedEntityCommands() {
return this.visibleCommands.entity
?.map(group => group.filter(command => command.authorization))
?? [];
}
get primaryCommand(): CommandData {
return this.allowedEntityCommands.flat().find(command => command.primary);
}
get canSelect() {
return this.allowedEntityCommands.flat().some(command => command.instanceSelection);
}
get canReorder() {
return this.config.reorderable
&& this.authorizations.reorder
&& this.data.length > 1;
}
withRefreshedItems(refreshedItems: EntityListInstance[]): EntityList {
this.data = this.data.map(item => {
return refreshedItems.find(refreshedItem => this.instanceId(refreshedItem) === this.instanceId(item))
?? item;
});
return new EntityList(this, this.entityKey, this.hiddenFilters, this.hiddenCommands);
}
dropdownEntityCommands(selecting: boolean) {
return this.allowedEntityCommands.map(commandGroup =>
commandGroup.filter(command => {
if(selecting) {
return !!command.instanceSelection;
}
return !command.primary;
})
);
}
instanceId(instance: EntityListInstance): InstanceId {
return instance[this.config.instanceIdAttribute];
}
instanceState(instance: EntityListInstance): string | number | null {
return this.config.state
? instance[this.config.state.attribute]
: null;
}
instanceStateValue(instance: EntityListInstance): EntityStateValueData | undefined {
return this.config.state?.values.find(item => item.value === this.instanceState(instance));
}
instanceCanUpdateState(instance: EntityListInstance): boolean {
if(Array.isArray(this.config.state.authorization)) {
return this.config.state.authorization.includes(this.instanceId(instance));
}
return !!this.config.state.authorization;
}
instanceCanDelete(instance: EntityListInstance): boolean {
return instance._meta.authorizations.delete;
}
instanceCommands(instance: EntityListInstance): Array<Array<CommandData>> | undefined {
return this.visibleCommands?.instance
?.map(group => group.filter(command => {
if(Array.isArray(command.authorization)) {
return command.authorization.includes(this.instanceId(instance));
}
return command.authorization;
}));
}
instanceHasActions(instance: EntityListInstance, showEntityState: boolean): boolean {
return (
this.instanceCommands(instance)?.flat().length > 0 ||
this.config.state && showEntityState && this.instanceCanUpdateState(instance) ||
!this.config.deleteHidden && this.instanceCanDelete(instance)
);
}
fieldShouldBeVisible(field: EntityListFieldData, showEntityState: boolean): boolean {
if(field.type === 'badge') {
return this.data.some(item =>
item[field.key] === true
|| typeof item[field.key] === 'number'
|| typeof item[field.key] === 'string' && item[field.key].length > 0
);
}
if(field.type === 'state') {
return this.config.state && showEntityState;
}
return true;
}
}