blob: 9a57509a7007e71ba38f474ff6501ea14b0ec075 [file] [log] [blame]
Adrià Vilanova Martínezf276ac72022-10-13 22:16:22 +02001import './actions/ReplyWithCR.js';
2
Adrià Vilanova Martínez54964a52022-10-26 23:53:29 +02003import '@material/mwc-circular-progress/mwc-circular-progress.js';
4
5import {html, LitElement, nothing} from 'lit';
Adrià Vilanova Martínezf276ac72022-10-13 22:16:22 +02006import {map} from 'lit/directives/map.js';
7import {createRef, ref} from 'lit/directives/ref.js';
8
9import * as pb from '../../proto/main_pb.js';
Adrià Vilanova Martínez54964a52022-10-26 23:53:29 +020010import {kActionHeadings, kActionStyles, kSupportedActions} from '../shared/actions.js';
Adrià Vilanova Martínezf276ac72022-10-13 22:16:22 +020011
Adrià Vilanova Martínezf276ac72022-10-13 22:16:22 +020012const actionCases = Object.entries(pb.workflows.Action.ActionCase);
13
14export default class WFActionEditor extends LitElement {
15 static properties = {
16 action: {type: Object},
17 readOnly: {type: Boolean},
18 disableRemoveButton: {type: Boolean},
19 step: {type: Number},
Adrià Vilanova Martínez54964a52022-10-26 23:53:29 +020020 status: {type: String},
Adrià Vilanova Martínezf276ac72022-10-13 22:16:22 +020021 };
22
Adrià Vilanova Martínez54964a52022-10-26 23:53:29 +020023 static styles = kActionStyles;
Adrià Vilanova Martínezf276ac72022-10-13 22:16:22 +020024
25 selectRef = createRef();
26
27 constructor() {
28 super();
29 this.action = new pb.workflows.Action();
30 this.readOnly = false;
31 }
32
33 renderActionTitle() {
34 if (this.readOnly) return html`<h3 class="title">${this._stepTitle()}</h3>`;
35
36 let selectedActionCase = this._actionCase;
37
38 return html`
39 <select ${ref(this.selectRef)}
40 class="select"
41 @change=${this._actionCaseChanged}>
42 ${map(actionCases, ([actionName, num]) => {
Adrià Vilanova Martínez6d912742022-10-16 23:57:26 +020043 if (!kSupportedActions.has(num)) return nothing;
Adrià Vilanova Martínezf276ac72022-10-13 22:16:22 +020044 return html`
45 <option value=${num} ?selected=${selectedActionCase == num}>
46 ${kActionHeadings[num] ?? actionName}
47 </option>
48 `;
49 })}
50 </select>
51 `;
52 }
53
54 renderSpecificActionEditor() {
55 switch (this._actionCase) {
56 case pb.workflows.Action.ActionCase.REPLY_WITH_CR_ACTION:
57 return html`
58 <wf-action-reply-with-cr
59 ?readOnly=${this.readOnly}
60 .action=${this.action.getReplyWithCrAction()}>
61 </wf-action-reply-with-cr>
62 `;
63
Adrià Vilanova Martínez6c4739a2022-11-07 00:11:53 +010064 case pb.workflows.Action.ActionCase.MARK_AS_READ_ACTION:
65 case pb.workflows.Action.ActionCase.MARK_AS_UNREAD_ACTION:
66 return nothing;
67
Adrià Vilanova Martínezf276ac72022-10-13 22:16:22 +020068 default:
69 return html`<p>This action has not yet been implemented.</p>`;
70 }
71 }
72
73 render() {
Adrià Vilanova Martínez54964a52022-10-26 23:53:29 +020074 let actionClass = '';
75 if (this.readOnly && this.status) actionClass = 'action--' + this.status;
76 return html`
77 <div class="action ${actionClass}">
78 <div class="header">
79 <div class="step">
80 ${this.step}
Adrià Vilanova Martínezf276ac72022-10-13 22:16:22 +020081 ${
Adrià Vilanova Martínez54964a52022-10-26 23:53:29 +020082 this.status == 'running' ?
83 html`<mwc-circular-progress indeterminate density="-1"></mwc-circular-progress>` :
84 ''}
Adrià Vilanova Martínezf276ac72022-10-13 22:16:22 +020085 </div>
Adrià Vilanova Martínez54964a52022-10-26 23:53:29 +020086 ${this.renderActionTitle()}
87 ${
88 !this.readOnly ? html`
89 <button
90 ?disabled=${this.disableRemoveButton}
91 @click=${this._remove}>
92 Remove
93 </button>
94 ` :
95 nothing}
Adrià Vilanova Martínezf276ac72022-10-13 22:16:22 +020096 </div>
Adrià Vilanova Martínez54964a52022-10-26 23:53:29 +020097 ${this.renderSpecificActionEditor()}
98 </div>
99 `;
Adrià Vilanova Martínezf276ac72022-10-13 22:16:22 +0200100 }
101
102 checkValidity() {
103 if (this.readOnly || !kSupportedActions.has(this._actionCase)) return true;
Adrià Vilanova Martínez6c4739a2022-11-07 00:11:53 +0100104
105 const s = this._specificActionEditor();
106 if (!s) return true;
107
Adrià Vilanova Martínezf276ac72022-10-13 22:16:22 +0200108 return this._specificActionEditor().checkValidity();
109 }
110
111 _actionCaseChanged() {
112 this._actionCaseString = this.selectRef.value.value;
113 }
114
115 _dispatchUpdateEvent() {
116 // Transmit to other components that the action has changed
117 const e = new Event('action-updated', {bubbles: true, composed: true});
118 this.renderRoot.dispatchEvent(e);
119 }
120
121 _remove() {
122 // Transmit to other components that the action has to be removed
123 const e = new Event('action-removed', {bubbles: true, composed: true});
124 this.renderRoot.dispatchEvent(e);
125 }
126
127 _stepTitle() {
128 return kActionHeadings[this._actionCase] ?? this._actionCase;
129 }
130
131 get _actionCase() {
132 return this.action.getActionCase();
133 }
134
135 set _actionCase(newCase) {
136 let value;
137 switch (newCase) {
138 case pb.workflows.Action.ActionCase.REPLY_ACTION:
139 value = new pb.workflows.Action.ReplyAction;
140 this.action.setReplyAction(value);
141 break;
142 case pb.workflows.Action.ActionCase.MOVE_ACTION:
143 value = new pb.workflows.Action.MoveAction;
144 this.action.setMoveAction(value);
145 break;
146 case pb.workflows.Action.ActionCase.MARK_DUPLICATE_ACTION:
147 value = new pb.workflows.Action.MarkDuplicateAction;
148 this.action.setMarkDuplicateAction(value);
149 break;
150 case pb.workflows.Action.ActionCase.UNMARK_DUPLICATE_ACTION:
151 value = new pb.workflows.Action.UnmarkDuplicateAction;
152 this.action.setUnmarkDuplicateAction(value);
153 break;
154 case pb.workflows.Action.ActionCase.ATTRIBUTE_ACTION:
155 value = new pb.workflows.Action.AttributeAction;
156 this.action.setAttributeAction(value);
157 break;
158 case pb.workflows.Action.ActionCase.REPLY_WITH_CR_ACTION:
159 value = new pb.workflows.Action.ReplyWithCRAction;
160 this.action.setReplyWithCrAction(value);
161 break;
162 case pb.workflows.Action.ActionCase.STAR_ACTION:
163 value = new pb.workflows.Action.StarAction;
164 this.action.setStarAction(value);
165 break;
166 case pb.workflows.Action.ActionCase.SUBSCRIBE_ACTION:
167 value = new pb.workflows.Action.SubscribeAction;
168 this.action.setSubscribeAction(value);
169 break;
170 case pb.workflows.Action.ActionCase.VOTE_ACTION:
171 value = new pb.workflows.Action.VoteAction;
172 this.action.setVoteAction(value);
173 break;
174 case pb.workflows.Action.ActionCase.REPORT_ACTION:
175 value = new pb.workflows.Action.ReportAction;
176 this.action.setReportAction(value);
177 break;
Adrià Vilanova Martínez6c4739a2022-11-07 00:11:53 +0100178 case pb.workflows.Action.ActionCase.MARK_AS_READ_ACTION:
179 value = new pb.workflows.Action.MarkAsReadAction;
180 this.action.setMarkAsReadAction(value);
181 break;
182 case pb.workflows.Action.ActionCase.MARK_AS_UNREAD_ACTION:
183 value = new pb.workflows.Action.MarkAsUnreadAction;
184 this.action.setMarkAsUnreadAction(value);
185 break;
Adrià Vilanova Martínezf276ac72022-10-13 22:16:22 +0200186 default:
187 this.action.clearReplyAction();
188 this.action.clearMoveAction();
189 this.action.clearMarkDuplicateAction();
190 this.action.clearUnmarkDuplicateAction();
191 this.action.clearAttributeAction();
192 this.action.clearReplyWithCrAction();
193 this.action.clearStarAction();
194 this.action.clearSubscribeAction();
195 this.action.clearVoteAction();
196 this.action.clearReportAction();
Adrià Vilanova Martínez6c4739a2022-11-07 00:11:53 +0100197 this.action.clearMarkAsReadAction();
198 this.action.clearMarkAsUnreadAction();
Adrià Vilanova Martínezf276ac72022-10-13 22:16:22 +0200199 }
200
201 this.requestUpdate();
202 this._dispatchUpdateEvent();
203 }
204
205 // The same as _actionCase, but represented as a String instead of a Number
206 get _actionCaseString() {
207 return this._actionCase.toString();
208 }
209
210 set _actionCaseString(newCase) {
211 this._actionCase = parseInt(newCase);
212 }
213
214 _specificActionEditor() {
215 switch (this._actionCase) {
216 case pb.workflows.Action.ActionCase.REPLY_WITH_CR_ACTION:
217 return this.renderRoot.querySelector('wf-action-reply-with-cr');
218
219 default:
220 return null;
221 }
222 }
223}
224window.customElements.define('wf-action-editor', WFActionEditor);