Skip to content

Commit

Permalink
Addressing review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
benfrancis committed Jan 14, 2015
1 parent 58cc878 commit 7b0400e
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 2 deletions.
72 changes: 72 additions & 0 deletions apps/search/js/providers/places.js
Expand Up @@ -31,6 +31,77 @@
cachedLink.href = url;
return cachedLink;
}

// Storage for locally fetched icons
var icons = {};
var iconUrls = {};

function getIcon(place) {

if (place.url in icons && icons[place.url]) {
return icons[place.url];
}

IconsHelper.getIcon(place.url, null, place).then(icon => {
saveIcon(place.url, icon);
});

return false;
}

function saveIcon(key, url) {
if (key in icons) {
return;
}
icons[key] = null;
iconUrls[key] = url;
fetchIcon(url, function(err, icon) {
if (!err) {
icons[key] = icon;
}
});
}

function fetchIcon(uri, callback) {
var xhr = new XMLHttpRequest({mozSystem: true});
xhr.open('GET', uri, true);
xhr.responseType = 'blob';
xhr.addEventListener('load', function() {
// Check icon was successfully downloded
// 0 is due to https://bugzilla.mozilla.org/show_bug.cgi?id=716491
if (!(xhr.status === 200 || xhr.status === 0)) {
return callback(new Error('error_downloading'));
}

var blob = xhr.response;

if (blob.size > this.MAX_ICON_SIZE) {
return callback(new Error('image_to_large'));
}

// Only save the icon if it can be loaded as an image bigger than 0px
var img = document.createElement('img');
img.src = window.URL.createObjectURL(blob);

img.onload = function() {
window.URL.revokeObjectURL(img.src);
if (img.naturalWidth <= 0) {
return callback(new Error('Cannot load image'));
}
callback(null, blob);
};

img.onerror = function() {
window.URL.revokeObjectURL(img.src);
return callback(new Error('Cannot load image'));
};
});

xhr.onerror = function(err) {
return callback(new Error('Cannot load uri'));
};
xhr.send();
}

function itemClicked(e) {
if (e.target.dataset.url) {
Expand Down Expand Up @@ -207,6 +278,7 @@
var result = {
'title': placeObj.title,
'meta': placeObj.url,
'icon': getIcon(placeObj),
'dataset': {
'url': placeObj.url
},
Expand Down
11 changes: 9 additions & 2 deletions apps/search/js/providers/provider.js
Expand Up @@ -106,7 +106,6 @@ Provider.prototype = {

result.classList.add('result');
iconWrapper.classList.add('icon');
iconWrapper.classList.add('empty');
description.classList.add('description');
title.classList.add('title');
meta.classList.add('meta');
Expand All @@ -127,6 +126,11 @@ Provider.prototype = {
}

icon.setAttribute('role', 'presentation');
if (config.icon) {
icon.src = window.URL.createObjectURL(config.icon);
} else {
iconWrapper.classList.add('empty');
}

result.setAttribute('role', 'link');
// Either use an explicit label or, if not present, title.
Expand All @@ -138,7 +142,10 @@ Provider.prototype = {
result.appendChild(iconWrapper);
result.appendChild(description);
frag.appendChild(result);
this.updateIcon(config, iconWrapper);

if (!config.icon) {
this.updateIcon(config, iconWrapper);
}
}, this);
return frag;
},
Expand Down

0 comments on commit 7b0400e

Please sign in to comment.