forked from getify/h5ive-DEPRECATED
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xhr.h5ive.js
84 lines (70 loc) · 2.06 KB
/
xhr.h5ive.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
/*! xhr.h5ive.js | (c) Kyle Simpson | MIT License: http://getify.mit-license.org */
(function(h5){
if (!h5) throw new Error("xhr.h5ive: core.h5ive required.");
h5.xhr = function(opts) {
var XHR, publicAPI, used = false;
function connect(url) {
if (used) reset();
used = true;
for (var header in opts.headers) {
XHR.setRequestHeader(header,opts.headers[header]);
}
XHR.open(opts.method,url,false,opts.user,opts.pw);
return publicAPI;
}
function send(data) {
XHR.send(data);
return publicAPI;
}
function reset(newOpts) {
XHR.removeEventListener("readystatechange",readyStateChange);
XHR.removeEventListener("progress",progressUpdate);
publicAPI.__raw__ = XHR = new XMLHttpRequest();
XHR.addEventListener("readystatechange",readyStateChange);
if (newOpts) opts = newOpts;
processOpts();
return publicAPI;
}
function success(cb) { opts.success = cb; return publicAPI; }
function progress(cb) { opts.progress = cb; return publicAPI; }
function error(cb) { opts.error = cb; return publicAPI; }
function progressUpdate(evt) {
opts.progress.call(publicAPI,{
bytesLoaded: evt.loaded,
bytesTotal: evt.total
});
}
function readyStateChange() {
if (XHR.readyState == 4) {
XHR.removeEventListener("readystatechange",readyStateChange);
XHR.removeEventListener("progress",progressUpdate);
if (XHR.status == 200) {
opts.success.call(publicAPI,XHR.response);
}
else {
opts.error.call(publicAPI,XHR.status,XHR.statusText);
}
}
}
function processOpts() {
opts = opts || {};
opts.method = opts.method || "GET";
opts.success = opts.success || function(){};
opts.progress = opts.progress || function(){};
opts.error = opts.error || function(){};
opts.headers = opts.headers || {};
opts.responseType = opts.responseType || "text"; // TODO: check this
}
publicAPI = {
__raw__: XHR,
connect: connect,
send: send,
success: success,
progress: progress,
error: error,
reset: reset
};
processOpts();
return publicAPI;
};
})(this.h5);