EventEmitter (v4.1.0) - RegExp DOM caster

Piping multiple DOM events through an emitter using the new RegExp functionality.

by Oliver Caldwell

HTML

<div id='foo'></div>
<div id='bar'></div>
<div id='baz'></div>

CSS

div {
    border: 3px solid #777777;
    width: 100px;
    height: 100px;
    margin: 20px;
}

div:hover {
    border-color: #222222;
    cursor: pointer;
}

#foo {
    background-color: red;
}

#bar {
    background-color: green;
}

#baz {
    background-color: blue;
}

.current {
    border-style: dotted;
}

JavaScript

function init() {
    // I'm createing a stand alone instance, you could also create a new class and extend EventEmitter.
    var ee = new EventEmitter();
    
    // This will house the previously clicked element.
    var previousElement;
    
    // I'm going to map the click events of multiple elements using regular expressions.
    // First you have to define the possible events though.
    // You could set up a function to prefix all of the IDs with "click-".
    ee.defineEvents(['click-foo', 'click-bar', 'click-baz']);
        
    // Now we map all click events through to the right event using this bit of magic. Man, I love this stuff.
    // Using MooTools for this DOM stuff so it works everywhere. Mad4Milk, yo.
    $$('body').addEvent('click', function(evt) {
        ee.emitEvent('click-' + evt.target.get('id'), [evt.target]);
    });
    
    // Now we can add a listener for all those events using a regular expression!
    ee.addListener(/click\-\w+/, function(target) {
        if (previousElement) {
            previousElement.removeClass('current');
        }
        
        target.addClass('current');
        previousElement = target;
    });
}

/*!
 * EventEmitter v4.1.0 - git.io/ee
 * Oliver Caldwell
 * MIT license
 * @preserve
 */
!function(r){"use strict";function t(){}function n(n,e){if(i)return e.indexOf(n);for(var t=e.length;t--;)if(e[t]===n)return t;return-1}var e=t.prototype,i=Array.prototype.indexOf?!0:!1;e._getEvents=function(){return this._events||(this._events={})},e.getListeners=function(n){var r,e,t=this._getEvents();if("object"==typeof n){r={};for(e in t)t.hasOwnProperty(e)&&n.test(e)&&(r[e]=t[e])}else r=t[n]||(t[n]=[]);return r},e.getListenersAsObject=function(n){var e,t=this.getListeners(n);return t instanceof Array&&(e={},e[n]=t),e||t},e.addListener=function(i,r){var e,t=this.getListenersAsObject(i);for(e in t)t.hasOwnProperty(e)&&-1===n(r,t[e])&&t[e].push(r);return this},e.on=e.addListener,e.defineEvent=function(e){return...