update event controller
This commit is contained in:
parent
6dd23683f0
commit
2731f8dc89
1 changed files with 34 additions and 15 deletions
|
@ -42,9 +42,13 @@ Ox.Event = (function() {
|
||||||
/*@
|
/*@
|
||||||
.bind <f> Adds an event handler
|
.bind <f> Adds an event handler
|
||||||
(callback) -> <o> Ox.Event
|
(callback) -> <o> Ox.Event
|
||||||
|
Add a global event handler
|
||||||
(self, callback) -> <o> Ox.Event
|
(self, callback) -> <o> Ox.Event
|
||||||
|
Add a catch-all event handler
|
||||||
(self, event, callback) -> <o> Ox.Event
|
(self, event, callback) -> <o> Ox.Event
|
||||||
|
Add an event handler for a single event
|
||||||
(self, {event: callback, ...}) -> <o> Ox.Event
|
(self, {event: callback, ...}) -> <o> Ox.Event
|
||||||
|
Add event handlers for multiple events
|
||||||
self <o> The element's shared private object
|
self <o> The element's shared private object
|
||||||
callback <f> Callback function
|
callback <f> Callback function
|
||||||
data <o> Event data
|
data <o> Event data
|
||||||
|
@ -64,11 +68,10 @@ Ox.Event = (function() {
|
||||||
if (Ox.len(args) && !self.eventHandlers) {
|
if (Ox.len(args) && !self.eventHandlers) {
|
||||||
self.eventHandlers = {};
|
self.eventHandlers = {};
|
||||||
}
|
}
|
||||||
Ox.forEach(args, function(handler, event) {
|
Ox.forEach(args, function(callback, event) {
|
||||||
handler.once = once;
|
|
||||||
self.eventHandlers[event] = (
|
self.eventHandlers[event] = (
|
||||||
self.eventHandlers[event] || []
|
self.eventHandlers[event] || []
|
||||||
).concat(handler);
|
).concat({callback: callback, once: once});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
return that;
|
return that;
|
||||||
|
@ -77,8 +80,11 @@ Ox.Event = (function() {
|
||||||
/*@
|
/*@
|
||||||
.bindOnce <f> Adds an event handler that fires once
|
.bindOnce <f> Adds an event handler that fires once
|
||||||
(self, callback) -> <o> Ox.Event
|
(self, callback) -> <o> Ox.Event
|
||||||
|
Add a catch-all event handler
|
||||||
(self, event, callback) -> <o> Ox.Event
|
(self, event, callback) -> <o> Ox.Event
|
||||||
|
Add an event handler for a single event
|
||||||
(self, {event: callback, ...}) -> <o> Ox.Event
|
(self, {event: callback, ...}) -> <o> Ox.Event
|
||||||
|
Add event handlers for multiple events
|
||||||
self <o> The element's shared private object
|
self <o> The element's shared private object
|
||||||
callback <f> Callback function
|
callback <f> Callback function
|
||||||
data <o> Event data
|
data <o> Event data
|
||||||
|
@ -104,8 +110,11 @@ Ox.Event = (function() {
|
||||||
/*@
|
/*@
|
||||||
.trigger <f> Triggers an event
|
.trigger <f> Triggers an event
|
||||||
(self, event) -> <o> Ox.Event
|
(self, event) -> <o> Ox.Event
|
||||||
|
Trigger an event
|
||||||
(self, event, data) -> <o> Ox.Event
|
(self, event, data) -> <o> Ox.Event
|
||||||
|
Trigger an event with event data
|
||||||
(self, {event: data, ...}) -> Ox.Event
|
(self, {event: data, ...}) -> Ox.Event
|
||||||
|
Trigger multiple events with event data
|
||||||
self <o> The element's shared private object
|
self <o> The element's shared private object
|
||||||
event <s> Event name
|
event <s> Event name
|
||||||
data <o> Event data
|
data <o> Event data
|
||||||
|
@ -113,21 +122,19 @@ Ox.Event = (function() {
|
||||||
that.trigger = function(self) {
|
that.trigger = function(self) {
|
||||||
var element = this;
|
var element = this;
|
||||||
if (self.eventHandlers) {
|
if (self.eventHandlers) {
|
||||||
Ox.forEach(Ox.makeObject(
|
Ox.forEach(Ox.makeObject(Ox.slice(arguments, 1)), function(data, event) {
|
||||||
Ox.slice(arguments, 1)
|
|
||||||
), function(data, event) {
|
|
||||||
var triggered = event.split('.');
|
var triggered = event.split('.');
|
||||||
triggered.map(function(v, i) {
|
triggered.map(function(v, i) {
|
||||||
return triggered.slice(0, i + 1).join('.');
|
return triggered.slice(0, i + 1).join('.');
|
||||||
}).concat('*').forEach(function(triggered) {
|
}).concat('*').forEach(function(triggered) {
|
||||||
var handlers = self.eventHandlers[triggered];
|
var handlers = self.eventHandlers[triggered];
|
||||||
handlers && handlers.forEach(function(handler) {
|
handlers && handlers.forEach(function(handler, i) {
|
||||||
handler.once && that.unbind(self, triggered, handler);
|
handler.once && handlers.splice(i, 1);
|
||||||
handler.call(element, data || {}, event);
|
handler.callback.call(element, data || {}, event);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
eventHandlers.forEach(function(handler) {
|
eventHandlers.forEach(function(callback) {
|
||||||
handler.call(element, data || {}, event, element);
|
callback.call(element, data || {}, event, element);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -136,19 +143,29 @@ Ox.Event = (function() {
|
||||||
|
|
||||||
/*@
|
/*@
|
||||||
.unbind <f> Removes an event handler
|
.unbind <f> Removes an event handler
|
||||||
|
() -> Ox.Event
|
||||||
|
Remove all global event handlers
|
||||||
(callback) -> <o> Ox.Event
|
(callback) -> <o> Ox.Event
|
||||||
|
Remove a global event handler
|
||||||
(self) -> <o> Ox.Event
|
(self) -> <o> Ox.Event
|
||||||
|
Remove all event handlers
|
||||||
(self, callback) -> <o> Ox.Event
|
(self, callback) -> <o> Ox.Event
|
||||||
|
Unbind a specific catch-all event handler
|
||||||
(self, event) -> <o> Ox.Event
|
(self, event) -> <o> Ox.Event
|
||||||
|
Unbind all event handlers for a single event
|
||||||
(self, event, callback) -> <o> Ox.Event
|
(self, event, callback) -> <o> Ox.Event
|
||||||
|
Unbind a specific event handler for a single event
|
||||||
(self, {event: callback, ...}) -> <o> Ox.Event
|
(self, {event: callback, ...}) -> <o> Ox.Event
|
||||||
|
Unbind specific event handlers for multiple events
|
||||||
self <o> The element's shared private object
|
self <o> The element's shared private object
|
||||||
callback <f> Callback function
|
callback <f> Callback function
|
||||||
event <s> Event name
|
event <s> Event name
|
||||||
*/
|
*/
|
||||||
that.unbind = function() {
|
that.unbind = function() {
|
||||||
var args = Ox.slice(arguments), self;
|
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) {
|
eventHandlers.forEach(function(handler) {
|
||||||
handler === args[0] && eventHandlers.splice(i, 1);
|
handler === args[0] && eventHandlers.splice(i, 1);
|
||||||
});
|
});
|
||||||
|
@ -159,12 +176,14 @@ Ox.Event = (function() {
|
||||||
if (Ox.isFunction(args[0])) {
|
if (Ox.isFunction(args[0])) {
|
||||||
args = {'*': args[0]};
|
args = {'*': args[0]};
|
||||||
}
|
}
|
||||||
Ox.forEach(Ox.makeObject(args), function(unbind, event) {
|
Ox.forEach(Ox.makeObject(args), function(callback, event) {
|
||||||
if (Ox.isUndefined(unbind)) {
|
if (Ox.isUndefined(callback)) {
|
||||||
delete self.eventHandlers[event];
|
delete self.eventHandlers[event];
|
||||||
} else {
|
} else {
|
||||||
self.eventHandlers[event].forEach(function(handler, i) {
|
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);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue