| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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', cr.bind(this.redraw, this)); | 28 window.addEventListener('resize', cr.bind(this.redraw, this)); |
| 29 | 29 |
| 30 this.addEventListener('click', this.handleClick_); |
| 31 |
| 30 var self = this; | 32 var self = this; |
| 31 if (!this.boundHandleChange_) { | |
| 32 this.boundHandleChange_ = | |
| 33 cr.bind(this.handleChange_, this); | |
| 34 } | |
| 35 | 33 |
| 36 // Listens to pref changes. | 34 // Listens to pref changes. |
| 37 Preferences.getInstance().addEventListener(this.pref, | 35 Preferences.getInstance().addEventListener(this.pref, |
| 38 function(event) { | 36 function(event) { |
| 39 self.load_(event.value); | 37 self.load_(event.value); |
| 40 }); | 38 }); |
| 41 | |
| 42 // Listens to list selection change. | |
| 43 this.addEventListener('change', this.boundHandleChange_); | |
| 44 }, | 39 }, |
| 45 | 40 |
| 46 createItem: function(user) { | 41 createItem: function(user) { |
| 47 return new ListItem({label: user.email}); | 42 return new UserListItem(user); |
| 48 }, | 43 }, |
| 49 | 44 |
| 50 /** | 45 /** |
| 51 * Adds given user to model and update backend. | 46 * Adds given user to model and update backend. |
| 52 * @param {Object} user A user to be added to user list. | 47 * @param {Object} user A user to be added to user list. |
| 53 */ | 48 */ |
| 54 addUser: function(user) { | 49 addUser: function(user) { |
| 55 var dataModel = this.dataModel; | 50 var dataModel = this.dataModel; |
| 56 dataModel.splice(dataModel.length, 0, user); | 51 dataModel.splice(dataModel.length, 0, user); |
| 57 | 52 |
| 58 this.updateBackend_(); | 53 this.updateBackend_(); |
| 59 }, | 54 }, |
| 60 | 55 |
| 61 /** | 56 /** |
| 62 * Removes currently selected user from model and update backend. | 57 * Removes given user from model and update backend. |
| 63 */ | 58 */ |
| 64 removeSelectedUser: function() { | 59 removeUser: function(user) { |
| 65 var sm = this.selectionModel; | |
| 66 var dataModel = this.dataModel; | 60 var dataModel = this.dataModel; |
| 67 | 61 |
| 68 var newUsers = []; | 62 var index = dataModel.indexOf(user); |
| 69 for (var i = 0; i < dataModel.length; ++i) { | 63 if (index >= 0) { |
| 70 if (!sm.getIndexSelected(i)) { | 64 dataModel.splice(index, 1); |
| 71 newUsers.push(dataModel.item(i)); | 65 this.updateBackend_(); |
| 72 } | |
| 73 } | 66 } |
| 74 this.load_(newUsers); | |
| 75 | |
| 76 this.updateBackend_(); | |
| 77 }, | 67 }, |
| 78 | 68 |
| 79 /** | 69 /** |
| 70 * Handles the clicks on the list and triggers user removal if the click |
| 71 * is on the remove user button. |
| 72 * @private |
| 73 * @param {!Event} e The click event object. |
| 74 */ |
| 75 handleClick_: function(e) { |
| 76 // Handle left button click |
| 77 if (e.button == 0) { |
| 78 var el = e.target; |
| 79 if (el.className == 'remove-user-button') { |
| 80 this.removeUser(el.parentNode.user); |
| 81 } |
| 82 } |
| 83 }, |
| 84 |
| 85 /** |
| 80 * Loads given user list. | 86 * Loads given user list. |
| 81 * @param {Array} users An array of user object. | 87 * @param {Array} users An array of user object. |
| 82 */ | 88 */ |
| 83 load_: function(users) { | 89 load_: function(users) { |
| 84 this.dataModel = new ArrayDataModel(users); | 90 this.dataModel = new ArrayDataModel(users); |
| 85 }, | 91 }, |
| 86 | 92 |
| 87 /** | 93 /** |
| 88 * Updates backend. | 94 * Updates backend. |
| 89 */ | 95 */ |
| 90 updateBackend_: function() { | 96 updateBackend_: function() { |
| 91 Preferences.setObjectPref(this.pref, this.dataModel.slice()); | 97 Preferences.setObjectPref(this.pref, this.dataModel.slice()); |
| 92 }, | |
| 93 | |
| 94 /** | |
| 95 * Handles selection change. | |
| 96 */ | |
| 97 handleChange_: function(e) { | |
| 98 $('removeUserButton').disabled = this.selectionModel.selectedIndex == -1; | |
| 99 } | 98 } |
| 100 }; | 99 }; |
| 101 | 100 |
| 101 /** |
| 102 * Creates a new user list item. |
| 103 * @param user The user account this represents. |
| 104 * @constructor |
| 105 * @extends {cr.ui.ListItem} |
| 106 */ |
| 107 function UserListItem(user) { |
| 108 var el = cr.doc.createElement('div'); |
| 109 el.user = user; |
| 110 UserListItem.decorate(el); |
| 111 return el; |
| 112 } |
| 113 |
| 114 /** |
| 115 * Decorates an element as a user account item. |
| 116 * @param {!HTMLElement} el The element to decorate. |
| 117 */ |
| 118 UserListItem.decorate = function(el) { |
| 119 el.__proto__ = UserListItem.prototype; |
| 120 el.decorate(); |
| 121 }; |
| 122 |
| 123 UserListItem.prototype = { |
| 124 __proto__: ListItem.prototype, |
| 125 |
| 126 /** @inheritDoc */ |
| 127 decorate: function() { |
| 128 ListItem.prototype.decorate.call(this); |
| 129 |
| 130 this.className = 'user-list-item'; |
| 131 |
| 132 var icon = this.ownerDocument.createElement('img'); |
| 133 icon.className = 'user-icon'; |
| 134 // TODO(xiyuan): Replace this with real user picture when ready. |
| 135 icon.src = 'chrome://theme/IDR_LOGIN_DEFAULT_USER'; |
| 136 |
| 137 var labelEmail = this.ownerDocument.createElement('span'); |
| 138 labelEmail.className = 'user-email-label'; |
| 139 labelEmail.textContent = this.user.email; |
| 140 |
| 141 var labelName = this.ownerDocument.createElement('span'); |
| 142 labelName.className = 'user-name-label'; |
| 143 labelName.textContent = this.user.owner ? |
| 144 localStrings.getStringF('username_format', this.user.name) : |
| 145 this.user.name; |
| 146 |
| 147 this.appendChild(icon); |
| 148 this.appendChild(labelEmail); |
| 149 this.appendChild(labelName); |
| 150 |
| 151 if (!this.user.owner) { |
| 152 var removeButton = this.ownerDocument.createElement('button'); |
| 153 removeButton.className = 'remove-user-button'; |
| 154 this.appendChild(removeButton); |
| 155 } |
| 156 } |
| 157 }; |
| 158 |
| 102 return { | 159 return { |
| 103 UserList: UserList | 160 UserList: UserList |
| 104 }; | 161 }; |
| 105 }); | 162 }); |
| OLD | NEW |