Start experiment: associate key bindings with CSS selectors

KeyEventHandler holds references to BindingSets. The name "binding set"
is based on the concept of a CSS ruleset. The idea is to choose a key
binding for an event based on what selectors (match / most closely
contain) the event's target DOM node.
This commit is contained in:
Nathan Sobo
2012-01-09 16:53:10 -08:00
committed by Corey Johnson & Nathan Sobo
parent 3a1d167a0f
commit 18e614e88d
4 changed files with 105 additions and 0 deletions

View File

@@ -0,0 +1,46 @@
$ = require 'jquery'
module.exports =
class BindingSet
namedKeys:
backspace: 8, tab: 9, clear: 12, enter: 13, 'return': 13,
esc: 27, escape: 27, space: 32, left: 37, up: 38, right: 39,
down: 40, del: 46, 'delete': 46, home: 36, end: 35, pageup: 33,
pagedown: 34, ',': 188, '.': 190, '/': 191, '`': 192, '-': 189,
'=': 187, ';': 186, '\'': 222, '[': 219, ']': 221, '\\': 220
selector: null
bindings: null
constructor: (@selector, @bindings) ->
commandForEvent: (event) ->
if $(event.target).is(@selector)
for pattern, command of @bindings
return command if @eventMatchesPattern(event, pattern)
null
eventMatchesPattern: (event, pattern) ->
pattern = @parseKeyPattern pattern
pattern.ctrlKey == event.ctrlKey and
pattern.altKey == event.altKey and
pattern.shiftKey == event.shiftKey and
pattern.metaKey == event.metaKey and
pattern.which == event.which
parseKeyPattern: (pattern) ->
[modifiers..., key] = pattern.split '+'
if @namedKeys[key]
charCode = @namedKeys[key]
key = null
else
charCode = key.toUpperCase().charCodeAt 0
ctrlKey: 'ctrl' in modifiers
altKey: 'alt' in modifiers
shiftKey: 'shift' in modifiers
metaKey: 'meta' in modifiers
which: charCode
key: key

View File

@@ -0,0 +1,19 @@
$ = require 'jquery'
BindingSet = require 'binding-set'
module.exports =
class KeyEventHandler
bindingSetsBySelector: null
constructor: ->
@bindingSets = []
bindKeys: (selector, bindings) ->
@bindingSets.push(new BindingSet(selector, bindings))
handleKeypress: (event) ->
for bindingSet in @bindingSets
if command = bindingSet.commandForEvent(event)
$(event.target).trigger(command)
return