WIP: try to convert to ES modules

This commit is contained in:
j 2026-02-09 18:59:12 +01:00
commit 299a08b6a3
29 changed files with 3003 additions and 2014 deletions

View file

@ -1,5 +1,25 @@
'use strict';
import * as OxArray from './Array.js';
import * as OxObject from './Object.js';
import * as OxConstants from './Constants.js';
import * as OxMath from './Math.js';
import * as OxString from './String.js';
import * as OxType from './Type.js';
import * as OxCollection from './Collection.js';
const Ox = {};
Object.assign(Ox,
OxArray,
OxObject,
OxConstants,
OxMath,
OxString,
OxType,
OxCollection,
);
/*@
Ox.get <f> Get a remote resource
(url, callback) -> <u> undefined
@ -10,7 +30,7 @@ Ox.get <f> Get a remote resource
code <n> Status code
text <s> Status text
@*/
Ox.get = function(url, callback) {
export function get(url, callback) {
var request = new XMLHttpRequest();
request.open('GET', url, true);
request.onreadystatechange = function() {
@ -49,7 +69,7 @@ Ox.getAsync <f> Runs an asynchonous loader for an array of URLs
code <n> Error code (like `404`)
text <s> Error text (like `'Not Found'`)
@*/
Ox.getAsync = function(urls, get, callback) {
export function getAsync(urls, get, callback) {
urls = Ox.makeArray(urls);
var errors = {}, i = 0, n = urls.length, results = {};
function done() {
@ -76,7 +96,7 @@ Ox.getAsync = function(urls, get, callback) {
}
function getSerial() {
var url = urls.shift();
Ox.getAsync(url, get, function(result, error) {
getAsync(url, get, function(result, error) {
extend(results, result, url);
extend(errors, error, url);
urls.length ? getSerial() : done();
@ -85,142 +105,139 @@ Ox.getAsync = function(urls, get, callback) {
urls.some(Ox.isArray) ? getSerial() : getParallel();
};
(function() {
var cache = {},
head = document.head
|| document.getElementsByTagName('head')[0]
|| document.documentElement;
var cache = {},
head = document.head
|| document.getElementsByTagName('head')[0]
|| document.documentElement;
function getFile(type, url, callback) {
var element, tagValue, typeValue, urlKey;
if (!cache[url]) {
if (!type) {
type = Ox.parseURL(url).pathname.split('.').pop();
type = type == 'css' ? 'stylesheet'
: type == 'js' ? 'script' : 'image';
}
if (type == 'image') {
element = new Image();
element.onerror = onError;
element.onload = onLoad;
element.src = url;
} else {
tagValue = type == 'script' ? 'script' : 'link';
typeValue = type == 'script' ? 'text/javascript' : 'text/css';
urlKey = type == 'script' ? 'src' : 'href';
if (Ox.some(
document.getElementsByTagName(tagValue),
function(element) {
return element[urlKey] == url;
}
)) {
onLoad();
} else {
element = document.createElement(tagValue);
element.onerror = onError;
element.onload = element.onreadystatechange = onLoad;
element.type = typeValue;
element[urlKey] = url;
if (type == 'stylesheet') {
element.rel = 'stylesheet';
}
head.appendChild(element);
}
if (type == 'stylesheet') {
//fixme only call if browser does not support onload
// Safari 5 does not fire onload
waitForCSS();
}
}
function _getFile(type, url, callback) {
var element, tagValue, typeValue, urlKey;
if (!cache[url]) {
if (!type) {
type = Ox.parseURL(url).pathname.split('.').pop();
type = type == 'css' ? 'stylesheet'
: type == 'js' ? 'script' : 'image';
}
if (type == 'image') {
element = new Image();
element.onerror = onError;
element.onload = onLoad;
element.src = url;
} else {
tagValue = type == 'script' ? 'script' : 'link';
typeValue = type == 'script' ? 'text/javascript' : 'text/css';
urlKey = type == 'script' ? 'src' : 'href';
if (Ox.some(
document.getElementsByTagName(tagValue),
function(element) {
return element[urlKey] == url;
}
)) {
onLoad();
} else {
element = document.createElement(tagValue);
element.onerror = onError;
element.onload = element.onreadystatechange = onLoad;
element.type = typeValue;
element[urlKey] = url;
if (type == 'stylesheet') {
element.rel = 'stylesheet';
}
head.appendChild(element);
}
if (type == 'stylesheet') {
//fixme only call if browser does not support onload
// Safari 5 does not fire onload
waitForCSS();
}
}
} else {
callback(cache[url], null);
}
function onError() {
callback(null, {code: 404, text: 'Not Found'});
}
function onLoad() {
if (
!this || !this.readyState
|| this.readyState == 'loaded' || this.readyState == 'complete'
) {
// for an image, keep a reference to the element
// to keep the image in the browser cache
cache[url] = type == 'image' ? this : true;
callback(cache[url], null);
}
function onError() {
callback(null, {code: 404, text: 'Not Found'});
}
function onLoad() {
if (
!this || !this.readyState
|| this.readyState == 'loaded' || this.readyState == 'complete'
) {
// for an image, keep a reference to the element
// to keep the image in the browser cache
cache[url] = type == 'image' ? this : true;
callback(cache[url], null);
}
}
function waitForCSS() {
var error = false;
try {
element.sheet.cssRule;
} catch (e) {
error = true;
setTimeout(function() {
waitForCSS();
}, 25);
}
!error && onLoad();
}
}
function getFiles(type, urls, callback) {
Ox.getAsync(urls, function(url, callback) {
getFile(type, url, callback);
}, callback);
function waitForCSS() {
var error = false;
try {
element.sheet.cssRule;
} catch (e) {
error = true;
setTimeout(function() {
waitForCSS();
}, 25);
}
!error && onLoad();
}
}
/*@
Ox.getFile <f> Loads a file (image, script or stylesheet)
(file, callback) -> <u> undefined
file <s|[s]> Local path or remote URL, or array of those, or array of such arrays
Multiple files in the same array will be processed simultaneously,
but multiple arrays of files will be processed in that order.
callback <f> Callback function
image <h> DOM element (if the file is an image)
@*/
Ox.getFile = function(url, callback) {
getFiles(null, url, callback);
};
function getFiles(type, urls, callback) {
getAsync(urls, function(url, callback) {
_getFile(type, url, callback);
}, callback);
}
/*@
Ox.getImage <f> Loads an image
(file, callback) -> <u> undefined
file <s|[s]> Local path or remote URL, or array of those, or array of such arrays
Multiple files in the same array will be processed simultaneously,
but multiple arrays of files will be processed in that order.
callback <f> Callback function
image <h> DOM element
@*/
Ox.getImage = function(url, callback) {
getFiles('image', url, callback);
};
/*@
Ox.getFile <f> Loads a file (image, script or stylesheet)
(file, callback) -> <u> undefined
file <s|[s]> Local path or remote URL, or array of those, or array of such arrays
Multiple files in the same array will be processed simultaneously,
but multiple arrays of files will be processed in that order.
callback <f> Callback function
image <h> DOM element (if the file is an image)
@*/
export function getFile(url, callback) {
getFiles(null, url, callback);
};
/*@
Ox.getScript <f> Loads a script
(file, callback) -> <u> undefined
file <s|[s]> Local path or remote URL, or array of those, or array of such arrays
Multiple files in the same array will be processed simultaneously,
but multiple arrays of files will be processed in that order.
callback <f> Callback function
@*/
Ox.getScript = function(url, callback) {
getFiles('script', url, callback);
};
/*@
Ox.getImage <f> Loads an image
(file, callback) -> <u> undefined
file <s|[s]> Local path or remote URL, or array of those, or array of such arrays
Multiple files in the same array will be processed simultaneously,
but multiple arrays of files will be processed in that order.
callback <f> Callback function
image <h> DOM element
@*/
export function getImage(url, callback) {
getFiles('image', url, callback);
};
/*@
Ox.getStylesheet <f> Loads a stylesheet
(file, callback) -> <u> undefined
file <s|[s]> Local path or remote URL, or array of those, or array of such arrays
Multiple files in the same array will be processed simultaneously,
but multiple arrays of files will be processed in that order.
callback <f> Callback function
@*/
Ox.getStylesheet = function(url, callback) {
getFiles('stylesheet', url, callback);
};
/*@
Ox.getScript <f> Loads a script
(file, callback) -> <u> undefined
file <s|[s]> Local path or remote URL, or array of those, or array of such arrays
Multiple files in the same array will be processed simultaneously,
but multiple arrays of files will be processed in that order.
callback <f> Callback function
@*/
export function getScript(url, callback) {
getFiles('script', url, callback);
};
}());
/*@
Ox.getStylesheet <f> Loads a stylesheet
(file, callback) -> <u> undefined
file <s|[s]> Local path or remote URL, or array of those, or array of such arrays
Multiple files in the same array will be processed simultaneously,
but multiple arrays of files will be processed in that order.
callback <f> Callback function
@*/
export function getStylesheet(url, callback) {
getFiles('stylesheet', url, callback);
};
/*@
Ox.getJSON <f> Get and parse one or more remote JSON files
@ -234,10 +251,10 @@ Ox.getJSON <f> Get and parse one or more remote JSON files
code <n> Error code (like `404`)
text <s> Error text (like `'Not Found'`)
@*/
Ox.getJSON = function(url, callback, isJSONC) {
export function getJSON(url, callback, isJSONC) {
var urls = Ox.makeArray(url);
Ox.getAsync(urls, function(url, callback) {
Ox.get(url, function(data, error) {
getAsync(urls, function(url, callback) {
get(url, function(data, error) {
callback(JSON.parse(
isJSONC ? Ox.minify(data || '') : data
), error);
@ -258,7 +275,7 @@ Ox.getJSONC <f> Get and parse a remote JSONC file
code <n> Error code (like `404`)
text <s> Error text (like `'Not Found'`)
@*/
Ox.getJSONC = function(url, callback) {
export function getJSONC(url, callback) {
Ox.getJSON(url, callback, true);
};
@ -275,9 +292,9 @@ Ox.getJSONP <f> Get and parse one or more remote JSONP files
code <n> Error code (like `404`)
text <s> Error text (like `'Not Found'`)
@*/
Ox.getJSONP = function(url, callback) {
export function getJSONP(url, callback) {
var urls = Ox.makeArray(url);
Ox.getAsync(urls, function(url, callback) {
getAsync(urls, function(url, callback) {
var id = 'callback' + Ox.uid();
Ox.getJSONP[id] = function(data) {
delete Ox.getJSONP[id];
@ -301,7 +318,7 @@ Ox.post <f> post to a remote resource
code <n> Status code
text <s> Status text
@*/
Ox.post = function(url, data, callback) {
export function post(url, data, callback) {
var request = new XMLHttpRequest();
request.open('post', url, true);
request.onreadystatechange = function() {