Dave Borowitz | 8cdc76b | 2018-03-26 10:04:27 -0400 | [diff] [blame] | 1 | /** |
| 2 | * @license |
| 3 | * Copyright (C) 2016 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 | */ |
Wyatt Allen | 767b760 | 2016-06-15 16:51:20 -0700 | [diff] [blame] | 17 | (function() { |
| 18 | 'use strict'; |
| 19 | |
| 20 | Polymer({ |
| 21 | is: 'gr-account-info', |
| 22 | |
Logan Hanks | 25f49af | 2016-10-10 17:26:43 -0700 | [diff] [blame] | 23 | /** |
| 24 | * Fired when account details are changed. |
| 25 | * |
| 26 | * @event account-detail-update |
| 27 | */ |
| 28 | |
Wyatt Allen | 767b760 | 2016-06-15 16:51:20 -0700 | [diff] [blame] | 29 | properties: { |
Kasper Nilsson | 8a4c47e | 2017-11-09 16:08:06 -0800 | [diff] [blame] | 30 | usernameMutable: { |
Wyatt Allen | 767b760 | 2016-06-15 16:51:20 -0700 | [diff] [blame] | 31 | type: Boolean, |
| 32 | notify: true, |
Kasper Nilsson | f4d03fc | 2017-11-14 15:12:50 -0800 | [diff] [blame] | 33 | computed: '_computeUsernameMutable(_serverConfig, _account.username)', |
Kasper Nilsson | 8a4c47e | 2017-11-09 16:08:06 -0800 | [diff] [blame] | 34 | }, |
| 35 | nameMutable: { |
| 36 | type: Boolean, |
| 37 | notify: true, |
| 38 | computed: '_computeNameMutable(_serverConfig)', |
Wyatt Allen | 767b760 | 2016-06-15 16:51:20 -0700 | [diff] [blame] | 39 | }, |
| 40 | hasUnsavedChanges: { |
| 41 | type: Boolean, |
| 42 | notify: true, |
Kasper Nilsson | 8a4c47e | 2017-11-09 16:08:06 -0800 | [diff] [blame] | 43 | computed: '_computeHasUnsavedChanges(_hasNameChange, ' + |
| 44 | '_hasUsernameChange, _hasStatusChange)', |
Wyatt Allen | 767b760 | 2016-06-15 16:51:20 -0700 | [diff] [blame] | 45 | }, |
| 46 | |
Kasper Nilsson | 5cb34e7 | 2017-12-27 14:12:15 -0800 | [diff] [blame] | 47 | _hasNameChange: Boolean, |
| 48 | _hasUsernameChange: Boolean, |
| 49 | _hasStatusChange: Boolean, |
Wyatt Allen | 767b760 | 2016-06-15 16:51:20 -0700 | [diff] [blame] | 50 | _loading: { |
| 51 | type: Boolean, |
| 52 | value: false, |
| 53 | }, |
| 54 | _saving: { |
| 55 | type: Boolean, |
| 56 | value: false, |
| 57 | }, |
Becky Siegel | 8d92d53 | 2017-08-11 16:32:47 -0700 | [diff] [blame] | 58 | /** @type {?} */ |
Wyatt Allen | 767b760 | 2016-06-15 16:51:20 -0700 | [diff] [blame] | 59 | _account: Object, |
| 60 | _serverConfig: Object, |
Kasper Nilsson | f4d03fc | 2017-11-14 15:12:50 -0800 | [diff] [blame] | 61 | _username: { |
| 62 | type: String, |
| 63 | observer: '_usernameChanged', |
| 64 | }, |
Paladox none | 7e4726c | 2018-10-07 14:29:45 +0000 | [diff] [blame^] | 65 | _avatarChangeUrl: { |
| 66 | type: String, |
| 67 | value: '', |
| 68 | }, |
Wyatt Allen | 767b760 | 2016-06-15 16:51:20 -0700 | [diff] [blame] | 69 | }, |
| 70 | |
| 71 | observers: [ |
Kasper Nilsson | 5827312 | 2017-03-09 10:55:10 -0800 | [diff] [blame] | 72 | '_nameChanged(_account.name)', |
| 73 | '_statusChanged(_account.status)', |
Wyatt Allen | 767b760 | 2016-06-15 16:51:20 -0700 | [diff] [blame] | 74 | ], |
| 75 | |
Kasper Nilsson | 9513134 | 2017-05-15 17:13:16 -0700 | [diff] [blame] | 76 | loadData() { |
| 77 | const promises = []; |
Wyatt Allen | 767b760 | 2016-06-15 16:51:20 -0700 | [diff] [blame] | 78 | |
| 79 | this._loading = true; |
| 80 | |
Kasper Nilsson | 9513134 | 2017-05-15 17:13:16 -0700 | [diff] [blame] | 81 | promises.push(this.$.restAPI.getConfig().then(config => { |
Wyatt Allen | 767b760 | 2016-06-15 16:51:20 -0700 | [diff] [blame] | 82 | this._serverConfig = config; |
Kasper Nilsson | 9513134 | 2017-05-15 17:13:16 -0700 | [diff] [blame] | 83 | })); |
Wyatt Allen | 767b760 | 2016-06-15 16:51:20 -0700 | [diff] [blame] | 84 | |
Kasper Nilsson | 9513134 | 2017-05-15 17:13:16 -0700 | [diff] [blame] | 85 | promises.push(this.$.restAPI.getAccount().then(account => { |
Kasper Nilsson | 5cb34e7 | 2017-12-27 14:12:15 -0800 | [diff] [blame] | 86 | this._hasNameChange = false; |
| 87 | this._hasUsernameChange = false; |
| 88 | this._hasStatusChange = false; |
Kasper Nilsson | f4d03fc | 2017-11-14 15:12:50 -0800 | [diff] [blame] | 89 | // Provide predefined value for username to trigger computation of |
| 90 | // username mutability. |
| 91 | account.username = account.username || ''; |
Wyatt Allen | 767b760 | 2016-06-15 16:51:20 -0700 | [diff] [blame] | 92 | this._account = account; |
Kasper Nilsson | f4d03fc | 2017-11-14 15:12:50 -0800 | [diff] [blame] | 93 | this._username = account.username; |
Kasper Nilsson | 9513134 | 2017-05-15 17:13:16 -0700 | [diff] [blame] | 94 | })); |
Wyatt Allen | 767b760 | 2016-06-15 16:51:20 -0700 | [diff] [blame] | 95 | |
Paladox none | 7e4726c | 2018-10-07 14:29:45 +0000 | [diff] [blame^] | 96 | promises.push(this.$.restAPI.getAvatarChangeUrl().then(url => { |
| 97 | this._avatarChangeUrl = url; |
| 98 | })); |
| 99 | |
Kasper Nilsson | 9513134 | 2017-05-15 17:13:16 -0700 | [diff] [blame] | 100 | return Promise.all(promises).then(() => { |
Wyatt Allen | 767b760 | 2016-06-15 16:51:20 -0700 | [diff] [blame] | 101 | this._loading = false; |
Kasper Nilsson | 9513134 | 2017-05-15 17:13:16 -0700 | [diff] [blame] | 102 | }); |
Wyatt Allen | 767b760 | 2016-06-15 16:51:20 -0700 | [diff] [blame] | 103 | }, |
| 104 | |
Kasper Nilsson | 9513134 | 2017-05-15 17:13:16 -0700 | [diff] [blame] | 105 | save() { |
Paladox none | fbe932e | 2017-04-26 21:36:06 +0000 | [diff] [blame] | 106 | if (!this.hasUnsavedChanges) { |
Wyatt Allen | 767b760 | 2016-06-15 16:51:20 -0700 | [diff] [blame] | 107 | return Promise.resolve(); |
| 108 | } |
| 109 | |
| 110 | this._saving = true; |
Kasper Nilsson | 5827312 | 2017-03-09 10:55:10 -0800 | [diff] [blame] | 111 | // Set only the fields that have changed. |
| 112 | // Must be done in sequence to avoid race conditions (@see Issue 5721) |
| 113 | return this._maybeSetName() |
Kasper Nilsson | 8a4c47e | 2017-11-09 16:08:06 -0800 | [diff] [blame] | 114 | .then(this._maybeSetUsername.bind(this)) |
Kasper Nilsson | 5827312 | 2017-03-09 10:55:10 -0800 | [diff] [blame] | 115 | .then(this._maybeSetStatus.bind(this)) |
Kasper Nilsson | 9513134 | 2017-05-15 17:13:16 -0700 | [diff] [blame] | 116 | .then(() => { |
Kasper Nilsson | 5827312 | 2017-03-09 10:55:10 -0800 | [diff] [blame] | 117 | this._hasNameChange = false; |
| 118 | this._hasStatusChange = false; |
| 119 | this._saving = false; |
| 120 | this.fire('account-detail-update'); |
Kasper Nilsson | 9513134 | 2017-05-15 17:13:16 -0700 | [diff] [blame] | 121 | }); |
Kasper Nilsson | 5827312 | 2017-03-09 10:55:10 -0800 | [diff] [blame] | 122 | }, |
| 123 | |
Kasper Nilsson | 9513134 | 2017-05-15 17:13:16 -0700 | [diff] [blame] | 124 | _maybeSetName() { |
Kasper Nilsson | 8a4c47e | 2017-11-09 16:08:06 -0800 | [diff] [blame] | 125 | return this._hasNameChange && this.nameMutable ? |
| 126 | this.$.restAPI.setAccountName(this._account.name) : |
| 127 | Promise.resolve(); |
| 128 | }, |
| 129 | |
| 130 | _maybeSetUsername() { |
| 131 | return this._hasUsernameChange && this.usernameMutable ? |
Kasper Nilsson | f4d03fc | 2017-11-14 15:12:50 -0800 | [diff] [blame] | 132 | this.$.restAPI.setAccountUsername(this._username) : |
Kasper Nilsson | 8a4c47e | 2017-11-09 16:08:06 -0800 | [diff] [blame] | 133 | Promise.resolve(); |
Kasper Nilsson | 5827312 | 2017-03-09 10:55:10 -0800 | [diff] [blame] | 134 | }, |
| 135 | |
Kasper Nilsson | 9513134 | 2017-05-15 17:13:16 -0700 | [diff] [blame] | 136 | _maybeSetStatus() { |
Kasper Nilsson | 5827312 | 2017-03-09 10:55:10 -0800 | [diff] [blame] | 137 | return this._hasStatusChange ? |
| 138 | this.$.restAPI.setAccountStatus(this._account.status) : |
| 139 | Promise.resolve(); |
| 140 | }, |
| 141 | |
Kasper Nilsson | 8a4c47e | 2017-11-09 16:08:06 -0800 | [diff] [blame] | 142 | _computeHasUnsavedChanges(nameChanged, usernameChanged, statusChanged) { |
| 143 | return nameChanged || usernameChanged || statusChanged; |
Wyatt Allen | 767b760 | 2016-06-15 16:51:20 -0700 | [diff] [blame] | 144 | }, |
| 145 | |
Kasper Nilsson | f4d03fc | 2017-11-14 15:12:50 -0800 | [diff] [blame] | 146 | _computeUsernameMutable(config, username) { |
| 147 | // Username may not be changed once it is set. |
| 148 | return config.auth.editable_account_fields.includes('USER_NAME') && |
| 149 | !username; |
Kasper Nilsson | 8a4c47e | 2017-11-09 16:08:06 -0800 | [diff] [blame] | 150 | }, |
| 151 | |
| 152 | _computeNameMutable(config) { |
Kasper Nilsson | 9513134 | 2017-05-15 17:13:16 -0700 | [diff] [blame] | 153 | return config.auth.editable_account_fields.includes('FULL_NAME'); |
Wyatt Allen | 767b760 | 2016-06-15 16:51:20 -0700 | [diff] [blame] | 154 | }, |
| 155 | |
Kasper Nilsson | 9513134 | 2017-05-15 17:13:16 -0700 | [diff] [blame] | 156 | _statusChanged() { |
Wyatt Allen | 767b760 | 2016-06-15 16:51:20 -0700 | [diff] [blame] | 157 | if (this._loading) { return; } |
Kasper Nilsson | 5827312 | 2017-03-09 10:55:10 -0800 | [diff] [blame] | 158 | this._hasStatusChange = true; |
| 159 | }, |
| 160 | |
Kasper Nilsson | 8a4c47e | 2017-11-09 16:08:06 -0800 | [diff] [blame] | 161 | _usernameChanged() { |
Kasper Nilsson | 5cb34e7 | 2017-12-27 14:12:15 -0800 | [diff] [blame] | 162 | if (this._loading || !this._account) { return; } |
| 163 | this._hasUsernameChange = |
| 164 | (this._account.username || '') !== (this._username || ''); |
Kasper Nilsson | 8a4c47e | 2017-11-09 16:08:06 -0800 | [diff] [blame] | 165 | }, |
| 166 | |
Kasper Nilsson | 9513134 | 2017-05-15 17:13:16 -0700 | [diff] [blame] | 167 | _nameChanged() { |
Kasper Nilsson | 5827312 | 2017-03-09 10:55:10 -0800 | [diff] [blame] | 168 | if (this._loading) { return; } |
| 169 | this._hasNameChange = true; |
Wyatt Allen | 767b760 | 2016-06-15 16:51:20 -0700 | [diff] [blame] | 170 | }, |
| 171 | |
Kasper Nilsson | 9513134 | 2017-05-15 17:13:16 -0700 | [diff] [blame] | 172 | _handleKeydown(e) { |
Wyatt Allen | 767b760 | 2016-06-15 16:51:20 -0700 | [diff] [blame] | 173 | if (e.keyCode === 13) { // Enter |
| 174 | e.stopPropagation(); |
| 175 | this.save(); |
| 176 | } |
| 177 | }, |
Paladox none | 7e4726c | 2018-10-07 14:29:45 +0000 | [diff] [blame^] | 178 | |
| 179 | _hideAvatarChangeUrl(avatarChangeUrl) { |
| 180 | if (!avatarChangeUrl) { |
| 181 | return 'hide'; |
| 182 | } |
| 183 | |
| 184 | return ''; |
| 185 | }, |
Wyatt Allen | 767b760 | 2016-06-15 16:51:20 -0700 | [diff] [blame] | 186 | }); |
| 187 | })(); |