mirror of
https://github.com/atom/atom.git
synced 2026-02-08 21:55:05 -05:00
Only trigger bindings on the closest ancestor node of an event target.
Say we have a structure like:
div.parent
div.child
div.grandchild
And we have two mappings:
.parent { x: foo }
.child { x:bar }
If there's an event originating on grandchild, it will *only* trigger
bar, because that selector selects a closer ancestor.
This commit is contained in:
committed by
Corey Johnson & Nathan Sobo
parent
9c98e971fc
commit
f5be55e000
@@ -15,9 +15,8 @@ class BindingSet
|
||||
constructor: (@selector, @bindings) ->
|
||||
|
||||
commandForEvent: (event) ->
|
||||
if @selectorMatchesEvent(event)
|
||||
for pattern, command of @bindings
|
||||
return command if @eventMatchesPattern(event, pattern)
|
||||
for pattern, command of @bindings
|
||||
return command if @eventMatchesPattern(event, pattern)
|
||||
null
|
||||
|
||||
eventMatchesPattern: (event, pattern) ->
|
||||
@@ -44,10 +43,3 @@ class BindingSet
|
||||
which: charCode
|
||||
key: key
|
||||
|
||||
selectorMatchesEvent: (event) ->
|
||||
currentNode = event.target
|
||||
while currentNode
|
||||
return true if $(currentNode).is(@selector)
|
||||
currentNode = currentNode.parentNode
|
||||
false
|
||||
|
||||
|
||||
@@ -12,8 +12,12 @@ class KeyEventHandler
|
||||
@bindingSets.push(new BindingSet(selector, bindings))
|
||||
|
||||
handleKeypress: (event) ->
|
||||
for bindingSet in @bindingSets
|
||||
if command = bindingSet.commandForEvent(event)
|
||||
$(event.target).trigger(command)
|
||||
return
|
||||
currentNode = event.target
|
||||
while currentNode
|
||||
for bindingSet in @bindingSets
|
||||
if $(currentNode).is(bindingSet.selector)
|
||||
if command = bindingSet.commandForEvent(event)
|
||||
$(event.target).trigger(command)
|
||||
return
|
||||
currentNode = currentNode.parentNode
|
||||
|
||||
|
||||
Reference in New Issue
Block a user