{"size": 1500, "ext": "js", "max_stars_repo_path": "client/interaction-event-syncronizer.js", "max_stars_repo_name": "thomaspeklak/orchestrator", "max_stars_repo_stars_event_min_datetime": "2015-04-29T19:06:12.000Z", "max_stars_repo_stars_event_max_datetime": "2015-04-29T19:06:12.000Z", "max_issues_repo_path": "client/interaction-event-syncronizer.js", "max_issues_repo_name": "thomaspeklak/orchestrator", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "client/interaction-event-syncronizer.js", "max_forks_repo_name": "thomaspeklak/orchestrator", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 31.914893617, "max_line_length": 129, "alphanum_fraction": 0.618}
"use strict";

var getQuerySelector = require("get-query-selector");
require("../lib/custom-event")(window);

function handleInteractionWrapper(state) {
    return function handleInteraction(e) {
        if (e.detail.synchronizationEvent) return;
        var selector = getQuerySelector(e.target);
        state.set("interaction", {
            type: e.type,
            selector: selector
        });
    };
}

function InteractionEventSynchronizer(state) {
    ["click", "mouseover", "mouseout", "mousedown", "mouseup", "mouseenter", "mouseleave", "keypress"].forEach(function (event) {
        document.body.addEventListener(event, handleInteractionWrapper(state), true);
    });
}

InteractionEventSynchronizer.prototype.register = function registerInteractionEventSynchronizer(apply) {
    return apply("interaction", function (event) {
        if (!event) return;

        var customEvent = new CustomEvent(event.type, {
            detail: {
                synchronizationEvent: true
            }
        });
        var node = document.querySelector(event.selector);

        if (node.dispatchEvent) {
            node.dispatchEvent(customEvent);
        } else if (node.fireEvent) { // IE < 9
            node.fireEvent("on" + customEvent.eventType, customEvent);
        } else if (node[event.type]) {
            node[event.type]();
        } else if (node["on" + event.type]) {
            node["on" + event.type]();
        }
    });
};

module.exports = InteractionEventSynchronizer;
