blob: 32a2cae11781292406fc73840d21fbe2180ca1b3 [file] [log] [blame]
Adrià Vilanova Martínezf276ac72022-10-13 22:16:22 +02001import '@material/mwc-dialog/mwc-dialog.js';
2import '@material/web/button/text-button.js';
Adrià Vilanova Martínez7f1e8ea2022-10-14 15:50:11 +02003import '@material/web/button/filled-button.js';
Adrià Vilanova Martínezf276ac72022-10-13 22:16:22 +02004import './WorkflowEditor.js';
5
6import {css, html, LitElement} from 'lit';
7import {createRef, ref} from 'lit/directives/ref.js';
8
9import * as pb from '../../proto/main_pb.js';
10
11export default class WFAddDialog extends LitElement {
12 static properties = {
13 open: {type: Boolean},
14 };
15
16 static styles = css`
17 :host {
18 --mdc-dialog-content-ink-color: var(--mdc-theme-on-surface, #000);
19 }
20 `;
21
22 workflowEditorRef = createRef();
23
24 constructor() {
25 super();
26 this.open = false;
27 }
28
29 render() {
30 return html`
31 <mwc-dialog
Adrià Vilanova Martínezf276ac72022-10-13 22:16:22 +020032 ?open=${this.open}
33 @opening=${this._openingDialog}
34 @closing=${this._closingDialog}
35 @closed=${this._closedDialog}>
36 <wf-workflow-editor ${ref(this.workflowEditorRef)}>
37 </wf-workflow-editor>
Adrià Vilanova Martínez7f1e8ea2022-10-14 15:50:11 +020038 <md-filled-button
Adrià Vilanova Martínezf276ac72022-10-13 22:16:22 +020039 slot="primaryAction"
40 label="Add"
41 @click=${this._save}>
Adrià Vilanova Martínez7f1e8ea2022-10-14 15:50:11 +020042 </md-filled-button>
Adrià Vilanova Martínezf276ac72022-10-13 22:16:22 +020043 <md-text-button
44 slot="secondaryAction"
45 dialogAction="cancel"
46 label="Cancel">
47 </md-text-button>
48 </mwc-dialog>
49 `;
50 }
51
52 firstUpdated() {
53 this._resetWorkflow();
54 }
55
56 _resetWorkflow() {
57 this.workflowEditorRef.value.workflow = this._defaultWorkflow();
58 }
59
60 _getWorkflow() {
61 return this.workflowEditorRef.value.workflow;
62 }
63
64 _defaultWorkflow() {
65 let wf = new pb.workflows.Workflow();
66 let action = new pb.workflows.Action();
67 let rAction = new pb.workflows.Action.ReplyWithCRAction();
68 action.setReplyWithCrAction(rAction);
69 wf.addActions(action);
70 return wf;
71 }
72
73 _openingDialog() {
74 this.open = true;
75 }
76
77 _closingDialog() {
78 this.open = false;
79 }
80
81 _closedDialog(e) {
82 if (e.detail?.action === 'cancel') this._resetWorkflow();
83 }
84
85 _save() {
86 let success = this.workflowEditorRef.value.save();
87 if (success) {
88 this.open = false;
89 this._resetWorkflow();
90 }
91 }
92}
93window.customElements.define('wf-add-dialog', WFAddDialog);