blob: 7ab9a872805d4930ccaa42f414fe641d905f32ce [file] [log] [blame]
Adrià Vilanova Martínez78dcfdf2024-02-26 02:02:22 +01001import '@material/web/select/outlined-select.js';
2import '@material/web/select/select-option.js';
3
4import {html, LitElement} from 'lit';
5import {createRef, ref} from 'lit/directives/ref.js';
6
7import {SHARED_MD3_STYLES} from '../../../../common/styles/md3.js';
8import * as pb from '../../../proto/main_pb.js';
9
10import {FORM_STYLES} from './common.js';
11
12const kHiddenActions = [
13 pb.workflows.Action.AttributeAction.AttributeAction.AA_NONE,
Adrià Vilanova Martínez78dcfdf2024-02-26 02:02:22 +010014];
15
16export default class WFActionAttribute extends LitElement {
17 static properties = {
18 action: {type: Object},
19 readOnly: {type: Boolean},
20 };
21
22 static styles = [
23 SHARED_MD3_STYLES,
24 FORM_STYLES,
25 ];
26
27 attributeActionRef = createRef();
28
29 constructor() {
30 super();
31 this.action = new pb.workflows.Action.AttributeAction;
32 }
33
34 render() {
35 return html`
36 <div class="form-line">
37 <md-outlined-select ${ref(this.attributeActionRef)}
38 required
39 label="Action"
40 value=${this.action}
41 ?disabled=${this.readOnly}
42 @change=${this._attributeActionChanged}>
43 ${this.renderAttributeActions()}
44 </md-outlined-select>
45 </div>
46 `;
47 }
48
49 renderAttributeActions() {
50 const attributeActions =
51 Object.entries(pb.workflows.Action.AttributeAction.AttributeAction);
52 return attributeActions.filter(([, id]) => !kHiddenActions.includes(id))
53 .map(([actionCodename, id]) => html`
54 <md-select-option value=${id}>
55 <div slot="headline">${actionCodename}</div>
56 </md-select-option>
57 `);
58 }
59
60 checkValidity() {
61 return this.attributeActionRef.value.reportValidity();
62 }
63
64 _dispatchUpdateEvent() {
65 // Request an update for this component
66 this.requestUpdate();
67
68 // Transmit to other components that the action has changed
69 const e =
70 new Event('attribute-action-updated', {bubbles: true, composed: true});
71 this.renderRoot.dispatchEvent(e);
72 }
73
74 _attributeActionChanged() {
75 this.attributeAction = this.attributeActionRef.value.value;
76 }
77
78 get attributeAction() {
79 return this.action.getAttributeAction();
80 }
81
82 set attributeAction(value) {
83 this.action.setAttributeAction(value);
84 this._dispatchUpdateEvent();
85 }
86}
87window.customElements.define('wf-action-attribute', WFActionAttribute);