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 | |
32 var self = this; | 30 var self = this; |
| 31 if (!this.boundHandleChange_) { |
| 32 this.boundHandleChange_ = |
| 33 cr.bind(this.handleChange_, this); |
| 34 } |
33 | 35 |
34 // Listens to pref changes. | 36 // Listens to pref changes. |
35 Preferences.getInstance().addEventListener(this.pref, | 37 Preferences.getInstance().addEventListener(this.pref, |
36 function(event) { | 38 function(event) { |
37 self.load_(event.value); | 39 self.load_(event.value); |
38 }); | 40 }); |
| 41 |
| 42 // Listens to list selection change. |
| 43 this.addEventListener('change', this.boundHandleChange_); |
39 }, | 44 }, |
40 | 45 |
41 createItem: function(user) { | 46 createItem: function(user) { |
42 return new UserListItem(user); | 47 return new ListItem({label: user.email}); |
43 }, | 48 }, |
44 | 49 |
45 /** | 50 /** |
46 * Adds given user to model and update backend. | 51 * Adds given user to model and update backend. |
47 * @param {Object} user A user to be added to user list. | 52 * @param {Object} user A user to be added to user list. |
48 */ | 53 */ |
49 addUser: function(user) { | 54 addUser: function(user) { |
50 this.dataModel.push(user); | 55 this.dataModel.push(user); |
51 this.updateBackend_(); | 56 this.updateBackend_(); |
52 }, | 57 }, |
53 | 58 |
54 /** | 59 /** |
55 * Removes given user from model and update backend. | 60 * Removes currently selected user from model and update backend. |
56 */ | 61 */ |
57 removeUser: function(user) { | 62 removeSelectedUser: function() { |
| 63 var sm = this.selectionModel; |
58 var dataModel = this.dataModel; | 64 var dataModel = this.dataModel; |
59 | 65 |
60 var index = dataModel.indexOf(user); | 66 var newUsers = []; |
61 if (index >= 0) { | 67 for (var i = 0; i < dataModel.length; ++i) { |
62 dataModel.splice(index, 1); | 68 if (!sm.getIndexSelected(i)) { |
63 this.updateBackend_(); | 69 newUsers.push(dataModel.item(i)); |
| 70 } |
64 } | 71 } |
| 72 this.load_(newUsers); |
| 73 |
| 74 this.updateBackend_(); |
65 }, | 75 }, |
66 | 76 |
67 /** | 77 /** |
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 /** | |
84 * Loads given user list. | 78 * Loads given user list. |
85 * @param {Array} users An array of user object. | 79 * @param {Array} users An array of user object. |
86 */ | 80 */ |
87 load_: function(users) { | 81 load_: function(users) { |
88 this.dataModel = new ArrayDataModel(users); | 82 this.dataModel = new ArrayDataModel(users); |
89 }, | 83 }, |
90 | 84 |
91 /** | 85 /** |
92 * Updates backend. | 86 * Updates backend. |
93 */ | 87 */ |
94 updateBackend_: function() { | 88 updateBackend_: function() { |
95 Preferences.setObjectPref(this.pref, this.dataModel.slice()); | 89 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; |
96 } | 97 } |
97 }; | 98 }; |
98 | 99 |
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 | |
157 return { | 100 return { |
158 UserList: UserList | 101 UserList: UserList |
159 }; | 102 }; |
160 }); | 103 }); |
OLD | NEW |