Start on CommandRegistry

This commit is contained in:
Nathan Sobo
2014-09-05 08:22:28 -06:00
parent 04caea9bb0
commit decc983420
2 changed files with 51 additions and 0 deletions

View File

@@ -0,0 +1,21 @@
module.exports =
class CommandRegistry
constructor: (@rootNode) ->
@listenersByCommandName = {}
add: (commandName, selector, callback) ->
unless @listenersByCommandName[commandName]?
@rootNode.addEventListener(commandName, @dispatchCommand, true)
@listenersByCommandName[commandName] = []
@listenersByCommandName[commandName].push({selector, callback})
dispatchCommand: (event) =>
syntheticEvent = Object.create event,
eventPhase: value: Event.BUBBLING_PHASE
currentTarget: get: -> currentTarget
currentTarget = event.target
for listener in @listenersByCommandName[event.type]
if event.target.webkitMatchesSelector(listener.selector)
listener.callback.call(currentTarget, syntheticEvent)