From 0e9432076d152daa1e2103950556b2f03413d11f Mon Sep 17 00:00:00 2001 From: rlx <0x0073@0x2620.org> Date: Tue, 19 Apr 2011 19:06:37 +0000 Subject: [PATCH] make Ox a function, allow for Ox(obj).fn() instead of Ox.fn(obj) --- build/js/ox.js | 96 +++++++++++++++++++++++++++-------------------- build/js/ox.ui.js | 10 +++-- 2 files changed, 61 insertions(+), 45 deletions(-) diff --git a/build/js/ox.js b/build/js/ox.js index bc9732dd..ba78b30f 100644 --- a/build/js/ox.js +++ b/build/js/ox.js @@ -1,29 +1,8 @@ // todo: check http://ejohn.org/blog/ecmascript-5-strict-mode-json-and-more/ -/* -(function() { - var methods = { - 'array': ['pop', 'push', 'shift', 'unshift'], - 'string': [ - 'charAt', 'charCodeAt', 'concat', 'indexOf', 'lastIndexOf', - 'match', 'replace', 'search', 'slice', 'split', - 'substr', 'substring', 'toLocaleLowerCase', 'toLocaleUpperCase', 'toLowerCase', - 'toString', 'toUpperCase', 'trim', 'trimLeft, 'trimRight', - 'valueOf' - ] - }, - global = this; - return function Ox(val) { - var ret, - type = Ox.typeof(val); - if (type == 'array') { - - } - } -})(); -*/ - -Ox = {}; +Ox = function(val) { + return Ox.wrap(val); +}; /* ================================================================================ @@ -180,6 +159,36 @@ Ox.uid = (function() { }; }()); +Ox.wrap = function(val, chained) { + /*** + >>> Ox.wrap('foobar').reverse() + 'raboof' + >>> Ox.wrap('foobar').chain().reverse().reverse().value() + 'foobar' + ***/ + var wrapper = { + chain: function() { + wrapper.chained = true; + return wrapper; + }, + chained: chained || false, + value: function() { + return val; + } + }; + Object.getOwnPropertyNames(Ox).forEach(function(name) { + if (name[0] == name[0].toLowerCase() && Ox.isFunction(Ox[name])) { + wrapper[name] = function() { + var args = Array.prototype.slice.call(arguments), ret; + args.unshift(val); + ret = Ox[name].apply(Ox, args); + return wrapper.chained ? Ox.wrap(ret, true) : ret; + } + } + }); + return wrapper; +}; + /* ================================================================================ Array and Object functions @@ -194,7 +203,7 @@ Ox.avg = function(obj) { >>> Ox.avg({a: 1, b: 2, c: 3}) 2 ***/ - return Ox.sum(obj) / Ox.length(obj); + return Ox.sum(obj) / Ox.len(obj); }; Ox.clone = function(obj) { @@ -269,7 +278,7 @@ Ox.every = function(obj, fn) { */ return Ox.filter(Ox.values(obj), fn || function(v) { return v; - }).length == Ox.length(obj); + }).length == Ox.len(obj); }; Ox.extend = function() { @@ -395,7 +404,7 @@ Ox.getPositionById = function(arr, id) { }; Ox.isEmpty = function(val) { - return Ox.length(val) == 0; + return Ox.len(val) == 0; }; Ox.isEqual = function(obj0, obj1) { @@ -441,12 +450,13 @@ Ox.isEqual = function(obj0, obj1) { Ox.keys = function(obj) { /* - >>> Ox.keys('123') - [0, 1, 2] + works for arrays, objects and strings, unlike Object.keys() >>> Ox.keys([1, 2, 3]) [0, 1, 2] >>> Ox.keys({a: 1, b: 2, c: 3}) ["a", "b", "c"] + >>> Ox.keys('abc') + [0, 1, 2] >>> Ox.keys([,]) [0] */ @@ -457,16 +467,18 @@ Ox.keys = function(obj) { return keys.sort(); }; -Ox.length = function(obj) { +Ox.len = function(obj) { /* - >>> Ox.length([1, 2, 3]) + >>> Ox.len([1, 2, 3]) 3 - >>> Ox.length({"a": 1, "b": 2, "c": 3}) + >>> Ox.len({a: 1, b: 2, c: 3}) 3 - >>> Ox.length([,]) + >>> Ox.len('abc') + 3 + >>> Ox.len([,]) 1 */ - return Ox.isArray(obj) ? obj.length : Ox.values(obj).length; + return Ox.isObject(obj) ? Ox.values(obj).length : obj.length; }; Ox.makeArray = function(arg) { @@ -734,12 +746,12 @@ Ox.unserialize = function(str) { Ox.values = function(obj) { /* - >>> Ox.values('123') - ['1', '2', '3'] >>> Ox.values([1, 2, 3]) [1, 2, 3] >>> Ox.values({a: 1, b: 2, c: 3}) [1, 2, 3] + >>> Ox.values('abc') + ['a', 'b', 'c'] >>> Ox.values([1,]) [1] */ @@ -1843,10 +1855,11 @@ Geo functions }; Ox.getDegreesPerMeter = function(lat) { - /* - >>> Ox.EARTH_CIRCUMFERENCE = 360 / Ox.getDegreesPerMeter(0) + /*** + return degrees per meter at a given latitude + >>> Ox.EARTH_CIRCUMFERENCE == 360 / Ox.getDegreesPerMeter(0) true - */ + ***/ return 360 / Ox.EARTH_CIRCUMFERENCE / Math.cos(lat * Math.PI / 180); }; @@ -1879,10 +1892,11 @@ Geo functions }; Ox.getMetersPerDegree = function(lat) { - /* + /*** + returns meters per degree at a given latitude >>> Ox.EARTH_CIRCUMFERENCE == Ox.getMetersPerDegree(0) * 360 true - */ + ***/ return Math.cos(lat * Math.PI / 180) * Ox.EARTH_CIRCUMFERENCE / 360; }; diff --git a/build/js/ox.ui.js b/build/js/ox.ui.js index 7a76dca5..39cefdb3 100644 --- a/build/js/ox.ui.js +++ b/build/js/ox.ui.js @@ -561,7 +561,6 @@ requires event[k] && keys.push(v); }); // avoid pushing modifier twice - //Ox.print('keys', keys) if (keyNames[event.keyCode] && keys.indexOf(keyNames[event.keyCode]) == -1) { keys.push(keyNames[event.keyCode]); } @@ -606,6 +605,7 @@ requires ***/ var cache = {}, + //dfd = $.Deferred(), pending = {}, requests = {}, self = { @@ -644,7 +644,7 @@ requires }, requests: function() { - return Ox.length(requests); + return Ox.len(requests); }, send: function(options) { @@ -691,7 +691,7 @@ requires function callback(data) { delete requests[options.id]; - //Ox.length(requests) == 0 && $body.trigger('requestStop'); + //Ox.len(requests) == 0 && $body.trigger('requestStop'); options.callback && options.callback(data); } @@ -801,6 +801,8 @@ requires callback(data); } + // return dfd.promise(); + return options.id; } @@ -6579,7 +6581,7 @@ requires }); */ - // fixme: without this, horizontal lists don't get their full width ... weird + // fixme: without this, horizontal lists don't get their full width self.options.orientation == 'horizontal' && that.$content.css({height: '1px'}); $.extend(self, {