Adrià Vilanova MartÃnez | 78dcfdf | 2024-02-26 02:02:22 +0100 | [diff] [blame] | 1 | import '@material/web/select/outlined-select.js'; |
| 2 | import '@material/web/select/select-option.js'; |
| 3 | |
| 4 | import {html, LitElement} from 'lit'; |
| 5 | import {createRef, ref} from 'lit/directives/ref.js'; |
| 6 | |
| 7 | import {SHARED_MD3_STYLES} from '../../../../common/styles/md3.js'; |
| 8 | import * as pb from '../../../proto/main_pb.js'; |
| 9 | |
| 10 | import {FORM_STYLES} from './common.js'; |
| 11 | |
| 12 | const 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 | |
| 18 | export 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 | } |
| 89 | window.customElements.define('wf-action-attribute', WFActionAttribute); |