'use strict'; /*@ Ox.Event Basic event handler @*/ // FIXME: unused Ox.Event = (function() { var that = {}; /*@ bind Binds a callback to an event (event, callback) -> The event handler ({event: callback, ...}) -> The event handler callback Callback function data <*> Event data event Event name Event names can be namespaced, like `'click.foo'` @*/ 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 ({event: callback, ...}) -> The event handler callback Callback function data <*> Event data event Event name Event names can be namespaced, like `'click.foo'` @*/ 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 Triggers an event (event) -> The event handler (event, data) -> The event handler ({event: data, ...}) -> The event handler event Event name data <*> Event data @*/ that.trigger = function() { Ox.forEach(Ox.makeObject(arguments), function(data, event) { Ox.Log('Core', 'Ox.Event.trigger', event, data) self.$eventHandler.trigger('ox_' + event, {value: data}); }); return that; }; /*@ unbind Unbinds all callbacks from an event To unbind a specific handler, use namespaced events, like `bind('click.foo', callback)`, and then `unbind('click.foo')`. () -> The event handler Unbinds all events (event) -> The event handler Unbinds one event (event, event, ...) -> The event handler Unbinds multiple events ([event, event, ...]) -> The event handler Unbinds multiple events event Event name @*/ that.unbind = function() { if (arguments.length == 0) { self.$eventHandler.off(); } else { Ox.toArray(arguments).forEach(function(event) { self.$eventHandler.off('ox_' + event); }); } return that; }; return that; }());