Key binding event handlers can call abortKeyBinding on the event object to abort and try the next binding

This commit is contained in:
Nathan Sobo 2012-06-20 22:08:16 -06:00
parent c276a4029e
commit e1309f7c66
2 changed files with 25 additions and 2 deletions

View File

@ -72,8 +72,10 @@ describe "Keymap", ->
expect(insertCharHandler).toHaveBeenCalled()
describe "when the event's target node descends from multiple nodes that match selectors with a binding", ->
it "only triggers bindings on selectors associated with the closest ancestor node", ->
beforeEach ->
keymap.bindKeys '.child-node', 'x': 'foo'
it "only triggers bindings on selectors associated with the closest ancestor node", ->
fooHandler = jasmine.createSpy 'fooHandler'
fragment.on 'foo', fooHandler
@ -83,6 +85,22 @@ describe "Keymap", ->
expect(deleteCharHandler).not.toHaveBeenCalled()
expect(insertCharHandler).not.toHaveBeenCalled()
describe "when 'abortKeyBinding' is called on the triggered event", ->
it "aborts the current event and tries again with the next-most-specific key binding", ->
fooHandler1 = jasmine.createSpy('fooHandler1').andCallFake (e) ->
expect(deleteCharHandler).not.toHaveBeenCalled()
e.abortKeyBinding()
fooHandler2 = jasmine.createSpy('fooHandler2')
fragment.find('.child-node').on 'foo', fooHandler1
fragment.on 'foo', fooHandler2
target = fragment.find('.grandchild-node')[0]
keymap.handleKeyEvent(keydownEvent('x', target: target))
expect(fooHandler1).toHaveBeenCalled()
expect(fooHandler2).not.toHaveBeenCalled()
expect(deleteCharHandler).toHaveBeenCalled()
describe "when the event bubbles to a node that matches multiple selectors", ->
describe "when the matching selectors differ in specificity", ->
it "triggers the binding for the most specific selector", ->

View File

@ -51,7 +51,7 @@ class Keymap
for bindingSet in candidateBindingSets
command = bindingSet.commandForEvent(event)
if command
@triggerCommandEvent(event, command)
continue if @triggerCommandEvent(event, command)
return false
else if command == false
return false
@ -66,7 +66,12 @@ class Keymap
triggerCommandEvent: (keyEvent, commandName) ->
commandEvent = $.Event(commandName)
commandEvent.keyEvent = keyEvent
aborted = false
commandEvent.abortKeyBinding = ->
@stopImmediatePropagation()
aborted = true
$(keyEvent.target).trigger(commandEvent)
aborted
multiKeystrokeStringForEvent: (event) ->
currentKeystroke = @keystrokeStringForEvent(event)