blob: cdcb85acea565ced8b09fc4960640efa177c72b2 [file] [log] [blame]
Adrià Vilanova Martínez78dcfdf2024-02-26 02:02:22 +01001import './actions/Attribute.js';
Adrià Vilanova Martínezf276ac72022-10-13 22:16:22 +02002import './actions/ReplyWithCR.js';
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ínez78dcfdf2024-02-26 02:02:22 +010064 case pb.workflows.Action.ActionCase.ATTRIBUTE_ACTION:
65 return html`
66 <wf-action-attribute
67 ?readOnly=${this.readOnly}
68 .action=${this.action.getAttributeAction()}>
69 </wf-action-attribute>
70 `;
71
Adrià Vilanova Martínez6c4739a2022-11-07 00:11:53 +010072 case pb.workflows.Action.ActionCase.MARK_AS_READ_ACTION:
73 case pb.workflows.Action.ActionCase.MARK_AS_UNREAD_ACTION:
74 return nothing;
75
Adrià Vilanova Martínezf276ac72022-10-13 22:16:22 +020076 default:
77 return html`<p>This action has not yet been implemented.</p>`;
78 }
79 }
80
81 render() {
Adrià Vilanova Martínez54964a52022-10-26 23:53:29 +020082 let actionClass = '';
83 if (this.readOnly && this.status) actionClass = 'action--' + this.status;
84 return html`
85 <div class="action ${actionClass}">
86 <div class="header">
87 <div class="step">
88 ${this.step}
Adrià Vilanova Martínezf276ac72022-10-13 22:16:22 +020089 ${
Adrià Vilanova Martínez54964a52022-10-26 23:53:29 +020090 this.status == 'running' ?
91 html`<mwc-circular-progress indeterminate density="-1"></mwc-circular-progress>` :
92 ''}
Adrià Vilanova Martínezf276ac72022-10-13 22:16:22 +020093 </div>
Adrià Vilanova Martínez54964a52022-10-26 23:53:29 +020094 ${this.renderActionTitle()}
95 ${
96 !this.readOnly ? html`
97 <button
98 ?disabled=${this.disableRemoveButton}
99 @click=${this._remove}>
100 Remove
101 </button>
102 ` :
103 nothing}
Adrià Vilanova Martínezf276ac72022-10-13 22:16:22 +0200104 </div>
Adrià Vilanova Martínez54964a52022-10-26 23:53:29 +0200105 ${this.renderSpecificActionEditor()}
106 </div>
107 `;
Adrià Vilanova Martínezf276ac72022-10-13 22:16:22 +0200108 }
109
110 checkValidity() {
111 if (this.readOnly || !kSupportedActions.has(this._actionCase)) return true;
Adrià Vilanova Martínez6c4739a2022-11-07 00:11:53 +0100112
113 const s = this._specificActionEditor();
114 if (!s) return true;
115
Adrià Vilanova Martínezf276ac72022-10-13 22:16:22 +0200116 return this._specificActionEditor().checkValidity();
117 }
118
119 _actionCaseChanged() {
120 this._actionCaseString = this.selectRef.value.value;
121 }
122
123 _dispatchUpdateEvent() {
124 // Transmit to other components that the action has changed
125 const e = new Event('action-updated', {bubbles: true, composed: true});
126 this.renderRoot.dispatchEvent(e);
127 }
128
129 _remove() {
130 // Transmit to other components that the action has to be removed
131 const e = new Event('action-removed', {bubbles: true, composed: true});
132 this.renderRoot.dispatchEvent(e);
133 }
134
135 _stepTitle() {
136 return kActionHeadings[this._actionCase] ?? this._actionCase;
137 }
138
139 get _actionCase() {
140 return this.action.getActionCase();
141 }
142
143 set _actionCase(newCase) {
144 let value;
145 switch (newCase) {
146 case pb.workflows.Action.ActionCase.REPLY_ACTION:
147 value = new pb.workflows.Action.ReplyAction;
148 this.action.setReplyAction(value);
149 break;
150 case pb.workflows.Action.ActionCase.MOVE_ACTION:
151 value = new pb.workflows.Action.MoveAction;
152 this.action.setMoveAction(value);
153 break;
154 case pb.workflows.Action.ActionCase.MARK_DUPLICATE_ACTION:
155 value = new pb.workflows.Action.MarkDuplicateAction;
156 this.action.setMarkDuplicateAction(value);
157 break;
158 case pb.workflows.Action.ActionCase.UNMARK_DUPLICATE_ACTION:
159 value = new pb.workflows.Action.UnmarkDuplicateAction;
160 this.action.setUnmarkDuplicateAction(value);
161 break;
162 case pb.workflows.Action.ActionCase.ATTRIBUTE_ACTION:
163 value = new pb.workflows.Action.AttributeAction;
164 this.action.setAttributeAction(value);
165 break;
166 case pb.workflows.Action.ActionCase.REPLY_WITH_CR_ACTION:
167 value = new pb.workflows.Action.ReplyWithCRAction;
168 this.action.setReplyWithCrAction(value);
169 break;
170 case pb.workflows.Action.ActionCase.STAR_ACTION:
171 value = new pb.workflows.Action.StarAction;
172 this.action.setStarAction(value);
173 break;
174 case pb.workflows.Action.ActionCase.SUBSCRIBE_ACTION:
175 value = new pb.workflows.Action.SubscribeAction;
176 this.action.setSubscribeAction(value);
177 break;
178 case pb.workflows.Action.ActionCase.VOTE_ACTION:
179 value = new pb.workflows.Action.VoteAction;
180 this.action.setVoteAction(value);
181 break;
182 case pb.workflows.Action.ActionCase.REPORT_ACTION:
183 value = new pb.workflows.Action.ReportAction;
184 this.action.setReportAction(value);
185 break;
Adrià Vilanova Martínez6c4739a2022-11-07 00:11:53 +0100186 case pb.workflows.Action.ActionCase.MARK_AS_READ_ACTION:
187 value = new pb.workflows.Action.MarkAsReadAction;
188 this.action.setMarkAsReadAction(value);
189 break;
190 case pb.workflows.Action.ActionCase.MARK_AS_UNREAD_ACTION:
191 value = new pb.workflows.Action.MarkAsUnreadAction;
192 this.action.setMarkAsUnreadAction(value);
193 break;
Adrià Vilanova Martínezf276ac72022-10-13 22:16:22 +0200194 default:
195 this.action.clearReplyAction();
196 this.action.clearMoveAction();
197 this.action.clearMarkDuplicateAction();
198 this.action.clearUnmarkDuplicateAction();
199 this.action.clearAttributeAction();
200 this.action.clearReplyWithCrAction();
201 this.action.clearStarAction();
202 this.action.clearSubscribeAction();
203 this.action.clearVoteAction();
204 this.action.clearReportAction();
Adrià Vilanova Martínez6c4739a2022-11-07 00:11:53 +0100205 this.action.clearMarkAsReadAction();
206 this.action.clearMarkAsUnreadAction();
Adrià Vilanova Martínezf276ac72022-10-13 22:16:22 +0200207 }
208
209 this.requestUpdate();
210 this._dispatchUpdateEvent();
211 }
212
213 // The same as _actionCase, but represented as a String instead of a Number
214 get _actionCaseString() {
215 return this._actionCase.toString();
216 }
217
218 set _actionCaseString(newCase) {
219 this._actionCase = parseInt(newCase);
220 }
221
222 _specificActionEditor() {
223 switch (this._actionCase) {
224 case pb.workflows.Action.ActionCase.REPLY_WITH_CR_ACTION:
225 return this.renderRoot.querySelector('wf-action-reply-with-cr');
226
Adrià Vilanova Martínez78dcfdf2024-02-26 02:02:22 +0100227 case pb.workflows.Action.ActionCase.ATTRIBUTE_ACTION:
228 return this.renderRoot.querySelector('wf-action-attribute');
229
Adrià Vilanova Martínezf276ac72022-10-13 22:16:22 +0200230 default:
231 return null;
232 }
233 }
234}
235window.customElements.define('wf-action-editor', WFActionEditor);