Ox.Element: use new event controller

This commit is contained in:
rolux 2012-06-30 11:24:38 +02:00
parent 32f85e42e4
commit b5910f5f62

View file

@ -81,10 +81,6 @@ Ox.Element = function(options, self) {
self.defaults = {}; self.defaults = {};
// allow for Ox.TestElement('<tagname>') or Ox.TestElement('cssSelector') // allow for Ox.TestElement('<tagname>') or Ox.TestElement('cssSelector')
self.options = Ox.isString(options) ? {element: options} : options || {}; self.options = Ox.isString(options) ? {element: options} : options || {};
// the actual event handler
self.$eventHandler = self.$eventHandler || $('<div>');
// array of callbacks bound to any event
self.eventCallbacks = self.eventCallbacks || [];
// stack of callbacks bound to option updates // stack of callbacks bound to option updates
self.updateCallbacks = self.updateCallbacks || []; self.updateCallbacks = self.updateCallbacks || [];
@ -95,20 +91,6 @@ Ox.Element = function(options, self) {
setTooltip(); setTooltip();
function bind(event, callback, once) {
self.$eventHandler[
once ? 'one' : 'on'
]('ox_' + event, function(event, data) {
call(callback, data, event);
});
}
function call(callback, data, event) {
event.ox_id = that.oxid;
event.ox_type = event.type.replace(/^ox_/, '');
callback.call(that, data || {}, event);
}
function mousedown(e) { function mousedown(e) {
/* /*
better mouse events better mouse events
@ -275,13 +257,7 @@ Ox.Element = function(options, self) {
Event names can be namespaced, like `'click.foo'` Event names can be namespaced, like `'click.foo'`
@*/ @*/
that.bindEvent = function() { that.bindEvent = function() {
if (Ox.typeOf(arguments[0]) == 'function') { Ox.Event.bind.apply(null, [self].concat(Ox.slice(arguments)));
self.eventCallbacks.push(arguments[0]);
} else {
Ox.forEach(Ox.makeObject(arguments), function(callback, event) {
bind(event, callback);
});
}
return that; return that;
}; };
@ -295,9 +271,7 @@ Ox.Element = function(options, self) {
Event names can be namespaced, like `'click.foo'` Event names can be namespaced, like `'click.foo'`
@*/ @*/
that.bindEventOnce = function() { that.bindEventOnce = function() {
Ox.forEach(Ox.makeObject(arguments), function(callback, event) { Ox.Event.bindOnce.apply(null, [self].concat(Ox.slice(arguments)));
bind(event, callback, true);
});
return that; return that;
}; };
@ -381,13 +355,11 @@ Ox.Element = function(options, self) {
@*/ @*/
that.remove = function(remove) { that.remove = function(remove) {
remove !== false && that.find('.OxElement').each(function() { remove !== false && that.find('.OxElement').each(function() {
var oxid = $(this).data('oxid'), var element = Ox.UI.elements[$(this).data('oxid')];
element = Ox.UI.elements[oxid];
element && element.remove(false); element && element.remove(false);
}); });
Ox.Focus.remove(that.oxid); Ox.Focus.remove(that.oxid);
Ox.Keyboard.unbind(that.oxid); Ox.Keyboard.unbind(that.oxid);
delete self.$eventHandler;
delete Ox.UI.elements[that.oxid]; delete Ox.UI.elements[that.oxid];
that.$tooltip && that.$tooltip.remove(); that.$tooltip && that.$tooltip.remove();
remove !== false && that.$element.remove(); remove !== false && that.$element.remove();
@ -428,27 +400,7 @@ Ox.Element = function(options, self) {
data <object> Event data (key/value pairs) data <object> Event data (key/value pairs)
@*/ @*/
that.triggerEvent = function() { that.triggerEvent = function() {
Ox.forEach(Ox.makeObject(arguments), function(data, event) { Ox.Event.trigger.apply(that, [self].concat(Ox.slice(arguments)));
var type = 'ox_' + event;
// FIXME: remove this
if ([
'mousedown', 'mouserepeat', 'anyclick', 'singleclick', 'doubleclick',
'dragstart', 'drag', 'dragenter', 'dragleave', 'dragpause', 'dragend',
'draganddropstart', 'draganddrop', 'draganddropenter', 'draganddropleave', 'draganddropend',
'playing', 'position', 'progress', 'request'
].indexOf(event) == -1) {
if (!/^pandora_/.test(event)) {
Ox.Log('EVENT', that.oxid, self.options.id, 'trigger', event, data);
}
}
// it is necessary to check if self.$eventHandler exists,
// since, for example, when removing the element on click,
// singleclick will fire after the removal of the event handler
self.$eventHandler && self.$eventHandler.trigger(type, data);
self.eventCallbacks.forEach(function(callback) {
call(callback, data, {type: type});
});
});
return that; return that;
}; };
@ -469,19 +421,7 @@ Ox.Element = function(options, self) {
event <string> Event name event <string> Event name
@*/ @*/
that.unbindEvent = function() { that.unbindEvent = function() {
var callback = arguments[0]; Ox.Event.unbind.apply(null, [self].concat(Ox.slice(arguments)));
if (arguments.length == 0) {
self.eventCallbacks = [];
self.$eventHandler.off();
} else if (Ox.typeOf(callback) == 'function') {
self.eventCallbacks = self.eventCallbacks.filter(function(fn) {
return fn !== callback;
});
} else {
Ox.forEach(Ox.makeObject(arguments), function(callback, event) {
self.$eventHandler.off('ox_' + event, callback);
});
}
return that; return that;
}; };