don't loop through all jQuery functions on every Ox.Element creation

This commit is contained in:
rolux 2011-04-19 11:09:51 +02:00
commit 9a61861bf5
4 changed files with 2757 additions and 518 deletions

View file

@ -24,7 +24,7 @@ requires
Ox.theme(Ox.UI.DEFAULT_THEME);
});
return {
$elements: {},
elements: {},
DEFAULT_THEME: 'classic',
DIMENSIONS: {
horizontal: ['width', 'height'],
@ -362,7 +362,7 @@ requires
stack.splice(stack.length - 2, 0, stack.pop());
//$elements[id].removeClass('OxFocus');
$('.OxFocus').removeClass('OxFocus'); // fixme: the above is better, and should work
stack.length && Ox.UI.$elements[stack[stack.length - 1]].addClass('OxFocus');
stack.length && Ox.UI.elements[stack[stack.length - 1]].addClass('OxFocus');
Ox.print('blur', id, stack);
}
},
@ -372,7 +372,7 @@ requires
index > -1 && stack.splice(index, 1);
stack.push(id);
$('.OxFocus').removeClass('OxFocus'); // fixme: see above
Ox.UI.$elements[id].addClass('OxFocus');
Ox.UI.elements[id].addClass('OxFocus');
Ox.print('focus', id, stack);
}
},
@ -568,8 +568,8 @@ requires
buffer += key == 'SPACE' ? ' ' : key;
bufferTime = time;
}
focused !== null && Ox.UI.$elements[focused].triggerEvent('key_' + key);
if (['down', 'space', 'up'].indexOf(key) > -1 && !Ox.UI.$elements[focused].hasClass('OxInput')) {
focused !== null && Ox.UI.elements[focused].triggerEvent('key_' + key);
if (['down', 'space', 'up'].indexOf(key) > -1 && !Ox.UI.elements[focused].hasClass('OxInput')) {
// prevent chrome from scrolling
return false;
}
@ -834,44 +834,38 @@ requires
return that;
};
Ox.jQueryElement = (function() {
// Basic jQuery element
var jQueryFunctions = (function() {
var functions = [];
Ox.each($('<div>'), function(key, val) {
typeof val == 'function' && functions.push(key);
});
return functions.sort();
})();
return function(element) {
var that = {};
Ox.each(jQueryFunctions, function(i, fn) {
that[fn] = function() {
var args = arguments, id, ret;
$.each(args, function(i, arg) {
// 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 = element.$element[fn].apply(element.$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()
/*
if (fn == 'appendTo') {
Ox.print('ret', ret, $element, ret.jquery && Ox.UI.$elements[id = ret.data('ox')] == true)
// Basic jQuery element
Ox.$Element = function($element) {
var that = this;
that.id = Ox.uid();
that.ox = Ox.VERSION;
that.$element = $element.data({
oxid: that.id
});
Ox.UI.elements[that.id] = that;
return that;
};
Ox.each($('<div>'), function(key, val) {
if (Ox.isFunction(val)) {
Ox.$Element.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;
}
*/
return ret.jquery && Ox.UI.$elements[id = ret.data('ox')] ?
Ox.UI.$elements[id] : ret;
};
});
return that;
});
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()
return ret.jquery && Ox.UI.elements[id = ret.data('oxid')] ?
Ox.UI.elements[id] : ret;
};
}
})();
});
// check out http://ejohn.org/apps/learn/#36 (-#38, making fns work w/o new)
@ -889,28 +883,18 @@ requires
self = self || {};
self.options = options || {};
if (!self.$eventHandler) {
self.$eventHandler = $('<div>');
}
var that = this;
// allow for Ox.Element('tagname', self)
if (typeof self.options == 'string') {
self.options = {
element: self.options
};
}
that.ox = Ox.VERSION;
that.id = Ox.uid();
that.$element = $('<' + (self.options.element || 'div') + '/>', {
data: {
ox: that.id
},
mousedown: mousedown
});
Ox.UI.$elements[that.id] = that;
if (!self.$eventHandler) {
self.$eventHandler = $('<div>');
}
$.extend(that, Ox.jQueryElement(that));
var that = new Ox.$Element($('<' + (self.options.element || 'div') + '>'));
that.$element.mousedown(mousedown);
function mousedown(e) {
/*
@ -1088,7 +1072,7 @@ requires
that.loseFocus();
delete self.$eventHandler;
that.$element.remove();
delete Ox.UI.$elements[that.ox];
delete Ox.UI.elements[that.id];
return that;
};