1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
//
// piecon.js
//
// https://github.com/lipka/piecon
//
// Copyright (c) 2015 Lukas Lipka <lukaslipka@gmail.com>. All rights reserved.
//
(function(){
var Piecon = {};
var currentFavicon = null;
var originalFavicon = null;
var originalTitle = null;
var canvas = null;
var options = {};
var defaults = {
color: '#ff0084',
background: '#bbb',
shadow: '#fff',
fallback: false
};
var isRetina = window.devicePixelRatio > 1;
var ua = (function () {
var agent = navigator.userAgent.toLowerCase();
return function (browser) {
return agent.indexOf(browser) !== -1;
};
}());
var browser = {
ie: ua('msie'),
chrome: ua('chrome'),
webkit: ua('chrome') || ua('safari'),
safari: ua('safari') && !ua('chrome'),
mozilla: ua('mozilla') && !ua('chrome') && !ua('safari')
};
var getFaviconTag = function() {
var links = document.getElementsByTagName('link');
for (var i = 0, l = links.length; i < l; i++) {
if (links[i].getAttribute('rel') === 'icon' || links[i].getAttribute('rel') === 'shortcut icon') {
return links[i];
}
}
return false;
};
var removeFaviconTag = function() {
var links = Array.prototype.slice.call(document.getElementsByTagName('link'), 0);
var head = document.getElementsByTagName('head')[0];
for (var i = 0, l = links.length; i < l; i++) {
if (links[i].getAttribute('rel') === 'icon' || links[i].getAttribute('rel') === 'shortcut icon') {
head.removeChild(links[i]);
}
}
};
var setFaviconTag = function(url) {
removeFaviconTag();
var link = document.createElement('link');
link.type = 'image/x-icon';
link.rel = 'icon';
link.href = url;
document.getElementsByTagName('head')[0].appendChild(link);
};
var getCanvas = function () {
if (!canvas) {
canvas = document.createElement("canvas");
if (isRetina) {
canvas.width = 32;
canvas.height = 32;
} else {
canvas.width = 16;
canvas.height = 16;
}
}
return canvas;
};
var drawFavicon = function(percentage) {
var canvas = getCanvas();
var context = canvas.getContext("2d");
percentage = percentage || 0;
if (context) {
context.clearRect(0, 0, canvas.width, canvas.height);
// Draw shadow
context.beginPath();
context.moveTo(canvas.width / 2, canvas.height / 2);
context.arc(canvas.width / 2, canvas.height / 2, Math.min(canvas.width / 2, canvas.height / 2), 0, Math.PI * 2, false);
context.fillStyle = options.shadow;
context.fill();
// Draw background
context.beginPath();
context.moveTo(canvas.width / 2, canvas.height / 2);
context.arc(canvas.width / 2, canvas.height / 2, Math.min(canvas.width / 2, canvas.height / 2) - 2, 0, Math.PI * 2, false);
context.fillStyle = options.background;
context.fill();
// Draw pie
if (percentage > 0) {
context.beginPath();
context.moveTo(canvas.width / 2, canvas.height / 2);
context.arc(canvas.width / 2, canvas.height / 2, Math.min(canvas.width / 2, canvas.height / 2) - 2, (-0.5) * Math.PI, (-0.5 + 2 * percentage / 100) * Math.PI, false);
context.lineTo(canvas.width / 2, canvas.height / 2);
context.fillStyle = options.color;
context.fill();
}
setFaviconTag(canvas.toDataURL());
}
};
var updateTitle = function(percentage) {
if (percentage > 0) {
document.title = '(' + percentage + '%) ' + originalTitle;
} else {
document.title = originalTitle;
}
};
Piecon.setOptions = function(custom) {
options = {};
for (var key in defaults){
options[key] = custom.hasOwnProperty(key) ? custom[key] : defaults[key];
}
return this;
};
Piecon.setProgress = function(percentage) {
if (!originalTitle) {
originalTitle = document.title;
}
if (!originalFavicon || !currentFavicon) {
var tag = getFaviconTag();
originalFavicon = currentFavicon = tag ? tag.getAttribute('href') : '/favicon.ico';
}
if (!isNaN(parseFloat(percentage)) && isFinite(percentage)) {
if (!getCanvas().getContext || browser.ie || browser.safari || options.fallback === true) {
// Fallback to updating the browser title if unsupported
return updateTitle(percentage);
} else if (options.fallback === 'force') {
updateTitle(percentage);
}
return drawFavicon(percentage);
}
return false;
};
Piecon.reset = function() {
if (originalTitle) {
document.title = originalTitle;
}
if (originalFavicon) {
currentFavicon = originalFavicon;
setFaviconTag(currentFavicon);
}
};
Piecon.setOptions(defaults);
if(typeof define === 'function' && define.amd) {
define(Piecon);
} else if (typeof module !== 'undefined') {
module.exports = Piecon;
} else {
window.Piecon = Piecon;
}
})();
|