ox.js cleanup and fixes
This commit is contained in:
parent
11d46c4d17
commit
195cff5bfe
2 changed files with 46 additions and 36 deletions
|
@ -17,13 +17,15 @@ Ox.JQueryElement = function($element) {
|
||||||
that.id = Ox.uid();
|
that.id = Ox.uid();
|
||||||
//@ ox <string> OxJS version
|
//@ ox <string> OxJS version
|
||||||
that.ox = Ox.VERSION;
|
that.ox = Ox.VERSION;
|
||||||
//@ $element <object> The jQuery DOM element
|
//@ $element <object> The jQuery-wrapped DOM element
|
||||||
that.$element = $element.data({
|
that.$element = $element.data({
|
||||||
oxid: that.id
|
oxid: that.id
|
||||||
});
|
});
|
||||||
// FIXME: the following two lines should make it possible to do
|
// FIXME: the following two lines should make it possible to do
|
||||||
// $('<div>').appendTo($element) ... check if it works, then replace all
|
// $('<div>').appendTo($element) ... check if it works, then replace all
|
||||||
|
//@ 0 <element> The DOM element
|
||||||
that[0] = that.$element[0];
|
that[0] = that.$element[0];
|
||||||
|
//@ length <number> 1 (for compatibility with jQuery)
|
||||||
that.length = 1;
|
that.length = 1;
|
||||||
Ox.UI.elements[that.id] = that;
|
Ox.UI.elements[that.id] = that;
|
||||||
return that;
|
return that;
|
||||||
|
@ -35,6 +37,7 @@ Ox.forEach($('<div>'), function(val, key) {
|
||||||
Ox.JQueryElement.prototype[key] = function() {
|
Ox.JQueryElement.prototype[key] = function() {
|
||||||
var args = arguments, id, ret, that = this;
|
var args = arguments, id, ret, that = this;
|
||||||
Ox.forEach(args, function(arg, i) {
|
Ox.forEach(args, function(arg, i) {
|
||||||
|
// FIXME: with the changes above, is this still needed?
|
||||||
// if an ox object was passed
|
// if an ox object was passed
|
||||||
// then pass its $element instead
|
// then pass its $element instead
|
||||||
// so that we can do oxObj.jqFn(oxObj)
|
// so that we can do oxObj.jqFn(oxObj)
|
||||||
|
|
77
source/Ox.js
77
source/Ox.js
|
@ -67,8 +67,9 @@ Ox.load = function(module, options, callback) {
|
||||||
// fixme: no way to load multiple modules
|
// fixme: no way to load multiple modules
|
||||||
// problem: where do multiple options go?
|
// problem: where do multiple options go?
|
||||||
// [{name: "", options: {}}, {name: "", options: {}}] isn't pretty
|
// [{name: "", options: {}}, {name: "", options: {}}] isn't pretty
|
||||||
callback = arguments[arguments.length - 1];
|
var len = arguments.length;
|
||||||
options = arguments.length == 3 ? arguments[1] : {};
|
callback = arguments[len - 1];
|
||||||
|
options = len == 3 ? arguments[1] : {};
|
||||||
Ox.loadFile(Ox.PATH + 'Ox.' + module + '/Ox.' + module + '.js', function() {
|
Ox.loadFile(Ox.PATH + 'Ox.' + module + '/Ox.' + module + '.js', function() {
|
||||||
Ox.load[module](options, callback);
|
Ox.load[module](options, callback);
|
||||||
});
|
});
|
||||||
|
@ -220,9 +221,9 @@ Ox.sort = function(arr, fn) {
|
||||||
// pad leading numbers, and make lower case
|
// pad leading numbers, and make lower case
|
||||||
values.forEach(function(val) {
|
values.forEach(function(val) {
|
||||||
sort[val] = (
|
sort[val] = (
|
||||||
matches[val] ?
|
matches[val] ? Ox.pad(
|
||||||
Ox.pad(matches[val], len) + val.toString().substr(matches[val].length) :
|
matches[val], len
|
||||||
val
|
) + val.toString().substr(matches[val].length) : val
|
||||||
).toLowerCase();
|
).toLowerCase();
|
||||||
});
|
});
|
||||||
return arr.sort(function(a, b) {
|
return arr.sort(function(a, b) {
|
||||||
|
@ -245,11 +246,9 @@ Ox.unique <f> Returns an array without duplicate values
|
||||||
@*/
|
@*/
|
||||||
|
|
||||||
Ox.unique = function(arr) {
|
Ox.unique = function(arr) {
|
||||||
var unique = [];
|
return Ox.map(arr, function(val) {
|
||||||
Ox.forEach(arr, function(val) {
|
return arr.indexOf(val) == arr.lastIndexOf(val) ? val : false;
|
||||||
unique.indexOf(val) == -1 && unique.push(val);
|
|
||||||
});
|
});
|
||||||
return unique;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/*@
|
/*@
|
||||||
|
@ -264,8 +263,8 @@ Ox.zip = function() {
|
||||||
arr = [];
|
arr = [];
|
||||||
args[0].forEach(function(v, i) {
|
args[0].forEach(function(v, i) {
|
||||||
arr[i] = [];
|
arr[i] = [];
|
||||||
args.forEach(function(v_, i_) {
|
args.forEach(function(v) {
|
||||||
arr[i].push(v_[i]);
|
arr[i].push(v[i]);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
return arr;
|
return arr;
|
||||||
|
@ -629,20 +628,20 @@ Ox.last = function(arr, val) {
|
||||||
Ox.len <f> Returns the length of an array, function, object or string
|
Ox.len <f> Returns the length of an array, function, object or string
|
||||||
Not to be confused with <code>Ox.length</code>, which is the
|
Not to be confused with <code>Ox.length</code>, which is the
|
||||||
<code>length</code> property of the <code>Ox</code> function
|
<code>length</code> property of the <code>Ox</code> function
|
||||||
(<code>1</code).
|
(<code>1</code>).
|
||||||
> Ox.len([1, 2, 3])
|
> Ox.len([1, 2, 3])
|
||||||
3
|
3
|
||||||
> Ox.len([,])
|
> Ox.len([,])
|
||||||
1
|
1
|
||||||
> Ox.len({a: 1, b: 2, c: 3})
|
> Ox.len({a: 1, b: 2, c: 3})
|
||||||
3
|
3
|
||||||
> Ox.len(function(x, y, z) { return x + y + z; })
|
> Ox.len(function(a, b, c) {})
|
||||||
3
|
3
|
||||||
> Ox.len('abc')
|
> Ox.len('abc')
|
||||||
3
|
3
|
||||||
@*/
|
@*/
|
||||||
Ox.len = function(obj) {
|
Ox.len = function(obj) {
|
||||||
return Ox.isObject(obj) ? Ox.values(obj).length : obj.length;
|
return (Ox.isObject(obj) ? Ox.values(obj) : obj).length;
|
||||||
};
|
};
|
||||||
|
|
||||||
/*@
|
/*@
|
||||||
|
@ -672,11 +671,11 @@ Ox.loop <f> For-loop, functional-style
|
||||||
4
|
4
|
||||||
@*/
|
@*/
|
||||||
Ox.loop = function() {
|
Ox.loop = function() {
|
||||||
var length = arguments.length,
|
var len = arguments.length,
|
||||||
start = length > 2 ? arguments[0] : 0,
|
start = len > 2 ? arguments[0] : 0,
|
||||||
stop = arguments[length > 2 ? 1 : 0],
|
stop = arguments[len > 2 ? 1 : 0],
|
||||||
step = length == 4 ? arguments[2] : (start <= stop ? 1 : -1),
|
step = len == 4 ? arguments[2] : (start <= stop ? 1 : -1),
|
||||||
callback = arguments[length - 1],
|
callback = arguments[len - 1],
|
||||||
i;
|
i;
|
||||||
for (i = start; step > 0 ? i < stop : i > stop; i += step) {
|
for (i = start; step > 0 ? i < stop : i > stop; i += step) {
|
||||||
if (callback(i) === false) {
|
if (callback(i) === false) {
|
||||||
|
@ -1017,8 +1016,8 @@ Ox.hsl <f> Takes RGB values and returns HSL values
|
||||||
[120, 1, 0.5]
|
[120, 1, 0.5]
|
||||||
@*/
|
@*/
|
||||||
Ox.hsl = function(rgb) {
|
Ox.hsl = function(rgb) {
|
||||||
rgb = rgb.map(function(v) {
|
rgb = rgb.map(function(val) {
|
||||||
return v / 255;
|
return val / 255;
|
||||||
});
|
});
|
||||||
var max = Ox.max(rgb),
|
var max = Ox.max(rgb),
|
||||||
min = Ox.min(rgb),
|
min = Ox.min(rgb),
|
||||||
|
@ -2034,8 +2033,8 @@ Ox.element = function(str) {
|
||||||
see http://en.wikipedia.org/wiki/UTF-8
|
see http://en.wikipedia.org/wiki/UTF-8
|
||||||
(string) -> <s> UTF-8 encoded string
|
(string) -> <s> UTF-8 encoded string
|
||||||
string <s> Any string
|
string <s> Any string
|
||||||
> Ox.encodeUTF8("foo")
|
> Ox.encodeUTF8("YES")
|
||||||
"foo"
|
"YES"
|
||||||
> Ox.encodeUTF8("¥€$")
|
> Ox.encodeUTF8("¥€$")
|
||||||
"\u00C2\u00A5\u00E2\u0082\u00AC\u0024"
|
"\u00C2\u00A5\u00E2\u0082\u00AC\u0024"
|
||||||
@*/
|
@*/
|
||||||
|
@ -2062,8 +2061,8 @@ Ox.element = function(str) {
|
||||||
see http://en.wikipedia.org/wiki/UTF-8
|
see http://en.wikipedia.org/wiki/UTF-8
|
||||||
(utf8) -> <s> string
|
(utf8) -> <s> string
|
||||||
utf8 <s> Any UTF-8-encoded string
|
utf8 <s> Any UTF-8-encoded string
|
||||||
> Ox.decodeUTF8('foo')
|
> Ox.decodeUTF8('YES')
|
||||||
'foo'
|
'YES'
|
||||||
> Ox.decodeUTF8('\u00C2\u00A5\u00E2\u0082\u00AC\u0024')
|
> Ox.decodeUTF8('\u00C2\u00A5\u00E2\u0082\u00AC\u0024')
|
||||||
'¥€$'
|
'¥€$'
|
||||||
@*/
|
@*/
|
||||||
|
@ -3398,7 +3397,9 @@ Ox.test = function(file, callback) {
|
||||||
name: item.name,
|
name: item.name,
|
||||||
section: item.section,
|
section: item.section,
|
||||||
statement: example.statement,
|
statement: example.statement,
|
||||||
passed: Ox.isEqual(eval('Ox.test.result = ' + example.result), actual)
|
passed: Ox.isEqual(eval(
|
||||||
|
'Ox.test.result = ' + example.result
|
||||||
|
), actual)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -3837,17 +3838,16 @@ Ox.rad = function(deg) {
|
||||||
|
|
||||||
/*@
|
/*@
|
||||||
Ox.random <f> Returns a random integer
|
Ox.random <f> Returns a random integer
|
||||||
> [0, 1, 2, 3].indexOf(Ox.random(3)) > -1
|
> [0, 1, 2].indexOf(Ox.random(3)) > -1
|
||||||
true
|
true
|
||||||
> [1, 2].indexOf(Ox.random(1, 2)) > -1
|
> Ox.random(1, 2) == 1
|
||||||
true
|
true
|
||||||
@*/
|
@*/
|
||||||
Ox.random = function() {
|
Ox.random = function() {
|
||||||
// FIXME: weird, reimplement with Ox.loop / Ox.range
|
|
||||||
var len = arguments.length,
|
var len = arguments.length,
|
||||||
min = len == 1 ? 0 : arguments[0],
|
min = len == 1 ? 0 : arguments[0],
|
||||||
max = arguments[len - 1];
|
max = arguments[len - 1];
|
||||||
return min + parseInt(Math.random() * (max - min + 1));
|
return min + parseInt(Math.random() * (max - min));
|
||||||
};
|
};
|
||||||
|
|
||||||
/*@
|
/*@
|
||||||
|
@ -3901,6 +3901,8 @@ Ox.extend = function() {
|
||||||
Ox.serialize <f> Parses an object into query parameters
|
Ox.serialize <f> Parses an object into query parameters
|
||||||
> Ox.serialize({a: 1, b: 2, c: 3})
|
> Ox.serialize({a: 1, b: 2, c: 3})
|
||||||
'a=1&b=2&c=3'
|
'a=1&b=2&c=3'
|
||||||
|
> Ox.serialize({a: 1, b: 2.3, c: [4, 5]})
|
||||||
|
'a=1&b=2.3&c=3,4,5'
|
||||||
@*/
|
@*/
|
||||||
Ox.serialize = function(obj) {
|
Ox.serialize = function(obj) {
|
||||||
var arr = [];
|
var arr = [];
|
||||||
|
@ -3913,14 +3915,19 @@ Ox.serialize = function(obj) {
|
||||||
/*@
|
/*@
|
||||||
Ox.unserialize <f> Parses query parameters into an object
|
Ox.unserialize <f> Parses query parameters into an object
|
||||||
> Ox.unserialize('a=1&b=2&c=3')
|
> Ox.unserialize('a=1&b=2&c=3')
|
||||||
{a: 1, b: 2, c: 3}
|
{a: '1', b: '2', c: '3'}
|
||||||
|
> Ox.unserialize('a=1&b=2.3&c=4,5', true)
|
||||||
|
{a: 1, b: 2.3, c: [4, 5]}
|
||||||
@*/
|
@*/
|
||||||
Ox.unserialize = function(str) {
|
Ox.unserialize = function(str, toNumber) {
|
||||||
var obj = {};
|
var obj = {};
|
||||||
Ox.forEach(str.split('&'), function(val) {
|
Ox.forEach(str.split('&'), function(val) {
|
||||||
var arr = val.split('='),
|
var arr = val.split('=');
|
||||||
num = +arr[1];
|
obj[arr[0]] = !toNumber ? arr[1]
|
||||||
obj[arr[0]] = Ox.isNumber(num) ? num : arr[1];
|
: arr[1].indexOf(',') == -1 ? +arr[1]
|
||||||
|
: arr[1].split(',').map(function(val) {
|
||||||
|
return +val;
|
||||||
|
});
|
||||||
});
|
});
|
||||||
return obj;
|
return obj;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue