-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathappcachePercent.js
63 lines (52 loc) · 1.84 KB
/
appcachePercent.js
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
// Some of this is from
// Eric Bidelman's Article "A Beginner's Guide to Using the Application Cache"
// http://www.html5rocks.com/en/tutorials/appcache/beginner/
// also this: http://www.whatwg.org/specs/web-apps/current-work/
// appcahce stuff
var appCache = window.applicationCache;
var cacheProg = 0;
var prog = document.getElementById('prog');
window.addEventListener('load', function(e) {
// Check if a new cache is available on page load.
window.applicationCache.addEventListener('updateready', function(e) {
if (window.applicationCache.status == window.applicationCache.UPDATEREADY) {
// Browser downloaded a new app cache.
if (confirm('A new version of this site is available. Load it?')) {
window.location.reload();
}
} else {
// Manifest didn't changed. Nothing new to server.
}
}, false);
// Once the update is done, start er up
window.applicationCache.addEventListener('cached', function() {
loadingDone();
}, false);
// If there is no update, start er up
window.applicationCache.addEventListener('noupdate', function() {
loadingDone();
}, false);
}, false);
// hide the loading bar when it's done
function loadingDone() {
prog.className = 'hidden';
}
// fires every time a file is loaded
function progressEvent(e) {
prog.className = '';
var progInner = document.getElementById('prog-inner');
var progPercent = document.getElementById('prog-percent');
// update the percent
totalfiles = Number(e.total);
cacheProg = cacheProg + 1;
var progWidth = Math.round(cacheProg / totalfiles * 100);
// update the numerical percent
progPercent.innerHTML = progWidth - 1 +'%';
// update the width of the percent bar
progInner.style.width = progWidth - 1 +'%';
// when it's done, run the loading done function
if(cacheProg >= totalfiles) {
loadingDone();
}
}
appCache.addEventListener('progress', progressEvent, false);