Adrià Vilanova Martínez | f276ac7 | 2022-10-13 22:16:22 +0200 | [diff] [blame] | 1 | import '@material/mwc-dialog/mwc-dialog.js'; |
| 2 | import '@material/web/button/text-button.js'; |
Adrià Vilanova Martínez | 7f1e8ea | 2022-10-14 15:50:11 +0200 | [diff] [blame] | 3 | import '@material/web/button/filled-button.js'; |
Adrià Vilanova Martínez | f276ac7 | 2022-10-13 22:16:22 +0200 | [diff] [blame] | 4 | import './WorkflowEditor.js'; |
| 5 | |
| 6 | import {css, html, LitElement} from 'lit'; |
| 7 | import {createRef, ref} from 'lit/directives/ref.js'; |
| 8 | |
| 9 | import * as pb from '../../proto/main_pb.js'; |
| 10 | |
| 11 | export 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ínez | f276ac7 | 2022-10-13 22:16:22 +0200 | [diff] [blame] | 32 | ?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ínez | 7f1e8ea | 2022-10-14 15:50:11 +0200 | [diff] [blame] | 38 | <md-filled-button |
Adrià Vilanova Martínez | f276ac7 | 2022-10-13 22:16:22 +0200 | [diff] [blame] | 39 | slot="primaryAction" |
| 40 | label="Add" |
| 41 | @click=${this._save}> |
Adrià Vilanova Martínez | 7f1e8ea | 2022-10-14 15:50:11 +0200 | [diff] [blame] | 42 | </md-filled-button> |
Adrià Vilanova Martínez | f276ac7 | 2022-10-13 22:16:22 +0200 | [diff] [blame] | 43 | <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() { |
Adrià Vilanova Martínez | 8803b6c | 2022-10-17 00:36:38 +0200 | [diff] [blame] | 86 | const success = this.workflowEditorRef.value.save(); |
Adrià Vilanova Martínez | f276ac7 | 2022-10-13 22:16:22 +0200 | [diff] [blame] | 87 | if (success) { |
| 88 | this.open = false; |
| 89 | this._resetWorkflow(); |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | window.customElements.define('wf-add-dialog', WFAddDialog); |