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