-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathActionList.ts
252 lines (226 loc) · 7.44 KB
/
ActionList.ts
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
import { css, html, TemplateResult } from 'lit';
import { classMap } from 'lit/directives/class-map.js';
import { property } from 'lit/decorators.js';
import { MdMenu, Menu } from '@scopedelement/material-web/menu/MdMenu.js';
import { Icon } from '@scopedelement/material-web/icon/internal/icon.js';
import { MdDivider } from '@scopedelement/material-web/divider/MdDevider.js';
import { MdIcon } from '@scopedelement/material-web/icon/MdIcon.js';
import { MdList } from '@scopedelement/material-web/list/MdList.js';
import { MdListItem } from '@scopedelement/material-web/list/MdListItem.js';
import { MdMenuItem } from '@scopedelement/material-web/menu/MdMenuItem.js';
import { MdOutlinedTextField } from '@scopedelement/material-web/textfield/MdOutlinedTextField.js';
import { FilterListBase } from './base-list.js';
type Action = {
icon: string;
label?: string;
callback: () => void;
};
export type ActionItem = {
/** The main information of the list item */
headline: string;
/** Supporting information rendered in a second line */
supportingText?: string;
/** An attached XML element */
attachedElement?: Element;
/** An icon rendered left to the list item content */
startingIcon?: string;
/** An icon rendered right to the list item content */
endingIcon?: string;
/** Whether to add a divider at the bottom of the item */
divider?: boolean;
/** Specifies additional filter terms */
filtergroup?: string[];
/** The action to be performed when clicking the list item */
primaryAction?: () => void;
/** Additional actions for the item. The first rendered is visible */
actions?: Action[];
};
function term(item: ActionItem): string {
return `${item.headline} ${item.supportingText ?? ''}${
item.filtergroup?.join(' ') ?? ''
}`;
}
/** TextField designed to be used for SCL element */
export class ActionList extends FilterListBase {
static scopedElements = {
'md-outlined-text-field': MdOutlinedTextField,
'md-icon': MdIcon,
'md-list': MdList,
'md-list-item': MdListItem,
'md-divider': MdDivider,
'md-menu': MdMenu,
'md-menu-item': MdMenuItem,
};
/** ListItems and potential */
@property({ type: Array })
items: ActionItem[] = [];
/** Height of each list item */
@property({ type: Number })
height: number = 72;
private renderMoreVertItem(item: ActionItem): TemplateResult {
item.actions!.shift();
const otherActions = item.actions!;
return html`
<span style="position: relative">
<md-list-item
id="more-vert-anchor"
type="button"
class="${classMap({
hidden: !this.searchRegex.test(term(item)),
})}"
@click=${(evt: Event) => {
const menu =
evt.target instanceof Icon
? ((evt.target.parentElement as Element)
.nextElementSibling as Menu)
: ((evt.target as Element).nextElementSibling as Menu);
menu.show();
}}
>
<md-icon slot="start">more_vert</md-icon>
</md-list-item>
<md-menu id="more-vert-menu" anchor="more-vert-anchor">
${otherActions.map(
action => html`<md-menu-item @click=${action.callback}>
<div slot="headline">${action.label}</div>
<md-icon slot="start">${action.icon}</md-icon>
</md-menu-item>`
)}
</md-menu> </span
>${item.divider
? html`<md-divider
class="${classMap({ hidden: !this.searchRegex.test(term(item)) })}"
></md-divider>`
: html``}
`;
}
private renderActionItem(item: ActionItem, index = 0): TemplateResult {
const action = item.actions ? item.actions[index] : null;
if (!action)
return html`<md-list-item
class="${classMap({
hidden: !this.searchRegex.test(term(item)),
})}"
></md-list-item
>${item.divider
? html`<md-divider
class="${classMap({
hidden: !this.searchRegex.test(term(item)),
})}"
></md-divider>`
: html``}`;
return html`<md-list-item
type="button"
class="${classMap({
hidden: !this.searchRegex.test(term(item)),
})}"
@click=${action.callback}
>
<md-icon slot="start">${action.icon}</md-icon> </md-list-item
>${item.divider
? html`<md-divider
class="${classMap({ hidden: !this.searchRegex.test(term(item)) })}"
></md-divider>`
: html``}`;
}
private renderOtherActions(): TemplateResult {
return html`<md-list>
${this.items.map(item =>
item.actions && item.actions?.length > 2
? this.renderMoreVertItem(item)
: this.renderActionItem(item, 1)
)}</md-list
>`;
}
private renderFirstAction(): TemplateResult {
return html`<md-list>
${this.items.map(item => this.renderActionItem(item))}</md-list
>`;
}
private renderActions(): TemplateResult {
return html`
${this.items.some(item => item.actions && item.actions[0])
? this.renderFirstAction()
: html``}
${this.items.some(item => item.actions && item.actions.length > 1)
? this.renderOtherActions()
: html``}
`;
}
private renderActionListItem(item: ActionItem): TemplateResult {
return html`<md-list-item
type="${item.primaryAction ? 'link' : 'text'}"
class="${classMap({
hidden: !this.searchRegex.test(term(item)),
})}"
title="${item.headline ?? ''}
${item.headline && item.supportingText ? '-' : ''}${item.supportingText}"
@click="${item.primaryAction}"
>
<div slot="headline" class="firstLine">${item.headline}</div>
${item.supportingText
? html`<div slot="supporting-text">${item.supportingText}</div>`
: html``}
${item.startingIcon
? html`<md-icon slot="start">${item.startingIcon}</md-icon>`
: html``}
${item.endingIcon
? html`<md-icon slot="end">${item.endingIcon}</md-icon>`
: html``} </md-list-item
>${item.divider
? html`<md-divider
class="${classMap({ hidden: !this.searchRegex.test(term(item)) })}"
></md-divider>`
: html``}`;
}
private renderListItem(item: ActionItem): TemplateResult {
return this.renderActionListItem(item);
}
render(): TemplateResult {
return html`<style>
md-list-item {
height: ${this.height}px;
}
[slot='supporting-text'] {
max-height: ${this.height - 24}px;
}
</style>
<section>
${this.renderSearchField()}
<div style="display: flex;">
<md-list class="listitems">
${this.items.map(item => this.renderListItem(item))}</md-list
>
${this.renderActions()}
</div>
</section>`;
}
static styles = css`
section {
display: flex;
flex-direction: column;
justify-content: space-between;
}
md-outlined-text-field {
background-color: var(--md-sys-color-surface, #fef7ff);
--md-outlined-text-field-container-shape: 32px;
padding: 8px;
}
[slot='headline'] {
white-space: pre;
}
[slot='supporting-text'] {
overflow: hidden;
text-overflow: ellipsis;
white-space: normal;
}
.listitems {
flex: auto;
overflow: hidden;
text-overflow: ellipsis;
}
.hidden {
display: none;
}
`;
}