Created
April 17, 2015 18:03
-
-
Save johntron/cc61a9887db9bc4f5f9b to your computer and use it in GitHub Desktop.
Get CanJS Control from DOM elements
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function handlers($el) { | |
$el = $($el); | |
var events = $._data(document.body).events; | |
var matched_handlers = []; | |
Object.keys(events).forEach(function(event_type) { | |
var handlers = events[event_type].filter(function (handler) { | |
if (!handler.selector) { return false; } | |
return $el.is(handler.selector) || $el.has(handler.selector).length; | |
}); | |
matched_handlers = matched_handlers.concat(handlers) | |
}); | |
return matched_handlers; | |
} | |
function controls_for_handler(handler) { | |
return $(document.body).controls().filter(function (control) { | |
var actions = control.constructor.actions; | |
var action = handler.selector + ' ' + handler.origType; | |
return actions[action]; | |
}); | |
} | |
var obs = new MutationObserver(function(mutations) { | |
mutations.forEach(function (mutation) { | |
[].forEach.call(mutation.addedNodes, function (node) { | |
var h = handlers(node); | |
if (!h.length) { | |
return; | |
} | |
var controls = []; | |
h.forEach(function (handler) { | |
controls = controls.concat(controls_for_handler(handler)); | |
}); | |
controls = controls.reduce(function (prev, current, i, arr) { | |
console.log(current, prev.indexOf(current), prev); | |
if (prev.indexOf(current) === -1) { | |
prev.push(current); | |
} | |
return prev; | |
}, []); | |
console.log('node has handler(s), control(s)', node, h, controls); | |
}); | |
}); | |
}); | |
obs.observe(document.body, {childList: true}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment