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