2019-03-20 18:37:46 +00:00
|
|
|
const { platform } = require('../utils');
|
|
|
|
const assets = require('../../common/assets');
|
|
|
|
|
|
|
|
module.exports.updateFavicon = function(percentageString) {
|
|
|
|
if (platform() === 'web') {
|
2019-03-20 19:38:11 +00:00
|
|
|
let progress = parseInt(percentageString.replace('%', ''));
|
|
|
|
if (progress === 0 || progress === 100) {
|
2019-03-20 18:37:46 +00:00
|
|
|
const link = document.querySelector("link[rel*='icon']");
|
|
|
|
link.type = 'image/png';
|
|
|
|
link.href = assets.get('favicon-32x32.png');
|
|
|
|
document.getElementsByTagName('head')[0].appendChild(link);
|
|
|
|
return;
|
|
|
|
}
|
2019-03-20 19:38:11 +00:00
|
|
|
progress = progress * 0.01;
|
2019-03-20 18:37:46 +00:00
|
|
|
const link = document.querySelector("link[rel*='icon']");
|
|
|
|
const canvas = document.createElement('canvas');
|
|
|
|
const size = 32;
|
|
|
|
|
2019-03-20 19:38:11 +00:00
|
|
|
const loaderWidth = 5;
|
|
|
|
const loaderColor = '#0090ed';
|
2019-03-20 18:37:46 +00:00
|
|
|
|
|
|
|
const span = document.createElement('span');
|
|
|
|
span.textContent = percentageString;
|
|
|
|
const context = canvas.getContext('2d');
|
|
|
|
canvas.width = canvas.height = size;
|
|
|
|
|
2019-03-20 19:50:35 +00:00
|
|
|
context.translate(size * 0.5, size * 0.5);
|
2019-03-20 18:37:46 +00:00
|
|
|
|
2019-03-20 19:50:35 +00:00
|
|
|
const drawCircle = function(color, lineWidth, outerWidth, percent) {
|
|
|
|
const radius = (outerWidth - loaderWidth) * 0.5;
|
2019-03-20 18:37:46 +00:00
|
|
|
context.beginPath();
|
|
|
|
context.arc(0, 0, radius, 0, Math.PI * 2 * percent, false);
|
|
|
|
context.strokeStyle = color;
|
2019-03-20 18:57:52 +00:00
|
|
|
context.lineCap = 'square';
|
2019-03-20 18:37:46 +00:00
|
|
|
context.lineWidth = lineWidth;
|
|
|
|
context.stroke();
|
|
|
|
};
|
|
|
|
|
2019-03-20 18:42:12 +00:00
|
|
|
const drawNewFavicon = function() {
|
2019-03-20 19:50:35 +00:00
|
|
|
drawCircle('#efefef', loaderWidth, size, 1);
|
|
|
|
drawCircle(loaderColor, loaderWidth, size, progress);
|
2019-03-20 18:37:46 +00:00
|
|
|
};
|
|
|
|
|
2019-03-20 18:42:12 +00:00
|
|
|
drawNewFavicon(link);
|
2019-03-20 18:37:46 +00:00
|
|
|
link.href = canvas.toDataURL();
|
|
|
|
document.getElementsByTagName('head')[0].appendChild(link);
|
|
|
|
}
|
|
|
|
};
|