1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 cr.define('options.accounts', function() {
6 /** @const */ var List = cr.ui.List;
7 /** @const */ var ListItem = cr.ui.ListItem;
8 /** @const */ var ArrayDataModel = cr.ui.ArrayDataModel;
11 * Creates a new user list.
12 * @param {Object=} opt_propertyBag Optional properties.
14 * @extends {cr.ui.List}
16 var UserList = cr.ui.define('list');
18 UserList.prototype = {
19 __proto__: List.prototype,
21 pref: 'cros.accounts.users',
24 decorate: function() {
25 List.prototype.decorate.call(this);
27 // HACK(arv): http://crbug.com/40902
28 window.addEventListener('resize', this.redraw.bind(this));
32 // Listens to pref changes.
33 Preferences.getInstance().addEventListener(this.pref,
35 self.load_(event.value.value);
39 createItem: function(user) {
40 return new UserListItem(user);
44 * Finds the index of user by given username (canonicalized email).
46 * @param {string} username The username to look for.
47 * @return {number} The index of the found user or -1 if not found.
49 indexOf_: function(username) {
50 var dataModel = this.dataModel;
54 var length = dataModel.length;
55 for (var i = 0; i < length; ++i) {
56 var user = dataModel.item(i);
57 if (user.username == username) {
66 * Update given user's account picture.
67 * @param {string} username User for which to update the image.
69 updateAccountPicture: function(username) {
70 var index = this.indexOf_(username);
72 var item = this.getListItemByIndex(index);
79 * Loads given user list.
80 * @param {Array.<Object>} users An array of user info objects.
83 load_: function(users) {
84 this.dataModel = new ArrayDataModel(users);
88 * Removes given user from the list.
89 * @param {Object} user User info object to be removed from user list.
92 removeUser_: function(user) {
93 var e = new Event('remove');
95 this.dispatchEvent(e);
100 * Whether the user list is disabled. Only used for display purpose.
103 cr.defineProperty(UserList, 'disabled', cr.PropertyKind.BOOL_ATTR);
106 * Creates a new user list item.
107 * @param {Object} user The user account this represents.
109 * @extends {cr.ui.ListItem}
111 function UserListItem(user) {
112 var el = cr.doc.createElement('div');
114 UserListItem.decorate(el);
119 * Decorates an element as a user account item.
120 * @param {!HTMLElement} el The element to decorate.
122 UserListItem.decorate = function(el) {
123 el.__proto__ = UserListItem.prototype;
127 UserListItem.prototype = {
128 __proto__: ListItem.prototype,
131 decorate: function() {
132 ListItem.prototype.decorate.call(this);
134 this.className = 'user-list-item';
136 this.icon_ = this.ownerDocument.createElement('img');
137 this.icon_.className = 'user-icon';
138 this.updatePicture();
140 var labelEmail = this.ownerDocument.createElement('span');
141 labelEmail.className = 'user-email-label';
142 labelEmail.textContent = this.user.email;
144 var labelName = this.ownerDocument.createElement('span');
145 labelName.className = 'user-name-label';
146 labelName.textContent = this.user.owner ?
147 loadTimeData.getStringF('username_format', this.user.name) :
150 var emailNameBlock = this.ownerDocument.createElement('div');
151 emailNameBlock.className = 'user-email-name-block';
152 emailNameBlock.appendChild(labelEmail);
153 emailNameBlock.appendChild(labelName);
154 emailNameBlock.title = this.user.owner ?
155 loadTimeData.getStringF('username_format', this.user.email) :
158 this.appendChild(this.icon_);
159 this.appendChild(emailNameBlock);
161 if (!this.user.owner) {
162 var removeButton = this.ownerDocument.createElement('button');
163 removeButton.className =
164 'raw-button remove-user-button custom-appearance';
165 removeButton.addEventListener(
166 'click', this.handleRemoveButtonClick_.bind(this));
167 this.appendChild(removeButton);
172 * Handles click on the remove button.
173 * @param {Event} e Click event.
176 handleRemoveButtonClick_: function(e) {
177 // Handle left button click
179 this.parentNode.removeUser_(this.user);
183 * Reloads user picture.
185 updatePicture: function() {
186 this.icon_.src = 'chrome://userimage/' + this.user.username +
187 '?id=' + (new Date()).getTime();