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} |
41 */ | 31 */ |
42 function NavigationModelShortcutItem(label, entry) { | 32 function NavigationModelShortcutItem(label, entry) { |
43 NavigationModelItem.call(this, label); | 33 NavigationModelItem.call(this, label); |
44 this.entry_ = entry; | 34 this.entry_ = entry; |
45 Object.freeze(this); | 35 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} |
62 */ | 51 */ |
63 function NavigationModelVolumeItem(label, volumeInfo) { | 52 function NavigationModelVolumeItem(label, volumeInfo) { |
64 NavigationModelItem.call(this, label); | 53 NavigationModelItem.call(this, label); |
65 this.volumeInfo_ = volumeInfo; | 54 this.volumeInfo_ = volumeInfo; |
66 // Start resolving the display root because it is used | 55 // Start resolving the display root because it is used |
67 // for determining executability of commands. | 56 // for determining executability of commands. |
68 this.volumeInfo_.resolveDisplayRoot( | 57 this.volumeInfo_.resolveDisplayRoot( |
69 function() {}, function() {}); | 58 function() {}, function() {}); |
70 Object.freeze(this); | 59 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. |
hirono
2015/04/01 11:50:36
Cannot be this wrapper of cr.ui.Command? We usuall
mtomasz
2015/04/02 02:24:27
Done.
| |
70 * | |
71 * @param {string} label | |
72 * @param {string} icon | |
73 * @param {function()} callback | |
74 * @constructor | |
75 * @extends {NavigationModelItem} | |
hirono
2015/04/01 11:50:36
Can we add @struct?
mtomasz
2015/04/02 02:24:27
Done.
| |
76 */ | |
77 function NavigationModelCommandItem(label, icon, callback) { | |
78 NavigationModelItem.call(this, label); | |
79 this.icon_ = icon; | |
80 this.callback_ = callback; | |
81 Object.freeze(this); | |
hirono
2015/04/01 11:50:36
We no longer relay on Object.freeze. crbug.com/412
mtomasz
2015/04/02 02:24:27
Done.
| |
82 } | |
83 | |
84 NavigationModelCommandItem.prototype = { | |
85 __proto__: NavigationModelItem.prototype, | |
86 get icon() { return this.icon_; }, | |
87 get callback() { return this.callback_; }, | |
88 get type() { return NavigationModelItem.Type.COMMAND; } | |
89 }; | |
90 | |
91 /** | |
92 * A navigation list model. This model combines multiple models. | |
82 * @param {VolumeManagerWrapper} volumeManager VolumeManagerWrapper instance. | 93 * @param {VolumeManagerWrapper} volumeManager VolumeManagerWrapper instance. |
83 * @param {(cr.ui.ArrayDataModel|FolderShortcutsDataModel)} shortcutListModel | 94 * @param {(cr.ui.ArrayDataModel|FolderShortcutsDataModel)} shortcutListModel |
84 * The list of folder shortcut. | 95 * The list of folder shortcut. |
96 * @param {NavigationModelCommandItem} commandModel, Command button at the | |
97 * end of the list. | |
85 * @constructor | 98 * @constructor |
86 * @extends {cr.EventTarget} | 99 * @extends {cr.EventTarget} |
87 */ | 100 */ |
88 function NavigationListModel(volumeManager, shortcutListModel) { | 101 function NavigationListModel(volumeManager, shortcutListModel, commandModel) { |
89 cr.EventTarget.call(this); | 102 cr.EventTarget.call(this); |
90 | 103 |
91 this.volumeManager_ = volumeManager; | 104 this.volumeManager_ = volumeManager; |
92 this.shortcutListModel_ = shortcutListModel; | 105 this.shortcutListModel_ = shortcutListModel; |
106 this.commandModel_ = commandModel; | |
93 | 107 |
94 var volumeInfoToModelItem = function(volumeInfo) { | 108 var volumeInfoToModelItem = function(volumeInfo) { |
95 return new NavigationModelVolumeItem( | 109 return new NavigationModelVolumeItem( |
96 volumeInfo.label, | 110 volumeInfo.label, |
97 volumeInfo); | 111 volumeInfo); |
98 }.bind(this); | 112 }.bind(this); |
99 | 113 |
100 var entryToModelItem = function(entry) { | 114 var entryToModelItem = function(entry) { |
101 var item = new NavigationModelShortcutItem( | 115 var item = new NavigationModelShortcutItem( |
102 entry.name, | 116 entry.name, |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
235 get length() { return this.length_(); }, | 249 get length() { return this.length_(); }, |
236 get folderShortcutList() { return this.shortcutList_; } | 250 get folderShortcutList() { return this.shortcutList_; } |
237 }; | 251 }; |
238 | 252 |
239 /** | 253 /** |
240 * Returns the item at the given index. | 254 * Returns the item at the given index. |
241 * @param {number} index The index of the entry to get. | 255 * @param {number} index The index of the entry to get. |
242 * @return {NavigationModelItem} The item at the given index. | 256 * @return {NavigationModelItem} The item at the given index. |
243 */ | 257 */ |
244 NavigationListModel.prototype.item = function(index) { | 258 NavigationListModel.prototype.item = function(index) { |
245 var offset = this.volumeList_.length; | 259 if (index < this.volumeList_.length) |
246 if (index < offset) | |
247 return this.volumeList_[index]; | 260 return this.volumeList_[index]; |
248 return this.shortcutList_[index - offset]; | 261 if (index < this.volumeList_.length + this.shortcutList_.length) |
262 return this.shortcutList_[index - this.volumeList_.length]; | |
263 if (index === this.length_() - 1) | |
264 return this.commandModel_; | |
249 }; | 265 }; |
250 | 266 |
251 /** | 267 /** |
252 * Returns the number of items in the model. | 268 * Returns the number of items in the model. |
253 * @return {number} The length of the model. | 269 * @return {number} The length of the model. |
254 * @private | 270 * @private |
255 */ | 271 */ |
256 NavigationListModel.prototype.length_ = function() { | 272 NavigationListModel.prototype.length_ = function() { |
257 return this.volumeList_.length + this.shortcutList_.length; | 273 return this.volumeList_.length + this.shortcutList_.length + |
274 (this.commandModel_ ? 1 : 0); | |
hirono
2015/04/01 11:50:36
nit: Please fix indent.
hirono
2015/04/02 04:27:24
Please fix this.
mtomasz
2015/04/02 04:51:50
Done.
| |
258 }; | 275 }; |
259 | 276 |
260 /** | 277 /** |
261 * Returns the first matching item. | 278 * Returns the first matching item. |
262 * @param {NavigationModelItem} modelItem The entry to find. | 279 * @param {NavigationModelItem} modelItem The entry to find. |
263 * @param {number=} opt_fromIndex If provided, then the searching start at | 280 * @param {number=} opt_fromIndex If provided, then the searching start at |
264 * the {@code opt_fromIndex}. | 281 * the {@code opt_fromIndex}. |
265 * @return {number} The index of the first found element or -1 if not found. | 282 * @return {number} The index of the first found element or -1 if not found. |
266 */ | 283 */ |
267 NavigationListModel.prototype.indexOf = function(modelItem, opt_fromIndex) { | 284 NavigationListModel.prototype.indexOf = function(modelItem, opt_fromIndex) { |
268 for (var i = opt_fromIndex || 0; i < this.length; i++) { | 285 for (var i = opt_fromIndex || 0; i < this.length; i++) { |
269 if (modelItem === this.item(i)) | 286 if (modelItem === this.item(i)) |
270 return i; | 287 return i; |
271 } | 288 } |
272 return -1; | 289 return -1; |
273 }; | 290 }; |
274 | 291 |
275 /** | 292 /** |
276 * Called externally when one of the items is not found on the filesystem. | 293 * Called externally when one of the items is not found on the filesystem. |
277 * @param {NavigationModelItem} modelItem The entry which is not found. | 294 * @param {NavigationModelItem} modelItem The entry which is not found. |
278 */ | 295 */ |
279 NavigationListModel.prototype.onItemNotFoundError = function(modelItem) { | 296 NavigationListModel.prototype.onItemNotFoundError = function(modelItem) { |
280 if (modelItem.isVolume) { | 297 switch (modelItem.type) { |
281 // TODO(mtomasz, yoshiki): Implement when needed. | 298 case NavigationModelItem.Type.VOLUME: |
282 return; | 299 // TODO(mtomasz, yoshiki): Implement when needed. |
hirono
2015/04/01 11:50:36
If we know when it becomes to be needed, please ad
hirono
2015/04/02 04:27:24
ditto.
mtomasz
2015/04/02 04:51:50
Done.
| |
283 } | 300 break; |
284 if (modelItem.isShortcut) { | 301 case NavigationModelItem.Type.SHORTCUT: |
285 // For shortcuts, lets the shortcut model handle this situation. | 302 // For shortcuts, lets the shortcut model handle this situation. |
286 this.shortcutListModel_.onItemNotFoundError(modelItem.entry); | 303 this.shortcutListModel_.onItemNotFoundError(modelItem.entry); |
304 break; | |
287 } | 305 } |
288 }; | 306 }; |
OLD | NEW |