Dave Borowitz | 8cdc76b | 2018-03-26 10:04:27 -0400 | [diff] [blame] | 1 | /** |
| 2 | * @license |
Ben Rohlfs | 94fcbbc | 2022-05-27 10:45:03 +0200 | [diff] [blame] | 3 | * Copyright 2017 Google LLC |
| 4 | * SPDX-License-Identifier: Apache-2.0 |
Dave Borowitz | 8cdc76b | 2018-03-26 10:04:27 -0400 | [diff] [blame] | 5 | */ |
Milutin Kristofic | ccc164e | 2020-08-13 22:40:40 +0200 | [diff] [blame] | 6 | import '@polymer/iron-autogrow-textarea/iron-autogrow-textarea'; |
Milutin Kristofic | ccc164e | 2020-08-13 22:40:40 +0200 | [diff] [blame] | 7 | import '../../shared/gr-button/gr-button'; |
| 8 | import '../../shared/gr-select/gr-select'; |
Milutin Kristofic | ccc164e | 2020-08-13 22:40:40 +0200 | [diff] [blame] | 9 | import {encodeURL, getBaseUrl} from '../../../utils/url-util'; |
Milutin Kristofic | c1137bb | 2020-08-27 11:23:48 +0200 | [diff] [blame] | 10 | import {AccessPermissionId} from '../../../utils/access-util'; |
Ben Rohlfs | 44f0104 | 2023-02-18 13:27:57 +0100 | [diff] [blame] | 11 | import {fire} from '../../../utils/event-util'; |
Milutin Kristofic | aa1c08b | 2023-09-06 10:34:16 +0200 | [diff] [blame] | 12 | import {grFormStyles} from '../../../styles/gr-form-styles'; |
Paladox none | 83c5fa6 | 2021-11-24 22:26:52 +0000 | [diff] [blame] | 13 | import {sharedStyles} from '../../../styles/shared-styles'; |
| 14 | import {LitElement, PropertyValues, html, css} from 'lit'; |
Frank Borden | 42c1a45 | 2022-08-11 16:27:20 +0200 | [diff] [blame] | 15 | import {customElement, property, state} from 'lit/decorators.js'; |
Ben Rohlfs | 6bb9053 | 2023-02-17 18:55:56 +0100 | [diff] [blame] | 16 | import {BindValueChangeEvent, ValueChangedEvent} from '../../../types/events'; |
Frank Borden | 42c1a45 | 2022-08-11 16:27:20 +0200 | [diff] [blame] | 17 | import {ifDefined} from 'lit/directives/if-defined.js'; |
Ben Rohlfs | c3ce140 | 2022-02-22 10:35:34 +0100 | [diff] [blame] | 18 | import {EditablePermissionRuleInfo} from '../gr-repo-access/gr-repo-access-interfaces'; |
| 19 | import {PermissionAction} from '../../../constants/constants'; |
Milutin Kristofic | aa1c08b | 2023-09-06 10:34:16 +0200 | [diff] [blame] | 20 | import {formStyles} from '../../../styles/form-styles'; |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 21 | |
Ben Rohlfs | c3ce140 | 2022-02-22 10:35:34 +0100 | [diff] [blame] | 22 | const PRIORITY_OPTIONS = [PermissionAction.BATCH, PermissionAction.INTERACTIVE]; |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 23 | |
| 24 | const Action = { |
Ben Rohlfs | c3ce140 | 2022-02-22 10:35:34 +0100 | [diff] [blame] | 25 | ALLOW: PermissionAction.ALLOW, |
| 26 | DENY: PermissionAction.DENY, |
| 27 | BLOCK: PermissionAction.BLOCK, |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 28 | }; |
| 29 | |
Ben Rohlfs | c3ce140 | 2022-02-22 10:35:34 +0100 | [diff] [blame] | 30 | const DROPDOWN_OPTIONS = [ |
| 31 | PermissionAction.ALLOW, |
| 32 | PermissionAction.DENY, |
| 33 | PermissionAction.BLOCK, |
| 34 | ]; |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 35 | |
| 36 | const ForcePushOptions = { |
| 37 | ALLOW: [ |
| 38 | {name: 'Allow pushing (but not force pushing)', value: false}, |
| 39 | {name: 'Allow pushing with or without force', value: true}, |
| 40 | ], |
| 41 | BLOCK: [ |
| 42 | {name: 'Block pushing with or without force', value: false}, |
| 43 | {name: 'Block force pushing', value: true}, |
| 44 | ], |
| 45 | }; |
| 46 | |
| 47 | const FORCE_EDIT_OPTIONS = [ |
| 48 | { |
| 49 | name: 'No Force Edit', |
| 50 | value: false, |
| 51 | }, |
| 52 | { |
| 53 | name: 'Force Edit', |
| 54 | value: true, |
| 55 | }, |
| 56 | ]; |
| 57 | |
Ben Rohlfs | c3ce140 | 2022-02-22 10:35:34 +0100 | [diff] [blame] | 58 | type Rule = {value?: EditablePermissionRuleInfo}; |
Becky Siegel | 7c57cf9 | 2018-04-23 14:44:32 -0700 | [diff] [blame] | 59 | |
Milutin Kristofic | ccc164e | 2020-08-13 22:40:40 +0200 | [diff] [blame] | 60 | interface RuleLabel { |
| 61 | values: RuleLabelValue[]; |
| 62 | } |
Becky Siegel | 7198afd | 2017-08-15 17:49:30 -0700 | [diff] [blame] | 63 | |
Milutin Kristofic | ccc164e | 2020-08-13 22:40:40 +0200 | [diff] [blame] | 64 | interface RuleLabelValue { |
| 65 | value: number; |
| 66 | text: string; |
| 67 | } |
| 68 | |
| 69 | declare global { |
| 70 | interface HTMLElementTagNameMap { |
| 71 | 'gr-rule-editor': GrRuleEditor; |
| 72 | } |
Ben Rohlfs | 6bb9053 | 2023-02-17 18:55:56 +0100 | [diff] [blame] | 73 | interface HTMLElementEventMap { |
Ben Rohlfs | 5b3c655 | 2023-02-18 13:02:46 +0100 | [diff] [blame] | 74 | /** Fired when a rule that was previously added was removed. */ |
| 75 | 'added-rule-removed': CustomEvent<{}>; |
Ben Rohlfs | 6bb9053 | 2023-02-17 18:55:56 +0100 | [diff] [blame] | 76 | 'rule-changed': ValueChangedEvent<Rule | undefined>; |
| 77 | } |
Milutin Kristofic | ccc164e | 2020-08-13 22:40:40 +0200 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | @customElement('gr-rule-editor') |
Paladox none | 83c5fa6 | 2021-11-24 22:26:52 +0000 | [diff] [blame] | 81 | export class GrRuleEditor extends LitElement { |
Milutin Kristofic | ccc164e | 2020-08-13 22:40:40 +0200 | [diff] [blame] | 82 | @property({type: Boolean}) |
| 83 | hasRange?: boolean; |
| 84 | |
| 85 | @property({type: Object}) |
| 86 | label?: RuleLabel; |
| 87 | |
Paladox none | 83c5fa6 | 2021-11-24 22:26:52 +0000 | [diff] [blame] | 88 | @property({type: Boolean}) |
Milutin Kristofic | ccc164e | 2020-08-13 22:40:40 +0200 | [diff] [blame] | 89 | editing = false; |
| 90 | |
| 91 | @property({type: String}) |
| 92 | groupId?: string; |
| 93 | |
| 94 | @property({type: String}) |
| 95 | groupName?: string; |
| 96 | |
| 97 | // This is required value for this component |
| 98 | @property({type: String}) |
Milutin Kristofic | c1137bb | 2020-08-27 11:23:48 +0200 | [diff] [blame] | 99 | permission!: AccessPermissionId; |
Milutin Kristofic | ccc164e | 2020-08-13 22:40:40 +0200 | [diff] [blame] | 100 | |
Paladox none | 83c5fa6 | 2021-11-24 22:26:52 +0000 | [diff] [blame] | 101 | @property({type: Object}) |
Milutin Kristofic | ccc164e | 2020-08-13 22:40:40 +0200 | [diff] [blame] | 102 | rule?: Rule; |
| 103 | |
| 104 | @property({type: String}) |
| 105 | section?: string; |
| 106 | |
Paladox none | 83c5fa6 | 2021-11-24 22:26:52 +0000 | [diff] [blame] | 107 | // private but used in test |
| 108 | @state() deleted = false; |
Milutin Kristofic | ccc164e | 2020-08-13 22:40:40 +0200 | [diff] [blame] | 109 | |
Paladox none | 83c5fa6 | 2021-11-24 22:26:52 +0000 | [diff] [blame] | 110 | // private but used in test |
Ben Rohlfs | c3ce140 | 2022-02-22 10:35:34 +0100 | [diff] [blame] | 111 | @state() originalRuleValues?: EditablePermissionRuleInfo; |
Wyatt Allen | b38a1be | 2018-06-27 09:59:04 -0700 | [diff] [blame] | 112 | |
Ben Rohlfs | f7f1e8e | 2021-03-12 14:36:40 +0100 | [diff] [blame] | 113 | constructor() { |
| 114 | super(); |
Paladox none | 83c5fa6 | 2021-11-24 22:26:52 +0000 | [diff] [blame] | 115 | this.addEventListener('access-saved', () => this.handleAccessSaved()); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 116 | } |
Becky Siegel | 7198afd | 2017-08-15 17:49:30 -0700 | [diff] [blame] | 117 | |
Chris Poucet | 59dad57 | 2021-08-20 15:25:36 +0000 | [diff] [blame] | 118 | override connectedCallback() { |
Ben Rohlfs | 5f520da | 2021-03-10 14:58:43 +0100 | [diff] [blame] | 119 | super.connectedCallback(); |
Paladox none | 83c5fa6 | 2021-11-24 22:26:52 +0000 | [diff] [blame] | 120 | if (this.rule) { |
| 121 | this.setupValues(); |
| 122 | } |
Milutin Kristofic | ccc164e | 2020-08-13 22:40:40 +0200 | [diff] [blame] | 123 | // Check needed for test purposes. |
Paladox none | 83c5fa6 | 2021-11-24 22:26:52 +0000 | [diff] [blame] | 124 | if (!this.originalRuleValues && this.rule) { |
| 125 | this.setOriginalRuleValues(); |
Dmitrii Filippov | 3fd2b10 | 2019-11-15 16:16:46 +0100 | [diff] [blame] | 126 | } |
Dmitrii Filippov | 3fd2b10 | 2019-11-15 16:16:46 +0100 | [diff] [blame] | 127 | } |
| 128 | |
Paladox none | 83c5fa6 | 2021-11-24 22:26:52 +0000 | [diff] [blame] | 129 | static override get styles() { |
| 130 | return [ |
Milutin Kristofic | aa1c08b | 2023-09-06 10:34:16 +0200 | [diff] [blame] | 131 | grFormStyles, |
Paladox none | 83c5fa6 | 2021-11-24 22:26:52 +0000 | [diff] [blame] | 132 | formStyles, |
| 133 | sharedStyles, |
| 134 | css` |
| 135 | :host { |
| 136 | border-bottom: 1px solid var(--border-color); |
| 137 | padding: var(--spacing-m); |
| 138 | display: block; |
| 139 | } |
| 140 | #removeBtn { |
| 141 | display: none; |
| 142 | } |
| 143 | .editing #removeBtn { |
| 144 | display: flex; |
| 145 | } |
| 146 | #options { |
| 147 | align-items: baseline; |
| 148 | display: flex; |
| 149 | } |
| 150 | #options > * { |
| 151 | margin-right: var(--spacing-m); |
| 152 | } |
| 153 | #mainContainer { |
| 154 | align-items: baseline; |
| 155 | display: flex; |
| 156 | flex-wrap: nowrap; |
| 157 | justify-content: space-between; |
| 158 | } |
| 159 | #deletedContainer.deleted { |
| 160 | align-items: baseline; |
| 161 | display: flex; |
| 162 | justify-content: space-between; |
| 163 | } |
| 164 | #undoBtn, |
| 165 | #force, |
| 166 | #deletedContainer, |
| 167 | #mainContainer.deleted { |
| 168 | display: none; |
| 169 | } |
| 170 | #undoBtn.modified, |
| 171 | #force.force { |
| 172 | display: block; |
| 173 | } |
| 174 | .groupPath { |
| 175 | color: var(--deemphasized-text-color); |
| 176 | } |
| 177 | iron-autogrow-textarea { |
| 178 | width: 14em; |
| 179 | } |
| 180 | `, |
| 181 | ]; |
| 182 | } |
| 183 | |
| 184 | override render() { |
| 185 | return html` |
| 186 | <div |
| 187 | id="mainContainer" |
| 188 | class="gr-form-styles ${this.computeSectionClass()}" |
| 189 | > |
| 190 | <div id="options"> |
| 191 | <gr-select |
| 192 | id="action" |
| 193 | .bindValue=${this.rule?.value?.action} |
| 194 | @bind-value-changed=${(e: BindValueChangeEvent) => { |
| 195 | this.handleActionBindValueChanged(e); |
| 196 | }} |
| 197 | > |
| 198 | <select ?disabled=${!this.editing}> |
| 199 | ${this.computeOptions().map( |
| 200 | item => html` <option value=${item}>${item}</option> ` |
| 201 | )} |
| 202 | </select> |
| 203 | </gr-select> |
| 204 | ${this.renderMinAndMaxLabel()} ${this.renderMinAndMaxInput()} |
Milutin Kristofic | e0503d5 | 2022-01-11 12:17:50 +0100 | [diff] [blame] | 205 | <a |
| 206 | class="groupPath" |
Ben Rohlfs | 8003bd3 | 2022-04-05 18:24:42 +0200 | [diff] [blame] | 207 | href=${ifDefined(this.computeGroupPath(this.groupId))} |
Milutin Kristofic | e0503d5 | 2022-01-11 12:17:50 +0100 | [diff] [blame] | 208 | > |
Paladox none | 83c5fa6 | 2021-11-24 22:26:52 +0000 | [diff] [blame] | 209 | ${this.groupName} |
| 210 | </a> |
| 211 | <gr-select |
| 212 | id="force" |
Ben Rohlfs | 8003bd3 | 2022-04-05 18:24:42 +0200 | [diff] [blame] | 213 | class=${this.computeForce(this.rule?.value?.action) ? 'force' : ''} |
Paladox none | 83c5fa6 | 2021-11-24 22:26:52 +0000 | [diff] [blame] | 214 | .bindValue=${this.rule?.value?.force} |
| 215 | @bind-value-changed=${(e: BindValueChangeEvent) => { |
| 216 | this.handleForceBindValueChanged(e); |
| 217 | }} |
| 218 | > |
| 219 | <select ?disabled=${!this.editing}> |
| 220 | ${this.computeForceOptions(this.rule?.value?.action).map( |
| 221 | item => html` |
Ben Rohlfs | c3ce140 | 2022-02-22 10:35:34 +0100 | [diff] [blame] | 222 | <option value=${item.value}>${item.name}</option> |
Paladox none | 83c5fa6 | 2021-11-24 22:26:52 +0000 | [diff] [blame] | 223 | ` |
| 224 | )} |
| 225 | </select> |
| 226 | </gr-select> |
| 227 | </div> |
| 228 | <gr-button |
| 229 | link |
| 230 | id="removeBtn" |
| 231 | @click=${() => { |
| 232 | this.handleRemoveRule(); |
| 233 | }} |
| 234 | >Remove</gr-button |
| 235 | > |
| 236 | </div> |
| 237 | <div |
| 238 | id="deletedContainer" |
| 239 | class="gr-form-styles ${this.computeSectionClass()}" |
| 240 | > |
| 241 | ${this.groupName} was deleted |
| 242 | <gr-button |
| 243 | link |
| 244 | id="undoRemoveBtn" |
| 245 | @click=${() => { |
| 246 | this.handleUndoRemove(); |
| 247 | }} |
| 248 | >Undo</gr-button |
| 249 | > |
| 250 | </div> |
| 251 | `; |
| 252 | } |
| 253 | |
| 254 | private renderMinAndMaxLabel() { |
| 255 | if (!this.label) return; |
| 256 | |
| 257 | return html` |
| 258 | <gr-select |
| 259 | id="labelMin" |
| 260 | .bindValue=${this.rule?.value?.min} |
| 261 | @bind-value-changed=${(e: BindValueChangeEvent) => { |
| 262 | this.handleMinBindValueChanged(e); |
| 263 | }} |
| 264 | > |
| 265 | <select ?disabled=${!this.editing}> |
| 266 | ${this.label.values.map( |
| 267 | item => html` <option value=${item.value}>${item.value}</option> ` |
| 268 | )} |
| 269 | </select> |
| 270 | </gr-select> |
| 271 | <gr-select |
| 272 | id="labelMax" |
| 273 | .bindValue=${this.rule?.value?.max} |
| 274 | @bind-value-changed=${(e: BindValueChangeEvent) => { |
| 275 | this.handleMaxBindValueChanged(e); |
| 276 | }} |
| 277 | > |
| 278 | <select ?disabled=${!this.editing}> |
| 279 | ${this.label.values.map( |
| 280 | item => html` <option value=${item.value}>${item.value}</option> ` |
| 281 | )} |
| 282 | </select> |
| 283 | </gr-select> |
| 284 | `; |
| 285 | } |
| 286 | |
| 287 | private renderMinAndMaxInput() { |
| 288 | if (!this.hasRange) return; |
| 289 | |
| 290 | return html` |
| 291 | <iron-autogrow-textarea |
| 292 | id="minInput" |
| 293 | class="min" |
| 294 | autocomplete="on" |
| 295 | placeholder="Min value" |
| 296 | .bindValue=${this.rule?.value?.min} |
| 297 | ?disabled=${!this.editing} |
| 298 | @bind-value-changed=${(e: BindValueChangeEvent) => { |
| 299 | this.handleMinBindValueChanged(e); |
| 300 | }} |
| 301 | ></iron-autogrow-textarea> |
| 302 | <iron-autogrow-textarea |
| 303 | id="maxInput" |
| 304 | class="max" |
| 305 | autocomplete="on" |
| 306 | placeholder="Max value" |
| 307 | .bindValue=${this.rule?.value?.max} |
| 308 | ?disabled=${!this.editing} |
| 309 | @bind-value-changed=${(e: BindValueChangeEvent) => { |
| 310 | this.handleMaxBindValueChanged(e); |
| 311 | }} |
| 312 | ></iron-autogrow-textarea> |
| 313 | `; |
| 314 | } |
| 315 | |
| 316 | override willUpdate(changedProperties: PropertyValues) { |
| 317 | if (changedProperties.has('editing')) { |
| 318 | this.handleEditingChanged(changedProperties.get('editing') as boolean); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 319 | } |
| 320 | } |
| 321 | |
Paladox none | 83c5fa6 | 2021-11-24 22:26:52 +0000 | [diff] [blame] | 322 | // private but used in test |
| 323 | setupValues() { |
| 324 | if (!this.rule?.value) { |
| 325 | this.setDefaultRuleValues(); |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | // private but used in test |
| 330 | computeForce(action?: string) { |
| 331 | if (AccessPermissionId.PUSH === this.permission && action !== Action.DENY) { |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 332 | return true; |
| 333 | } |
| 334 | |
Paladox none | 83c5fa6 | 2021-11-24 22:26:52 +0000 | [diff] [blame] | 335 | return AccessPermissionId.EDIT_TOPIC_NAME === this.permission; |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 336 | } |
| 337 | |
Paladox none | 83c5fa6 | 2021-11-24 22:26:52 +0000 | [diff] [blame] | 338 | // private but used in test |
| 339 | computeGroupPath(groupId?: string) { |
| 340 | if (!groupId) return; |
Ben Rohlfs | c02facb | 2023-01-27 18:46:02 +0100 | [diff] [blame] | 341 | return `${getBaseUrl()}/admin/groups/${encodeURL(groupId)}`; |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 342 | } |
| 343 | |
Paladox none | 83c5fa6 | 2021-11-24 22:26:52 +0000 | [diff] [blame] | 344 | // private but used in test |
| 345 | handleAccessSaved() { |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 346 | // Set a new 'original' value to keep track of after the value has been |
| 347 | // saved. |
Paladox none | 83c5fa6 | 2021-11-24 22:26:52 +0000 | [diff] [blame] | 348 | this.setOriginalRuleValues(); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 349 | } |
| 350 | |
Paladox none | 83c5fa6 | 2021-11-24 22:26:52 +0000 | [diff] [blame] | 351 | private handleEditingChanged(editingOld: boolean) { |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 352 | // Ignore when editing gets set initially. |
Milutin Kristofic | ccc164e | 2020-08-13 22:40:40 +0200 | [diff] [blame] | 353 | if (!editingOld) { |
| 354 | return; |
| 355 | } |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 356 | // Restore original values if no longer editing. |
Paladox none | 83c5fa6 | 2021-11-24 22:26:52 +0000 | [diff] [blame] | 357 | if (!this.editing) { |
| 358 | this.handleUndoChange(); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 359 | } |
| 360 | } |
| 361 | |
Paladox none | 83c5fa6 | 2021-11-24 22:26:52 +0000 | [diff] [blame] | 362 | // private but used in test |
| 363 | computeSectionClass() { |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 364 | const classList = []; |
Paladox none | 83c5fa6 | 2021-11-24 22:26:52 +0000 | [diff] [blame] | 365 | if (this.editing) { |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 366 | classList.push('editing'); |
| 367 | } |
Paladox none | 83c5fa6 | 2021-11-24 22:26:52 +0000 | [diff] [blame] | 368 | if (this.deleted) { |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 369 | classList.push('deleted'); |
| 370 | } |
| 371 | return classList.join(' '); |
| 372 | } |
| 373 | |
Paladox none | 83c5fa6 | 2021-11-24 22:26:52 +0000 | [diff] [blame] | 374 | // private but used in test |
| 375 | computeForceOptions(action?: string) { |
| 376 | if (this.permission === AccessPermissionId.PUSH) { |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 377 | if (action === Action.ALLOW) { |
| 378 | return ForcePushOptions.ALLOW; |
| 379 | } else if (action === Action.BLOCK) { |
| 380 | return ForcePushOptions.BLOCK; |
| 381 | } else { |
| 382 | return []; |
| 383 | } |
Paladox none | 83c5fa6 | 2021-11-24 22:26:52 +0000 | [diff] [blame] | 384 | } else if (this.permission === AccessPermissionId.EDIT_TOPIC_NAME) { |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 385 | return FORCE_EDIT_OPTIONS; |
| 386 | } |
| 387 | return []; |
| 388 | } |
| 389 | |
Paladox none | 83c5fa6 | 2021-11-24 22:26:52 +0000 | [diff] [blame] | 390 | // private but used in test |
Ben Rohlfs | c3ce140 | 2022-02-22 10:35:34 +0100 | [diff] [blame] | 391 | getDefaultRuleValues(): EditablePermissionRuleInfo { |
Paladox none | 83c5fa6 | 2021-11-24 22:26:52 +0000 | [diff] [blame] | 392 | if (this.permission === AccessPermissionId.PRIORITY) { |
Ben Rohlfs | c3ce140 | 2022-02-22 10:35:34 +0100 | [diff] [blame] | 393 | return {action: PRIORITY_OPTIONS[0]}; |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 394 | } |
Ben Rohlfs | c3ce140 | 2022-02-22 10:35:34 +0100 | [diff] [blame] | 395 | if (this.label) { |
| 396 | return { |
| 397 | action: DROPDOWN_OPTIONS[0], |
| 398 | min: this.label.values[0].value, |
| 399 | max: this.label.values[this.label.values.length - 1].value, |
| 400 | }; |
| 401 | } |
| 402 | if (this.computeForce(Action.ALLOW)) { |
| 403 | return { |
| 404 | action: DROPDOWN_OPTIONS[0], |
| 405 | force: this.computeForceOptions(Action.ALLOW)[0].value, |
| 406 | }; |
| 407 | } |
| 408 | return {action: DROPDOWN_OPTIONS[0]}; |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 409 | } |
| 410 | |
Paladox none | 83c5fa6 | 2021-11-24 22:26:52 +0000 | [diff] [blame] | 411 | // private but used in test |
| 412 | setDefaultRuleValues() { |
| 413 | this.rule!.value = this.getDefaultRuleValues(); |
| 414 | |
| 415 | this.handleRuleChange(); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 416 | } |
| 417 | |
Paladox none | 83c5fa6 | 2021-11-24 22:26:52 +0000 | [diff] [blame] | 418 | // private but used in test |
| 419 | computeOptions() { |
| 420 | if (this.permission === 'priority') { |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 421 | return PRIORITY_OPTIONS; |
| 422 | } |
| 423 | return DROPDOWN_OPTIONS; |
| 424 | } |
| 425 | |
Paladox none | 83c5fa6 | 2021-11-24 22:26:52 +0000 | [diff] [blame] | 426 | private handleRemoveRule() { |
Paladox none | 4c88fce | 2021-11-24 20:36:22 +0000 | [diff] [blame] | 427 | if (!this.rule?.value) return; |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 428 | if (this.rule.value.added) { |
Ben Rohlfs | 44f0104 | 2023-02-18 13:27:57 +0100 | [diff] [blame] | 429 | fire(this, 'added-rule-removed', {}); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 430 | } |
Paladox none | 83c5fa6 | 2021-11-24 22:26:52 +0000 | [diff] [blame] | 431 | this.deleted = true; |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 432 | this.rule.value.deleted = true; |
Paladox none | 83c5fa6 | 2021-11-24 22:26:52 +0000 | [diff] [blame] | 433 | |
| 434 | this.handleRuleChange(); |
| 435 | |
Ben Rohlfs | 44f0104 | 2023-02-18 13:27:57 +0100 | [diff] [blame] | 436 | fire(this, 'access-modified', {}); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 437 | } |
| 438 | |
Paladox none | 83c5fa6 | 2021-11-24 22:26:52 +0000 | [diff] [blame] | 439 | private handleUndoRemove() { |
Paladox none | 4c88fce | 2021-11-24 20:36:22 +0000 | [diff] [blame] | 440 | if (!this.rule?.value) return; |
Paladox none | 83c5fa6 | 2021-11-24 22:26:52 +0000 | [diff] [blame] | 441 | this.deleted = false; |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 442 | delete this.rule.value.deleted; |
Paladox none | 83c5fa6 | 2021-11-24 22:26:52 +0000 | [diff] [blame] | 443 | |
| 444 | this.handleRuleChange(); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 445 | } |
| 446 | |
Paladox none | 83c5fa6 | 2021-11-24 22:26:52 +0000 | [diff] [blame] | 447 | private handleUndoChange() { |
Ben Rohlfs | c3ce140 | 2022-02-22 10:35:34 +0100 | [diff] [blame] | 448 | if (!this.originalRuleValues || !this.rule?.value) { |
| 449 | return; |
| 450 | } |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 451 | // gr-permission will take care of removing rules that were added but |
| 452 | // unsaved. We need to keep the added bit for the filter. |
Milutin Kristofic | ccc164e | 2020-08-13 22:40:40 +0200 | [diff] [blame] | 453 | if (this.rule.value.added) { |
| 454 | return; |
| 455 | } |
Paladox none | 83c5fa6 | 2021-11-24 22:26:52 +0000 | [diff] [blame] | 456 | this.rule.value = {...this.originalRuleValues}; |
| 457 | this.deleted = false; |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 458 | delete this.rule.value.deleted; |
| 459 | delete this.rule.value.modified; |
Paladox none | 83c5fa6 | 2021-11-24 22:26:52 +0000 | [diff] [blame] | 460 | |
| 461 | this.handleRuleChange(); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 462 | } |
| 463 | |
Paladox none | 83c5fa6 | 2021-11-24 22:26:52 +0000 | [diff] [blame] | 464 | // private but used in test |
| 465 | handleValueChange() { |
| 466 | if (!this.originalRuleValues || !this.rule?.value) { |
Milutin Kristofic | ccc164e | 2020-08-13 22:40:40 +0200 | [diff] [blame] | 467 | return; |
| 468 | } |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 469 | this.rule.value.modified = true; |
Paladox none | 83c5fa6 | 2021-11-24 22:26:52 +0000 | [diff] [blame] | 470 | |
| 471 | this.handleRuleChange(); |
| 472 | |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 473 | // Allows overall access page to know a change has been made. |
Ben Rohlfs | 44f0104 | 2023-02-18 13:27:57 +0100 | [diff] [blame] | 474 | fire(this, 'access-modified', {}); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 475 | } |
| 476 | |
Paladox none | 83c5fa6 | 2021-11-24 22:26:52 +0000 | [diff] [blame] | 477 | // private but used in test |
| 478 | setOriginalRuleValues() { |
| 479 | if (!this.rule?.value) return; |
Ben Rohlfs | c3ce140 | 2022-02-22 10:35:34 +0100 | [diff] [blame] | 480 | this.originalRuleValues = {...this.rule.value}; |
Paladox none | 83c5fa6 | 2021-11-24 22:26:52 +0000 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | private handleActionBindValueChanged(e: BindValueChangeEvent) { |
| 484 | if ( |
| 485 | !this.rule?.value || |
| 486 | e.detail.value === undefined || |
| 487 | this.rule.value.action === String(e.detail.value) |
| 488 | ) |
| 489 | return; |
| 490 | |
Ben Rohlfs | c3ce140 | 2022-02-22 10:35:34 +0100 | [diff] [blame] | 491 | this.rule.value.action = String(e.detail.value) as PermissionAction; |
Paladox none | 83c5fa6 | 2021-11-24 22:26:52 +0000 | [diff] [blame] | 492 | |
| 493 | this.handleValueChange(); |
| 494 | } |
| 495 | |
| 496 | private handleMinBindValueChanged(e: BindValueChangeEvent) { |
| 497 | if ( |
| 498 | !this.rule?.value || |
| 499 | e.detail.value === undefined || |
| 500 | this.rule.value.min === Number(e.detail.value) |
| 501 | ) |
| 502 | return; |
| 503 | this.rule.value.min = Number(e.detail.value); |
| 504 | |
| 505 | this.handleValueChange(); |
| 506 | } |
| 507 | |
| 508 | private handleMaxBindValueChanged(e: BindValueChangeEvent) { |
| 509 | if ( |
| 510 | !this.rule?.value || |
| 511 | e.detail.value === undefined || |
| 512 | this.rule.value.max === Number(e.detail.value) |
| 513 | ) |
| 514 | return; |
| 515 | this.rule.value.max = Number(e.detail.value); |
| 516 | |
| 517 | this.handleValueChange(); |
| 518 | } |
| 519 | |
| 520 | private handleForceBindValueChanged(e: BindValueChangeEvent) { |
| 521 | const forceValue = String(e.detail.value) === 'true' ? true : false; |
| 522 | if ( |
| 523 | !this.rule?.value || |
| 524 | e.detail.value === undefined || |
| 525 | this.rule.value.force === forceValue |
| 526 | ) |
| 527 | return; |
| 528 | this.rule.value.force = forceValue; |
| 529 | |
| 530 | this.handleValueChange(); |
| 531 | } |
| 532 | |
| 533 | private handleRuleChange() { |
| 534 | this.requestUpdate('rule'); |
Ben Rohlfs | 6bb9053 | 2023-02-17 18:55:56 +0100 | [diff] [blame] | 535 | fire(this, 'rule-changed', {value: this.rule}); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 536 | } |
| 537 | } |