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