2011-11-05 16:46:53 +00:00
|
|
|
'use strict';
|
|
|
|
|
2011-10-07 01:04:47 +00:00
|
|
|
/*@
|
2012-07-03 21:55:05 +00:00
|
|
|
Ox.get <f> Get a remote resource
|
2011-10-07 01:04:47 +00:00
|
|
|
(url, callback) -> <u> undefined
|
|
|
|
url <s> Remote URL
|
|
|
|
callback <f> Callback function
|
2012-07-03 21:55:05 +00:00
|
|
|
data <s|null> The contents of the remote resource, or `null` on error
|
|
|
|
error <o|null> Error, or `null` on success
|
2012-06-17 13:36:55 +00:00
|
|
|
code <n> Status code
|
|
|
|
text <s> Status text
|
2011-10-07 01:04:47 +00:00
|
|
|
@*/
|
|
|
|
Ox.get = function(url, callback) {
|
2012-06-17 13:36:55 +00:00
|
|
|
var request = new XMLHttpRequest();
|
|
|
|
request.open('GET', url, true);
|
|
|
|
request.onreadystatechange = function() {
|
|
|
|
if (request.readyState == 4) {
|
|
|
|
if (request.status == 200) {
|
|
|
|
callback(request.responseText, null);
|
2011-10-07 01:04:47 +00:00
|
|
|
} else {
|
2012-06-17 13:36:55 +00:00
|
|
|
callback(null, {
|
|
|
|
code: request.status,
|
|
|
|
text: request.statusText
|
|
|
|
});
|
2011-10-07 01:04:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2012-06-17 13:36:55 +00:00
|
|
|
request.send();
|
2011-10-07 01:04:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*@
|
2012-06-17 13:36:55 +00:00
|
|
|
Ox.getAsync <f> Runs an asynchonous loader for an array of URLs
|
2012-06-27 12:55:49 +00:00
|
|
|
(urls, get, callback) -> <u> undefined
|
2013-12-04 15:49:42 +00:00
|
|
|
urls <s|a> URL or array of URLs or arrays of such arrays
|
2012-06-17 13:36:55 +00:00
|
|
|
Multiple URLs in the same array will be processed simultaneously, but
|
|
|
|
multiple arrays of URLs will be processed in that order.
|
2012-06-27 12:55:49 +00:00
|
|
|
get <f> Asynchronous function that loads a URL (for example Ox.get)
|
|
|
|
url <s> URL
|
|
|
|
callback <f> Callback function
|
2012-07-03 21:55:05 +00:00
|
|
|
result <s|null> Result, or `null` on error
|
|
|
|
error <o|null> Error, or `null` on success
|
2012-06-27 12:55:49 +00:00
|
|
|
code <n> Status code
|
|
|
|
text <s> Status text
|
2011-10-07 01:04:47 +00:00
|
|
|
callback <f> Callback function
|
2013-12-04 15:49:42 +00:00
|
|
|
results <o|{}> Results, or empty on error
|
2012-06-17 13:36:55 +00:00
|
|
|
Keys are file names, values are results
|
2013-12-04 15:49:42 +00:00
|
|
|
errors <o|{}> Errors, or empty on success
|
2012-06-17 13:36:55 +00:00
|
|
|
Keys are file names, values are error objects
|
2011-10-07 01:04:47 +00:00
|
|
|
@*/
|
2012-06-27 12:55:49 +00:00
|
|
|
Ox.getAsync = function(urls, get, callback) {
|
2013-12-04 15:49:42 +00:00
|
|
|
urls = Ox.makeArray(urls);
|
2012-06-17 13:36:55 +00:00
|
|
|
var errors = {}, i = 0, n = urls.length, results = {};
|
|
|
|
function done() {
|
2013-12-04 15:49:42 +00:00
|
|
|
callback && callback(filter(results), filter(errors));
|
2012-06-17 13:36:55 +00:00
|
|
|
}
|
2013-12-04 15:49:42 +00:00
|
|
|
function extend(object, value, urls) {
|
|
|
|
value !== null && Ox.extend.apply(null, [object].concat(
|
|
|
|
urls.length === 1 ? [urls[0], value] : [value]
|
|
|
|
));
|
|
|
|
}
|
|
|
|
function filter(object) {
|
|
|
|
return n == 1 ? object[urls[0]] : Ox.filter(object, function(value) {
|
|
|
|
return value !== null;
|
2012-04-19 11:23:24 +00:00
|
|
|
});
|
2012-06-17 13:36:55 +00:00
|
|
|
}
|
2013-12-04 15:49:42 +00:00
|
|
|
function getParallel() {
|
2012-06-17 13:36:55 +00:00
|
|
|
urls.forEach(function(url) {
|
2012-06-27 12:55:49 +00:00
|
|
|
get(url, function(result, error) {
|
2013-05-09 20:32:19 +00:00
|
|
|
results[url] = result;
|
|
|
|
errors[url] = error;
|
2012-06-17 13:36:55 +00:00
|
|
|
++i == n && done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2013-12-04 15:49:42 +00:00
|
|
|
function getSerial() {
|
|
|
|
var url = urls.shift();
|
|
|
|
Ox.getAsync(url, get, function(result, error) {
|
|
|
|
extend(results, result, url);
|
|
|
|
extend(errors, error, url);
|
|
|
|
urls.length ? getSerial() : done();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
urls.some(Ox.isArray) ? getSerial() : getParallel();
|
2011-11-23 14:53:17 +00:00
|
|
|
};
|
2011-10-07 01:04:47 +00:00
|
|
|
|
|
|
|
/*@
|
2012-06-17 13:36:55 +00:00
|
|
|
Ox.getFile <f> Loads a file (image, script or stylesheet)
|
2013-03-02 10:31:47 +00:00
|
|
|
(file, callback) -> <u> undefined
|
2013-03-02 10:38:03 +00:00
|
|
|
file <s|a> Local path or remote URL, or array of those, or array of arrays
|
|
|
|
Multiple files in the same array will be processed simultaneously, but
|
|
|
|
multiple arrays of files will be processed in that order.
|
2011-10-07 01:04:47 +00:00
|
|
|
callback <f> Callback function
|
2012-06-23 10:46:41 +00:00
|
|
|
image <h> DOM element (if the file is an image)
|
2011-10-07 01:04:47 +00:00
|
|
|
@*/
|
2012-06-17 13:36:55 +00:00
|
|
|
// FIXME: this doesn't handle errors yet
|
|
|
|
Ox.getFile = (function() {
|
|
|
|
|
2011-10-07 01:04:47 +00:00
|
|
|
var cache = {};
|
2012-05-22 23:17:17 +00:00
|
|
|
|
2012-06-17 13:36:55 +00:00
|
|
|
function getFile(file, callback) {
|
2011-10-07 01:04:47 +00:00
|
|
|
var element,
|
2012-06-17 13:36:55 +00:00
|
|
|
head = document.head
|
|
|
|
|| document.getElementsByTagName('head')[0]
|
|
|
|
|| document.documentElement,
|
2011-10-07 01:04:47 +00:00
|
|
|
request,
|
2011-11-05 16:46:53 +00:00
|
|
|
type = file.split('.').pop(),
|
2011-10-07 01:04:47 +00:00
|
|
|
isImage = type != 'css' && type != 'js';
|
|
|
|
if (!cache[file]) {
|
|
|
|
if (isImage) {
|
|
|
|
element = new Image();
|
|
|
|
element.onload = addFileToCache;
|
|
|
|
element.src = file;
|
|
|
|
} else {
|
|
|
|
if (!findFileInHead()) {
|
|
|
|
element = document.createElement(
|
|
|
|
type == 'css' ? 'link' : 'script'
|
|
|
|
);
|
2012-06-17 13:36:55 +00:00
|
|
|
element[
|
|
|
|
type == 'css' ? 'href' : 'src'
|
2013-09-05 11:09:30 +00:00
|
|
|
] = file + '?' + (Ox.DEBUG ? Ox.random(1000000) : Ox.VERSION);
|
2011-10-07 01:04:47 +00:00
|
|
|
element.type = type == 'css' ? 'text/css' : 'text/javascript';
|
2012-05-25 16:28:05 +00:00
|
|
|
if (type == 'css') {
|
|
|
|
element.rel = 'stylesheet';
|
|
|
|
}
|
2011-10-07 01:04:47 +00:00
|
|
|
if (/MSIE/.test(navigator.userAgent)) {
|
2012-06-17 13:36:55 +00:00
|
|
|
// fixme: find a way to check
|
|
|
|
// if css/js have loaded in msie
|
2011-10-07 01:04:47 +00:00
|
|
|
setTimeout(addFileToCache, 2500);
|
|
|
|
} else {
|
|
|
|
if (type == 'css') {
|
|
|
|
waitForCSS();
|
|
|
|
} else {
|
|
|
|
element.onload = addFileToCache;
|
|
|
|
}
|
|
|
|
}
|
2012-02-17 10:24:42 +00:00
|
|
|
head.appendChild(element);
|
2011-10-07 01:04:47 +00:00
|
|
|
} else {
|
|
|
|
addFileToCache();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
function addFileToCache() {
|
|
|
|
if (isImage) {
|
|
|
|
// for an image, save the element itself,
|
|
|
|
// so that it remains in the browser cache
|
2011-10-23 16:00:59 +00:00
|
|
|
cache[file] = element;
|
2011-10-07 01:04:47 +00:00
|
|
|
callback(element);
|
|
|
|
} else {
|
2011-10-23 16:00:59 +00:00
|
|
|
cache[file] = true;
|
2011-10-07 01:04:47 +00:00
|
|
|
callback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function findFileInHead() {
|
2013-12-01 13:57:52 +00:00
|
|
|
return Ox.slice(
|
2011-10-07 01:04:47 +00:00
|
|
|
document.getElementsByTagName(type == 'css' ? 'link' : 'script')
|
|
|
|
).map(function(element) {
|
|
|
|
return element[type == 'css' ? 'href' : 'src'] == file;
|
|
|
|
}).reduce(function(prev, curr) {
|
|
|
|
return prev || curr;
|
|
|
|
}, false);
|
|
|
|
}
|
|
|
|
function waitForCSS() {
|
|
|
|
var error = false;
|
|
|
|
try {
|
|
|
|
element.sheet.cssRule;
|
2012-05-25 09:41:51 +00:00
|
|
|
} catch (e) {
|
2011-10-07 01:04:47 +00:00
|
|
|
error = true;
|
|
|
|
setTimeout(function() {
|
|
|
|
waitForCSS();
|
|
|
|
}, 25);
|
|
|
|
}
|
|
|
|
!error && addFileToCache();
|
|
|
|
}
|
|
|
|
};
|
2012-06-17 13:36:55 +00:00
|
|
|
|
|
|
|
return function(files, callback) {
|
|
|
|
Ox.getAsync(files, function(file, callback) {
|
|
|
|
getFile(file, function(images) {
|
|
|
|
callback(images ? {file: images} : {});
|
|
|
|
});
|
|
|
|
}, callback);
|
|
|
|
};
|
|
|
|
|
2012-02-17 10:24:42 +00:00
|
|
|
}());
|
2012-04-19 11:13:43 +00:00
|
|
|
|
|
|
|
/*@
|
2012-06-17 13:36:55 +00:00
|
|
|
Ox.getJSON <f> Get and parse one or more remote JSON files
|
|
|
|
(url, callback) -> <u> undefined
|
|
|
|
url <s|a> One or more remote URLs
|
2012-04-19 11:13:43 +00:00
|
|
|
callback <f> Callback function
|
2012-07-03 21:55:05 +00:00
|
|
|
data <*|o|null> The parsed content of the remote resource(s)
|
2012-06-17 13:36:55 +00:00
|
|
|
For multiple URLs, keys are file names, values are parsed contents
|
|
|
|
error <o|null> Error(s)
|
|
|
|
For multiple URLs, keys are file names, values are error objects
|
2012-04-19 11:13:43 +00:00
|
|
|
@*/
|
2012-06-17 13:36:55 +00:00
|
|
|
Ox.getJSON = function(url, callback, isJSONC) {
|
|
|
|
var urls = Ox.makeArray(url);
|
|
|
|
Ox.getAsync(urls, function(url, callback) {
|
|
|
|
Ox.get(url, function(data, error) {
|
|
|
|
callback(JSON.parse(
|
|
|
|
isJSONC ? Ox.minify(data || '') : data
|
|
|
|
), error);
|
2012-04-19 11:13:43 +00:00
|
|
|
});
|
2012-06-17 13:36:55 +00:00
|
|
|
}, callback);
|
|
|
|
};
|
|
|
|
|
|
|
|
/*@
|
|
|
|
Ox.getJSONC <f> Get and parse a remote JSONC file
|
|
|
|
JSONC is JSON with JavaScript line or block comments
|
|
|
|
(url, callback) -> <u> undefined
|
|
|
|
url <s|a> One or more remote URLs
|
|
|
|
callback <f> Callback function
|
2012-07-03 21:55:05 +00:00
|
|
|
data <*|o|null> The parsed content of the remote resource(s)
|
2012-06-17 13:36:55 +00:00
|
|
|
For multiple URLs, keys are file names, values are parsed contents
|
|
|
|
error <o|null> Error(s)
|
|
|
|
For multiple URLs, keys are file names, values are error objects
|
|
|
|
@*/
|
|
|
|
Ox.getJSONC = function(url, callback) {
|
2013-11-30 18:40:27 +00:00
|
|
|
Ox.getJSON(url, callback, true);
|
2012-06-17 13:36:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*@
|
|
|
|
Ox.getJSONP <f> Get and parse one or more remote JSONP files
|
|
|
|
(url, callback) -> <u> undefined
|
|
|
|
url <s|a> One or more remote URLs
|
|
|
|
{callback} gets replaced with jsonp callback function name
|
|
|
|
callback <f> Callback function
|
2012-07-03 21:55:05 +00:00
|
|
|
data <*|o|null> The parsed content of the remote resource(s)
|
2012-06-17 13:36:55 +00:00
|
|
|
For multiple URLs, keys are file names, values are parsed contents
|
|
|
|
error <o|null> Error(s)
|
|
|
|
For multiple URLs, keys are file names, values are error objects
|
|
|
|
@*/
|
|
|
|
Ox.getJSONP = function(url, callback) {
|
|
|
|
var urls = Ox.makeArray(url);
|
|
|
|
Ox.getAsync(urls, function(url, callback) {
|
|
|
|
var id = 'callback' + Ox.uid();
|
|
|
|
Ox.getJSONP[id] = function(data) {
|
|
|
|
delete Ox.getJSONP[id];
|
|
|
|
callback(data, null);
|
|
|
|
};
|
|
|
|
Ox.$('body').append(Ox.$('<script>').attr({
|
|
|
|
'src': url.replace('{callback}', 'Ox.getJSONP.' + id),
|
|
|
|
'type': 'text/javascript'
|
|
|
|
}));
|
|
|
|
}, callback);
|
2012-05-22 23:17:17 +00:00
|
|
|
};
|