Adrià Vilanova Martínez | f276ac7 | 2022-10-13 22:16:22 +0200 | [diff] [blame] | 1 | import '@material/web/button/outlined-button.js'; |
Adrià Vilanova Martínez | c5507dd | 2022-10-13 23:04:01 +0200 | [diff] [blame^] | 2 | import '@material/web/textfield/filled-text-field.js'; |
Adrià Vilanova Martínez | f276ac7 | 2022-10-13 22:16:22 +0200 | [diff] [blame] | 3 | import './ActionEditor.js'; |
| 4 | |
Adrià Vilanova Martínez | c5507dd | 2022-10-13 23:04:01 +0200 | [diff] [blame^] | 5 | import {css, html, LitElement, nothing} from 'lit'; |
| 6 | import {createRef, ref} from 'lit/directives/ref.js'; |
Adrià Vilanova Martínez | f276ac7 | 2022-10-13 22:16:22 +0200 | [diff] [blame] | 7 | import {repeat} from 'lit/directives/repeat.js'; |
| 8 | |
| 9 | import * as pb from '../../proto/main_pb.js'; |
| 10 | |
| 11 | export default class WFWorkflowEditor extends LitElement { |
| 12 | static properties = { |
| 13 | workflow: {type: Object}, |
| 14 | readOnly: {type: Boolean}, |
| 15 | }; |
| 16 | |
Adrià Vilanova Martínez | c5507dd | 2022-10-13 23:04:01 +0200 | [diff] [blame^] | 17 | static styles = css` |
| 18 | .name { |
| 19 | width: 100%; |
| 20 | margin-bottom: 20px; |
| 21 | } |
| 22 | `; |
| 23 | |
| 24 | nameRef = createRef(); |
| 25 | |
Adrià Vilanova Martínez | f276ac7 | 2022-10-13 22:16:22 +0200 | [diff] [blame] | 26 | constructor() { |
| 27 | super(); |
| 28 | this.workflow = new pb.workflows.Workflow(); |
| 29 | this.readOnly = false; |
| 30 | } |
| 31 | |
Adrià Vilanova Martínez | c5507dd | 2022-10-13 23:04:01 +0200 | [diff] [blame^] | 32 | renderName() { |
| 33 | return html` |
| 34 | <md-filled-text-field ${ref(this.nameRef)} |
| 35 | class="name" |
| 36 | placeholder="Untitled workflow" |
| 37 | value=${this.workflow.getName()} |
| 38 | required |
| 39 | @input=${this._nameChanged}> |
| 40 | </md-filled-text-field> |
| 41 | `; |
| 42 | } |
| 43 | |
Adrià Vilanova Martínez | f276ac7 | 2022-10-13 22:16:22 +0200 | [diff] [blame] | 44 | renderActions() { |
| 45 | return repeat(this._actions(), (action, i) => html` |
| 46 | <wf-action-editor |
| 47 | .action=${action} |
| 48 | ?readOnly=${this.readOnly} |
| 49 | ?disableRemoveButton=${this._actions().length <= 1} |
| 50 | step=${i + 1} |
| 51 | @action-removed=${() => this._removeAction(i)}> |
| 52 | </wf-action-editor> |
| 53 | `); |
| 54 | } |
| 55 | |
| 56 | renderAddActionBtn() { |
| 57 | if (this.readOnly) return nothing; |
| 58 | return html` |
| 59 | <md-outlined-button |
| 60 | icon="add" |
| 61 | label="Add another action" |
| 62 | @click=${this._addAction}> |
| 63 | </md-outlined-button> |
| 64 | `; |
| 65 | } |
| 66 | |
| 67 | render() { |
| 68 | return [ |
Adrià Vilanova Martínez | c5507dd | 2022-10-13 23:04:01 +0200 | [diff] [blame^] | 69 | this.renderName(), |
Adrià Vilanova Martínez | f276ac7 | 2022-10-13 22:16:22 +0200 | [diff] [blame] | 70 | this.renderActions(), |
| 71 | this.renderAddActionBtn(), |
| 72 | ]; |
| 73 | } |
| 74 | |
| 75 | save() { |
| 76 | let allValid = true; |
Adrià Vilanova Martínez | c5507dd | 2022-10-13 23:04:01 +0200 | [diff] [blame^] | 77 | |
| 78 | // Check the workflow name is set |
| 79 | allValid &&= this.nameRef.value.reportValidity(); |
| 80 | |
| 81 | // Check all the actions are well-formed |
Adrià Vilanova Martínez | f276ac7 | 2022-10-13 22:16:22 +0200 | [diff] [blame] | 82 | const actionEditors = this.renderRoot.querySelectorAll('wf-action-editor'); |
Adrià Vilanova Martínez | c5507dd | 2022-10-13 23:04:01 +0200 | [diff] [blame^] | 83 | for (const editor of actionEditors) allValid &&= editor.checkValidity(); |
| 84 | |
Adrià Vilanova Martínez | f276ac7 | 2022-10-13 22:16:22 +0200 | [diff] [blame] | 85 | // @TODO: Save if allValid === true |
Adrià Vilanova Martínez | c5507dd | 2022-10-13 23:04:01 +0200 | [diff] [blame^] | 86 | |
Adrià Vilanova Martínez | f276ac7 | 2022-10-13 22:16:22 +0200 | [diff] [blame] | 87 | return allValid; |
| 88 | } |
| 89 | |
| 90 | _actions() { |
| 91 | return this.workflow.getActionsList(); |
| 92 | } |
| 93 | |
Adrià Vilanova Martínez | c5507dd | 2022-10-13 23:04:01 +0200 | [diff] [blame^] | 94 | _nameChanged() { |
| 95 | this.workflow.setName(this.nameRef.value.value); |
| 96 | this._dispatchUpdateEvent(); |
| 97 | } |
| 98 | |
Adrià Vilanova Martínez | f276ac7 | 2022-10-13 22:16:22 +0200 | [diff] [blame] | 99 | _addAction() { |
| 100 | let action = new pb.workflows.Action(); |
| 101 | let rAction = new pb.workflows.Action.ReplyWithCRAction(); |
| 102 | action.setReplyWithCrAction(rAction); |
| 103 | this.workflow.addActions(action); |
| 104 | this._dispatchUpdateEvent(); |
| 105 | } |
| 106 | |
| 107 | _removeAction(index) { |
| 108 | let actions = this.workflow.getActionsList(); |
| 109 | actions.splice(index, 1); |
| 110 | this.workflow.setActionsList(actions); |
| 111 | this._dispatchUpdateEvent(); |
| 112 | } |
| 113 | |
| 114 | _updateAction(index, action) { |
| 115 | let actions = this.workflow.getActionsList(); |
| 116 | actions[index] = action; |
| 117 | this.workflow.setActionsList(actions); |
| 118 | this._dispatchUpdateEvent(); |
| 119 | } |
| 120 | |
| 121 | _dispatchUpdateEvent() { |
| 122 | // Request an update for this component |
| 123 | this.requestUpdate(); |
| 124 | |
| 125 | // Transmit to other components that the workflow has changed |
| 126 | const e = new Event('workflow-updated', {bubbles: true, composed: true}); |
| 127 | this.renderRoot.dispatchEvent(e); |
| 128 | } |
| 129 | } |
| 130 | window.customElements.define('wf-workflow-editor', WFWorkflowEditor); |