| OLD | NEW |
| 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 /** | 5 /** |
| 6 * Base item of NavigationListModel. Should not be created directly. | 6 * Base item of NavigationListModel. Should not be created directly. |
| 7 * @param {string} label Label. | 7 * @param {string} label Label. |
| 8 * @constructor | 8 * @constructor |
| 9 */ | 9 */ |
| 10 function NavigationModelItem(label) { | 10 function NavigationModelItem(label) { |
| 11 this.label_ = label; | 11 this.label_ = label; |
| 12 } | 12 } |
| 13 | 13 |
| 14 NavigationModelItem.Type = { |
| 15 SHORTCUT: 'shortcut', |
| 16 VOLUME: 'volume', |
| 17 COMMAND: 'command' |
| 18 }; |
| 19 |
| 14 NavigationModelItem.prototype = { | 20 NavigationModelItem.prototype = { |
| 15 get label() { return this.label_; } | 21 get label() { return this.label_; } |
| 16 }; | 22 }; |
| 17 | 23 |
| 18 /** | 24 /** |
| 19 * Check whether given two model items are same. | |
| 20 * @param {NavigationModelItem} item1 The first item to be compared. | |
| 21 * @param {NavigationModelItem} item2 The second item to be compared. | |
| 22 * @return {boolean} True if given two model items are same. | |
| 23 */ | |
| 24 NavigationModelItem.isSame = function(item1, item2) { | |
| 25 if (item1.isVolume != item2.isVolume) | |
| 26 return false; | |
| 27 | |
| 28 if (item1.isVolume) | |
| 29 return item1.volumeInfo === item2.volumeInfo; | |
| 30 else | |
| 31 return util.isSameEntry(item1.entry, item2.entry); | |
| 32 }; | |
| 33 | |
| 34 /** | |
| 35 * Item of NavigationListModel for shortcuts. | 25 * Item of NavigationListModel for shortcuts. |
| 36 * | 26 * |
| 37 * @param {string} label Label. | 27 * @param {string} label Label. |
| 38 * @param {!DirectoryEntry} entry Entry. Cannot be null. | 28 * @param {!DirectoryEntry} entry Entry. Cannot be null. |
| 39 * @constructor | 29 * @constructor |
| 40 * @extends {NavigationModelItem} | 30 * @extends {NavigationModelItem} |
| 31 * @struct |
| 41 */ | 32 */ |
| 42 function NavigationModelShortcutItem(label, entry) { | 33 function NavigationModelShortcutItem(label, entry) { |
| 43 NavigationModelItem.call(this, label); | 34 NavigationModelItem.call(this, label); |
| 44 this.entry_ = entry; | 35 this.entry_ = entry; |
| 45 Object.freeze(this); | |
| 46 } | 36 } |
| 47 | 37 |
| 48 NavigationModelShortcutItem.prototype = { | 38 NavigationModelShortcutItem.prototype = { |
| 49 __proto__: NavigationModelItem.prototype, | 39 __proto__: NavigationModelItem.prototype, |
| 50 get entry() { return this.entry_; }, | 40 get entry() { return this.entry_; }, |
| 51 get isVolume() { return false; }, | 41 get type() { return NavigationModelItem.Type.SHORTCUT; } |
| 52 get isShortcut() { return true; } | |
| 53 }; | 42 }; |
| 54 | 43 |
| 55 /** | 44 /** |
| 56 * Item of NavigationListModel for volumes. | 45 * Item of NavigationListModel for volumes. |
| 57 * | 46 * |
| 58 * @param {string} label Label. | 47 * @param {string} label Label. |
| 59 * @param {!VolumeInfo} volumeInfo Volume info for the volume. Cannot be null. | 48 * @param {!VolumeInfo} volumeInfo Volume info for the volume. Cannot be null. |
| 60 * @constructor | 49 * @constructor |
| 61 * @extends {NavigationModelItem} | 50 * @extends {NavigationModelItem} |
| 51 * @struct |
| 62 */ | 52 */ |
| 63 function NavigationModelVolumeItem(label, volumeInfo) { | 53 function NavigationModelVolumeItem(label, volumeInfo) { |
| 64 NavigationModelItem.call(this, label); | 54 NavigationModelItem.call(this, label); |
| 65 this.volumeInfo_ = volumeInfo; | 55 this.volumeInfo_ = volumeInfo; |
| 66 // Start resolving the display root because it is used | 56 // Start resolving the display root because it is used |
| 67 // for determining executability of commands. | 57 // for determining executability of commands. |
| 68 this.volumeInfo_.resolveDisplayRoot( | 58 this.volumeInfo_.resolveDisplayRoot( |
| 69 function() {}, function() {}); | 59 function() {}, function() {}); |
| 70 Object.freeze(this); | |
| 71 } | 60 } |
| 72 | 61 |
| 73 NavigationModelVolumeItem.prototype = { | 62 NavigationModelVolumeItem.prototype = { |
| 74 __proto__: NavigationModelItem.prototype, | 63 __proto__: NavigationModelItem.prototype, |
| 75 get volumeInfo() { return this.volumeInfo_; }, | 64 get volumeInfo() { return this.volumeInfo_; }, |
| 76 get isVolume() { return true; }, | 65 get type() { return NavigationModelItem.Type.VOLUME; } |
| 77 get isShortcut() { return false; } | |
| 78 }; | 66 }; |
| 79 | 67 |
| 80 /** | 68 /** |
| 81 * A navigation list model. This model combines the 2 lists. | 69 * Item of NavigationListModel for commands. |
| 70 * |
| 71 * @param {cr.ui.Command} command |
| 72 * @constructor |
| 73 * @extends {NavigationModelItem} |
| 74 * @struct |
| 75 */ |
| 76 function NavigationModelCommandItem(command) { |
| 77 NavigationModelItem.call(this, command.label); |
| 78 this.command_ = command; |
| 79 } |
| 80 |
| 81 NavigationModelCommandItem.prototype = { |
| 82 __proto__: NavigationModelItem.prototype, |
| 83 get command() { return this.command_; }, |
| 84 get type() { return NavigationModelItem.Type.COMMAND; } |
| 85 }; |
| 86 |
| 87 /** |
| 88 * A navigation list model. This model combines multiple models. |
| 82 * @param {VolumeManagerWrapper} volumeManager VolumeManagerWrapper instance. | 89 * @param {VolumeManagerWrapper} volumeManager VolumeManagerWrapper instance. |
| 83 * @param {(cr.ui.ArrayDataModel|FolderShortcutsDataModel)} shortcutListModel | 90 * @param {(cr.ui.ArrayDataModel|FolderShortcutsDataModel)} shortcutListModel |
| 84 * The list of folder shortcut. | 91 * The list of folder shortcut. |
| 92 * @param {NavigationModelCommandItem} commandModel, Command button at the |
| 93 * end of the list. |
| 85 * @constructor | 94 * @constructor |
| 86 * @extends {cr.EventTarget} | 95 * @extends {cr.EventTarget} |
| 87 */ | 96 */ |
| 88 function NavigationListModel(volumeManager, shortcutListModel) { | 97 function NavigationListModel(volumeManager, shortcutListModel, commandModel) { |
| 89 cr.EventTarget.call(this); | 98 cr.EventTarget.call(this); |
| 90 | 99 |
| 91 this.volumeManager_ = volumeManager; | 100 this.volumeManager_ = volumeManager; |
| 92 this.shortcutListModel_ = shortcutListModel; | 101 this.shortcutListModel_ = shortcutListModel; |
| 102 this.commandModel_ = commandModel; |
| 93 | 103 |
| 94 var volumeInfoToModelItem = function(volumeInfo) { | 104 var volumeInfoToModelItem = function(volumeInfo) { |
| 95 return new NavigationModelVolumeItem( | 105 return new NavigationModelVolumeItem( |
| 96 volumeInfo.label, | 106 volumeInfo.label, |
| 97 volumeInfo); | 107 volumeInfo); |
| 98 }.bind(this); | 108 }.bind(this); |
| 99 | 109 |
| 100 var entryToModelItem = function(entry) { | 110 var entryToModelItem = function(entry) { |
| 101 var item = new NavigationModelShortcutItem( | 111 var item = new NavigationModelShortcutItem( |
| 102 entry.name, | 112 entry.name, |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 235 get length() { return this.length_(); }, | 245 get length() { return this.length_(); }, |
| 236 get folderShortcutList() { return this.shortcutList_; } | 246 get folderShortcutList() { return this.shortcutList_; } |
| 237 }; | 247 }; |
| 238 | 248 |
| 239 /** | 249 /** |
| 240 * Returns the item at the given index. | 250 * Returns the item at the given index. |
| 241 * @param {number} index The index of the entry to get. | 251 * @param {number} index The index of the entry to get. |
| 242 * @return {NavigationModelItem} The item at the given index. | 252 * @return {NavigationModelItem} The item at the given index. |
| 243 */ | 253 */ |
| 244 NavigationListModel.prototype.item = function(index) { | 254 NavigationListModel.prototype.item = function(index) { |
| 245 var offset = this.volumeList_.length; | 255 if (index < this.volumeList_.length) |
| 246 if (index < offset) | |
| 247 return this.volumeList_[index]; | 256 return this.volumeList_[index]; |
| 248 return this.shortcutList_[index - offset]; | 257 if (index < this.volumeList_.length + this.shortcutList_.length) |
| 258 return this.shortcutList_[index - this.volumeList_.length]; |
| 259 if (index === this.length_() - 1) |
| 260 return this.commandModel_; |
| 249 }; | 261 }; |
| 250 | 262 |
| 251 /** | 263 /** |
| 252 * Returns the number of items in the model. | 264 * Returns the number of items in the model. |
| 253 * @return {number} The length of the model. | 265 * @return {number} The length of the model. |
| 254 * @private | 266 * @private |
| 255 */ | 267 */ |
| 256 NavigationListModel.prototype.length_ = function() { | 268 NavigationListModel.prototype.length_ = function() { |
| 257 return this.volumeList_.length + this.shortcutList_.length; | 269 return this.volumeList_.length + this.shortcutList_.length + |
| 270 (this.commandModel_ ? 1 : 0); |
| 258 }; | 271 }; |
| 259 | 272 |
| 260 /** | 273 /** |
| 261 * Returns the first matching item. | 274 * Returns the first matching item. |
| 262 * @param {NavigationModelItem} modelItem The entry to find. | 275 * @param {NavigationModelItem} modelItem The entry to find. |
| 263 * @param {number=} opt_fromIndex If provided, then the searching start at | 276 * @param {number=} opt_fromIndex If provided, then the searching start at |
| 264 * the {@code opt_fromIndex}. | 277 * the {@code opt_fromIndex}. |
| 265 * @return {number} The index of the first found element or -1 if not found. | 278 * @return {number} The index of the first found element or -1 if not found. |
| 266 */ | 279 */ |
| 267 NavigationListModel.prototype.indexOf = function(modelItem, opt_fromIndex) { | 280 NavigationListModel.prototype.indexOf = function(modelItem, opt_fromIndex) { |
| 268 for (var i = opt_fromIndex || 0; i < this.length; i++) { | 281 for (var i = opt_fromIndex || 0; i < this.length; i++) { |
| 269 if (modelItem === this.item(i)) | 282 if (modelItem === this.item(i)) |
| 270 return i; | 283 return i; |
| 271 } | 284 } |
| 272 return -1; | 285 return -1; |
| 273 }; | 286 }; |
| 274 | 287 |
| 275 /** | 288 /** |
| 276 * Called externally when one of the items is not found on the filesystem. | 289 * Called externally when one of the items is not found on the filesystem. |
| 277 * @param {NavigationModelItem} modelItem The entry which is not found. | 290 * @param {NavigationModelItem} modelItem The entry which is not found. |
| 278 */ | 291 */ |
| 279 NavigationListModel.prototype.onItemNotFoundError = function(modelItem) { | 292 NavigationListModel.prototype.onItemNotFoundError = function(modelItem) { |
| 280 if (modelItem.isVolume) { | 293 if (modelItem.type === NavigationModelItem.Type.SHORTCUT) |
| 281 // TODO(mtomasz, yoshiki): Implement when needed. | |
| 282 return; | |
| 283 } | |
| 284 if (modelItem.isShortcut) { | |
| 285 // For shortcuts, lets the shortcut model handle this situation. | |
| 286 this.shortcutListModel_.onItemNotFoundError(modelItem.entry); | 294 this.shortcutListModel_.onItemNotFoundError(modelItem.entry); |
| 287 } | |
| 288 }; | 295 }; |
| OLD | NEW |