src/handler.js
function setListeners(doc, cfg = {}, add = true) {
    if (!doc || !(doc instanceof Document)) {
        return;
    }

    let listenerFn = doc.addEventListener;
    if (!add) {
        listenerFn = doc.removeEventListener;
    }
    if (_.isObject(cfg.events)) {
        let events = cfg.events;
        
        _.each(events, (fns, e) => {
            let [ev, selector] = e.split(' ');
            let elements = null;
            if (!_.isArray(fns)) { // support list of event handlers
                fns = [fns];
            }
            if (selector) {
                selector = e.substring(e.indexOf(' ') + 1); // everything after space
                elements = _.attempt(() => doc.querySelectorAll(selector)); // catch any errors while document selection
            } else {
                elements = [doc];
            }
            elements = _.isError(elements) ? [] : elements;
            _.each(fns, (fn) => {
                fn = _.isString(fn) ? cfg[fn] : fn; // assume the function to be present on the page configuration obeject
                if (_.isFunction(fn)) {
                    console.log((add ? 'adding' : 'removing') + ' event on documents...', ev, elements);
                    _.each(elements, (el) => listenerFn.call(el, ev, (e) => fn.call(cfg, e))); // bind to the original configuration object
                }
            });
        })
    }
}
function setListeners(doc, cfg = {}, add = true) {
    if (!doc || !(doc instanceof Document)) {
        return;
    }

    let listenerFn = doc.addEventListener;
    if (!add) {
        listenerFn = doc.removeEventListener;
    }
    if (_.isObject(cfg.events)) {
        let events = cfg.events;
        
        _.each(events, (fns, e) => {
            let [ev, selector] = e.split(' ');
            let elements = null;
            if (!_.isArray(fns)) { // support list of event handlers
                fns = [fns];
            }
            if (selector) {
                selector = e.substring(e.indexOf(' ') + 1); // everything after space
                elements = _.attempt(() => doc.querySelectorAll(selector)); // catch any errors while document selection
            } else {
                elements = [doc];
            }
            elements = _.isError(elements) ? [] : elements;
            _.each(fns, (fn) => {
                fn = _.isString(fn) ? cfg[fn] : fn; // assume the function to be present on the page configuration obeject
                if (_.isFunction(fn)) {
                    console.log((add ? 'adding' : 'removing') + ' event on documents...', ev, elements);
                    _.each(elements, (el) => listenerFn.call(el, ev, (e) => fn.call(cfg, e))); // bind to the original configuration object
                }
            });
        })
    }
}
function setListeners ( doc , cfg = { } , add = true ) { if ( ! doc || ! ( doc instanceof Document ) ) { return ; } let listenerFn = doc . addEventListener ; if ( ! add ) { listenerFn = doc . removeEventListener ; } if ( _ . isObject ( cfg . events ) ) { let events = cfg . events ; _ . each ( events , ( fns , e ) => { let [ ev , selector ] = e . split ( ' ' ) ; let elements = null ; if ( ! _ . isArray ( fns ) ) { // support list of event handlers fns = [ fns ] ; } if ( selector ) { selector = e . substring ( e . indexOf ( ' ' ) + 1 ) ; // everything after space elements = _ . attempt ( ( ) => doc . querySelectorAll ( selector ) ) ; // catch any errors while document selection } else { elements = [ doc ] ; } elements = _ . isError ( elements ) ? [ ] : elements ; _ . each ( fns , ( fn ) => { fn = _ . isString ( fn ) ? cfg [ fn ] : fn ; // assume the function to be present on the page configuration obeject if ( _ . isFunction ( fn ) ) { console . log ( ( add ? 'adding' : 'removing' ) + ' event on documents...' , ev , elements ) ; _ . each ( elements , ( el ) => listenerFn . call ( el , ev , ( e ) => fn . call ( cfg , e ) ) ) ; // bind to the original configuration object } } ) ; } ) } }
Iterates over the events configuration and add event listeners to the document.

@example
{
events: {
'scroll': function(e) { // do the magic here },
'select listItemLockup title': 'onTitleSelect',
'someOtherEvent': ['onTitleSelect', function(e) { // some other magic }, ...]
},
onTitleSelect: function(e) {
// do the magic here
}
}

@todo Implement querySelectorAll polyfill (it doesn't seem to exist on the xml document)

@private

@param {Document} doc            The document to add the listeners on.
@param {Object} cfg              The page object configuration.
@param {Boolean} [add=true]      Whether to add or remove listeners. Defaults to true (add)
Iterates over the events configuration and add event listeners to the document .