Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(9)

Side by Side Diff: chrome/browser/resources/file_manager/js/volume_list.js

Issue 22021003: Move the initialization code of volume item to VolumeItem constructor. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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 'use strict'; 5 'use strict';
6 6
7 /** 7 /**
8 * A volume list model. This model combines the 2 lists. 8 * A volume list model. This model combines the 2 lists.
9 * @param {cr.ui.ArrayDataModel} volumesList The first list of the model. 9 * @param {cr.ui.ArrayDataModel} volumesList The first list of the model.
10 * @param {cr.ui.ArrayDataModel} pinnedList The second list of the model. 10 * @param {cr.ui.ArrayDataModel} pinnedList The second list of the model.
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
130 130
131 /** 131 /**
132 * A volume item. 132 * A volume item.
133 * @constructor 133 * @constructor
134 * @extends {HTMLLIElement} 134 * @extends {HTMLLIElement}
135 */ 135 */
136 var VolumeItem = cr.ui.define('li'); 136 var VolumeItem = cr.ui.define('li');
137 137
138 VolumeItem.prototype = { 138 VolumeItem.prototype = {
139 __proto__: HTMLLIElement.prototype, 139 __proto__: HTMLLIElement.prototype,
140
141 decorate: function() {
142 // Nothing to do.
143 },
144 }; 140 };
145 141
146 /** 142 /**
143 * Decorate the item.
144 */
145 VolumeItem.prototype.decorate = function() {
146 // decorate() may be called twice: from the constructor and from
147 // List.createItem().
148 if (this.decorated_)
mtomasz 2013/08/06 01:33:06 It looks tricky but it seems that we have to do it
yoshiki 2013/08/06 04:42:20 This is because of a design mismatch between cr.ui
149 return;
150 this.decorated_ = true;
151
152 this.className = 'root-item';
153 this.setAttribute('role', 'option');
154
155 this.iconDiv_ = cr.doc.createElement('div');
156 this.iconDiv_.className = 'volume-icon';
157 this.appendChild(this.iconDiv_);
158
159 this.label_ = cr.doc.createElement('div');
160 this.label_.className = 'root-label';
161 this.appendChild(this.label_);
162
163 cr.defineProperty(this, 'lead', cr.PropertyKind.BOOL_ATTR);
164 cr.defineProperty(this, 'selected', cr.PropertyKind.BOOL_ATTR);
165 };
166
167 /**
168 * Associate a path with this item.
169 * @param {string} path Path of this item.
170 */
171 VolumeItem.prototype.setPath = function(path) {
172 this.path_ = path;
173
174 var rootType = PathUtil.getRootType(path);
175
176 this.iconDiv_.setAttribute('volume-type-icon', rootType);
177 if (rootType === RootType.REMOVABLE) {
178 this.iconDiv_.setAttribute('volume-subtype',
179 VolumeManager.getInstance().getDeviceType(path));
180 }
181
182 this.label_.textContent = PathUtil.getFolderLabel(path);
183
184 if (rootType === RootType.ARCHIVE || rootType === RootType.REMOVABLE) {
185 if (!this.eject_) {
186 this.eject_ = cr.doc.createElement('div');
187 // Block other mouse handlers.
188 this.eject_.addEventListener(
189 'mouseup', function(e) { e.stopPropagation() });
190 this.eject_.addEventListener(
191 'mousedown', function(e) { e.stopPropagation() });
192
193 this.eject_.className = 'root-eject';
194 this.eject_.addEventListener('click', function(event) {
195 event.stopPropagation();
196 cr.dispatchSimpleEvent(this, 'eject');
197 }.bind(this));
198
199 this.appendChild(this.eject_);
200 }
201 } else {
mtomasz 2013/08/06 01:33:06 nit: Can setPath be called more than once?
yoshiki 2013/08/06 04:42:20 Shouldn't be. Removed the code around and added th
202 if (this.eject_) {
203 this.removeChild(this.eject_);
204 this.eject_ = null;
205 }
206 }
207 };
208
209 /**
210 * Associate a context menu with this item.
mtomasz 2013/08/06 01:33:06 I think this is confusing. This method sometimes w
yoshiki 2013/08/06 04:42:20 This method is called from 2 places, so moving 'if
mtomasz 2013/08/06 04:52:23 SGTM. Hiding menu with no items is a great idea. A
yoshiki 2013/08/06 04:58:44 Done.
211 * @param {cr.ui.Menu} menu Menu this item.
212 */
213 VolumeItem.prototype.setContextMenu = function(menu) {
214 var isRoot = PathUtil.isRootPath(this.path_);
215 var rootType = PathUtil.getRootType(this.path_);
216 if (!isRoot ||
217 rootType != RootType.DRIVE && rootType != RootType.DOWNLOADS)
mtomasz 2013/08/06 04:52:23 nit: Add () around for readability?
yoshiki 2013/08/06 04:58:44 Done.
218 cr.ui.contextMenuHandler.setContextMenu(this, menu);
219 };
220
221 /**
147 * A volume list. 222 * A volume list.
148 * @constructor 223 * @constructor
149 * @extends {cr.ui.List} 224 * @extends {cr.ui.List}
150 */ 225 */
151 function VolumeList() { 226 function VolumeList() {
152 } 227 }
153 228
154 /** 229 /**
155 * VolumeList inherits cr.ui.List. 230 * VolumeList inherits cr.ui.List.
156 */ 231 */
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
209 }; 284 };
210 285
211 /** 286 /**
212 * Creates an element of a volume. This method is called from cr.ui.List 287 * Creates an element of a volume. This method is called from cr.ui.List
213 * internally. 288 * internally.
214 * @param {string} path Path of the directory to be rendered. 289 * @param {string} path Path of the directory to be rendered.
215 * @return {VolumeItem} Rendered element. 290 * @return {VolumeItem} Rendered element.
216 * @private 291 * @private
217 */ 292 */
218 VolumeList.prototype.renderRoot_ = function(path) { 293 VolumeList.prototype.renderRoot_ = function(path) {
219 var li = new VolumeItem; 294 var item = new VolumeItem();
220 // TODO(yoshiki): Move the following initialization code to the constructor 295 item.setPath(path);
221 // of VolumeItem. 296
222 li.className = 'root-item';
223 li.setAttribute('role', 'option');
224 var dm = this.directoryModel_;
225 var handleClick = function() { 297 var handleClick = function() {
226 if (li.selected && path !== dm.getCurrentDirPath()) { 298 if (item.selected && path !== this.directoryModel_.getCurrentDirPath()) {
227 this.directoryModel_.changeDirectory(path); 299 this.directoryModel_.changeDirectory(path);
228 } 300 }
229 }.bind(this); 301 }.bind(this);
230 li.addEventListener('click', handleClick); 302 item.addEventListener('click', handleClick);
231 li.addEventListener(cr.ui.TouchHandler.EventType.TOUCH_START, handleClick);
232 303
233 var isRoot = PathUtil.isRootPath(path); 304 var handleEject = function() {
234 var rootType = PathUtil.getRootType(path); 305 var unmountCommand = cr.doc.querySelector('command#unmount');
306 // Let's make sure 'canExecute' state of the command is properly set for
307 // the root before executing it.
308 unmountCommand.canExecuteChange(item);
309 unmountCommand.execute(item);
310 };
311 item.addEventListener('eject', handleEject);
235 312
236 var iconDiv = cr.doc.createElement('div'); 313 item.addEventListener(cr.ui.TouchHandler.EventType.TOUCH_START, handleClick);
mtomasz 2013/08/06 01:33:06 Is this necessary? Isn't touching emitting click e
yoshiki 2013/08/06 04:42:20 I'm not sure but it has been here for a long time.
mtomasz 2013/08/06 04:52:23 nit: Please add a TODO, so we won't forget.
yoshiki 2013/08/06 04:58:44 Done.
237 iconDiv.className = 'volume-icon';
238 iconDiv.setAttribute('volume-type-icon', rootType);
239 if (rootType === RootType.REMOVABLE) {
240 iconDiv.setAttribute('volume-subtype',
241 this.volumeManager_.getDeviceType(path));
242 }
243 li.appendChild(iconDiv);
244 314
245 var div = cr.doc.createElement('div'); 315 if (this.contextMenu_)
246 div.className = 'root-label'; 316 item.setContextMenu(this.contextMenu_);
247
248 div.textContent = PathUtil.getFolderLabel(path);
249 li.appendChild(div);
250
251 if (rootType === RootType.ARCHIVE || rootType === RootType.REMOVABLE) {
252 var eject = cr.doc.createElement('div');
253 eject.className = 'root-eject';
254 eject.addEventListener('click', function(event) {
255 event.stopPropagation();
256 var unmountCommand = cr.doc.querySelector('command#unmount');
257 // Let's make sure 'canExecute' state of the command is properly set for
258 // the root before executing it.
259 unmountCommand.canExecuteChange(li);
260 unmountCommand.execute(li);
261 }.bind(this));
262 // Block other mouse handlers.
263 eject.addEventListener('mouseup', function(e) { e.stopPropagation() });
264 eject.addEventListener('mousedown', function(e) { e.stopPropagation() });
265 li.appendChild(eject);
266 }
267
268 if (this.contextMenu_ && (!isRoot ||
269 rootType != RootType.DRIVE && rootType != RootType.DOWNLOADS))
270 cr.ui.contextMenuHandler.setContextMenu(li, this.contextMenu_);
271
272 cr.defineProperty(li, 'lead', cr.PropertyKind.BOOL_ATTR);
273 cr.defineProperty(li, 'selected', cr.PropertyKind.BOOL_ATTR);
274 317
275 // If the current directory is already set. 318 // If the current directory is already set.
276 if (this.currentVolume_ == path) { 319 if (this.currentVolume_ == path) {
277 setTimeout(function() { 320 setTimeout(function() {
278 this.selectedItem = path; 321 this.selectedItem = path;
279 }.bind(this), 0); 322 }.bind(this), 0);
280 } 323 }
281 324 return item;
282 return li;
283 }; 325 };
284 326
285 /** 327 /**
286 * Sets a context menu. Context menu is enabled only on archive and removable 328 * Sets a context menu. Context menu is enabled only on archive and removable
287 * volumes as of now. 329 * volumes as of now.
288 * 330 *
289 * @param {cr.ui.Menu} menu Context menu. 331 * @param {cr.ui.Menu} menu Context menu.
290 */ 332 */
291 VolumeList.prototype.setContextMenu = function(menu) { 333 VolumeList.prototype.setContextMenu = function(menu) {
292 this.contextMenu_ = menu; 334 this.contextMenu_ = menu;
293 335
294 for (var i = 0; i < this.dataModel.length; i++) { 336 for (var i = 0; i < this.dataModel.length; i++) {
295 var path = this.dataModel.item(i); 337 this.getListItemByIndex(i).setContextMenu(this.contextMenu_);
mtomasz 2013/08/06 01:33:06 nit: I think the if logic here was a cleaner desig
296 var itemType = this.dataModel.getItemType(i);
297 var type = PathUtil.getRootType(path);
298 // Context menu is set only to archive and removable volumes.
299 if (itemType == VolumeListModel.ItemType.PINNED ||
300 type == RootType.ARCHIVE || type == RootType.REMOVABLE) {
301 cr.ui.contextMenuHandler.setContextMenu(this.getListItemByIndex(i),
302 this.contextMenu_);
303 }
304 } 338 }
305 }; 339 };
306 340
307 /** 341 /**
308 * Selects the n-th volume from the list. 342 * Selects the n-th volume from the list.
309 * @param {number} index Volume index. 343 * @param {number} index Volume index.
310 * @return {boolean} True for success, otherwise false. 344 * @return {boolean} True for success, otherwise false.
311 */ 345 */
312 VolumeList.prototype.selectByIndex = function(index) { 346 VolumeList.prototype.selectByIndex = function(index) {
313 if (index < 0 || index > this.dataModel.length - 1) 347 if (index < 0 || index > this.dataModel.length - 1)
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
387 var itemPath = this.dataModel.item(i); 421 var itemPath = this.dataModel.item(i);
388 if (PathUtil.getRootPath(itemPath) == newRootPath) { 422 if (PathUtil.getRootPath(itemPath) == newRootPath) {
389 // Not to invoke the handler of this instance, sets the guard. 423 // Not to invoke the handler of this instance, sets the guard.
390 this.dontHandleSelectionEvent_ = true; 424 this.dontHandleSelectionEvent_ = true;
391 this.selectionModel.selectedIndex = i; 425 this.selectionModel.selectedIndex = i;
392 this.dontHandleSelectionEvent_ = false; 426 this.dontHandleSelectionEvent_ = false;
393 return; 427 return;
394 } 428 }
395 } 429 }
396 }; 430 };
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698