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