Commit 9e0fa77f authored by yoshiki's avatar yoshiki Committed by Commit bot

[Gallery] Add reload feature

This patch adds a reload feature, which is that reload the gallery with new entries when a 'onLaunched' event is fired.

BUG=461218
TEST=manually tested

Review URL: https://codereview.chromium.org/959643003

Cr-Commit-Position: refs/heads/master@{#318216}
parent bf421f15
......@@ -23,6 +23,13 @@ var windowCreateOptions = {
var background = new BackgroundBase();
/**
* Wrapper of gallery window.
* @type {SingletonAppWindowWrapper}
*/
var gallery = new SingletonAppWindowWrapper('gallery.html',
windowCreateOptions);
// Initializes the strings. This needs for the volume manager.
var loadTimeDataPromise = new Promise(function(fulfill, reject) {
chrome.fileManagerPrivate.getStrings(function(stringData) {
......@@ -75,17 +82,6 @@ function onLaunched(launchData) {
});
}
/**
* Returns a function to generate an ID for window.
* @type {function():string} Function which returns an unique id.
*/
var generateWindowId = (function() {
var seq = 0;
return function() {
return 'GALLERY_' + seq++;
};
})();
/**
* Opens gallery window.
* @param {!Array.<string>} urls List of URL to show.
......@@ -101,14 +97,8 @@ function openGalleryWindow(urls, reopen) {
if (urls.length === 0)
return Promise.reject('No file to open.');
var windowId = generateWindowId();
// Opens a window.
return new Promise(function(fulfill, reject) {
var gallery = new AppWindowWrapper('gallery.html',
windowId,
windowCreateOptions);
gallery.launch(
{urls: urls},
reopen,
......
......@@ -61,6 +61,7 @@ function Gallery(volumeManager) {
this.selectedEntry_ = null;
this.metadataCacheObserverId_ = null;
this.onExternallyUnmountedBound_ = this.onExternallyUnmounted_.bind(this);
this.initialized_ = false;
this.dataModel_ = new GalleryDataModel(
this.context_.metadataCache,
......@@ -348,6 +349,11 @@ Gallery.prototype.loadInternal_ = function(entries, selectedEntries) {
return a.index - b.index;
});
if (loadingList.length === 0) {
this.dataModel_.splice(0, this.dataModel_.length);
return;
}
// Load entries.
// Use the self variable capture-by-closure because it is faster than bind.
var self = this;
......@@ -367,6 +373,14 @@ Gallery.prototype.loadInternal_ = function(entries, selectedEntries) {
if (chunk.length !== metadataList.length)
return Promise.reject('Failed to load metadata.');
// Remove all the previous items if it's the first chunk.
// Do it here because prevent a flicker between removing all the items
// and adding new ones.
if (firstChunk) {
self.dataModel_.splice(0, self.dataModel_.length);
self.updateThumbnails_(); // Remove the caches.
}
// Add items to the model.
var items = [];
chunk.forEach(function(chunkItem, index) {
......@@ -399,7 +413,7 @@ Gallery.prototype.loadInternal_ = function(entries, selectedEntries) {
self.onSelection_();
// Init modes after the first chunk is loaded.
if (firstChunk) {
if (firstChunk && !self.initialized_) {
// Determine the initial mode.
var shouldShowMosaic = selectedEntries.length > 1 ||
(self.context_.pageState &&
......@@ -427,6 +441,7 @@ Gallery.prototype.loadInternal_ = function(entries, selectedEntries) {
cr.dispatchSimpleEvent(self, 'loaded');
});
}
self.initialized_ = true;
}
// Continue to load chunks.
......@@ -916,6 +931,17 @@ Gallery.prototype.debugMe = function() {
*/
var gallery = null;
/**
* (Re-)loads entries.
*/
function reload() {
initializePromise.then(function() {
util.URLsToEntries(window.appState.urls, function(entries) {
gallery.load(entries);
});
});
}
/**
* Promise to initialize the load time data.
* @type {!Promise}
......@@ -946,27 +972,17 @@ var initializePromise =
Promise.all([loadTimeDataPromise, volumeManagerPromise]).
then(function(args) {
var volumeManager = args[1];
var gallery = new Gallery(volumeManager);
return gallery;
gallery = new Gallery(volumeManager);
});
// Loads entries.
initializePromise.then(
/**
* Loads entries.
* @param {!Gallery} gallery The gallery instance.
*/
function(gallery) {
util.URLsToEntries(window.appState.urls, function(entries) {
gallery.load(entries);
});
});
initializePromise.then(reload);
/**
* Enteres the debug mode.
*/
window.debugMe = function() {
initializePromise.then(function(gallery) {
initializePromise.then(function() {
gallery.debugMe();
});
};
......@@ -87,6 +87,8 @@
window.ImageUtil = ImageUtil;
window.ImageUtil.metrics = metrics;
window.Gallery = Gallery;
window.util = util;
window.reload = reload; // will be called by the background.
window.gallery = gallery; // for debug.
})();
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment