blob: f7651f0d9addd5139d77e0991e44b3a0fc45e673 [file] [log] [blame]
Adrià Vilanova Martínezca25b682022-10-24 00:11:03 +02001import './TwptConfirmDialog.js';
Adrià Vilanova Martíneze0d65f22022-11-06 18:49:35 +01002import './TwptCRImportButton.js';
Adrià Vilanova Martínez54964a52022-10-26 23:53:29 +02003import './TwptWorkflowDialog.js';
4import './TwptWorkflowsMenu.js';
Adrià Vilanova Martínez96ae96f2022-10-17 23:50:36 +02005
6import {css, html, LitElement} from 'lit';
Adrià Vilanova Martínezca25b682022-10-24 00:11:03 +02007import {createRef, ref} from 'lit/directives/ref.js';
Adrià Vilanova Martínez96ae96f2022-10-17 23:50:36 +02008
9import WorkflowsStorage from '../../../../workflows/workflowsStorage.js';
10
11export default class TwptWorkflowsInject extends LitElement {
12 static properties = {
13 _workflows: {type: Object},
Adrià Vilanova Martínezca25b682022-10-24 00:11:03 +020014 _selectedWorkflowUuid: {type: String},
Adrià Vilanova Martínez96ae96f2022-10-17 23:50:36 +020015 };
16
Adrià Vilanova Martínezca25b682022-10-24 00:11:03 +020017 confirmDialogRef = createRef();
18 workflowDialogRef = createRef();
19
Adrià Vilanova Martínez96ae96f2022-10-17 23:50:36 +020020 constructor() {
21 super();
22 this._workflows = null;
Adrià Vilanova Martínezca25b682022-10-24 00:11:03 +020023 this._selectedWorkflowUuid = null;
Adrià Vilanova Martínez96ae96f2022-10-17 23:50:36 +020024 this.addEventListener('twpt-workflows-update', e => {
25 const workflows = e.detail?.workflows ?? [];
26 WorkflowsStorage.convertRawListToProtobuf(workflows);
27 this._workflows = workflows;
28 });
29 }
30
31 render() {
32 return html`
Adrià Vilanova Martínezca25b682022-10-24 00:11:03 +020033 <twpt-workflows-menu
34 .workflows=${this._workflows}
35 @select=${this._workflowSelected}>
36 </twpt-workflows-menu>
37 <twpt-confirm-dialog ${ref(this.confirmDialogRef)}
38 .workflow=${this._selectedWorkflow}
39 @confirm=${this._startWorkflow}>
40 </twpt-confirm-dialog>
41 <twpt-workflow-dialog ${ref(this.workflowDialogRef)}>
42 </twpt-workflow-dialog>
Adrià Vilanova Martínez96ae96f2022-10-17 23:50:36 +020043 `;
44 }
Adrià Vilanova Martínezca25b682022-10-24 00:11:03 +020045
46 _workflowSelected(e) {
47 const uuid = e.detail?.selectedWorkflowUuid;
48 if (!uuid) {
49 console.error('Didn\'t get a correct uuid for the selected workflow.');
50 return;
51 }
52 this._selectedWorkflowUuid = uuid;
53 this.confirmDialogRef.value.open = true;
54 }
55
56 _startWorkflow() {
Adrià Vilanova Martíneze0d65f22022-11-06 18:49:35 +010057 this.workflowDialogRef.value.workflow =
58 this._selectedWorkflow.cloneMessage();
Adrià Vilanova Martínez54964a52022-10-26 23:53:29 +020059 this.workflowDialogRef.value.start();
Adrià Vilanova Martínezca25b682022-10-24 00:11:03 +020060 }
61
62 get _selectedWorkflow() {
Adrià Vilanova Martínez6f62c7d2022-11-05 20:48:52 +010063 if (!this._workflows) return null;
64
Adrià Vilanova Martínezca25b682022-10-24 00:11:03 +020065 for (const w of this._workflows) {
Adrià Vilanova Martínez54964a52022-10-26 23:53:29 +020066 if (w.uuid == this._selectedWorkflowUuid) return w.proto;
Adrià Vilanova Martínezca25b682022-10-24 00:11:03 +020067 }
68
69 return null;
70 }
Adrià Vilanova Martínez96ae96f2022-10-17 23:50:36 +020071}
72window.customElements.define('twpt-workflows-inject', TwptWorkflowsInject);