OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 cr.define('options.accounts', function() { | 5 cr.define('options.accounts', function() { |
6 const List = cr.ui.List; | 6 const List = cr.ui.List; |
7 const ListItem = cr.ui.ListItem; | 7 const ListItem = cr.ui.ListItem; |
8 const ArrayDataModel = cr.ui.ArrayDataModel; | 8 const ArrayDataModel = cr.ui.ArrayDataModel; |
9 | 9 |
10 /** | 10 /** |
11 * Creates a new user list. | 11 * Creates a new user list. |
12 * @param {Object=} opt_propertyBag Optional properties. | 12 * @param {Object=} opt_propertyBag Optional properties. |
13 * @constructor | 13 * @constructor |
14 * @extends {cr.ui.List} | 14 * @extends {cr.ui.List} |
15 */ | 15 */ |
16 var UserList = cr.ui.define('list'); | 16 var UserList = cr.ui.define('list'); |
17 | 17 |
18 UserList.prototype = { | 18 UserList.prototype = { |
19 __proto__: List.prototype, | 19 __proto__: List.prototype, |
20 | 20 |
21 pref: 'cros.accounts.users', | 21 pref: 'cros.accounts.users', |
22 | 22 |
23 /** @inheritDoc */ | 23 /** @inheritDoc */ |
24 decorate: function() { | 24 decorate: function() { |
25 List.prototype.decorate.call(this); | 25 List.prototype.decorate.call(this); |
26 | 26 |
27 // HACK(arv): http://crbug.com/40902 | 27 // HACK(arv): http://crbug.com/40902 |
28 window.addEventListener('resize', this.redraw.bind(this)); | 28 window.addEventListener('resize', this.redraw.bind(this)); |
29 | 29 |
30 this.addEventListener('click', this.handleClick_); | |
31 | |
32 var self = this; | 30 var self = this; |
33 | 31 |
34 // Listens to pref changes. | 32 // Listens to pref changes. |
35 Preferences.getInstance().addEventListener(this.pref, | 33 Preferences.getInstance().addEventListener(this.pref, |
36 function(event) { | 34 function(event) { |
37 self.load_(event.value); | 35 self.load_(event.value); |
38 }); | 36 }); |
39 }, | 37 }, |
40 | 38 |
41 createItem: function(user) { | 39 createItem: function(user) { |
42 return new UserListItem(user); | 40 return new UserListItem(user); |
43 }, | 41 }, |
44 | 42 |
45 /** | 43 /** |
46 * Finds the index of user by given email. | 44 * Finds the index of user by given username (canonicalized email). |
47 * @private | 45 * @private |
48 * @param {string} email The email address to look for. | 46 * @param {string} username The username to look for. |
49 * @return {number} The index of the found user or -1 if not found. | 47 * @return {number} The index of the found user or -1 if not found. |
50 */ | 48 */ |
51 findUserByEmail_: function(email) { | 49 indexOf_: function(username) { |
52 var dataModel = this.dataModel; | 50 var dataModel = this.dataModel; |
53 if (!dataModel) | 51 if (!dataModel) |
54 return -1; | 52 return -1; |
55 | 53 |
56 var length = dataModel.length; | 54 var length = dataModel.length; |
57 for (var i = 0; i < length; ++i) { | 55 for (var i = 0; i < length; ++i) { |
58 var user = dataModel.item(i); | 56 var user = dataModel.item(i); |
59 if (user.email == email) { | 57 if (user.username == username) { |
60 return i; | 58 return i; |
61 } | 59 } |
62 } | 60 } |
63 | 61 |
64 return -1; | 62 return -1; |
65 }, | 63 }, |
66 | 64 |
67 /** | 65 /** |
68 * Adds given user to model and update backend. | |
69 * @param {Object} user A user to be added to user list. | |
70 */ | |
71 addUser: function(user) { | |
72 var index = this.findUserByEmail_(user.email); | |
73 if (index == -1) { | |
74 this.dataModel.push(user); | |
75 chrome.send('whitelistUser', [user.email]); | |
76 } | |
77 }, | |
78 | |
79 /** | |
80 * Removes given user from model and update backend. | |
81 */ | |
82 removeUser: function(user) { | |
83 var dataModel = this.dataModel; | |
84 | |
85 var index = dataModel.indexOf(user); | |
86 if (index >= 0) { | |
87 dataModel.splice(index, 1); | |
88 chrome.send('unwhitelistUser', [user.email]); | |
89 } | |
90 }, | |
91 | |
92 /** | |
93 * Update given user's account picture. | 66 * Update given user's account picture. |
94 * @param {string} email Email of the user to update. | 67 * @param {string} username User for which to update the image. |
95 */ | 68 */ |
96 updateAccountPicture: function(email) { | 69 updateAccountPicture: function(username) { |
97 var index = this.findUserByEmail_(email); | 70 var index = this.indexOf_(username); |
98 if (index >= 0) { | 71 if (index >= 0) { |
99 var item = this.getListItemByIndex(index); | 72 var item = this.getListItemByIndex(index); |
100 if (item) | 73 if (item) |
101 item.updatePicture(); | 74 item.updatePicture(); |
102 } | 75 } |
103 }, | 76 }, |
104 | 77 |
105 /** | 78 /** |
106 * Handles the clicks on the list and triggers user removal if the click | |
107 * is on the remove user button. | |
108 * @private | |
109 * @param {!Event} e The click event object. | |
110 */ | |
111 handleClick_: function(e) { | |
112 // Handle left button click | |
113 if (e.button == 0) { | |
114 var el = e.target; | |
115 if (el.classList.contains('remove-user-button')) { | |
116 this.removeUser(el.parentNode.user); | |
117 } | |
118 } | |
119 }, | |
120 | |
121 /** | |
122 * Loads given user list. | 79 * Loads given user list. |
123 * @param {Array} users An array of user object. | 80 * @param {Array.<Object>} users An array of user info objects. |
| 81 * @private |
124 */ | 82 */ |
125 load_: function(users) { | 83 load_: function(users) { |
126 this.dataModel = new ArrayDataModel(users); | 84 this.dataModel = new ArrayDataModel(users); |
| 85 }, |
| 86 |
| 87 /** |
| 88 * Removes given user from the list. |
| 89 * @param {Object} user User info object to be removed from user list. |
| 90 * @private |
| 91 */ |
| 92 removeUser_: function(user) { |
| 93 var e = new Event('remove'); |
| 94 e.user = user; |
| 95 this.dispatchEvent(e); |
127 } | 96 } |
128 }; | 97 }; |
129 | 98 |
130 /** | 99 /** |
131 * Whether the user list is disabled. Only used for display purpose. | 100 * Whether the user list is disabled. Only used for display purpose. |
132 * @type {boolean} | 101 * @type {boolean} |
133 */ | 102 */ |
134 cr.defineProperty(UserList, 'disabled', cr.PropertyKind.BOOL_ATTR); | 103 cr.defineProperty(UserList, 'disabled', cr.PropertyKind.BOOL_ATTR); |
135 | 104 |
136 /** | 105 /** |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
186 localStrings.getStringF('username_format', this.user.email) : | 155 localStrings.getStringF('username_format', this.user.email) : |
187 this.user.email; | 156 this.user.email; |
188 | 157 |
189 this.appendChild(this.icon_); | 158 this.appendChild(this.icon_); |
190 this.appendChild(emailNameBlock); | 159 this.appendChild(emailNameBlock); |
191 | 160 |
192 if (!this.user.owner) { | 161 if (!this.user.owner) { |
193 var removeButton = this.ownerDocument.createElement('button'); | 162 var removeButton = this.ownerDocument.createElement('button'); |
194 removeButton.className = | 163 removeButton.className = |
195 'raw-button remove-user-button custom-appearance'; | 164 'raw-button remove-user-button custom-appearance'; |
| 165 removeButton.addEventListener( |
| 166 'click', this.handleRemoveButtonClick_.bind(this)); |
196 this.appendChild(removeButton); | 167 this.appendChild(removeButton); |
197 } | 168 } |
198 }, | 169 }, |
199 | 170 |
200 /** | 171 /** |
| 172 * Handles click on the remove button. |
| 173 * @param {Event} e Click event. |
| 174 * @private |
| 175 */ |
| 176 handleRemoveButtonClick_: function(e) { |
| 177 // Handle left button click |
| 178 if (e.button == 0) |
| 179 this.parentNode.removeUser_(this.user); |
| 180 }, |
| 181 |
| 182 /** |
201 * Reloads user picture. | 183 * Reloads user picture. |
202 */ | 184 */ |
203 updatePicture: function() { | 185 updatePicture: function() { |
204 this.icon_.src = 'chrome://userimage/' + this.user.email + | 186 this.icon_.src = 'chrome://userimage/' + this.user.username + |
205 '?id=' + (new Date()).getTime(); | 187 '?id=' + (new Date()).getTime(); |
206 } | 188 } |
207 }; | 189 }; |
208 | 190 |
209 return { | 191 return { |
210 UserList: UserList | 192 UserList: UserList |
211 }; | 193 }; |
212 }); | 194 }); |
OLD | NEW |