1
0
Fork 0
forked from 0x2620/oxjs

Ox.load(), and adding moved files

This commit is contained in:
rolux 2011-04-25 11:12:02 +02:00
commit 6cfb6b7647
594 changed files with 1381 additions and 19555 deletions

View file

@ -74,7 +74,6 @@ Ox.SYMBOLS = {
EDIT: '\uF802', CLICK: '\uF803', APPLE: '\uF8FF'
};
// local timezone offset in milliseconds
Ox.TIMEZONE_OFFSET = +new Date().getTimezoneOffset() * 60000;
Ox.TYPES = [
'Arguments', 'Array', 'Boolean', 'Date', 'Element', 'Function', 'Infinity',
'NaN', 'Null', 'Number', 'Object', 'RegExp', 'String', 'Undefined'
@ -144,6 +143,109 @@ Ox.getset = function(obj, args, callback, context) {
return ret;
}
Ox.PATH = Array.prototype.slice.apply(
document.getElementsByTagName('script')
).filter(function(element) {
return /Ox\.js$/.test(element.src);
})[0].src.replace('js/Ox.js', '');
Ox.load = (function() {
/***
loads Ox modules
***/
var cache = {};
return function(module, options, callback) {
callback = arguments[arguments.length - 1];
options = arguments.length == 3 ? arguments[1] : {};
Ox.loadFile(Ox.PATH + 'js/Ox.' + module + '/Ox.' + module + '.js', function() {
Ox.load[module](options, callback);
});
};
}());
Ox.loadFile = (function() {
/***
loads stylesheets, scripts and images
***/
var cache = {};
return function (file, callback) {
var element, request,
type = file.split('.').pop();
if (!cache[file]) {
if (type == 'css' || type == 'js') {
if (!findFileInHead()) {
element = document.createElement(type == 'css' ? 'link' : 'script');
element[type == 'css' ? 'href' : 'src'] = file;
element.type = type == 'css' ? 'text/css' : 'text/javascript';
if (type == 'css') {
element.rel = 'stylesheet';
waitForCSS();
} else {
element.onload = addFileToCache;
}
document.head.appendChild(element);
} else {
addFileToCache();
}
} else {
element = new Image();
element.onload = addFileToCache;
element.src = file;
}
} else {
callback();
}
function addFileToCache() {
if (type == 'css' || type == 'js') {
cache['file'] = true;
callback();
} else {
cache['file'] = element;
callback({
width: element.width,
height: element.heigth
});
}
}
function findFileInHead() {
return Array.prototype.slice.apply(
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;
} catch(e) {
error = true;
setTimeout(function() {
waitForCSS();
}, 25);
}
!error && addFileToCache();
}
};
}());
Ox.loadJSON = function(url, callback) {
var req = new XMLHttpRequest();
req.open('GET', url, true);
req.onreadystatechange = function() {
if (req.readyState == 4) {
if (req.status == 200) {
callback(JSON.parse(req.responseText));
} else {
throw new Error('URL ' + url + ', status ' + req.status);
}
}
};
req.send();
};
Ox.print = function() {
/*
*/
@ -1041,6 +1143,10 @@ Ox.getTime = function() {
return +new Date();
}
Ox.getTimezoneOffset = function() {
return new Date().getTimezoneOffset() * 60000;
}
Ox.getTimezoneOffsetString = function(date) {
/*
Time zone offset string ('-1200' - '+1200')
@ -1091,7 +1197,8 @@ Ox.makeYear = function(date, utc) {
};
[
'FullYear', 'Month', 'Date', 'Day', 'Hours', 'Minutes', 'Seconds'
'FullYear', 'Month', 'Date', 'Day',
'Hours', 'Minutes', 'Seconds', 'Milliseconds'
].forEach(function(noun) {
Ox['get' + noun] = function(date, utc) {
return Ox.makeDate(date)['get' + (utc ? 'UTC' : '') + noun]()
@ -1102,9 +1209,9 @@ Ox.makeYear = function(date, utc) {
return new Date(
Ox.makeDate(date)
)['set' + (utc ? 'UTC' : '') + noun](num);
// fixme: maybe we _want_ set to have side effects?
}
});
/*
================================================================================
@ -1118,7 +1225,7 @@ Ox.canvas = function() {
image = isImage ? arguments[0] : {
width: arguments[0], height: arguments[1]
};
c.context = (c.canvas = Ox.element('canvas').attr({
c.context = (c.canvas = Ox.element('<canvas>').attr({
width: image.width, height: image.height
})[0]).getContext('2d');
isImage && c.context.drawImage(image, 0, 0);
@ -1128,28 +1235,74 @@ Ox.canvas = function() {
return c;
};
Ox.documentReady = (function() {
var callbacks = [];
document.addEventListener('DOMContentLoaded', ready, false);
function ready() {
document.removeEventListener('DOMContentLoaded', ready, false);
callbacks.forEach(function(callback) {
callback();
});
delete callbacks;
}
return function(callback) {
if (document.readyState == 'complete') {
callback();
} else {
callbacks.push(callback);
}
}
}());
Ox.element = function(str) {
/*
Generic HTML element, mimics jQuery
>>> Ox.element('div').attr({id: 'foo'}).attr('id')
'foo'
>>> Ox.element('div').html('foo').html()
'foo'
*/
return {
0: str[0] == '#' ? document.getElementById(str.substr(1)) :
document.createElement(str),
0: str[0] == '<' ? document.createElement(str.substr(1, str.length - 2)) :
str[0] == '.' ? document.getElementsByClassName(str.substr(1))[0] :
str[0] == '#' ? document.getElementById(str.substr(1)) :
document.getElementsByTagName(str)[0],
addClass: function(str) {
this[0].className += (this[0].className ? ' ' : '') + str;
return this;
},
append: function(element) {
this[0].appendChild(element[0]);
return this;
},
appendTo: function(element) {
element[0].appendChild(this[0]);
return this;
},
attr: function() {
var args, ret, that = this;
var ret, that = this;
if (arguments.length == 1 && Ox.isString(arguments[0])) {
ret = this[0].getAttribute(arguments[0]);
} else {
Ox.forEach(Ox.makeObject.apply(this, arguments), function(v, k) {
Ox.forEach(Ox.makeObject.apply(null, arguments), function(v, k) {
that[0].setAttribute(k, v);
});
ret = this;
}
return ret;
},
css: function() {
var ret, that = this;
if (arguments.length == 1 && Ox.isString(arguments[0])) {
ret = this[0].style[arguments[0]];
} else {
Ox.forEach(Ox.makeObject.apply(null, arguments), function(v, k) {
that[0].style[k] = v;
});
ret = this;
}
return ret;
},
html: function(str) {
var ret;
if (Ox.isUndefined(str)) {
@ -1159,6 +1312,10 @@ Ox.element = function(str) {
ret = this;
}
return ret;
},
mousedown: function(fn) {
this[0].onmousedown = fn;
return this;
}
}
};
@ -1370,7 +1527,7 @@ Encoding functions
*/
// relies on dom, but shorter than using this:
// http://www.w3.org/TR/html5/named-character-references.html
return Ox.element('div').html(str)[0].childNodes[0].nodeValue;
return Ox.element('<div>').html(str)[0].childNodes[0].nodeValue;
//return $('<div/>').html(str)[0].childNodes[0].nodeValue;
};
@ -2203,7 +2360,7 @@ Ox.parseHTML = (function() {
// close extra opening (and remove extra closing) tags
// return $('<div>').html(html).html();
// fixme: this converts '&quot;' to '"'
return Ox.element('div').html(html).html();
return Ox.element('<div>').html(html).html();
}
}());
@ -2461,6 +2618,7 @@ Ox.pad = function(str, len, pad, pos) {
Ox.repeat = function(str, num) {
/*
fixme: make this work for arrays, like in python
>>> Ox.repeat(1, 3)
"111"
>>> Ox.repeat("foo", 3)