Adrià Vilanova Martínez | 78dcfdf | 2024-02-26 02:02:22 +0100 | [diff] [blame] | 1 | import './actions/Attribute.js'; |
Adrià Vilanova Martínez | f276ac7 | 2022-10-13 22:16:22 +0200 | [diff] [blame] | 2 | import './actions/ReplyWithCR.js'; |
Adrià Vilanova Martínez | 54964a5 | 2022-10-26 23:53:29 +0200 | [diff] [blame] | 3 | import '@material/mwc-circular-progress/mwc-circular-progress.js'; |
| 4 | |
| 5 | import {html, LitElement, nothing} from 'lit'; |
Adrià Vilanova Martínez | f276ac7 | 2022-10-13 22:16:22 +0200 | [diff] [blame] | 6 | import {map} from 'lit/directives/map.js'; |
| 7 | import {createRef, ref} from 'lit/directives/ref.js'; |
| 8 | |
| 9 | import * as pb from '../../proto/main_pb.js'; |
Adrià Vilanova Martínez | 54964a5 | 2022-10-26 23:53:29 +0200 | [diff] [blame] | 10 | import {kActionHeadings, kActionStyles, kSupportedActions} from '../shared/actions.js'; |
Adrià Vilanova Martínez | f276ac7 | 2022-10-13 22:16:22 +0200 | [diff] [blame] | 11 | |
Adrià Vilanova Martínez | f276ac7 | 2022-10-13 22:16:22 +0200 | [diff] [blame] | 12 | const actionCases = Object.entries(pb.workflows.Action.ActionCase); |
| 13 | |
| 14 | export 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ínez | 54964a5 | 2022-10-26 23:53:29 +0200 | [diff] [blame] | 20 | status: {type: String}, |
Adrià Vilanova Martínez | f276ac7 | 2022-10-13 22:16:22 +0200 | [diff] [blame] | 21 | }; |
| 22 | |
Adrià Vilanova Martínez | 54964a5 | 2022-10-26 23:53:29 +0200 | [diff] [blame] | 23 | static styles = kActionStyles; |
Adrià Vilanova Martínez | f276ac7 | 2022-10-13 22:16:22 +0200 | [diff] [blame] | 24 | |
| 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ínez | 6d91274 | 2022-10-16 23:57:26 +0200 | [diff] [blame] | 43 | if (!kSupportedActions.has(num)) return nothing; |
Adrià Vilanova Martínez | f276ac7 | 2022-10-13 22:16:22 +0200 | [diff] [blame] | 44 | 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ínez | 78dcfdf | 2024-02-26 02:02:22 +0100 | [diff] [blame] | 64 | 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ínez | 6c4739a | 2022-11-07 00:11:53 +0100 | [diff] [blame] | 72 | 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ínez | f276ac7 | 2022-10-13 22:16:22 +0200 | [diff] [blame] | 76 | default: |
| 77 | return html`<p>This action has not yet been implemented.</p>`; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | render() { |
Adrià Vilanova Martínez | 54964a5 | 2022-10-26 23:53:29 +0200 | [diff] [blame] | 82 | 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ínez | f276ac7 | 2022-10-13 22:16:22 +0200 | [diff] [blame] | 89 | ${ |
Adrià Vilanova Martínez | 54964a5 | 2022-10-26 23:53:29 +0200 | [diff] [blame] | 90 | this.status == 'running' ? |
| 91 | html`<mwc-circular-progress indeterminate density="-1"></mwc-circular-progress>` : |
| 92 | ''} |
Adrià Vilanova Martínez | f276ac7 | 2022-10-13 22:16:22 +0200 | [diff] [blame] | 93 | </div> |
Adrià Vilanova Martínez | 54964a5 | 2022-10-26 23:53:29 +0200 | [diff] [blame] | 94 | ${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ínez | f276ac7 | 2022-10-13 22:16:22 +0200 | [diff] [blame] | 104 | </div> |
Adrià Vilanova Martínez | 54964a5 | 2022-10-26 23:53:29 +0200 | [diff] [blame] | 105 | ${this.renderSpecificActionEditor()} |
| 106 | </div> |
| 107 | `; |
Adrià Vilanova Martínez | f276ac7 | 2022-10-13 22:16:22 +0200 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | checkValidity() { |
| 111 | if (this.readOnly || !kSupportedActions.has(this._actionCase)) return true; |
Adrià Vilanova Martínez | 6c4739a | 2022-11-07 00:11:53 +0100 | [diff] [blame] | 112 | |
| 113 | const s = this._specificActionEditor(); |
| 114 | if (!s) return true; |
| 115 | |
Adrià Vilanova Martínez | f276ac7 | 2022-10-13 22:16:22 +0200 | [diff] [blame] | 116 | 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ínez | 6c4739a | 2022-11-07 00:11:53 +0100 | [diff] [blame] | 186 | 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ínez | f276ac7 | 2022-10-13 22:16:22 +0200 | [diff] [blame] | 194 | 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ínez | 6c4739a | 2022-11-07 00:11:53 +0100 | [diff] [blame] | 205 | this.action.clearMarkAsReadAction(); |
| 206 | this.action.clearMarkAsUnreadAction(); |
Adrià Vilanova Martínez | f276ac7 | 2022-10-13 22:16:22 +0200 | [diff] [blame] | 207 | } |
| 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ínez | 78dcfdf | 2024-02-26 02:02:22 +0100 | [diff] [blame] | 227 | case pb.workflows.Action.ActionCase.ATTRIBUTE_ACTION: |
| 228 | return this.renderRoot.querySelector('wf-action-attribute'); |
| 229 | |
Adrià Vilanova Martínez | f276ac7 | 2022-10-13 22:16:22 +0200 | [diff] [blame] | 230 | default: |
| 231 | return null; |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | window.customElements.define('wf-action-editor', WFActionEditor); |