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

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: addressed comment 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(). This check prevents double-decorating.
148 if (this.className)
149 return;
150
151 this.className = 'root-item';
152 this.setAttribute('role', 'option');
153
154 this.iconDiv_ = cr.doc.createElement('div');
155 this.iconDiv_.className = 'volume-icon';
156 this.appendChild(this.iconDiv_);
157
158 this.label_ = cr.doc.createElement('div');
159 this.label_.className = 'root-label';
160 this.appendChild(this.label_);
161
162 cr.defineProperty(this, 'lead', cr.PropertyKind.BOOL_ATTR);
163 cr.defineProperty(this, 'selected', cr.PropertyKind.BOOL_ATTR);
164 };
165
166 /**
167 * Associate a path with this item.
168 * @param {string} path Path of this item.
169 */
170 VolumeItem.prototype.setPath = function(path) {
171 if (this.path_)
172 console.warn('VolumeItem.setPath should be called only once.');
173
174 this.path_ = path;
175
176 var rootType = PathUtil.getRootType(path);
177
178 this.iconDiv_.setAttribute('volume-type-icon', rootType);
179 if (rootType === RootType.REMOVABLE) {
180 this.iconDiv_.setAttribute('volume-subtype',
181 VolumeManager.getInstance().getDeviceType(path));
182 }
183
184 this.label_.textContent = PathUtil.getFolderLabel(path);
185
186 if (rootType === RootType.ARCHIVE || rootType === RootType.REMOVABLE) {
187 this.eject_ = cr.doc.createElement('div');
188 // Block other mouse handlers.
189 this.eject_.addEventListener(
190 'mouseup', function(e) { e.stopPropagation() });
191 this.eject_.addEventListener(
192 'mousedown', function(e) { e.stopPropagation() });
193
194 this.eject_.className = 'root-eject';
195 this.eject_.addEventListener('click', function(event) {
196 event.stopPropagation();
197 cr.dispatchSimpleEvent(this, 'eject');
198 }.bind(this));
199
200 this.appendChild(this.eject_);
201 }
202 };
203
204 /**
205 * Associate a context menu with this item.
206 * @param {cr.ui.Menu} menu Menu this item.
207 */
208 VolumeItem.prototype.maybeSetContextMenu = function(menu) {
209 if (!this.path_) {
210 console.error(
211 'VolumeItem.maybeSetContextMenu must be called after setPath().');
212 return;
213 }
214
215 var isRoot = PathUtil.isRootPath(this.path_);
216 var rootType = PathUtil.getRootType(this.path_);
217 // The context menu is shown on the following items:
218 // - Removable and Archive volumes
219 // - Folder shortcuts
220 if (!isRoot ||
221 (rootType != RootType.DRIVE && rootType != RootType.DOWNLOADS))
222 cr.ui.contextMenuHandler.setContextMenu(this, menu);
223 };
224
225 /**
147 * A volume list. 226 * A volume list.
148 * @constructor 227 * @constructor
149 * @extends {cr.ui.List} 228 * @extends {cr.ui.List}
150 */ 229 */
151 function VolumeList() { 230 function VolumeList() {
152 } 231 }
153 232
154 /** 233 /**
155 * VolumeList inherits cr.ui.List. 234 * VolumeList inherits cr.ui.List.
156 */ 235 */
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
209 }; 288 };
210 289
211 /** 290 /**
212 * Creates an element of a volume. This method is called from cr.ui.List 291 * Creates an element of a volume. This method is called from cr.ui.List
213 * internally. 292 * internally.
214 * @param {string} path Path of the directory to be rendered. 293 * @param {string} path Path of the directory to be rendered.
215 * @return {VolumeItem} Rendered element. 294 * @return {VolumeItem} Rendered element.
216 * @private 295 * @private
217 */ 296 */
218 VolumeList.prototype.renderRoot_ = function(path) { 297 VolumeList.prototype.renderRoot_ = function(path) {
219 var li = new VolumeItem; 298 var item = new VolumeItem();
220 // TODO(yoshiki): Move the following initialization code to the constructor 299 item.setPath(path);
221 // of VolumeItem. 300
222 li.className = 'root-item';
223 li.setAttribute('role', 'option');
224 var dm = this.directoryModel_;
225 var handleClick = function() { 301 var handleClick = function() {
226 if (li.selected && path !== dm.getCurrentDirPath()) { 302 if (item.selected && path !== this.directoryModel_.getCurrentDirPath()) {
227 this.directoryModel_.changeDirectory(path); 303 this.directoryModel_.changeDirectory(path);
228 } 304 }
229 }.bind(this); 305 }.bind(this);
230 li.addEventListener('click', handleClick); 306 item.addEventListener('click', handleClick);
231 li.addEventListener(cr.ui.TouchHandler.EventType.TOUCH_START, handleClick);
232 307
233 var isRoot = PathUtil.isRootPath(path); 308 var handleEject = function() {
234 var rootType = PathUtil.getRootType(path); 309 var unmountCommand = cr.doc.querySelector('command#unmount');
310 // Let's make sure 'canExecute' state of the command is properly set for
311 // the root before executing it.
312 unmountCommand.canExecuteChange(item);
313 unmountCommand.execute(item);
314 };
315 item.addEventListener('eject', handleEject);
316 // TODO(yoshiki): Check if the following touch handler is necessary or not.
317 // If unnecessary, remove it.
318 item.addEventListener(cr.ui.TouchHandler.EventType.TOUCH_START, handleClick);
235 319
236 var iconDiv = cr.doc.createElement('div'); 320 if (this.contextMenu_)
237 iconDiv.className = 'volume-icon'; 321 item.maybeSetContextMenu(this.contextMenu_);
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
245 var div = cr.doc.createElement('div');
246 div.className = 'root-label';
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 322
275 // If the current directory is already set. 323 // If the current directory is already set.
276 if (this.currentVolume_ == path) { 324 if (this.currentVolume_ == path) {
277 setTimeout(function() { 325 setTimeout(function() {
278 this.selectedItem = path; 326 this.selectedItem = path;
279 }.bind(this), 0); 327 }.bind(this), 0);
280 } 328 }
281 329 return item;
282 return li;
283 }; 330 };
284 331
285 /** 332 /**
286 * Sets a context menu. Context menu is enabled only on archive and removable 333 * Sets a context menu. Context menu is enabled only on archive and removable
287 * volumes as of now. 334 * volumes as of now.
288 * 335 *
289 * @param {cr.ui.Menu} menu Context menu. 336 * @param {cr.ui.Menu} menu Context menu.
290 */ 337 */
291 VolumeList.prototype.setContextMenu = function(menu) { 338 VolumeList.prototype.setContextMenu = function(menu) {
292 this.contextMenu_ = menu; 339 this.contextMenu_ = menu;
293 340
294 for (var i = 0; i < this.dataModel.length; i++) { 341 for (var i = 0; i < this.dataModel.length; i++) {
295 var path = this.dataModel.item(i); 342 this.getListItemByIndex(i).maybeSetContextMenu(this.contextMenu_);
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 } 343 }
305 }; 344 };
306 345
307 /** 346 /**
308 * Selects the n-th volume from the list. 347 * Selects the n-th volume from the list.
309 * @param {number} index Volume index. 348 * @param {number} index Volume index.
310 * @return {boolean} True for success, otherwise false. 349 * @return {boolean} True for success, otherwise false.
311 */ 350 */
312 VolumeList.prototype.selectByIndex = function(index) { 351 VolumeList.prototype.selectByIndex = function(index) {
313 if (index < 0 || index > this.dataModel.length - 1) 352 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); 426 var itemPath = this.dataModel.item(i);
388 if (PathUtil.getRootPath(itemPath) == newRootPath) { 427 if (PathUtil.getRootPath(itemPath) == newRootPath) {
389 // Not to invoke the handler of this instance, sets the guard. 428 // Not to invoke the handler of this instance, sets the guard.
390 this.dontHandleSelectionEvent_ = true; 429 this.dontHandleSelectionEvent_ = true;
391 this.selectionModel.selectedIndex = i; 430 this.selectionModel.selectedIndex = i;
392 this.dontHandleSelectionEvent_ = false; 431 this.dontHandleSelectionEvent_ = false;
393 return; 432 return;
394 } 433 }
395 } 434 }
396 }; 435 };
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