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

162 lines
4.8 KiB
JavaScript
Raw Normal View History

2011-11-05 16:46:53 +00:00
'use strict';
2011-09-23 10:43:57 +00:00
/*@
Ox.Event <o> Basic event handler
@*/
// FIXME: unused
2011-09-23 10:43:57 +00:00
Ox.Event = (function() {
2012-06-29 12:24:06 +00:00
var that = {};
2011-09-23 10:43:57 +00:00
/*@
bind <f> Binds a callback to an event
(event, callback) -> <o> The event handler
({event: callback, ...}) -> <o> The event handler
callback <f> Callback function
data <*> Event data
event <s> Event name
Event names can be namespaced, like `'click.foo'`
2011-09-23 10:43:57 +00:00
@*/
2012-06-29 12:24:06 +00:00
that.bind = function(self) {
var args = Ox.slice(arguments, 1), once;
if (Ox.isBoolean(Ox.last(args))) {
once = args.pop();
}
if (Ox.isFunction(args[0])) {
args = {'': args[0]};
}
Ox.forEach(Ox.makeObject(args), function(callback, event) {
if (once) {
callback.once = true;
}
self.eventHandlers = self.eventHandlers || {};
self.eventHandlers[event] = (
self.eventHandlers[event] || []
).concat(callback);
});
return that;
};
that.bindOnce = function() {
return that.bind.apply(null, Ox.slice(arguments).concat(true));
};
/*
*/
that.trigger = function(self) {
if (!self.eventHandlers) {
return;
}
Ox.forEach(Ox.makeObject(
Ox.slice(arguments, 1, -1)
), function(data, event) {
triggered = event.split('.');
triggered.map(function(v, i) {
return triggered.slice(0, i + 1).join('.');
}).forEach(function(triggered) {
Ox.forEach(
Ox.extend(
{'': self.eventHandlers[''] || []},
triggered, self.eventHandlers[triggered] || []
),
function(handlers, handled) {
handler.once && that.unbind(self, triggered, handler);
handler(data || {}, event, Ox.last(arguments).oxid);
}
);
2011-09-23 10:43:57 +00:00
});
});
return that;
};
2012-06-29 12:24:06 +00:00
that.unbind = function(self) {
if (!self.eventHandlers) {
return;
}
var args = Ox.slice(arguments, 1);
if (args.length == 0) {
delete self.eventHandlers;
} else {
if (Ox.isFunction(args[0])) {
args = {'': args[0]};
}
Ox.forEach(Ox.makeObject(args), function(unbound, event) {
if (!unbound) {
delete self.eventHandlers[event];
} else {
self.eventHandlers[event].forEach(function(bound, i) {
if (bound == unbound) {
self.eventHandlers[event].splice(i, 1);
}
});
}
});
}
return that;
};
2011-09-23 10:43:57 +00:00
/*@
bindOnce <f> Binds a callback to an event, once
(event, callback) -> <o> The event handler
({event: callback, ...}) -> <o> The event handler
callback <f> Callback function
data <*> Event data
event <s> Event name
Event names can be namespaced, like `'click.foo'`
2011-09-23 10:43:57 +00:00
@*/
that.bindOnce = function() {
Ox.forEach(Ox.makeObject(arguments), function(callback, event) {
self.$eventHandler.one('ox_' + event, function(event, data) {
callback(data.value);
});
});
return that;
};
/*@
trigger <function> Triggers an event
(event) -> <o> The event handler
(event, data) -> <o> The event handler
({event: data, ...}) -> <o> The event handler
event <s> Event name
data <*> Event data
@*/
that.trigger = function() {
Ox.forEach(Ox.makeObject(arguments), function(data, event) {
2011-11-04 15:54:28 +00:00
Ox.Log('Core', 'Ox.Event.trigger', event, data)
2011-09-23 10:43:57 +00:00
self.$eventHandler.trigger('ox_' + event, {value: data});
});
return that;
};
/*@
unbind <f> Unbinds all callbacks from an event
To unbind a specific handler, use namespaced events, like
`bind('click.foo', callback)`, and then `unbind('click.foo')`.
2011-09-23 10:43:57 +00:00
() -> <o> The event handler
Unbinds all events
(event) -> <o> The event handler
Unbinds one event
(event, event, ...) -> <o> The event handler
Unbinds multiple events
([event, event, ...]) -> <o> The event handler
Unbinds multiple events
event <s> Event name
@*/
that.unbind = function() {
if (arguments.length == 0) {
2012-05-28 14:06:22 +00:00
self.$eventHandler.off();
2011-09-23 10:43:57 +00:00
} else {
Ox.toArray(arguments).forEach(function(event) {
2012-05-28 14:06:22 +00:00
self.$eventHandler.off('ox_' + event);
2011-09-23 10:43:57 +00:00
});
}
return that;
};
return that;
}());