Wyatt Allen | 767b760 | 2016-06-15 16:51:20 -0700 | [diff] [blame] | 1 | // Copyright (C) 2016 The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | (function() { |
| 15 | 'use strict'; |
| 16 | |
| 17 | Polymer({ |
| 18 | is: 'gr-account-info', |
| 19 | |
Logan Hanks | 25f49af | 2016-10-10 17:26:43 -0700 | [diff] [blame] | 20 | /** |
| 21 | * Fired when account details are changed. |
| 22 | * |
| 23 | * @event account-detail-update |
| 24 | */ |
| 25 | |
Wyatt Allen | 767b760 | 2016-06-15 16:51:20 -0700 | [diff] [blame] | 26 | properties: { |
| 27 | mutable: { |
| 28 | type: Boolean, |
| 29 | notify: true, |
| 30 | computed: '_computeMutable(_serverConfig)', |
| 31 | }, |
| 32 | hasUnsavedChanges: { |
| 33 | type: Boolean, |
| 34 | notify: true, |
Kasper Nilsson | 5827312 | 2017-03-09 10:55:10 -0800 | [diff] [blame] | 35 | computed: '_computeHasUnsavedChanges(_hasNameChange, _hasStatusChange)', |
Wyatt Allen | 767b760 | 2016-06-15 16:51:20 -0700 | [diff] [blame] | 36 | }, |
| 37 | |
Kasper Nilsson | 5827312 | 2017-03-09 10:55:10 -0800 | [diff] [blame] | 38 | _hasNameChange: { |
| 39 | type: Boolean, |
| 40 | value: false, |
| 41 | }, |
| 42 | _hasStatusChange: { |
| 43 | type: Boolean, |
| 44 | value: false, |
| 45 | }, |
Wyatt Allen | 767b760 | 2016-06-15 16:51:20 -0700 | [diff] [blame] | 46 | _loading: { |
| 47 | type: Boolean, |
| 48 | value: false, |
| 49 | }, |
| 50 | _saving: { |
| 51 | type: Boolean, |
| 52 | value: false, |
| 53 | }, |
| 54 | _account: Object, |
| 55 | _serverConfig: Object, |
| 56 | }, |
| 57 | |
| 58 | observers: [ |
Kasper Nilsson | 5827312 | 2017-03-09 10:55:10 -0800 | [diff] [blame] | 59 | '_nameChanged(_account.name)', |
| 60 | '_statusChanged(_account.status)', |
Wyatt Allen | 767b760 | 2016-06-15 16:51:20 -0700 | [diff] [blame] | 61 | ], |
| 62 | |
| 63 | loadData: function() { |
| 64 | var promises = []; |
| 65 | |
| 66 | this._loading = true; |
| 67 | |
| 68 | promises.push(this.$.restAPI.getConfig().then(function(config) { |
| 69 | this._serverConfig = config; |
| 70 | }.bind(this))); |
| 71 | |
| 72 | promises.push(this.$.restAPI.getAccount().then(function(account) { |
| 73 | this._account = account; |
| 74 | }.bind(this))); |
| 75 | |
| 76 | return Promise.all(promises).then(function() { |
| 77 | this._loading = false; |
| 78 | }.bind(this)); |
| 79 | }, |
| 80 | |
| 81 | save: function() { |
Paladox none | fbe932e | 2017-04-26 21:36:06 +0000 | [diff] [blame^] | 82 | if (!this.hasUnsavedChanges) { |
Wyatt Allen | 767b760 | 2016-06-15 16:51:20 -0700 | [diff] [blame] | 83 | return Promise.resolve(); |
| 84 | } |
| 85 | |
| 86 | this._saving = true; |
Kasper Nilsson | 5827312 | 2017-03-09 10:55:10 -0800 | [diff] [blame] | 87 | // Set only the fields that have changed. |
| 88 | // Must be done in sequence to avoid race conditions (@see Issue 5721) |
| 89 | return this._maybeSetName() |
| 90 | .then(this._maybeSetStatus.bind(this)) |
| 91 | .then(function() { |
| 92 | this._hasNameChange = false; |
| 93 | this._hasStatusChange = false; |
| 94 | this._saving = false; |
| 95 | this.fire('account-detail-update'); |
| 96 | }.bind(this)); |
| 97 | }, |
| 98 | |
| 99 | _maybeSetName: function() { |
Paladox none | fbe932e | 2017-04-26 21:36:06 +0000 | [diff] [blame^] | 100 | return this._hasNameChange && this.mutable ? |
| 101 | this.$.restAPI.setAccountName(this._account.name) : |
| 102 | Promise.resolve(); |
Kasper Nilsson | 5827312 | 2017-03-09 10:55:10 -0800 | [diff] [blame] | 103 | }, |
| 104 | |
| 105 | _maybeSetStatus: function() { |
| 106 | return this._hasStatusChange ? |
| 107 | this.$.restAPI.setAccountStatus(this._account.status) : |
| 108 | Promise.resolve(); |
| 109 | }, |
| 110 | |
| 111 | _computeHasUnsavedChanges: function(name, status) { |
| 112 | return name || status; |
Wyatt Allen | 767b760 | 2016-06-15 16:51:20 -0700 | [diff] [blame] | 113 | }, |
| 114 | |
| 115 | _computeMutable: function(config) { |
| 116 | return config.auth.editable_account_fields.indexOf('FULL_NAME') !== -1; |
| 117 | }, |
| 118 | |
Kasper Nilsson | 5827312 | 2017-03-09 10:55:10 -0800 | [diff] [blame] | 119 | _statusChanged: function() { |
Wyatt Allen | 767b760 | 2016-06-15 16:51:20 -0700 | [diff] [blame] | 120 | if (this._loading) { return; } |
Kasper Nilsson | 5827312 | 2017-03-09 10:55:10 -0800 | [diff] [blame] | 121 | this._hasStatusChange = true; |
| 122 | }, |
| 123 | |
| 124 | _nameChanged: function() { |
| 125 | if (this._loading) { return; } |
| 126 | this._hasNameChange = true; |
Wyatt Allen | 767b760 | 2016-06-15 16:51:20 -0700 | [diff] [blame] | 127 | }, |
| 128 | |
Kasper Nilsson | 8587574 | 2017-02-02 12:13:01 -0800 | [diff] [blame] | 129 | _handleKeydown: function(e) { |
Wyatt Allen | 767b760 | 2016-06-15 16:51:20 -0700 | [diff] [blame] | 130 | if (e.keyCode === 13) { // Enter |
| 131 | e.stopPropagation(); |
| 132 | this.save(); |
| 133 | } |
| 134 | }, |
| 135 | }); |
| 136 | })(); |