diff options
Diffstat (limited to 'polygerrit-ui/app/elements/admin/gr-rule-editor/gr-rule-editor.js')
-rw-r--r-- | polygerrit-ui/app/elements/admin/gr-rule-editor/gr-rule-editor.js | 158 |
1 files changed, 106 insertions, 52 deletions
diff --git a/polygerrit-ui/app/elements/admin/gr-rule-editor/gr-rule-editor.js b/polygerrit-ui/app/elements/admin/gr-rule-editor/gr-rule-editor.js index 7f1a24541b..06f703f34b 100644 --- a/polygerrit-ui/app/elements/admin/gr-rule-editor/gr-rule-editor.js +++ b/polygerrit-ui/app/elements/admin/gr-rule-editor/gr-rule-editor.js @@ -1,40 +1,56 @@ -// Copyright (C) 2017 The Android Open Source Project -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/** + * @license + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ (function() { 'use strict'; + /** + * Fired when the rule has been modified or removed. + * + * @event access-modified + */ + + /** + * Fired when a rule that was previously added was removed. + * @event added-rule-removed + */ + const PRIORITY_OPTIONS = [ 'BATCH', 'INTERACTIVE', ]; - const DROPDOWN_OPTIONS = [ - 'ALLOW', - 'DENY', - 'BLOCK', - ]; + const Action = { + ALLOW: 'ALLOW', + DENY: 'DENY', + BLOCK: 'BLOCK', + }; - const FORCE_PUSH_OPTIONS = [ - { - name: 'No Force Push', - value: false, - }, - { - name: 'Force Push', - value: true, - }, - ]; + const DROPDOWN_OPTIONS = [Action.ALLOW, Action.DENY, Action.BLOCK]; + + const ForcePushOptions = { + ALLOW: [ + {name: 'Allow pushing (but not force pushing)', value: false}, + {name: 'Allow pushing with or without force', value: true}, + ], + BLOCK: [ + {name: 'Block pushing with or without force', value: false}, + {name: 'Block force pushing', value: true}, + ], + }; const FORCE_EDIT_OPTIONS = [ { @@ -51,11 +67,13 @@ is: 'gr-rule-editor', properties: { + hasRange: Boolean, /** @type {?} */ label: Object, editing: { type: Boolean, value: false, + observer: '_handleEditingChanged', }, groupId: String, groupName: String, @@ -66,15 +84,12 @@ notify: true, }, section: String, - _modified: { - type: Boolean, - value: false, - }, - _originalRuleValues: Object, + _deleted: { type: Boolean, value: false, }, + _originalRuleValues: Object, }, behaviors: [ @@ -87,6 +102,10 @@ '_handleValueChange(rule.value.*)', ], + listeners: { + 'access-saved': '_handleAccessSaved', + }, + ready() { // Called on ready rather than the observer because when new rules are // added, the observer is triggered prior to being ready. @@ -101,19 +120,38 @@ this._setOriginalRuleValues(rule.value); }, - _computeForce(permission) { - return this.permissionValues.push.id === permission || - this.permissionValues.editTopicName.id === permission; + _computeForce(permission, action) { + if (this.permissionValues.push.id === permission && + action !== Action.DENY) { + return true; + } + + return this.permissionValues.editTopicName.id === permission; }, - _computeForceClass(permission) { - return this._computeForce(permission) ? 'force' : ''; + _computeForceClass(permission, action) { + return this._computeForce(permission, action) ? 'force' : ''; }, _computeGroupPath(group) { return `${this.getBaseUrl()}/admin/groups/${this.encodeURL(group, true)}`; }, + _handleAccessSaved() { + // Set a new 'original' value to keep track of after the value has been + // saved. + this._setOriginalRuleValues(this.rule.value); + }, + + _handleEditingChanged(editing, editingOld) { + // Ignore when editing gets set initially. + if (!editingOld) { return; } + // Restore original values if no longer editing. + if (!editing) { + this._handleUndoChange(); + } + }, + _computeSectionClass(editing, deleted) { const classList = []; if (editing) { @@ -125,9 +163,15 @@ return classList.join(' '); }, - _computeForceOptions(permission) { + _computeForceOptions(permission, action) { if (permission === this.permissionValues.push.id) { - return FORCE_PUSH_OPTIONS; + if (action === Action.ALLOW) { + return ForcePushOptions.ALLOW; + } else if (action === Action.BLOCK) { + return ForcePushOptions.BLOCK; + } else { + return []; + } } else if (permission === this.permissionValues.editTopicName.id) { return FORCE_EDIT_OPTIONS; } @@ -135,6 +179,7 @@ }, _getDefaultRuleValues(permission, label) { + const ruleAction = Action.ALLOW; const value = {}; if (permission === 'priority') { value.action = PRIORITY_OPTIONS[0]; @@ -142,16 +187,17 @@ } else if (label) { value.min = label.values[0].value; value.max = label.values[label.values.length - 1].value; - } else if (this._computeForce(permission)) { - value.force = this._computeForceOptions(permission)[0].value; + } else if (this._computeForce(permission, ruleAction)) { + value.force = + this._computeForceOptions(permission, ruleAction)[0].value; } value.action = DROPDOWN_OPTIONS[0]; return value; }, _setDefaultRuleValues() { - this.set('rule.value', - this._getDefaultRuleValues(this.permission, this.label)); + this.set('rule.value', this._getDefaultRuleValues(this.permission, + this.label)); }, _computeOptions(permission) { @@ -162,8 +208,13 @@ }, _handleRemoveRule() { + if (this.rule.value.added) { + this.dispatchEvent(new CustomEvent('added-rule-removed', + {bubbles: true})); + } this._deleted = true; - this.set('rule.value.deleted', true); + this.rule.value.deleted = true; + this.dispatchEvent(new CustomEvent('access-modified', {bubbles: true})); }, _handleUndoRemove() { @@ -172,21 +223,24 @@ }, _handleUndoChange() { + // gr-permission will take care of removing rules that were added but + // unsaved. We need to keep the added bit for the filter. + if (this.rule.value.added) { return; } this.set('rule.value', Object.assign({}, this._originalRuleValues)); - this._modified = false; + this._deleted = false; + delete this.rule.value.deleted; + delete this.rule.value.modified; }, _handleValueChange() { if (!this._originalRuleValues) { return; } - this._modified = true; + this.rule.value.modified = true; + // Allows overall access page to know a change has been made. + this.dispatchEvent(new CustomEvent('access-modified', {bubbles: true})); }, _setOriginalRuleValues(value) { this._originalRuleValues = Object.assign({}, value); }, - - _computeModifiedClass(modified) { - return modified ? 'modified' : ''; - }, }); -})();
\ No newline at end of file +})(); |