blob: e97f6f4509927489b40ef21c1dfb4bb0acc22f77 [file] [log] [blame]
Renovate botaa5fb8e2024-02-25 18:10:09 +00001import '@material/web/icon/icon.js';
Adrià Vilanova Martíneze0d65f22022-11-06 18:49:35 +01002import '@material/web/switch/switch.js';
3import '@material/web/textfield/outlined-text-field.js';
Renovate botaa5fb8e2024-02-25 18:10:09 +00004import '../../../../common/components/FormField.js';
Adrià Vilanova Martínezf276ac72022-10-13 22:16:22 +02005
Adrià Vilanova Martínez41493f22022-11-06 22:38:21 +01006import {css, html, LitElement, nothing} from 'lit';
Adrià Vilanova Martínezf276ac72022-10-13 22:16:22 +02007import {createRef, ref} from 'lit/directives/ref.js';
8
Renovate botaa5fb8e2024-02-25 18:10:09 +00009import {SHARED_MD3_STYLES} from '../../../../common/styles/md3.js';
Adrià Vilanova Martínezf276ac72022-10-13 22:16:22 +020010import * as pb from '../../../proto/main_pb.js';
Adrià Vilanova Martínez78dcfdf2024-02-26 02:02:22 +010011import { FORM_STYLES } from './common.js';
Adrià Vilanova Martínezf276ac72022-10-13 22:16:22 +020012
13export default class WFActionReplyWithCR extends LitElement {
14 static properties = {
15 action: {type: Object},
16 readOnly: {type: Boolean},
Adrià Vilanova Martíneze0d65f22022-11-06 18:49:35 +010017 _importerWindow: {type: Object, state: true},
Adrià Vilanova Martínezf276ac72022-10-13 22:16:22 +020018 };
19
Renovate botaa5fb8e2024-02-25 18:10:09 +000020 static styles = [
21 SHARED_MD3_STYLES,
Adrià Vilanova Martínez78dcfdf2024-02-26 02:02:22 +010022 FORM_STYLES,
Renovate botaa5fb8e2024-02-25 18:10:09 +000023 css`
Renovate botaa5fb8e2024-02-25 18:10:09 +000024 .select-cr-btn {
25 --md-outlined-button-icon-size: 24px;
26 }
27 `,
28 ];
Adrià Vilanova Martíneze0d65f22022-11-06 18:49:35 +010029
Adrià Vilanova Martínezf276ac72022-10-13 22:16:22 +020030 cannedResponseRef = createRef();
31 subscribeRef = createRef();
32 markAsAnswerRef = createRef();
33
34 constructor() {
35 super();
36 this.action = new pb.workflows.Action.ReplyWithCRAction;
Adrià Vilanova Martíneze0d65f22022-11-06 18:49:35 +010037 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ínezf276ac72022-10-13 22:16:22 +020046 }
47
48 render() {
49 return html`
Adrià Vilanova Martíneze0d65f22022-11-06 18:49:35 +010050 <div class="form-line">
Adrià Vilanova Martínezf276ac72022-10-13 22:16:22 +020051 <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ínez41493f22022-11-06 22:38:21 +010059 ${this.readOnly ? nothing : html`
60 <md-outlined-button
Renovate botaa5fb8e2024-02-25 18:10:09 +000061 class="select-cr-btn"
Adrià Vilanova Martínez41493f22022-11-06 22:38:21 +010062 @click=${this._openCRImporter}>
Renovate botaa5fb8e2024-02-25 18:10:09 +000063 <md-icon slot="icon" filled>more</md-icon>
64 Select CR
Adrià Vilanova Martínez41493f22022-11-06 22:38:21 +010065 </md-outlined-button>
66 `}
Adrià Vilanova Martíneze0d65f22022-11-06 18:49:35 +010067 </div>
68 <div class="form-line">
Renovate botaa5fb8e2024-02-25 18:10:09 +000069 <twpt-form-field>
Adrià Vilanova Martínezf276ac72022-10-13 22:16:22 +020070 <md-switch ${ref(this.subscribeRef)}
71 ?selected=${this.subscribe}
72 ?disabled=${this.readOnly}
Renovate botaa5fb8e2024-02-25 18:10:09 +000073 @change=${this._subscribeChanged}/>
74 </md-switch>
75 <span slot="label">
76 Subscribe to thread
77 </span>
78 </twpt-form-field>
Adrià Vilanova Martíneze0d65f22022-11-06 18:49:35 +010079 </div>
80 <div class="form-line">
Renovate botaa5fb8e2024-02-25 18:10:09 +000081 <twpt-form-field>
Adrià Vilanova Martínezf276ac72022-10-13 22:16:22 +020082 <md-switch ${ref(this.markAsAnswerRef)}
83 ?selected=${this.markAsAnswer}
84 ?disabled=${this.readOnly}
Renovate botaa5fb8e2024-02-25 18:10:09 +000085 @change=${this._markAsAnswerChanged}/>
86 </md-switch>
87 <span slot="label">
88 Mark as answer
89 </span>
90 </twpt-form-field>
Adrià Vilanova Martíneze0d65f22022-11-06 18:49:35 +010091 </div>
Adrià Vilanova Martínezf276ac72022-10-13 22:16:22 +020092 `;
93 }
94
Adrià Vilanova Martíneze0d65f22022-11-06 18:49:35 +010095 disconnectedCallback() {
96 super.disconnectedCallback();
97 this._importerWindow?.close?.();
98 }
99
Adrià Vilanova Martínezf276ac72022-10-13 22:16:22 +0200100 checkValidity() {
101 return this.cannedResponseRef.value.reportValidity();
102 }
103
Adrià Vilanova Martínezf276ac72022-10-13 22:16:22 +0200104 _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íneze0d65f22022-11-06 18:49:35 +0100126 _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ínezf276ac72022-10-13 22:16:22 +0200136 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}
173window.customElements.define('wf-action-reply-with-cr', WFActionReplyWithCR);