diff options
Diffstat (limited to 'polygerrit-ui/app/elements/admin/gr-access-section/gr-access-section.js')
-rw-r--r-- | polygerrit-ui/app/elements/admin/gr-access-section/gr-access-section.js | 122 |
1 files changed, 98 insertions, 24 deletions
diff --git a/polygerrit-ui/app/elements/admin/gr-access-section/gr-access-section.js b/polygerrit-ui/app/elements/admin/gr-access-section/gr-access-section.js index 07a7a629f2..6fb7b0e018 100644 --- a/polygerrit-ui/app/elements/admin/gr-access-section/gr-access-section.js +++ b/polygerrit-ui/app/elements/admin/gr-access-section/gr-access-section.js @@ -1,19 +1,33 @@ -// 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 section has been modified or removed. + * + * @event access-modified + */ + + /** + * Fired when a section that was previously added was removed. + * @event added-section-removed + */ + const GLOBAL_NAME = 'GLOBAL_CAPABILITIES'; // The name that gets automatically input when a new reference is added. @@ -31,14 +45,17 @@ section: { type: Object, notify: true, - observer: '_sectionChanged', + observer: '_updateSection', }, groups: Object, labels: Object, editing: { type: Boolean, value: false, + observer: '_handleEditingChanged', }, + canUpload: Boolean, + ownerOf: Array, _originalId: String, _editingRef: { type: Boolean, @@ -55,11 +72,53 @@ Gerrit.AccessBehavior, ], - _sectionChanged(section) { + listeners: { + 'access-saved': '_handleAccessSaved', + }, + + _updateSection(section) { this._permissions = this.toSortedArray(section.value.permissions); this._originalId = section.id; }, + _handleAccessSaved() { + // Set a new 'original' value to keep track of after the value has been + // saved. + this._updateSection(this.section); + }, + + _handleValueChange() { + if (!this.section.value.added) { + this.section.value.modified = this.section.id !== this._originalId; + // Allows overall access page to know a change has been made. + // For a new section, this is not fired because new permissions and + // rules have to be added in order to save, modifying the ref is not + // enough. + this.dispatchEvent(new CustomEvent('access-modified', {bubbles: true})); + } + this.section.value.updatedId = this.section.id; + }, + + _handleEditingChanged(editing, editingOld) { + // Ignore when editing gets set initially. + if (!editingOld) { return; } + // Restore original values if no longer editing. + if (!editing) { + this._editingRef = false; + this._deleted = false; + delete this.section.value.deleted; + // Restore section ref. + this.set(['section', 'id'], this._originalId); + // Remove any unsaved but added permissions. + this._permissions = this._permissions.filter(p => !p.value.added); + for (const key of Object.keys(this.section.value.permissions)) { + if (this.section.value.permissions[key].added) { + delete this.section.value.permissions[key]; + } + } + } + }, + _computePermissions(name, capabilities, labels) { let allPermissions; if (name === GLOBAL_NAME) { @@ -74,6 +133,16 @@ }); }, + _computeHideEditClass(section) { + return section.id === 'GLOBAL_CAPABILITIES' ? 'hide' : ''; + }, + + _handleAddedPermissionRemoved(e) { + const index = e.model.index; + this._permissions = this._permissions.slice(0, index).concat( + this._permissions.slice(index + 1, this._permissions.length)); + }, + _computeLabelOptions(labels) { const labelOptions = []; for (const labelName of Object.keys(labels)) { @@ -128,8 +197,13 @@ }, _handleRemoveReference() { + if (this.section.value.added) { + this.dispatchEvent(new CustomEvent('added-section-removed', + {bubbles: true})); + } this._deleted = true; - this.set('section.value.deleted', true); + this.section.value.deleted = true; + this.dispatchEvent(new CustomEvent('access-modified', {bubbles: true})); }, _handleUndoRemove() { @@ -137,18 +211,18 @@ delete this.section.value.deleted; }, - _handleEditReference() { + editReference() { this._editingRef = true; + this.$.editRefInput.focus(); }, - _undoReferenceEdit() { - this._editingRef = false; - this.set('section.id', this._originalId); + _isEditEnabled(canUpload, ownerOf, sectionId) { + return canUpload || ownerOf.indexOf(sectionId) >= 0; }, - _computeSectionClass(editing, editingRef, deleted) { + _computeSectionClass(editing, canUpload, ownerOf, editingRef, deleted) { const classList = []; - if (editing) { + if (editing && this._isEditEnabled(canUpload, ownerOf, this.section.id)) { classList.push('editing'); } if (editingRef) { @@ -168,7 +242,7 @@ const value = this.$.permissionSelect.value; const permission = { id: value, - value: {rules: {}}, + value: {rules: {}, added: true}, }; // This is needed to update the 'label' property of the @@ -197,4 +271,4 @@ permission.value); }, }); -})();
\ No newline at end of file +})(); |