Adrià Vilanova MartÃnez | f276ac7 | 2022-10-13 22:16:22 +0200 | [diff] [blame^] | 1 | import '@material/web/button/outlined-button.js'; |
| 2 | import './ActionEditor.js'; |
| 3 | |
| 4 | import {html, LitElement, nothing} from 'lit'; |
| 5 | import {repeat} from 'lit/directives/repeat.js'; |
| 6 | |
| 7 | import * as pb from '../../proto/main_pb.js'; |
| 8 | |
| 9 | export default class WFWorkflowEditor extends LitElement { |
| 10 | static properties = { |
| 11 | workflow: {type: Object}, |
| 12 | readOnly: {type: Boolean}, |
| 13 | }; |
| 14 | |
| 15 | constructor() { |
| 16 | super(); |
| 17 | this.workflow = new pb.workflows.Workflow(); |
| 18 | this.readOnly = false; |
| 19 | } |
| 20 | |
| 21 | renderActions() { |
| 22 | return repeat(this._actions(), (action, i) => html` |
| 23 | <wf-action-editor |
| 24 | .action=${action} |
| 25 | ?readOnly=${this.readOnly} |
| 26 | ?disableRemoveButton=${this._actions().length <= 1} |
| 27 | step=${i + 1} |
| 28 | @action-removed=${() => this._removeAction(i)}> |
| 29 | </wf-action-editor> |
| 30 | `); |
| 31 | } |
| 32 | |
| 33 | renderAddActionBtn() { |
| 34 | if (this.readOnly) return nothing; |
| 35 | return html` |
| 36 | <md-outlined-button |
| 37 | icon="add" |
| 38 | label="Add another action" |
| 39 | @click=${this._addAction}> |
| 40 | </md-outlined-button> |
| 41 | `; |
| 42 | } |
| 43 | |
| 44 | render() { |
| 45 | return [ |
| 46 | this.renderActions(), |
| 47 | this.renderAddActionBtn(), |
| 48 | ]; |
| 49 | } |
| 50 | |
| 51 | save() { |
| 52 | let allValid = true; |
| 53 | const actionEditors = this.renderRoot.querySelectorAll('wf-action-editor'); |
| 54 | for (const editor of actionEditors) { |
| 55 | const isValid = editor.checkValidity(); |
| 56 | if (!isValid) allValid = false; |
| 57 | } |
| 58 | // @TODO: Save if allValid === true |
| 59 | return allValid; |
| 60 | } |
| 61 | |
| 62 | _actions() { |
| 63 | return this.workflow.getActionsList(); |
| 64 | } |
| 65 | |
| 66 | _addAction() { |
| 67 | let action = new pb.workflows.Action(); |
| 68 | let rAction = new pb.workflows.Action.ReplyWithCRAction(); |
| 69 | action.setReplyWithCrAction(rAction); |
| 70 | this.workflow.addActions(action); |
| 71 | this._dispatchUpdateEvent(); |
| 72 | } |
| 73 | |
| 74 | _removeAction(index) { |
| 75 | let actions = this.workflow.getActionsList(); |
| 76 | actions.splice(index, 1); |
| 77 | this.workflow.setActionsList(actions); |
| 78 | this._dispatchUpdateEvent(); |
| 79 | } |
| 80 | |
| 81 | _updateAction(index, action) { |
| 82 | let actions = this.workflow.getActionsList(); |
| 83 | actions[index] = action; |
| 84 | this.workflow.setActionsList(actions); |
| 85 | this._dispatchUpdateEvent(); |
| 86 | } |
| 87 | |
| 88 | _dispatchUpdateEvent() { |
| 89 | // Request an update for this component |
| 90 | this.requestUpdate(); |
| 91 | |
| 92 | // Transmit to other components that the workflow has changed |
| 93 | const e = new Event('workflow-updated', {bubbles: true, composed: true}); |
| 94 | this.renderRoot.dispatchEvent(e); |
| 95 | } |
| 96 | } |
| 97 | window.customElements.define('wf-workflow-editor', WFWorkflowEditor); |