mirror of
https://github.com/atom/atom.git
synced 2026-02-10 22:55:09 -05:00
40 lines
1.1 KiB
CoffeeScript
40 lines
1.1 KiB
CoffeeScript
$ = require 'jquery'
|
|
_ = require 'underscore'
|
|
Specificity = require 'specificity'
|
|
|
|
module.exports =
|
|
class BindingSet
|
|
selector: null
|
|
commandForEvent: null
|
|
|
|
constructor: (@selector, mapOrFunction) ->
|
|
@specificity = Specificity(@selector)
|
|
@commandForEvent = @buildEventHandler(mapOrFunction)
|
|
|
|
buildEventHandler: (mapOrFunction) ->
|
|
if _.isFunction(mapOrFunction)
|
|
mapOrFunction
|
|
else
|
|
mapOrFunction = @normalizeKeystrokePatterns(mapOrFunction)
|
|
(event) =>
|
|
for pattern, command of mapOrFunction
|
|
return command if @eventMatchesPattern(event, pattern)
|
|
null
|
|
|
|
eventMatchesPattern: (event, pattern) ->
|
|
pattern = pattern.replace(/^<|>$/g, '')
|
|
event.keystroke == pattern
|
|
|
|
normalizeKeystrokePatterns: (map) ->
|
|
normalizedMap = {}
|
|
for pattern, event of map
|
|
normalizedMap[@normalizeKeystrokePattern(pattern)] = event
|
|
normalizedMap
|
|
|
|
normalizeKeystrokePattern: (pattern) ->
|
|
keys = pattern.split('-')
|
|
modifiers = keys[0...-1]
|
|
modifiers.sort()
|
|
[modifiers..., _.last(keys)].join('-')
|
|
|