Skip to content
This repository has been archived by the owner on Nov 3, 2021. It is now read-only.

Commit

Permalink
Merge pull request #23035 from EverythingMe/1055498-refactor-create
Browse files Browse the repository at this point in the history
Bug 1055498 - [Collection] [Refactor] Clean up create_collection.js
  • Loading branch information
amirnissim committed Aug 21, 2014
2 parents 7855a89 + cb7001f commit a9627e3
Showing 1 changed file with 142 additions and 159 deletions.
301 changes: 142 additions & 159 deletions apps/collection/js/create_collection.js
Expand Up @@ -15,184 +15,167 @@

function HandleCreate(activity) {

function onOffline() {
alert(navigator.mozL10n.get('network-error-message'));
activity.postResult(false);
}

if (!navigator.onLine) {
return onOffline();
}

window.addEventListener('offline', onOffline);
const APPS_IN_ICON = Common.APPS_IN_ICON;

var request;
var loading = document.getElementById('loading');
var cancel = document.getElementById('cancel');
var maxIconSize = activity.source.data.maxIconSize;

CollectionIcon.init(maxIconSize);
const APPS_IN_ICON = Common.APPS_IN_ICON;

cancel.addEventListener('click', function() {
// TODO request should always have an 'abort' method
// but sometimes it doesn't. find out why!
// "TypeError: request.abort is not a function"
// {file: "app://collection.gaiamobile.org/js/activities.js" line: 20}
request.abort && request.abort();
activity.postResult(false);
});
function getWebIcons(query) {
return eme.api.Apps.search({query: query, limit: APPS_IN_ICON})
.then(response =>
response.response.apps.slice(0, APPS_IN_ICON).map(app => app.icon));
}

function generateIcons(collections) {
var iconTasks = collections.map(collection => {
Common.getBackground(collection, maxIconSize)
.then(bgObject => {
collection.background = bgObject;
return collection.renderIcon();
}, () => {
return collection.renderIcon();
});
});

return Promise.all(iconTasks).then(() => collections);
}

// filter installed categories
CollectionsDatabase.getAllCategories().then(function doRequest(installed) {
// TODO send existingExperienceIds instead of filtering on client side
// (when supported in Partners API)
request = eme.api.Categories.list().then(
function success(response) {
loading.style.display = 'none';

var data = response.response;
var categories = data.categories.filter(function filter(category) {
return installed.indexOf(category.categoryId) === -1;
});

Suggestions.load(categories).then(
function select(selected) {
// We can't cancel out of this for the time being.
document.querySelector('menu').style.display = 'none';
// Display spinner while we're resolving and creating the
// collections.
loading.style.display = 'inline';
eme.log('resolved with', selected);
var dataReady;

if (Array.isArray(selected)) {
// collections from categories
// we have the web app icons in the response
dataReady = Promise.resolve(
CategoryCollection.fromResponse(selected, data));
} else {
// collection from custom query
// we make another request to get web app icons
dataReady = new Promise(function getIcons(resolve) {
eme.api.Apps.search({query: selected, limit: APPS_IN_ICON})
.then(function success(response) {
var webicons =
response.response.apps.slice(0,APPS_IN_ICON).map(
function each(app) {
return app.icon;
function saveAll(collections) {
var trxs = collections.map(collection => {
return collection.save('add');
});
return Promise.all(trxs).then(() => collections);
}

function populateNativeInfo(collections) {
var nativeTasks = [];
collections.forEach(collection => {
nativeTasks.push(NativeInfo.processCollection(collection));
});
return Promise.all(nativeTasks).then(() => collections);
}

/**
* Return from the activity to the homescreen. Create a list of
* collection IDs and post it to the homescreen so it knows what
* collections will be created, and positions them accordingly.
*/
function postResultIds(collections) {
collections = collections || [];
// Generate an array of collection IDs.
var ids = collections.map(c => c.id);

activity.postResult(ids);
}

function createCollections(selected, data) {
var dataReady;

eme.log('resolved with', selected);

// We can't cancel out of this for the time being.
document.querySelector('menu').style.display = 'none';

// Display spinner while we're resolving and creating the
// collections.
loading.style.display = 'inline';


if (Array.isArray(selected)) {
// collections from categories
// we have the web app icons in the response
dataReady = Promise.resolve(
CategoryCollection.fromResponse(selected, data));
} else {
// collection from custom query
// we make another request to get web app icons
dataReady = getWebIcons(selected)
.then(webicons => {
var collection = new QueryCollection({
query: selected,
webicons: webicons
});
var collection = new QueryCollection({
query: selected,
webicons: webicons

return [collection];
})
.catch(e => {
eme.log('noIcons', e);
return [new QueryCollection({query: selected})];
});
}

dataReady
.then(generateIcons)
// XXX: We currently need to save before we populate info.
.then(saveAll)
.then(populateNativeInfo)
.then(generateIcons)
.then(saveAll)
.then(postResultIds)
.catch((ex) => {
eme.error('caught exception', ex);
postResultIds();
});
}

function onOffline() {
alert(navigator.mozL10n.get('network-error-message'));
activity.postResult(false);
}

function onOnline() {
CollectionsDatabase.getAllCategories()
.then(function doRequest(installed) {
request = eme.api.Categories.list().then(
function success(response) {
loading.style.display = 'none';

var data = response.response;
var categories = data.categories.filter(function filter(category) {
return installed.indexOf(category.categoryId) === -1;
});

resolve([collection]);
}, noIcons)
.catch(noIcons);

function noIcons(e) {
eme.log('noIcons', e);
resolve([new QueryCollection({query: selected})]);
}
});
}

// congrats! you have the webapps icons!
// but you are still not done,
// it's time to get the background images
dataReady.then(function success(collections) {
var iconsReady = [];
collections.forEach(function doIcon(collection) {
var promise =
Common.getBackground(collection, maxIconSize)
.then(function setBackground(bgObject) {
collection.background = bgObject;
return collection.renderIcon();
}, function noBackground() {
return collection.renderIcon();
});

iconsReady.push(promise);
});

Promise.all(iconsReady).then(function then() {
// Save the collections
function saveAll(collections) {
var trxs = collections.map(collection => {
return collection.save('add');
});
return trxs;
}

// XXX: We currently need to save before we populate info.
Promise.all(saveAll(collections))
.then(populateNativeInfo.bind(null, collections))
.then(generateIcons.bind(null, collections))
.then(() => {
return Promise.all(saveAll(collections));
})
.then(postResultIds.bind(null, collections), postResultIds);
}).catch(function _catch(ex) {
eme.log('caught exception', ex);
activity.postResult(false);
});
});

function populateNativeInfo(collections) {
var nativeTasks = [];
collections.forEach(collection => {
nativeTasks.push(NativeInfo.processCollection(collection));
});
return Promise.all(nativeTasks);
}

function generateIcons(collections) {
var iconTasks = [];
collections.forEach(collection => {
var promise =
Common.getBackground(collection, maxIconSize)
.then(function setBackground(bgObject) {
collection.background = bgObject;
return collection.renderIcon();
}, function noBackground() {
return collection.renderIcon();
});
iconTasks.push(promise);
});
return Promise.all(iconTasks);
}

/**
* Return from the activity to the homescreen. Create a list of
* collection IDs and post it to the homescreen so it knows what
* collections will be created, and positions them accordingly.
*/
function postResultIds(collections) {
collections = collections || [];
// Generate an array of collection IDs.
var ids = collections.map(c => c.id);

activity.postResult(ids);
}
},
function cancel(reason) {
Suggestions.load(categories)
.then(selected => {
createCollections(selected, data);
}, reason => {
eme.log('rejected with', reason);
activity.postResult(false);
});

}, function error(reason) {
eme.log('create-collection: error', reason);
activity.postError(reason === 'network error' ?
_('network-error-message') : undefined);
}).catch(function fail(ex) {
eme.log('create-collection: failed', ex);
activity.postError();
});
}, function error(reason) {
eme.log('create-collection: error', reason);
activity.postError(reason === 'network error' ?
_('network-error-message') : undefined);
}).catch(function fail(ex) {
eme.log('create-collection: failed', ex);
activity.postError();
});

}, activity.postError);
}, activity.postError);
}

window.addEventListener('offline', onOffline);
cancel.addEventListener('click', function() {
// TODO request should always have an 'abort' method
// but sometimes it doesn't. find out why!
// "TypeError: request.abort is not a function"
// {file: "app://collection.gaiamobile.org/js/activities.js" line: 20}
request.abort && request.abort();
activity.postResult(false);
});

document.body.dataset.testReady = true;

if (navigator.onLine) {
onOnline();
} else {
onOffline();
}
}

navigator.mozSetMessageHandler('activity', function onActivity(activity) {
Expand Down

0 comments on commit a9627e3

Please sign in to comment.