blob: 4a8ab900a2ef38bc294eab28d12297bcb0981353 [file] [log] [blame]
Adrià Vilanova Martínezf276ac72022-10-13 22:16:22 +02001import '@material/web/button/outlined-button.js';
Adrià Vilanova Martínezc5507dd2022-10-13 23:04:01 +02002import '@material/web/textfield/filled-text-field.js';
Adrià Vilanova Martínezf276ac72022-10-13 22:16:22 +02003import './ActionEditor.js';
4
Adrià Vilanova Martínezc5507dd2022-10-13 23:04:01 +02005import {css, html, LitElement, nothing} from 'lit';
6import {createRef, ref} from 'lit/directives/ref.js';
Adrià Vilanova Martínezf276ac72022-10-13 22:16:22 +02007import {repeat} from 'lit/directives/repeat.js';
8
9import * as pb from '../../proto/main_pb.js';
10
11export default class WFWorkflowEditor extends LitElement {
12 static properties = {
13 workflow: {type: Object},
14 readOnly: {type: Boolean},
15 };
16
Adrià Vilanova Martínezc5507dd2022-10-13 23:04:01 +020017 static styles = css`
18 .name {
19 width: 100%;
20 margin-bottom: 20px;
21 }
22 `;
23
24 nameRef = createRef();
25
Adrià Vilanova Martínezf276ac72022-10-13 22:16:22 +020026 constructor() {
27 super();
28 this.workflow = new pb.workflows.Workflow();
29 this.readOnly = false;
30 }
31
Adrià Vilanova Martínezc5507dd2022-10-13 23:04:01 +020032 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ínezf276ac72022-10-13 22:16:22 +020044 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ínezc5507dd2022-10-13 23:04:01 +020069 this.renderName(),
Adrià Vilanova Martínezf276ac72022-10-13 22:16:22 +020070 this.renderActions(),
71 this.renderAddActionBtn(),
72 ];
73 }
74
75 save() {
76 let allValid = true;
Adrià Vilanova Martínezc5507dd2022-10-13 23:04:01 +020077
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ínezf276ac72022-10-13 22:16:22 +020082 const actionEditors = this.renderRoot.querySelectorAll('wf-action-editor');
Adrià Vilanova Martínezc5507dd2022-10-13 23:04:01 +020083 for (const editor of actionEditors) allValid &&= editor.checkValidity();
84
Adrià Vilanova Martínezf276ac72022-10-13 22:16:22 +020085 // @TODO: Save if allValid === true
Adrià Vilanova Martínezc5507dd2022-10-13 23:04:01 +020086
Adrià Vilanova Martínezf276ac72022-10-13 22:16:22 +020087 return allValid;
88 }
89
90 _actions() {
91 return this.workflow.getActionsList();
92 }
93
Adrià Vilanova Martínezc5507dd2022-10-13 23:04:01 +020094 _nameChanged() {
95 this.workflow.setName(this.nameRef.value.value);
96 this._dispatchUpdateEvent();
97 }
98
Adrià Vilanova Martínezf276ac72022-10-13 22:16:22 +020099 _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}
130window.customElements.define('wf-workflow-editor', WFWorkflowEditor);