oxjs/source/Ox.UI/js/Core/Ox.JQueryElement.js

50 lines
1.6 KiB
JavaScript
Raw Normal View History

2011-07-29 18:48:43 +00:00
// vim: et:ts=4:sw=4:sts=4:ft=javascript
2011-04-25 09:12:02 +00:00
2011-05-05 18:02:56 +00:00
/*@
Ox.JQueryElement <function> Wrapper for jQuery
# Usage
($element) -> <object> Wrapped jQuery DOM element
# Arguments
$element <object> jQuery DOM Element
@*/
2011-04-25 09:12:02 +00:00
// fixme: it seems that children(), find() etc. don't work directly,
// and still have to be called on the $element
2011-04-22 22:03:10 +00:00
Ox.JQueryElement = function($element) {
var that = this;
2011-05-07 17:52:33 +00:00
//@ id <number> Unique id
2011-04-22 22:03:10 +00:00
that.id = Ox.uid();
2011-05-07 17:52:33 +00:00
//@ ox <string> OxJS version
2011-04-22 22:03:10 +00:00
that.ox = Ox.VERSION;
2011-05-07 17:52:33 +00:00
//@ $element <object> The jQuery DOM element
2011-04-22 22:03:10 +00:00
that.$element = $element.data({
oxid: that.id
});
Ox.UI.elements[that.id] = that;
return that;
};
2011-04-25 09:12:02 +00:00
2011-05-05 18:02:56 +00:00
// add all jQuery functions to the prototype of Ox.JQueryElement
2011-04-22 22:03:10 +00:00
Ox.forEach($('<div>'), function(val, key) {
if (Ox.isFunction(val)) {
Ox.JQueryElement.prototype[key] = function() {
var args = arguments, id, ret, that = this;
Ox.forEach(args, function(arg, i) {
// if an ox object was passed
// then pass its $element instead
// so that we can do oxObj.jqFn(oxObj)
if (arg && arg.ox) {
args[i] = arg.$element;
}
});
ret = that.$element[key].apply(that.$element, args);
// if the $element of an ox object was returned
// then return the ox object instead
// so that we can do oxObj.jqFn().oxFn()
2011-05-05 18:02:56 +00:00
return ret && ret.jquery && Ox.UI.elements[id = ret.data('oxid')] ?
2011-04-22 22:03:10 +00:00
Ox.UI.elements[id] : ret;
};
}
2011-04-25 12:14:03 +00:00
});