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

Side by Side Diff: ui/file_manager/gallery/js/gallery.js

Issue 853653004: Gallery: Add items to GalleryDataModel before loading their metadata. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixed. Created 5 years, 8 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 * Overrided metadata worker's path. 6 * Overrided metadata worker's path.
7 * @type {string} 7 * @type {string}
8 */ 8 */
9 ContentMetadataProvider.WORKER_SCRIPT = '/js/metadata_worker.js'; 9 ContentMetadataProvider.WORKER_SCRIPT = '/js/metadata_worker.js';
10 10
(...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after
284 */ 284 */
285 Gallery.prototype.load = function(selectedEntries) { 285 Gallery.prototype.load = function(selectedEntries) {
286 GalleryUtil.createEntrySet(selectedEntries).then(function(allEntries) { 286 GalleryUtil.createEntrySet(selectedEntries).then(function(allEntries) {
287 this.loadInternal_(allEntries, selectedEntries); 287 this.loadInternal_(allEntries, selectedEntries);
288 }.bind(this)); 288 }.bind(this));
289 }; 289 };
290 290
291 /** 291 /**
292 * Loads the content. 292 * Loads the content.
293 * 293 *
294 * @param {!Array.<!Entry>} entries Array of entries. 294 * @param {!Array.<!FileEntry>} entries Array of entries.
295 * @param {!Array.<!Entry>} selectedEntries Array of selected entries. 295 * @param {!Array.<!FileEntry>} selectedEntries Array of selected entries.
296 * @private 296 * @private
297 */ 297 */
298 Gallery.prototype.loadInternal_ = function(entries, selectedEntries) { 298 Gallery.prototype.loadInternal_ = function(entries, selectedEntries) {
299 // Add the entries to data model.
300 var items = [];
301 for (var i = 0; i < entries.length; i++) {
302 var locationInfo = this.volumeManager_.getLocationInfo(entries[i]);
303 if (!locationInfo) // Skip the item, since gone.
304 return;
305 items.push(new Gallery.Item(
306 entries[i],
307 locationInfo,
308 null,
309 null,
310 true));
311 }
312 this.dataModel_.splice(0, this.dataModel_.length);
313 this.updateThumbnails_(); // Remove the caches.
314
315 GalleryDataModel.prototype.splice.apply(
316 this.dataModel_, [0, 0].concat(items));
317
318 // Apply the selection.
319 var selectedSet = {};
320 for (var i = 0; i < selectedEntries.length; i++) {
321 selectedSet[selectedEntries[i].toURL()] = true;
322 }
323 for (var i = 0; i < items.length; i++) {
324 if (!selectedSet[items[i].getEntry().toURL()])
325 continue;
326 this.selectionModel_.setIndexSelected(i, true);
327 }
328 this.onSelection_();
329
299 // Obtains max chank size. 330 // Obtains max chank size.
300 var maxChunkSize = 20; 331 var maxChunkSize = 20;
301 var volumeInfo = this.volumeManager_.getVolumeInfo(entries[0]); 332 var volumeInfo = this.volumeManager_.getVolumeInfo(entries[0]);
302 if (volumeInfo) { 333 if (volumeInfo) {
303 if (volumeInfo.volumeType === VolumeManagerCommon.VolumeType.MTP) 334 if (volumeInfo.volumeType === VolumeManagerCommon.VolumeType.MTP)
304 maxChunkSize = 1; 335 maxChunkSize = 1;
305 if (volumeInfo.isReadOnly) 336 if (volumeInfo.isReadOnly)
306 this.context_.readonlyDirName = volumeInfo.label; 337 this.context_.readonlyDirName = volumeInfo.label;
307 } 338 }
308 339
309 // Make loading list. 340 // If items are empty, stop initialization.
310 var entrySet = {}; 341 if (items.length === 0) {
311 for (var i = 0; i < entries.length; i++) {
312 var entry = entries[i];
313 entrySet[entry.toURL()] = {
314 entry: entry,
315 selected: false,
316 index: i
317 };
318 }
319 for (var i = 0; i < selectedEntries.length; i++) {
320 var entry = selectedEntries[i];
321 entrySet[entry.toURL()] = {
322 entry: entry,
323 selected: true,
324 index: i
325 };
326 }
327 var loadingList = [];
328 for (var url in entrySet) {
329 loadingList.push(entrySet[url]);
330 }
331 loadingList = loadingList.sort(function(a, b) {
332 if (a.selected && !b.selected)
333 return -1;
334 else if (!a.selected && b.selected)
335 return 1;
336 else
337 return a.index - b.index;
338 });
339
340 if (loadingList.length === 0) {
341 this.dataModel_.splice(0, this.dataModel_.length); 342 this.dataModel_.splice(0, this.dataModel_.length);
342 return; 343 return;
343 } 344 }
344 345
345 // Load entries. 346 // Load entries.
346 // Use the self variable capture-by-closure because it is faster than bind. 347 // Use the self variable capture-by-closure because it is faster than bind.
347 var self = this; 348 var self = this;
348 var thumbnailModel = new ThumbnailModel(this.metadataModel_); 349 var thumbnailModel = new ThumbnailModel(this.metadataModel_);
349 var loadChunk = function(firstChunk) { 350 var loadChunk = function(firstChunk) {
350 // Extract chunk. 351 // Extract chunk.
351 var chunk = loadingList.splice(0, maxChunkSize); 352 var chunk = items.splice(0, maxChunkSize);
352 if (!chunk.length) 353 if (!chunk.length)
353 return; 354 return;
354 var entries = chunk.map(function(chunkItem) { 355 var entries = chunk.map(function(chunkItem) {
355 return chunkItem.entry; 356 return chunkItem.getEntry();
356 }); 357 });
357 var metadataPromise = self.metadataModel_.get( 358 var metadataPromise = self.metadataModel_.get(
358 entries, Gallery.PREFETCH_PROPERTY_NAMES); 359 entries, Gallery.PREFETCH_PROPERTY_NAMES);
359 var thumbnailPromise = thumbnailModel.get(entries); 360 var thumbnailPromise = thumbnailModel.get(entries);
360 return Promise.all([metadataPromise, thumbnailPromise]).then( 361 return Promise.all([metadataPromise, thumbnailPromise]).then(
361 function(metadataLists) { 362 function(metadataLists) {
362 // Remove all the previous items if it's the first chunk. 363 // Add items to the model.
363 // Do it here because prevent a flicker between removing all the items 364 chunk.forEach(function(chunkItem, index) {
364 // and adding new ones. 365 chunkItem.setMetadataItem(metadataLists[0][index]);
365 if (firstChunk) { 366 chunkItem.setThumbnailMetadataItem(metadataLists[1][index]);
366 self.dataModel_.splice(0, self.dataModel_.length);
367 self.updateThumbnails_(); // Remove the caches.
368 }
369 367
370 // Add items to the model. 368 if (!firstChunk || self.initialized_) {
371 var items = []; 369 var event = new Event('content');
372 chunk.forEach(function(chunkItem, index) { 370 event.item = chunkItem;
373 var locationInfo = self.volumeManager_.getLocationInfo(chunkItem.entry); 371 event.oldEntry = chunkItem.getEntry();
374 if (!locationInfo) // Skip the item, since gone. 372 event.thumbnailChanged = true;
375 return; 373 self.dataModel_.dispatchEvent(event);
376 items.push(new Gallery.Item( 374 }
377 chunkItem.entry,
378 locationInfo,
379 metadataLists[0][index],
380 metadataLists[1][index],
381 /* original */ true));
382 }); 375 });
383 self.dataModel_.push.apply(self.dataModel_, items);
384
385 // Apply the selection.
386 var selectionUpdated = false;
387 for (var i = 0; i < chunk.length; i++) {
388 if (!chunk[i].selected)
389 continue;
390 var index = self.dataModel_.indexOf(items[i]);
391 if (index < 0)
392 continue;
393 self.selectionModel_.setIndexSelected(index, true);
394 selectionUpdated = true;
395 }
396 if (selectionUpdated)
397 self.onSelection_();
398 376
399 // Init modes after the first chunk is loaded. 377 // Init modes after the first chunk is loaded.
400 if (firstChunk && !self.initialized_) { 378 if (firstChunk && !self.initialized_) {
401 // Determine the initial mode. 379 // Determine the initial mode.
402 var shouldShowMosaic = selectedEntries.length > 1 || 380 var shouldShowMosaic = selectedEntries.length > 1 ||
403 (self.context_.pageState && 381 (self.context_.pageState &&
404 self.context_.pageState.gallery === 'mosaic'); 382 self.context_.pageState.gallery === 'mosaic');
405 self.setCurrentMode_( 383 self.setCurrentMode_(
406 shouldShowMosaic ? self.mosaicMode_ : self.slideMode_); 384 shouldShowMosaic ? self.mosaicMode_ : self.slideMode_);
407 385
(...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after
666 return 0 <= index && index < this.dataModel_.length; 644 return 0 <= index && index < this.dataModel_.length;
667 }.bind(this)); 645 }.bind(this));
668 }; 646 };
669 647
670 /** 648 /**
671 * Content change event handler. 649 * Content change event handler.
672 * @param {!Event} event Event. 650 * @param {!Event} event Event.
673 * @private 651 * @private
674 */ 652 */
675 Gallery.prototype.onContentChange_ = function(event) { 653 Gallery.prototype.onContentChange_ = function(event) {
676 var index = this.dataModel_.indexOf(event.item);
677 if (index !== this.selectionModel_.selectedIndex)
678 console.error('Content changed for unselected item');
679 this.updateSelectionAndState_(); 654 this.updateSelectionAndState_();
680 }; 655 };
681 656
682 /** 657 /**
683 * Keydown handler. 658 * Keydown handler.
684 * 659 *
685 * @param {!Event} event Event. 660 * @param {!Event} event Event.
686 * @private 661 * @private
687 */ 662 */
688 Gallery.prototype.onKeyDown_ = function(event) { 663 Gallery.prototype.onKeyDown_ = function(event) {
(...skipping 274 matching lines...) Expand 10 before | Expand all | Expand 10 after
963 initializePromise.then(reload); 938 initializePromise.then(reload);
964 939
965 /** 940 /**
966 * Enteres the debug mode. 941 * Enteres the debug mode.
967 */ 942 */
968 window.debugMe = function() { 943 window.debugMe = function() {
969 initializePromise.then(function() { 944 initializePromise.then(function() {
970 gallery.debugMe(); 945 gallery.debugMe();
971 }); 946 });
972 }; 947 };
OLDNEW
« no previous file with comments | « ui/file_manager/gallery/css/gallery.css ('k') | ui/file_manager/gallery/js/gallery_data_model.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698