update event controller

This commit is contained in:
rolux 2012-06-30 12:15:27 +02:00
parent 6dd23683f0
commit 2731f8dc89

View file

@ -42,9 +42,13 @@ Ox.Event = (function() {
/*@
.bind <f> Adds an event handler
(callback) -> <o> Ox.Event
Add a global event handler
(self, callback) -> <o> Ox.Event
Add a catch-all event handler
(self, event, callback) -> <o> Ox.Event
Add an event handler for a single event
(self, {event: callback, ...}) -> <o> Ox.Event
Add event handlers for multiple events
self <o> The element's shared private object
callback <f> Callback function
data <o> Event data
@ -64,11 +68,10 @@ Ox.Event = (function() {
if (Ox.len(args) && !self.eventHandlers) {
self.eventHandlers = {};
}
Ox.forEach(args, function(handler, event) {
handler.once = once;
Ox.forEach(args, function(callback, event) {
self.eventHandlers[event] = (
self.eventHandlers[event] || []
).concat(handler);
).concat({callback: callback, once: once});
});
}
return that;
@ -77,8 +80,11 @@ Ox.Event = (function() {
/*@
.bindOnce <f> Adds an event handler that fires once
(self, callback) -> <o> Ox.Event
Add a catch-all event handler
(self, event, callback) -> <o> Ox.Event
Add an event handler for a single event
(self, {event: callback, ...}) -> <o> Ox.Event
Add event handlers for multiple events
self <o> The element's shared private object
callback <f> Callback function
data <o> Event data
@ -104,8 +110,11 @@ Ox.Event = (function() {
/*@
.trigger <f> Triggers an event
(self, event) -> <o> Ox.Event
Trigger an event
(self, event, data) -> <o> Ox.Event
Trigger an event with event data
(self, {event: data, ...}) -> Ox.Event
Trigger multiple events with event data
self <o> The element's shared private object
event <s> Event name
data <o> Event data
@ -113,21 +122,19 @@ Ox.Event = (function() {
that.trigger = function(self) {
var element = this;
if (self.eventHandlers) {
Ox.forEach(Ox.makeObject(
Ox.slice(arguments, 1)
), function(data, event) {
Ox.forEach(Ox.makeObject(Ox.slice(arguments, 1)), function(data, event) {
var triggered = event.split('.');
triggered.map(function(v, i) {
return triggered.slice(0, i + 1).join('.');
}).concat('*').forEach(function(triggered) {
var handlers = self.eventHandlers[triggered];
handlers && handlers.forEach(function(handler) {
handler.once && that.unbind(self, triggered, handler);
handler.call(element, data || {}, event);
handlers && handlers.forEach(function(handler, i) {
handler.once && handlers.splice(i, 1);
handler.callback.call(element, data || {}, event);
});
});
eventHandlers.forEach(function(handler) {
handler.call(element, data || {}, event, element);
eventHandlers.forEach(function(callback) {
callback.call(element, data || {}, event, element);
});
});
}
@ -136,19 +143,29 @@ Ox.Event = (function() {
/*@
.unbind <f> Removes an event handler
() -> Ox.Event
Remove all global event handlers
(callback) -> <o> Ox.Event
Remove a global event handler
(self) -> <o> Ox.Event
Remove all event handlers
(self, callback) -> <o> Ox.Event
Unbind a specific catch-all event handler
(self, event) -> <o> Ox.Event
Unbind all event handlers for a single event
(self, event, callback) -> <o> Ox.Event
Unbind a specific event handler for a single event
(self, {event: callback, ...}) -> <o> Ox.Event
Unbind specific event handlers for multiple events
self <o> The element's shared private object
callback <f> Callback function
event <s> Event name
*/
that.unbind = function() {
var args = Ox.slice(arguments), self;
if (Ox.isFunction(args[0])) {
if (args.length == 0) {
eventHandlers = [];
} else if (Ox.isFunction(args[0])) {
eventHandlers.forEach(function(handler) {
handler === args[0] && eventHandlers.splice(i, 1);
});
@ -159,12 +176,14 @@ Ox.Event = (function() {
if (Ox.isFunction(args[0])) {
args = {'*': args[0]};
}
Ox.forEach(Ox.makeObject(args), function(unbind, event) {
if (Ox.isUndefined(unbind)) {
Ox.forEach(Ox.makeObject(args), function(callback, event) {
if (Ox.isUndefined(callback)) {
delete self.eventHandlers[event];
} else {
self.eventHandlers[event].forEach(function(handler, i) {
handler === unbind && self.eventHandlers[event].splice(i, 1);
if (handler.callback === callback) {
self.eventHandlers[event].splice(i, 1);
}
});
}
});