blob: 150e7da2b6e19f551b13dfc0de952bb8e4c34a04 [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,
14 pb.workflows.Action.AttributeAction.AttributeAction.AA_SOFT_LOCK,
15 pb.workflows.Action.AttributeAction.AttributeAction.AA_UNSOFT_LOCK,
16];
17
18export default class WFActionAttribute extends LitElement {
19 static properties = {
20 action: {type: Object},
21 readOnly: {type: Boolean},
22 };
23
24 static styles = [
25 SHARED_MD3_STYLES,
26 FORM_STYLES,
27 ];
28
29 attributeActionRef = createRef();
30
31 constructor() {
32 super();
33 this.action = new pb.workflows.Action.AttributeAction;
34 }
35
36 render() {
37 return html`
38 <div class="form-line">
39 <md-outlined-select ${ref(this.attributeActionRef)}
40 required
41 label="Action"
42 value=${this.action}
43 ?disabled=${this.readOnly}
44 @change=${this._attributeActionChanged}>
45 ${this.renderAttributeActions()}
46 </md-outlined-select>
47 </div>
48 `;
49 }
50
51 renderAttributeActions() {
52 const attributeActions =
53 Object.entries(pb.workflows.Action.AttributeAction.AttributeAction);
54 return attributeActions.filter(([, id]) => !kHiddenActions.includes(id))
55 .map(([actionCodename, id]) => html`
56 <md-select-option value=${id}>
57 <div slot="headline">${actionCodename}</div>
58 </md-select-option>
59 `);
60 }
61
62 checkValidity() {
63 return this.attributeActionRef.value.reportValidity();
64 }
65
66 _dispatchUpdateEvent() {
67 // Request an update for this component
68 this.requestUpdate();
69
70 // Transmit to other components that the action has changed
71 const e =
72 new Event('attribute-action-updated', {bubbles: true, composed: true});
73 this.renderRoot.dispatchEvent(e);
74 }
75
76 _attributeActionChanged() {
77 this.attributeAction = this.attributeActionRef.value.value;
78 }
79
80 get attributeAction() {
81 return this.action.getAttributeAction();
82 }
83
84 set attributeAction(value) {
85 this.action.setAttributeAction(value);
86 this._dispatchUpdateEvent();
87 }
88}
89window.customElements.define('wf-action-attribute', WFActionAttribute);