mirror of
https://github.com/atom/atom.git
synced 2026-04-06 03:02:13 -04:00
Key binding selectors match events on descendants
This commit is contained in:
committed by
Corey Johnson & Nathan Sobo
parent
18e614e88d
commit
9c98e971fc
@@ -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()
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user