diff --git a/source/Ox.UI/js/Core/Event.js b/source/Ox.UI/js/Core/Event.js index 8cbac9f0..9170805b 100644 --- a/source/Ox.UI/js/Core/Event.js +++ b/source/Ox.UI/js/Core/Event.js @@ -8,9 +8,7 @@ Ox.Event Basic event handler Ox.Event = (function() { - var self = {}, that = {}; - - self.$eventHandler = $('
'); + var that = {}; /*@ bind Binds a callback to an event @@ -21,17 +19,84 @@ Ox.Event = (function() { event Event name Event names can be namespaced, like `'click.foo'` @*/ - that.bind = function() { - Ox.forEach(Ox.makeObject(arguments), function(callback, event) { - var foo = event; - self.$eventHandler.on('ox_' + event, function(event, data) { - Ox.Log('Core', 'CALLBACK', foo, data.value); - callback(data.value); + 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); + } + ); }); }); return that; }; + 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; + }; + /*@ bindOnce Binds a callback to an event, once (event, callback) -> The event handler