Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display image name on download buttons and improve the buttons' formatting #340

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
24 changes: 13 additions & 11 deletions javascripts/api-cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,20 @@ function supportsHtml5Storage() {
}
}

function getApiValue(endpointUrl, cacheTime, callback) {
try {
var cacheData = supportsHtml5Storage() ? JSON.parse(localStorage[cacheKey]) : null;
// This does an exact match for the URL given. Different spacing, duplicate slashes,
// alternate caps, etc. will result in the cache being bypassed.
if (cacheData && cacheData[endpointUrl] && Date.now() - cacheData[endpointUrl].dateRetrieved < cacheTime) {
callback(cacheData[endpointUrl].requestResult);
return;
function getApiValue(endpointUrl, cacheTime, callback, clearCache) {
if(clearCache != true) {
try {
var cacheData = supportsHtml5Storage() ? JSON.parse(localStorage[cacheKey]) : null;
// This does an exact match for the URL given. Different spacing, duplicate slashes,
// alternate caps, etc. will result in the cache being bypassed.
if (cacheData && cacheData[endpointUrl] && Date.now() - cacheData[endpointUrl].dateRetrieved < cacheTime) {
callback(cacheData[endpointUrl].requestResult);
return;
}
}
catch (e) {
// Ignore the error; if the saved JSON is invalid, we'll just request it from the server.
}
}
catch (e) {
// Ignore the error; if the saved JSON is invalid, we'll just request it from the server.
}

console.log('No cached copy of API data for endpoint "' + endpointUrl + '" found. Downloading from remote server.');
Expand Down
103 changes: 66 additions & 37 deletions javascripts/releases.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,83 @@
// Cache will time out after 20 minutes
var releaseCacheTimeMillis = 20 * 60 * 1000;
// Cache will time out after five minutes
var releaseCacheTimeMillis = 5 * 60 * 1000;

var releasePlatformRegexes = {
ev3: "ev3dev-jessie-ev3-generic-[\\d-]+\\.zip",
rpi: "ev3dev-jessie-rpi-generic-[\\d-]+\\.zip",
rpi2: "ev3dev-jessie-rpi2-generic-[\\d-]+\\.zip",
bone: "ev3dev-jessie-bone-generic-[\\d-]+\\.zip",
ev3: /ev3dev-jessie-ev3-generic-[\d-]+\.zip/,
rpi: /ev3dev-jessie-rpi-generic-[\d-]+\.zip/,
rpi2: /ev3dev-jessie-rpi2-generic-[\d-]+\.zip/,
bone: /ev3dev-jessie-bone-generic-[\d-]+\.zip/,
}

function initDownloadLinks() {
getApiValue('https://api.github.com/repos/ev3dev/ev3dev/releases', releaseCacheTimeMillis, function (releases, error) {
if(error) {
console.error("Download links not available! Falling back to static content.");
$('.release-link-container').hide();
$('.release-link-alt').show();

function loadReleasesByPlatform(successCallback, errorCallback) {
getApiValue('https://api.github.com/repos/ev3dev/ev3dev/releases', releaseCacheTimeMillis, function (releasesApiData, error) {
if (error) {
errorCallback(error);
return;
}

releases.sort(function (a, b) {
if (Date.parse(a['created_at']) < Date.parse(b['created_at']))
return 1;
if (Date.parse(a['created_at']) > Date.parse(b['created_at']))
return -1;

return 0;

var releaseMap = {};
releasesApiData.forEach(function (releaseApiData) {
releaseApiData['assets'].forEach(function (assetApiData) {
var assetPlatform = $.grep(Object.keys(releasePlatformRegexes), function (platId) {
return releasePlatformRegexes[platId].test(assetApiData['name']);
})[0];

if (!assetPlatform)
return true;

if (!Array.isArray(releaseMap[assetPlatform]))
releaseMap[assetPlatform] = [];

releaseMap[assetPlatform].push({
releaseName: releaseApiData['name'],
assetName: assetApiData['name'],
creationDate: Date.parse(releaseApiData['created_at']),
platform: assetPlatform,
size: assetApiData['size'],
downloadUrl: assetApiData['browser_download_url']
});
});
});

Object.keys(releaseMap).forEach(function (platformId) {
releaseMap[platformId].sort(function (a, b) {
return (b.creationDate > a.creationDate) - (b.creationDate < a.creationDate);
});
})

successCallback(releaseMap);
});
}

function initDownloadLinks() {
loadReleasesByPlatform(function (releaseMap) {

$('a[data-release-link-platform]').each(function (i, element) {
var $linkElem = $(element);
var targetReleasePlatform = $linkElem.data('release-link-platform');
if (!releasePlatformRegexes[targetReleasePlatform]) {
console.error('"' + targetReleasePlatform + '" is an invalid release target.');
var targetRelease = (releaseMap[targetReleasePlatform] || [])[0];

if (!targetRelease) {
console.error('"' + targetReleasePlatform + '" is an invalid release target or no releases for the given platform exist.');
return true;
}

var platformRegex = new RegExp(releasePlatformRegexes[targetReleasePlatform]);

for (var releaseIndex in releases) {
var releaseAssets = releases[releaseIndex].assets;
for (var assetIndex in releaseAssets) {
if (platformRegex.test(releaseAssets[assetIndex].name)) {
$linkElem.attr('href', releaseAssets[assetIndex]['browser_download_url']);
var fileSize = releaseAssets[assetIndex]['size'] >> 20;
$('<small/>').text(' (' + fileSize + ' MiB)').appendTo($linkElem);
return true;
}
}
}
$linkElem.attr('href', targetRelease.downloadUrl);

$linkElem.children('small.download-info-label').remove();
var fileSize = targetRelease.size >> 20;
$('<small/>').addClass('download-info-label badge').text(fileSize + ' MiB').appendTo($linkElem);
$('<small/>').addClass('download-info-label').css({
'display': 'block',
'font-size': '65%'
}).text(targetRelease.assetName).appendTo($linkElem);

});
},
function (error) {
console.error("Download links not available! Falling back to static content.");
$('.release-link-container').hide();
$('.release-link-alt').show();
});
}

Expand All @@ -57,7 +86,7 @@ $(document).ready(function () {
// We do this as soon as the document loads so that the page flash is minimal.
$('.release-link-alt').hide();
$('.release-link-container').show();

if ($('a[data-release-link-platform]').length > 0) {
initDownloadLinks();
}
Expand Down