Add Keymap.bindingsForElement method

This commit is contained in:
Corey Johnson
2012-05-29 14:15:54 -07:00
parent 7c105a0446
commit 64bd26a392
3 changed files with 40 additions and 17 deletions

View File

@@ -8,6 +8,7 @@ PEG = require 'pegjs'
module.exports =
class BindingSet
selector: null
keystrokeMap: null
commandForEvent: null
keystrokePatternParser: null
@@ -15,6 +16,7 @@ class BindingSet
@parser = PEG.buildParser(fs.read(require.resolve 'keystroke-pattern.pegjs'))
@specificity = Specificity(@selector)
@commandForEvent = @buildEventHandler(mapOrFunction)
@keystrokeMap = if not _.isFunction(mapOrFunction) then mapOrFunction else {}
buildEventHandler: (mapOrFunction) ->
if _.isFunction(mapOrFunction)

View File

@@ -6,7 +6,7 @@ $ = require 'jquery'
module.exports =
class Keymap
bindingSetsBySelector: null
bindingSets: null
constructor: ->
@bindingSets = []
@@ -26,10 +26,22 @@ class Keymap
bindKeys: (selector, bindings) ->
@bindingSets.unshift(new BindingSet(selector, bindings))
bindKey: (selector, pattern, eventName) ->
bindings = {}
bindings[pattern] = eventName
@bindKeys(selector, bindings)
bindingsForElement: (element) ->
currentNode = $(element)
keystrokeMap = {}
while currentNode.length
bindingSets = @bindingSets.filter (set) -> currentNode.is(set.selector)
console.log @bindingSets, currentNode
bindingSets.sort (a, b) -> b.specificity - a.specificity
for bindingSet in bindingSets
for keystroke, command of bindingSet.keystrokeMap
keystrokeMap[keystroke] ?= command
currentNode = currentNode.parent()
keystrokeMap
handleKeyEvent: (event) ->
event.keystroke = @keystrokeStringForEvent(event)