Adrià Vilanova Martínez | f276ac7 | 2022-10-13 22:16:22 +0200 | [diff] [blame] | 1 | import '@material/web/formfield/formfield.js'; |
Adrià Vilanova Martínez | e0d65f2 | 2022-11-06 18:49:35 +0100 | [diff] [blame] | 2 | import '@material/web/switch/switch.js'; |
| 3 | import '@material/web/textfield/outlined-text-field.js'; |
Adrià Vilanova Martínez | f276ac7 | 2022-10-13 22:16:22 +0200 | [diff] [blame] | 4 | |
Adrià Vilanova Martínez | 41493f2 | 2022-11-06 22:38:21 +0100 | [diff] [blame] | 5 | import {css, html, LitElement, nothing} from 'lit'; |
Adrià Vilanova Martínez | f276ac7 | 2022-10-13 22:16:22 +0200 | [diff] [blame] | 6 | import {createRef, ref} from 'lit/directives/ref.js'; |
| 7 | |
| 8 | import {CCApi} from '../../../../common/api.js'; |
| 9 | import * as pb from '../../../proto/main_pb.js'; |
| 10 | |
| 11 | export default class WFActionReplyWithCR extends LitElement { |
| 12 | static properties = { |
| 13 | action: {type: Object}, |
| 14 | readOnly: {type: Boolean}, |
Adrià Vilanova Martínez | e0d65f2 | 2022-11-06 18:49:35 +0100 | [diff] [blame] | 15 | _importerWindow: {type: Object, state: true}, |
Adrià Vilanova Martínez | f276ac7 | 2022-10-13 22:16:22 +0200 | [diff] [blame] | 16 | }; |
| 17 | |
Adrià Vilanova Martínez | e0d65f2 | 2022-11-06 18:49:35 +0100 | [diff] [blame] | 18 | static styles = css` |
| 19 | .form-line { |
| 20 | display: flex; |
| 21 | flex-direction: row; |
| 22 | align-items: center; |
| 23 | margin-block: 1em; |
| 24 | gap: .5rem; |
| 25 | } |
| 26 | `; |
| 27 | |
Adrià Vilanova Martínez | f276ac7 | 2022-10-13 22:16:22 +0200 | [diff] [blame] | 28 | cannedResponseRef = createRef(); |
| 29 | subscribeRef = createRef(); |
| 30 | markAsAnswerRef = createRef(); |
| 31 | |
| 32 | constructor() { |
| 33 | super(); |
| 34 | this.action = new pb.workflows.Action.ReplyWithCRAction; |
Adrià Vilanova Martínez | e0d65f2 | 2022-11-06 18:49:35 +0100 | [diff] [blame] | 35 | this._importerWindow = undefined; |
| 36 | |
| 37 | window.addEventListener('message', e => { |
| 38 | if (e.source === this._importerWindow && |
| 39 | e.data?.action === 'importCannedResponse') { |
| 40 | this._cannedResponseIdString = e.data?.cannedResponseId; |
| 41 | this._importerWindow?.close?.(); |
| 42 | } |
| 43 | }); |
Adrià Vilanova Martínez | f276ac7 | 2022-10-13 22:16:22 +0200 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | render() { |
| 47 | return html` |
Adrià Vilanova Martínez | e0d65f2 | 2022-11-06 18:49:35 +0100 | [diff] [blame] | 48 | <div class="form-line"> |
Adrià Vilanova Martínez | f276ac7 | 2022-10-13 22:16:22 +0200 | [diff] [blame] | 49 | <md-outlined-text-field ${ref(this.cannedResponseRef)} |
| 50 | type="number" |
| 51 | label="Canned response ID" |
| 52 | required |
| 53 | value=${this._cannedResponseIdString} |
| 54 | ?readonly=${this.readOnly} |
| 55 | @input=${this._cannedResponseIdChanged}> |
| 56 | </md-outlined-text-field> |
Adrià Vilanova Martínez | 41493f2 | 2022-11-06 22:38:21 +0100 | [diff] [blame] | 57 | ${this.readOnly ? nothing : html` |
| 58 | <md-outlined-button |
| 59 | icon="more" |
| 60 | label="Select CR" |
| 61 | @click=${this._openCRImporter}> |
| 62 | </md-outlined-button> |
| 63 | `} |
Adrià Vilanova Martínez | e0d65f2 | 2022-11-06 18:49:35 +0100 | [diff] [blame] | 64 | </div> |
| 65 | <div class="form-line"> |
Adrià Vilanova Martínez | f276ac7 | 2022-10-13 22:16:22 +0200 | [diff] [blame] | 66 | <md-formfield label="Subscribe to thread"> |
| 67 | <md-switch ${ref(this.subscribeRef)} |
| 68 | ?selected=${this.subscribe} |
| 69 | ?disabled=${this.readOnly} |
| 70 | @click=${this._subscribeChanged}/> |
| 71 | </md-formfield> |
Adrià Vilanova Martínez | e0d65f2 | 2022-11-06 18:49:35 +0100 | [diff] [blame] | 72 | </div> |
| 73 | <div class="form-line"> |
Adrià Vilanova Martínez | f276ac7 | 2022-10-13 22:16:22 +0200 | [diff] [blame] | 74 | <md-formfield label="Mark as answer"> |
| 75 | <md-switch ${ref(this.markAsAnswerRef)} |
| 76 | ?selected=${this.markAsAnswer} |
| 77 | ?disabled=${this.readOnly} |
| 78 | @click=${this._markAsAnswerChanged}/> |
| 79 | </md-formfield> |
Adrià Vilanova Martínez | e0d65f2 | 2022-11-06 18:49:35 +0100 | [diff] [blame] | 80 | </div> |
Adrià Vilanova Martínez | f276ac7 | 2022-10-13 22:16:22 +0200 | [diff] [blame] | 81 | `; |
| 82 | } |
| 83 | |
Adrià Vilanova Martínez | e0d65f2 | 2022-11-06 18:49:35 +0100 | [diff] [blame] | 84 | disconnectedCallback() { |
| 85 | super.disconnectedCallback(); |
| 86 | this._importerWindow?.close?.(); |
| 87 | } |
| 88 | |
Adrià Vilanova Martínez | f276ac7 | 2022-10-13 22:16:22 +0200 | [diff] [blame] | 89 | checkValidity() { |
| 90 | return this.cannedResponseRef.value.reportValidity(); |
| 91 | } |
| 92 | |
Adrià Vilanova Martínez | f276ac7 | 2022-10-13 22:16:22 +0200 | [diff] [blame] | 93 | _dispatchUpdateEvent() { |
| 94 | // Request an update for this component |
| 95 | this.requestUpdate(); |
| 96 | |
| 97 | // Transmit to other components that the action has changed |
| 98 | const e = new Event( |
| 99 | 'replywithcr-action-updated', {bubbles: true, composed: true}); |
| 100 | this.renderRoot.dispatchEvent(e); |
| 101 | } |
| 102 | |
| 103 | _cannedResponseIdChanged() { |
| 104 | this._cannedResponseIdString = this.cannedResponseRef.value.value; |
| 105 | } |
| 106 | |
| 107 | _subscribeChanged() { |
| 108 | this.subscribe = this.subscribeRef.value.selected; |
| 109 | } |
| 110 | |
| 111 | _markAsAnswerChanged() { |
| 112 | this.markAsAnswer = this.markAsAnswerRef.value.selected; |
| 113 | } |
| 114 | |
Adrià Vilanova Martínez | e0d65f2 | 2022-11-06 18:49:35 +0100 | [diff] [blame] | 115 | _openCRImporter() { |
| 116 | if (!(this._importerWindow?.closed ?? true)) |
| 117 | this._importerWindow?.close?.(); |
| 118 | |
| 119 | this._importerWindow = window.open( |
| 120 | 'https://support.google.com/s/community/cannedresponses?TWPTImportToWorkflow&TWPTSelectedId=' + |
| 121 | encodeURIComponent(this._cannedResponseIdString), |
| 122 | '', 'popup,width=720,height=540'); |
| 123 | } |
| 124 | |
Adrià Vilanova Martínez | f276ac7 | 2022-10-13 22:16:22 +0200 | [diff] [blame] | 125 | get cannedResponseId() { |
| 126 | return this.action.getCannedResponseId() ?? 0; |
| 127 | } |
| 128 | |
| 129 | set cannedResponseId(value) { |
| 130 | this.action.setCannedResponseId(value); |
| 131 | this._dispatchUpdateEvent(); |
| 132 | } |
| 133 | |
| 134 | get _cannedResponseIdString() { |
| 135 | let id = this.cannedResponseId; |
| 136 | if (id == 0) return ''; |
| 137 | return id.toString(); |
| 138 | } |
| 139 | |
| 140 | set _cannedResponseIdString(value) { |
| 141 | this.cannedResponseId = parseInt(value); |
| 142 | } |
| 143 | |
| 144 | get subscribe() { |
| 145 | return this.action.getSubscribe(); |
| 146 | } |
| 147 | |
| 148 | set subscribe(value) { |
| 149 | this.action.setSubscribe(value); |
| 150 | this._dispatchUpdateEvent(); |
| 151 | } |
| 152 | |
| 153 | get markAsAnswer() { |
| 154 | return this.action.getMarkAsAnswer(); |
| 155 | } |
| 156 | |
| 157 | set markAsAnswer(value) { |
| 158 | this.action.setMarkAsAnswer(value); |
| 159 | this._dispatchUpdateEvent(); |
| 160 | } |
| 161 | } |
| 162 | window.customElements.define('wf-action-reply-with-cr', WFActionReplyWithCR); |