Adrià Vilanova Martínez | ca25b68 | 2022-10-24 00:11:03 +0200 | [diff] [blame] | 1 | import './TwptConfirmDialog.js'; |
Adrià Vilanova Martínez | e0d65f2 | 2022-11-06 18:49:35 +0100 | [diff] [blame] | 2 | import './TwptCRImportButton.js'; |
Adrià Vilanova Martínez | 54964a5 | 2022-10-26 23:53:29 +0200 | [diff] [blame] | 3 | import './TwptWorkflowDialog.js'; |
| 4 | import './TwptWorkflowsMenu.js'; |
Adrià Vilanova Martínez | 96ae96f | 2022-10-17 23:50:36 +0200 | [diff] [blame] | 5 | |
| 6 | import {css, html, LitElement} from 'lit'; |
Adrià Vilanova Martínez | ca25b68 | 2022-10-24 00:11:03 +0200 | [diff] [blame] | 7 | import {createRef, ref} from 'lit/directives/ref.js'; |
Adrià Vilanova Martínez | 96ae96f | 2022-10-17 23:50:36 +0200 | [diff] [blame] | 8 | |
| 9 | import WorkflowsStorage from '../../../../workflows/workflowsStorage.js'; |
| 10 | |
| 11 | export default class TwptWorkflowsInject extends LitElement { |
| 12 | static properties = { |
| 13 | _workflows: {type: Object}, |
Adrià Vilanova Martínez | ca25b68 | 2022-10-24 00:11:03 +0200 | [diff] [blame] | 14 | _selectedWorkflowUuid: {type: String}, |
Adrià Vilanova Martínez | 96ae96f | 2022-10-17 23:50:36 +0200 | [diff] [blame] | 15 | }; |
| 16 | |
Adrià Vilanova Martínez | ca25b68 | 2022-10-24 00:11:03 +0200 | [diff] [blame] | 17 | confirmDialogRef = createRef(); |
| 18 | workflowDialogRef = createRef(); |
| 19 | |
Adrià Vilanova Martínez | 96ae96f | 2022-10-17 23:50:36 +0200 | [diff] [blame] | 20 | constructor() { |
| 21 | super(); |
| 22 | this._workflows = null; |
Adrià Vilanova Martínez | ca25b68 | 2022-10-24 00:11:03 +0200 | [diff] [blame] | 23 | this._selectedWorkflowUuid = null; |
Adrià Vilanova Martínez | 96ae96f | 2022-10-17 23:50:36 +0200 | [diff] [blame] | 24 | 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ínez | ca25b68 | 2022-10-24 00:11:03 +0200 | [diff] [blame] | 33 | <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ínez | 96ae96f | 2022-10-17 23:50:36 +0200 | [diff] [blame] | 43 | `; |
| 44 | } |
Adrià Vilanova Martínez | ca25b68 | 2022-10-24 00:11:03 +0200 | [diff] [blame] | 45 | |
| 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ínez | e0d65f2 | 2022-11-06 18:49:35 +0100 | [diff] [blame] | 57 | this.workflowDialogRef.value.workflow = |
| 58 | this._selectedWorkflow.cloneMessage(); |
Adrià Vilanova Martínez | 54964a5 | 2022-10-26 23:53:29 +0200 | [diff] [blame] | 59 | this.workflowDialogRef.value.start(); |
Adrià Vilanova Martínez | ca25b68 | 2022-10-24 00:11:03 +0200 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | get _selectedWorkflow() { |
Adrià Vilanova Martínez | 6f62c7d | 2022-11-05 20:48:52 +0100 | [diff] [blame] | 63 | if (!this._workflows) return null; |
| 64 | |
Adrià Vilanova Martínez | ca25b68 | 2022-10-24 00:11:03 +0200 | [diff] [blame] | 65 | for (const w of this._workflows) { |
Adrià Vilanova Martínez | 54964a5 | 2022-10-26 23:53:29 +0200 | [diff] [blame] | 66 | if (w.uuid == this._selectedWorkflowUuid) return w.proto; |
Adrià Vilanova Martínez | ca25b68 | 2022-10-24 00:11:03 +0200 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | return null; |
| 70 | } |
Adrià Vilanova Martínez | 96ae96f | 2022-10-17 23:50:36 +0200 | [diff] [blame] | 71 | } |
| 72 | window.customElements.define('twpt-workflows-inject', TwptWorkflowsInject); |