-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprefetcher.js
126 lines (105 loc) · 2.79 KB
/
prefetcher.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
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
/**
* PREFETCHER
* @luizpanariello - [email protected]
* 24/05/2014
* v: 1.0.0
*/
(function(window){
/**
* [PREFETCHER]
* Classe responsavel para fazer o download em prefetch.
*/
window.PREFETCHER = function(arr){
this._isIE = navigator.appName.indexOf('Microsoft') === 0;
this._loadedcount = 0;
this._prefetch = arr || [];
};
/**
* Inicia o download da lista
**/
PREFETCHER.prototype.listBegin = function(){};
/**
* Lista recebida
**/
PREFETCHER.prototype.listComplete = function(status, data){};
/**
* Inicio do download
*/
PREFETCHER.prototype.loadBegin = function(){};
/**
* Um item concluido com sucesso
*/
PREFETCHER.prototype.loadItemComplete = function(status){};
/**
* Um item concluido com sucesso
*/
PREFETCHER.prototype.loadItemSuccess = function(status){};
/**
* Um item concluido com erro
*/
PREFETCHER.prototype.loadItemError = function(status){};
/**
* Todos os itens foram concluidos
*/
PREFETCHER.prototype.loadComplete = function(status){};
/**
* Count dos downloads
*/
PREFETCHER.prototype._loaded = function(status){
var _self = this;
_self._loadedcount ++;
if(_self._loadedcount == _self._prefetch.length){
_self.loadComplete();
}
};
/**
* Busca a lista de arquivos
*/
PREFETCHER.prototype.getList = function(url){
var _self = this;
_self.listBegin();
if (typeof XMLHttpRequest === "undefined") {
XMLHttpRequest = function () {
try { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); }
catch (e) {}
try { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); }
catch (e) {}
try { return new ActiveXObject("Microsoft.XMLHTTP"); }
catch (e) {}
// Microsoft.XMLHTTP points to Msxml2.XMLHTTP and is redundant
throw new Error("This browser does not support XMLHttpRequest.");
};
}
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function(data) {
if (xmlhttp.readyState == XMLHttpRequest.DONE || xmlhttp.readyState == 4){
_self._prefetch = JSON.parse(xmlhttp.responseText);
_self.listComplete(xmlhttp.status, xmlhttp.responseText);
}
}
xmlhttp.open("GET", url, true);
xmlhttp.send();
};
/**
* Responsavel por carrega os arquivos
*/
PREFETCHER.prototype.loadPrefetch = function(){
var _self = this;
_self._loadedcount = 0;
_self.loadBegin();
for (i = 0; i < _self._prefetch.length; i ++) {
var img = new Image();
img.onerror = function(){
_self._loaded();
_self.loadItemError();
_self.loadItemComplete();
};
img.onload = function(){
_self._loaded();
_self.loadItemSuccess();
_self.loadItemComplete();
};
img.src = _self._prefetch[i];
}
};
})(window);