Dave Borowitz | 8cdc76b | 2018-03-26 10:04:27 -0400 | [diff] [blame] | 1 | /** |
| 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 Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 17 | import '@polymer/iron-autogrow-textarea/iron-autogrow-textarea.js'; |
| 18 | import '../../../styles/gr-form-styles.js'; |
| 19 | import '../../shared/gr-button/gr-button.js'; |
| 20 | import '../../shared/gr-copy-clipboard/gr-copy-clipboard.js'; |
| 21 | import '../../shared/gr-overlay/gr-overlay.js'; |
| 22 | import '../../shared/gr-rest-api-interface/gr-rest-api-interface.js'; |
| 23 | import '../../../styles/shared-styles.js'; |
| 24 | import {dom} from '@polymer/polymer/lib/legacy/polymer.dom.js'; |
| 25 | import {GestureEventListeners} from '@polymer/polymer/lib/mixins/gesture-event-listeners.js'; |
| 26 | import {LegacyElementMixin} from '@polymer/polymer/lib/legacy/legacy-element-mixin.js'; |
| 27 | import {PolymerElement} from '@polymer/polymer/polymer-element.js'; |
| 28 | import {htmlTemplate} from './gr-gpg-editor_html.js'; |
Paladox none | 4f53427 | 2017-10-31 21:29:08 +0000 | [diff] [blame] | 29 | |
Dmitrii Filippov | 1f9cac8 | 2020-04-23 15:08:21 +0200 | [diff] [blame] | 30 | /** @extends PolymerElement */ |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 31 | class GrGpgEditor extends GestureEventListeners( |
| 32 | LegacyElementMixin( |
| 33 | PolymerElement)) { |
| 34 | static get template() { return htmlTemplate; } |
Paladox none | 4f53427 | 2017-10-31 21:29:08 +0000 | [diff] [blame] | 35 | |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 36 | static get is() { return 'gr-gpg-editor'; } |
Paladox none | 4f53427 | 2017-10-31 21:29:08 +0000 | [diff] [blame] | 37 | |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 38 | 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 Filippov | 3fd2b10 | 2019-11-15 16:16:46 +0100 | [diff] [blame] | 57 | } |
| 58 | |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 59 | 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 | |
| 124 | customElements.define(GrGpgEditor.is, GrGpgEditor); |