blob: 6c6ad013f906bce04525b50f0b883c3456c9337d [file] [log] [blame]
Dave Borowitz8cdc76b2018-03-26 10:04:27 -04001/**
2 * @license
3 * Copyright (C) 2017 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010017import '@polymer/iron-autogrow-textarea/iron-autogrow-textarea.js';
18import '../../../styles/gr-form-styles.js';
19import '../../shared/gr-button/gr-button.js';
20import '../../shared/gr-copy-clipboard/gr-copy-clipboard.js';
21import '../../shared/gr-overlay/gr-overlay.js';
22import '../../shared/gr-rest-api-interface/gr-rest-api-interface.js';
23import '../../../styles/shared-styles.js';
24import {dom} from '@polymer/polymer/lib/legacy/polymer.dom.js';
25import {GestureEventListeners} from '@polymer/polymer/lib/mixins/gesture-event-listeners.js';
26import {LegacyElementMixin} from '@polymer/polymer/lib/legacy/legacy-element-mixin.js';
27import {PolymerElement} from '@polymer/polymer/polymer-element.js';
28import {htmlTemplate} from './gr-gpg-editor_html.js';
Paladox none4f534272017-10-31 21:29:08 +000029
Dmitrii Filippov1f9cac82020-04-23 15:08:21 +020030/** @extends PolymerElement */
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010031class GrGpgEditor extends GestureEventListeners(
32 LegacyElementMixin(
33 PolymerElement)) {
34 static get template() { return htmlTemplate; }
Paladox none4f534272017-10-31 21:29:08 +000035
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010036 static get is() { return 'gr-gpg-editor'; }
Paladox none4f534272017-10-31 21:29:08 +000037
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010038 static get properties() {
39 return {
40 hasUnsavedChanges: {
41 type: Boolean,
42 value: false,
43 notify: true,
44 },
45 _keys: Array,
46 /** @type {?} */
47 _keyToView: Object,
48 _newKey: {
49 type: String,
50 value: '',
51 },
52 _keysToRemove: {
53 type: Array,
54 value() { return []; },
55 },
56 };
Dmitrii Filippov3fd2b102019-11-15 16:16:46 +010057 }
58
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010059 loadData() {
60 this._keys = [];
61 return this.$.restAPI.getAccountGPGKeys().then(keys => {
62 if (!keys) {
63 return;
64 }
65 this._keys = Object.keys(keys)
66 .map(key => {
67 const gpgKey = keys[key];
68 gpgKey.id = key;
69 return gpgKey;
70 });
71 });
72 }
73
74 save() {
75 const promises = this._keysToRemove.map(key => {
76 this.$.restAPI.deleteAccountGPGKey(key.id);
77 });
78
79 return Promise.all(promises).then(() => {
80 this._keysToRemove = [];
81 this.hasUnsavedChanges = false;
82 });
83 }
84
85 _showKey(e) {
86 const el = dom(e).localTarget;
87 const index = parseInt(el.getAttribute('data-index'), 10);
88 this._keyToView = this._keys[index];
89 this.$.viewKeyOverlay.open();
90 }
91
92 _closeOverlay() {
93 this.$.viewKeyOverlay.close();
94 }
95
96 _handleDeleteKey(e) {
97 const el = dom(e).localTarget;
98 const index = parseInt(el.getAttribute('data-index'), 10);
99 this.push('_keysToRemove', this._keys[index]);
100 this.splice('_keys', index, 1);
101 this.hasUnsavedChanges = true;
102 }
103
104 _handleAddKey() {
105 this.$.addButton.disabled = true;
106 this.$.newKey.disabled = true;
107 return this.$.restAPI.addAccountGPGKey({add: [this._newKey.trim()]})
108 .then(key => {
109 this.$.newKey.disabled = false;
110 this._newKey = '';
111 this.loadData();
112 })
113 .catch(() => {
114 this.$.addButton.disabled = false;
115 this.$.newKey.disabled = false;
116 });
117 }
118
119 _computeAddButtonDisabled(newKey) {
120 return !newKey.length;
121 }
122}
123
124customElements.define(GrGpgEditor.is, GrGpgEditor);