blob: 3fa66da7166fa62dfabe47773c57b1fdca78e708 [file] [log] [blame]
// 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';
const Actions = {
EDIT: {label: 'Edit', key: 'edit'},
DELETE: {label: 'Delete', key: 'delete'},
/* TODO(kaspern): Implement these actions.
RENAME: {label: 'Rename', key: 'rename'},
REVERT: {label: 'Revert', key: 'revert'},
CHECKOUT: {label: 'Check out', key: 'checkout'},
*/
};
Polymer({
is: 'gr-edit-controls',
properties: {
change: Object,
_actions: {
type: Array,
value() { return Object.values(Actions); },
},
_path: {
type: String,
value: '',
},
_query: {
type: Function,
value() {
return this._queryFiles.bind(this);
},
},
},
behaviors: [
Gerrit.PatchSetBehavior,
],
_handleTap(e) {
e.preventDefault();
const action = Polymer.dom(e).localTarget.id;
// TODO(kaspern): Add all actions to this switch.
switch (action) {
case Actions.EDIT.key:
this.openEditDialog();
return;
case Actions.DELETE.key:
this.openDeleteDialog();
return;
}
},
openEditDialog(opt_path) {
if (opt_path) { this._path = opt_path; }
return this._showDialog(this.$.editDialog);
},
openDeleteDialog(opt_path) {
if (opt_path) { this._path = opt_path; }
return this._showDialog(this.$.deleteDialog);
},
/**
* Given a path string, checks that it is a valid file path.
* @param {string} path
* @return {boolean}
*/
_isValidPath(path) {
// Double negation needed for strict boolean return type.
return !!path.length && !path.endsWith('/');
},
/**
* Given a dom event, gets the dialog that lies along this event path.
* @param {!Event} e
* @return {!Element}
*/
_getDialogFromEvent(e) {
return Polymer.dom(e).path.find(element => {
if (!element.classList) { return false; }
return element.classList.contains('dialog');
});
},
_showDialog(dialog) {
return this.$.overlay.open().then(() => {
dialog.classList.toggle('invisible', false);
dialog.querySelector('.input').focus();
this.async(() => { this.$.overlay.center(); }, 1);
});
},
_closeDialog(dialog) {
dialog.querySelectorAll('gr-autocomplete')
.forEach(input => { input.text = ''; });
dialog.classList.toggle('invisible', true);
return this.$.overlay.close();
},
_handleDialogCancel(e) {
this._closeDialog(this._getDialogFromEvent(e));
},
_handleEditConfirm(e) {
const url = Gerrit.Nav.getEditUrlForDiff(this.change, this._path);
Gerrit.Nav.navigateToRelativeUrl(url);
this._closeDialog(this._getDialogFromEvent(e));
},
_handleDeleteConfirm(e) {
this.$.restAPI.deleteFileInChangeEdit(this.change._number, this._path)
.then(res => {
if (!res.ok) { return; }
this._closeDialog(this._getDialogFromEvent(e));
Gerrit.Nav.navigateToChange(this.change);
});
},
_queryFiles(input) {
return this.$.restAPI.queryChangeFiles(this.change._number,
this.EDIT_NAME, input).then(res => res.map(file => {
return {name: file};
}));
},
});
})();