OLD | NEW |
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 ContentProvider.WORKER_SCRIPT = '/js/metadata_worker.js'; | 9 ContentProvider.WORKER_SCRIPT = '/js/metadata_worker.js'; |
10 | 10 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
47 curDirEntry: null, | 47 curDirEntry: null, |
48 searchResults: null | 48 searchResults: null |
49 }; | 49 }; |
50 this.container_ = queryRequiredElement(document, '.gallery'); | 50 this.container_ = queryRequiredElement(document, '.gallery'); |
51 this.document_ = document; | 51 this.document_ = document; |
52 this.metadataCache_ = this.context_.metadataCache; | 52 this.metadataCache_ = this.context_.metadataCache; |
53 this.volumeManager_ = volumeManager; | 53 this.volumeManager_ = volumeManager; |
54 this.selectedEntry_ = null; | 54 this.selectedEntry_ = null; |
55 this.metadataCacheObserverId_ = null; | 55 this.metadataCacheObserverId_ = null; |
56 this.onExternallyUnmountedBound_ = this.onExternallyUnmounted_.bind(this); | 56 this.onExternallyUnmountedBound_ = this.onExternallyUnmounted_.bind(this); |
| 57 this.initialized_ = false; |
57 | 58 |
58 this.dataModel_ = new GalleryDataModel( | 59 this.dataModel_ = new GalleryDataModel( |
59 this.context_.metadataCache); | 60 this.context_.metadataCache); |
60 var downloadVolumeInfo = this.volumeManager_.getCurrentProfileVolumeInfo( | 61 var downloadVolumeInfo = this.volumeManager_.getCurrentProfileVolumeInfo( |
61 VolumeManagerCommon.VolumeType.DOWNLOADS); | 62 VolumeManagerCommon.VolumeType.DOWNLOADS); |
62 downloadVolumeInfo.resolveDisplayRoot().then(function(entry) { | 63 downloadVolumeInfo.resolveDisplayRoot().then(function(entry) { |
63 this.dataModel_.fallbackSaveDirectory = entry; | 64 this.dataModel_.fallbackSaveDirectory = entry; |
64 }.bind(this)).catch(function(error) { | 65 }.bind(this)).catch(function(error) { |
65 console.error( | 66 console.error( |
66 'Failed to obtain the fallback directory: ' + (error.stack || error)); | 67 'Failed to obtain the fallback directory: ' + (error.stack || error)); |
(...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
333 } | 334 } |
334 loadingList = loadingList.sort(function(a, b) { | 335 loadingList = loadingList.sort(function(a, b) { |
335 if (a.selected && !b.selected) | 336 if (a.selected && !b.selected) |
336 return -1; | 337 return -1; |
337 else if (!a.selected && b.selected) | 338 else if (!a.selected && b.selected) |
338 return 1; | 339 return 1; |
339 else | 340 else |
340 return a.index - b.index; | 341 return a.index - b.index; |
341 }); | 342 }); |
342 | 343 |
| 344 if (loadingList.length === 0) { |
| 345 this.dataModel_.splice(0, this.dataModel_.length); |
| 346 return; |
| 347 } |
| 348 |
343 // Load entries. | 349 // Load entries. |
344 // Use the self variable capture-by-closure because it is faster than bind. | 350 // Use the self variable capture-by-closure because it is faster than bind. |
345 var self = this; | 351 var self = this; |
346 var loadChunk = function(firstChunk) { | 352 var loadChunk = function(firstChunk) { |
347 // Extract chunk. | 353 // Extract chunk. |
348 var chunk = loadingList.splice(0, maxChunkSize); | 354 var chunk = loadingList.splice(0, maxChunkSize); |
349 if (!chunk.length) | 355 if (!chunk.length) |
350 return; | 356 return; |
351 | 357 |
352 return new Promise(function(fulfill) { | 358 return new Promise(function(fulfill) { |
353 // Obtains metadata for chunk. | 359 // Obtains metadata for chunk. |
354 var entries = chunk.map(function(chunkItem) { | 360 var entries = chunk.map(function(chunkItem) { |
355 return chunkItem.entry; | 361 return chunkItem.entry; |
356 }); | 362 }); |
357 self.metadataCache_.get(entries, Gallery.METADATA_TYPE, fulfill); | 363 self.metadataCache_.get(entries, Gallery.METADATA_TYPE, fulfill); |
358 }).then(function(metadataList) { | 364 }).then(function(metadataList) { |
359 if (chunk.length !== metadataList.length) | 365 if (chunk.length !== metadataList.length) |
360 return Promise.reject('Failed to load metadata.'); | 366 return Promise.reject('Failed to load metadata.'); |
361 | 367 |
| 368 // Remove all the previous items if it's the first chunk. |
| 369 // Do it here because prevent a flicker between removing all the items |
| 370 // and adding new ones. |
| 371 if (firstChunk) { |
| 372 self.dataModel_.splice(0, self.dataModel_.length); |
| 373 self.updateThumbnails_(); // Remove the caches. |
| 374 } |
| 375 |
362 // Add items to the model. | 376 // Add items to the model. |
363 var items = []; | 377 var items = []; |
364 chunk.forEach(function(chunkItem, index) { | 378 chunk.forEach(function(chunkItem, index) { |
365 var locationInfo = self.volumeManager_.getLocationInfo(chunkItem.entry); | 379 var locationInfo = self.volumeManager_.getLocationInfo(chunkItem.entry); |
366 if (!locationInfo) // Skip the item, since gone. | 380 if (!locationInfo) // Skip the item, since gone. |
367 return; | 381 return; |
368 var clonedMetadata = MetadataCache.cloneMetadata(metadataList[index]); | 382 var clonedMetadata = MetadataCache.cloneMetadata(metadataList[index]); |
369 items.push(new Gallery.Item( | 383 items.push(new Gallery.Item( |
370 chunkItem.entry, | 384 chunkItem.entry, |
371 locationInfo, | 385 locationInfo, |
(...skipping 11 matching lines...) Expand all Loading... |
383 var index = self.dataModel_.indexOf(items[i]); | 397 var index = self.dataModel_.indexOf(items[i]); |
384 if (index < 0) | 398 if (index < 0) |
385 continue; | 399 continue; |
386 self.selectionModel_.setIndexSelected(index, true); | 400 self.selectionModel_.setIndexSelected(index, true); |
387 selectionUpdated = true; | 401 selectionUpdated = true; |
388 } | 402 } |
389 if (selectionUpdated) | 403 if (selectionUpdated) |
390 self.onSelection_(); | 404 self.onSelection_(); |
391 | 405 |
392 // Init modes after the first chunk is loaded. | 406 // Init modes after the first chunk is loaded. |
393 if (firstChunk) { | 407 if (firstChunk && !self.initialized_) { |
394 // Determine the initial mode. | 408 // Determine the initial mode. |
395 var shouldShowMosaic = selectedEntries.length > 1 || | 409 var shouldShowMosaic = selectedEntries.length > 1 || |
396 (self.context_.pageState && | 410 (self.context_.pageState && |
397 self.context_.pageState.gallery === 'mosaic'); | 411 self.context_.pageState.gallery === 'mosaic'); |
398 self.setCurrentMode_( | 412 self.setCurrentMode_( |
399 shouldShowMosaic ? self.mosaicMode_ : self.slideMode_); | 413 shouldShowMosaic ? self.mosaicMode_ : self.slideMode_); |
400 | 414 |
401 // Init mosaic mode. | 415 // Init mosaic mode. |
402 var mosaic = self.mosaicMode_.getMosaic(); | 416 var mosaic = self.mosaicMode_.getMosaic(); |
403 mosaic.init(); | 417 mosaic.init(); |
404 | 418 |
405 // Do the initialization for each mode. | 419 // Do the initialization for each mode. |
406 if (shouldShowMosaic) { | 420 if (shouldShowMosaic) { |
407 mosaic.show(); | 421 mosaic.show(); |
408 self.inactivityWatcher_.check(); // Show the toolbar. | 422 self.inactivityWatcher_.check(); // Show the toolbar. |
409 cr.dispatchSimpleEvent(self, 'loaded'); | 423 cr.dispatchSimpleEvent(self, 'loaded'); |
410 } else { | 424 } else { |
411 self.slideMode_.enter( | 425 self.slideMode_.enter( |
412 null, | 426 null, |
413 function() { | 427 function() { |
414 // Flash the toolbar briefly to show it is there. | 428 // Flash the toolbar briefly to show it is there. |
415 self.inactivityWatcher_.kick(Gallery.FIRST_FADE_TIMEOUT); | 429 self.inactivityWatcher_.kick(Gallery.FIRST_FADE_TIMEOUT); |
416 }, | 430 }, |
417 function() { | 431 function() { |
418 cr.dispatchSimpleEvent(self, 'loaded'); | 432 cr.dispatchSimpleEvent(self, 'loaded'); |
419 }); | 433 }); |
420 } | 434 } |
| 435 self.initialized_ = true; |
421 } | 436 } |
422 | 437 |
423 // Continue to load chunks. | 438 // Continue to load chunks. |
424 return loadChunk(/* firstChunk */ false); | 439 return loadChunk(/* firstChunk */ false); |
425 }); | 440 }); |
426 }; | 441 }; |
427 loadChunk(/* firstChunk */ true).catch(function(error) { | 442 loadChunk(/* firstChunk */ true).catch(function(error) { |
428 console.error(error.stack || error); | 443 console.error(error.stack || error); |
429 }); | 444 }); |
430 }; | 445 }; |
(...skipping 470 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
901 this.mosaicMode_.debugMe(); | 916 this.mosaicMode_.debugMe(); |
902 }; | 917 }; |
903 | 918 |
904 /** | 919 /** |
905 * Singleton gallery. | 920 * Singleton gallery. |
906 * @type {Gallery} | 921 * @type {Gallery} |
907 */ | 922 */ |
908 var gallery = null; | 923 var gallery = null; |
909 | 924 |
910 /** | 925 /** |
| 926 * (Re-)loads entries. |
| 927 */ |
| 928 function reload() { |
| 929 initializePromise.then(function() { |
| 930 util.URLsToEntries(window.appState.urls, function(entries) { |
| 931 gallery.load(entries); |
| 932 }); |
| 933 }); |
| 934 } |
| 935 |
| 936 /** |
911 * Promise to initialize the load time data. | 937 * Promise to initialize the load time data. |
912 * @type {!Promise} | 938 * @type {!Promise} |
913 */ | 939 */ |
914 var loadTimeDataPromise = new Promise(function(fulfill, reject) { | 940 var loadTimeDataPromise = new Promise(function(fulfill, reject) { |
915 chrome.fileManagerPrivate.getStrings(function(strings) { | 941 chrome.fileManagerPrivate.getStrings(function(strings) { |
916 window.loadTimeData.data = strings; | 942 window.loadTimeData.data = strings; |
917 i18nTemplate.process(document, loadTimeData); | 943 i18nTemplate.process(document, loadTimeData); |
918 fulfill(true); | 944 fulfill(true); |
919 }); | 945 }); |
920 }); | 946 }); |
921 | 947 |
922 /** | 948 /** |
923 * Promise to initialize volume manager. | 949 * Promise to initialize volume manager. |
924 * @type {!Promise} | 950 * @type {!Promise} |
925 */ | 951 */ |
926 var volumeManagerPromise = new Promise(function(fulfill, reject) { | 952 var volumeManagerPromise = new Promise(function(fulfill, reject) { |
927 var volumeManager = new VolumeManagerWrapper( | 953 var volumeManager = new VolumeManagerWrapper( |
928 VolumeManagerWrapper.DriveEnabledStatus.DRIVE_ENABLED); | 954 VolumeManagerWrapper.DriveEnabledStatus.DRIVE_ENABLED); |
929 volumeManager.ensureInitialized(fulfill.bind(null, volumeManager)); | 955 volumeManager.ensureInitialized(fulfill.bind(null, volumeManager)); |
930 }); | 956 }); |
931 | 957 |
932 /** | 958 /** |
933 * Promise to initialize both the volume manager and the load time data. | 959 * Promise to initialize both the volume manager and the load time data. |
934 * @type {!Promise} | 960 * @type {!Promise} |
935 */ | 961 */ |
936 var initializePromise = | 962 var initializePromise = |
937 Promise.all([loadTimeDataPromise, volumeManagerPromise]). | 963 Promise.all([loadTimeDataPromise, volumeManagerPromise]). |
938 then(function(args) { | 964 then(function(args) { |
939 var volumeManager = args[1]; | 965 var volumeManager = args[1]; |
940 var gallery = new Gallery(volumeManager); | 966 gallery = new Gallery(volumeManager); |
941 return gallery; | |
942 }); | 967 }); |
943 | 968 |
944 // Loads entries. | 969 // Loads entries. |
945 initializePromise.then( | 970 initializePromise.then(reload); |
946 /** | |
947 * Loads entries. | |
948 * @param {!Gallery} gallery The gallery instance. | |
949 */ | |
950 function(gallery) { | |
951 util.URLsToEntries(window.appState.urls, function(entries) { | |
952 gallery.load(entries); | |
953 }); | |
954 }); | |
955 | 971 |
956 /** | 972 /** |
957 * Enteres the debug mode. | 973 * Enteres the debug mode. |
958 */ | 974 */ |
959 window.debugMe = function() { | 975 window.debugMe = function() { |
960 initializePromise.then(function(gallery) { | 976 initializePromise.then(function() { |
961 gallery.debugMe(); | 977 gallery.debugMe(); |
962 }); | 978 }); |
963 }; | 979 }; |
OLD | NEW |