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();
|
||||
//@ ox <string> OxJS version
|
||||
that.ox = Ox.VERSION;
|
||||
//@ $element <object> The jQuery DOM element
|
||||
//@ $element <object> The jQuery-wrapped DOM element
|
||||
that.$element = $element.data({
|
||||
oxid: that.id
|
||||
});
|
||||
// FIXME: the following two lines should make it possible to do
|
||||
// $('<div>').appendTo($element) ... check if it works, then replace all
|
||||
//@ 0 <element> The DOM element
|
||||
that[0] = that.$element[0];
|
||||
//@ length <number> 1 (for compatibility with jQuery)
|
||||
that.length = 1;
|
||||
Ox.UI.elements[that.id] = that;
|
||||
return that;
|
||||
|
@ -35,6 +37,7 @@ Ox.forEach($('<div>'), function(val, key) {
|
|||
Ox.JQueryElement.prototype[key] = function() {
|
||||
var args = arguments, id, ret, that = this;
|
||||
Ox.forEach(args, function(arg, i) {
|
||||
// FIXME: with the changes above, is this still needed?
|
||||
// if an ox object was passed
|
||||
// then pass its $element instead
|
||||
// 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
|
||||
// problem: where do multiple options go?
|
||||
// [{name: "", options: {}}, {name: "", options: {}}] isn't pretty
|
||||
callback = arguments[arguments.length - 1];
|
||||
options = arguments.length == 3 ? arguments[1] : {};
|
||||
var len = arguments.length;
|
||||
callback = arguments[len - 1];
|
||||
options = len == 3 ? arguments[1] : {};
|
||||
Ox.loadFile(Ox.PATH + 'Ox.' + module + '/Ox.' + module + '.js', function() {
|
||||
Ox.load[module](options, callback);
|
||||
});
|
||||
|
@ -220,9 +221,9 @@ Ox.sort = function(arr, fn) {
|
|||
// pad leading numbers, and make lower case
|
||||
values.forEach(function(val) {
|
||||
sort[val] = (
|
||||
matches[val] ?
|
||||
Ox.pad(matches[val], len) + val.toString().substr(matches[val].length) :
|
||||
val
|
||||
matches[val] ? Ox.pad(
|
||||
matches[val], len
|
||||
) + val.toString().substr(matches[val].length) : val
|
||||
).toLowerCase();
|
||||
});
|
||||
return arr.sort(function(a, b) {
|
||||
|
@ -245,11 +246,9 @@ Ox.unique <f> Returns an array without duplicate values
|
|||
@*/
|
||||
|
||||
Ox.unique = function(arr) {
|
||||
var unique = [];
|
||||
Ox.forEach(arr, function(val) {
|
||||
unique.indexOf(val) == -1 && unique.push(val);
|
||||
return Ox.map(arr, function(val) {
|
||||
return arr.indexOf(val) == arr.lastIndexOf(val) ? val : false;
|
||||
});
|
||||
return unique;
|
||||
};
|
||||
|
||||
/*@
|
||||
|
@ -264,8 +263,8 @@ Ox.zip = function() {
|
|||
arr = [];
|
||||
args[0].forEach(function(v, i) {
|
||||
arr[i] = [];
|
||||
args.forEach(function(v_, i_) {
|
||||
arr[i].push(v_[i]);
|
||||
args.forEach(function(v) {
|
||||
arr[i].push(v[i]);
|
||||
});
|
||||
});
|
||||
return arr;
|
||||
|
@ -629,20 +628,20 @@ Ox.last = function(arr, val) {
|
|||
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
|
||||
<code>length</code> property of the <code>Ox</code> function
|
||||
(<code>1</code).
|
||||
(<code>1</code>).
|
||||
> Ox.len([1, 2, 3])
|
||||
3
|
||||
> Ox.len([,])
|
||||
1
|
||||
> Ox.len({a: 1, b: 2, c: 3})
|
||||
3
|
||||
> Ox.len(function(x, y, z) { return x + y + z; })
|
||||
> Ox.len(function(a, b, c) {})
|
||||
3
|
||||
> Ox.len('abc')
|
||||
3
|
||||
@*/
|
||||
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
|
||||
@*/
|
||||
Ox.loop = function() {
|
||||
var length = arguments.length,
|
||||
start = length > 2 ? arguments[0] : 0,
|
||||
stop = arguments[length > 2 ? 1 : 0],
|
||||
step = length == 4 ? arguments[2] : (start <= stop ? 1 : -1),
|
||||
callback = arguments[length - 1],
|
||||
var len = arguments.length,
|
||||
start = len > 2 ? arguments[0] : 0,
|
||||
stop = arguments[len > 2 ? 1 : 0],
|
||||
step = len == 4 ? arguments[2] : (start <= stop ? 1 : -1),
|
||||
callback = arguments[len - 1],
|
||||
i;
|
||||
for (i = start; step > 0 ? i < stop : i > stop; i += step) {
|
||||
if (callback(i) === false) {
|
||||
|
@ -1017,8 +1016,8 @@ Ox.hsl <f> Takes RGB values and returns HSL values
|
|||
[120, 1, 0.5]
|
||||
@*/
|
||||
Ox.hsl = function(rgb) {
|
||||
rgb = rgb.map(function(v) {
|
||||
return v / 255;
|
||||
rgb = rgb.map(function(val) {
|
||||
return val / 255;
|
||||
});
|
||||
var max = Ox.max(rgb),
|
||||
min = Ox.min(rgb),
|
||||
|
@ -2034,8 +2033,8 @@ Ox.element = function(str) {
|
|||
see http://en.wikipedia.org/wiki/UTF-8
|
||||
(string) -> <s> UTF-8 encoded string
|
||||
string <s> Any string
|
||||
> Ox.encodeUTF8("foo")
|
||||
"foo"
|
||||
> Ox.encodeUTF8("YES")
|
||||
"YES"
|
||||
> Ox.encodeUTF8("¥€$")
|
||||
"\u00C2\u00A5\u00E2\u0082\u00AC\u0024"
|
||||
@*/
|
||||
|
@ -2062,8 +2061,8 @@ Ox.element = function(str) {
|
|||
see http://en.wikipedia.org/wiki/UTF-8
|
||||
(utf8) -> <s> string
|
||||
utf8 <s> Any UTF-8-encoded string
|
||||
> Ox.decodeUTF8('foo')
|
||||
'foo'
|
||||
> Ox.decodeUTF8('YES')
|
||||
'YES'
|
||||
> Ox.decodeUTF8('\u00C2\u00A5\u00E2\u0082\u00AC\u0024')
|
||||
'¥€$'
|
||||
@*/
|
||||
|
@ -3398,7 +3397,9 @@ Ox.test = function(file, callback) {
|
|||
name: item.name,
|
||||
section: item.section,
|
||||
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
|
||||
> [0, 1, 2, 3].indexOf(Ox.random(3)) > -1
|
||||
> [0, 1, 2].indexOf(Ox.random(3)) > -1
|
||||
true
|
||||
> [1, 2].indexOf(Ox.random(1, 2)) > -1
|
||||
> Ox.random(1, 2) == 1
|
||||
true
|
||||
@*/
|
||||
Ox.random = function() {
|
||||
// FIXME: weird, reimplement with Ox.loop / Ox.range
|
||||
var len = arguments.length,
|
||||
min = len == 1 ? 0 : arguments[0],
|
||||
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({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) {
|
||||
var arr = [];
|
||||
|
@ -3913,14 +3915,19 @@ Ox.serialize = function(obj) {
|
|||
/*@
|
||||
Ox.unserialize <f> Parses query parameters into an object
|
||||
> 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 = {};
|
||||
Ox.forEach(str.split('&'), function(val) {
|
||||
var arr = val.split('='),
|
||||
num = +arr[1];
|
||||
obj[arr[0]] = Ox.isNumber(num) ? num : arr[1];
|
||||
var arr = val.split('=');
|
||||
obj[arr[0]] = !toNumber ? arr[1]
|
||||
: arr[1].indexOf(',') == -1 ? +arr[1]
|
||||
: arr[1].split(',').map(function(val) {
|
||||
return +val;
|
||||
});
|
||||
});
|
||||
return obj;
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue