-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.js
More file actions
63 lines (53 loc) · 1.45 KB
/
Copy pathutil.js
File metadata and controls
63 lines (53 loc) · 1.45 KB
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
/* Utility functions */
/* Sets timeout for given function f */
function schedule(f, x, id, until) {
setTimeout(function() { f(x, id, until); }, 0);
}
/* Simplify getDocumentByElementId */
function $() {
if (arguments.length==1) {
return document.getElementById(arguments[0]);
}
var result=[], i=0, el;
while(el=document.getElementById(arguments[i++])) {
result.push(el);
}
return result;
}
/* Calls the given callback iff conditional evaluates to true,
* otherwise sets a timeout */
function fireWhenTrue(conditional, callback) {
if (conditional()) {
callback();
} else {
setTimeout(function() {fireWhenTrue(conditional, callback);}, 3000);
}
}
/* Builds and sends off the request, calling 'callback'
/* upon load with the xhr as a parameter */
function buildAndSendXhr(req, callback) {
var xhr = new XMLHttpRequest();
xhr.open(req.type, req.url, true);
if (req.headers) {
req.headers.forEach(function (header) {
xhr.setRequestHeader(header[0], header[1]);
});
}
xhr.addEventListener("readystatechange",
function() {
// Not loaded, just return
if (xhr.readyState != 4) return;
callback(xhr);
}, false);
if (req.data) {
xhr.send(req.data);
} else {
xhr.send();
}
}
/* Returns true iff req is valid */
function checkReq(req) {
return (typeof(req) === 'object') &&
typeof(req.data) === 'string' &&
typeof(req.url) === 'string';
}