Key binding selectors match events on descendants

This commit is contained in:
Nathan Sobo
2012-01-09 17:18:53 -08:00
committed by Corey Johnson & Nathan Sobo
parent 18e614e88d
commit 9c98e971fc
2 changed files with 41 additions and 15 deletions

View File

@@ -7,23 +7,28 @@ describe "KeyEventHandler", ->
beforeEach ->
handler = new KeyEventHandler
fdescribe "handleKeypress", ->
describe "when there is a mapping in a selector that matches the event's element", ->
fragment = null
deleteCharHandler = null
insertCharHandler = null
describe "handleKeypress", ->
fragment = null
deleteCharHandler = null
insertCharHandler = null
beforeEach ->
handler.bindKeys '.command-mode', 'x': 'deleteChar'
handler.bindKeys '.insert-mode', 'x': 'insertChar'
beforeEach ->
handler.bindKeys '.command-mode', 'x': 'deleteChar'
handler.bindKeys '.insert-mode', 'x': 'insertChar'
fragment = $('<div class="command-mode">')
deleteCharHandler = jasmine.createSpy 'deleteCharHandler'
insertCharHandler = jasmine.createSpy 'insertCharHandler'
fragment.on 'deleteChar', deleteCharHandler
fragment.on 'insertChar', insertCharHandler
fragment = $ """
<div class="command-mode">
<div class="descendant-node"/>
</div>
"""
it "only triggers an event based on the key-binding in that selector", ->
deleteCharHandler = jasmine.createSpy 'deleteCharHandler'
insertCharHandler = jasmine.createSpy 'insertCharHandler'
fragment.on 'deleteChar', deleteCharHandler
fragment.on 'insertChar', insertCharHandler
describe "when the event's target node matches a selector with a matching binding", ->
it "triggers the command event associated with that binding on the target node", ->
handler.handleKeypress(keypressEvent('x', target: fragment[0]))
expect(deleteCharHandler).toHaveBeenCalled()
expect(insertCharHandler).not.toHaveBeenCalled()
@@ -35,3 +40,17 @@ describe "KeyEventHandler", ->
expect(deleteCharHandler).not.toHaveBeenCalled()
expect(insertCharHandler).toHaveBeenCalled()
describe "when the event's target node *descends* from a selector with a matching binding", ->
it "triggers the command event associated with that binding on the target node", ->
target = fragment.find('.descendant-node')[0]
handler.handleKeypress(keypressEvent('x', target: target))
expect(deleteCharHandler).toHaveBeenCalled()
expect(insertCharHandler).not.toHaveBeenCalled()
deleteCharHandler.reset()
fragment.removeClass('command-mode').addClass('insert-mode')
handler.handleKeypress(keypressEvent('x', target: target))
expect(deleteCharHandler).not.toHaveBeenCalled()
expect(insertCharHandler).toHaveBeenCalled()

View File

@@ -15,7 +15,7 @@ class BindingSet
constructor: (@selector, @bindings) ->
commandForEvent: (event) ->
if $(event.target).is(@selector)
if @selectorMatchesEvent(event)
for pattern, command of @bindings
return command if @eventMatchesPattern(event, pattern)
null
@@ -44,3 +44,10 @@ class BindingSet
which: charCode
key: key
selectorMatchesEvent: (event) ->
currentNode = event.target
while currentNode
return true if $(currentNode).is(@selector)
currentNode = currentNode.parentNode
false