shell-ahk
Version:
`Lodash`/`jQuery` for `AHK`.
123 lines (84 loc) • 4.25 kB
text/coffeescript
# -check
import keyBindingShell_filter from './filter'
import keyBindingShell_forEach from './forEach'
import keyBindingShell_formatHotkey from './formatHotkey'
import keyBindingShell_isFunction from './isFunction'
import keyBindingShell_noop from './noop'
import keyBindingShell_replace from './replace'
import keyBindingShell_split from './split'
class KeyBindingShell
constructor: ->
###* import('./keyBindingShell').KeyBindingShell['mapBound'] ###
= {}
###* import('./keyBindingShell').KeyBindingShell['mapCallback'] ###
= {}
###* import('./keyBindingShell').KeyBindingShell['mapPrevented'] ###
= {}
###* import('./keyBindingShell').KeyBindingShell['add'] ###
add: (keyMixed, callback) ->
unless keyBindingShell_isFunction callback
throw new Error "KeyBindingShell.add: Callback is not a function for key: #{keyMixed}"
[keyBindingShell_key, keyBindingShell_name] = keyBindingShell_split (keyBindingShell_replace keyMixed, ':down', ''), '.'
unless keyBindingShell_name then keyBindingShell_name = ''
keyBindingShell_key
# Item: [Name, Function]
[keyBindingShell_key].Push [keyBindingShell_name, callback]
return
###* import('./keyBindingShell').KeyBindingShell['fire'] ###
fire: (keyMixed) ->
keyBindingShell_forEach ( keyMixed), (it) ->
unless keyBindingShell_isFunction it[1]
throw new Error "KeyBindingShell.fire: Callback is not a function for key: #{keyMixed}"
it[1]()
return
return
###* import('./keyBindingShell').KeyBindingShell['formatKey'] ###
formatKey: (key, prefix = '') ->
keyBindingShell_key = keyBindingShell_formatHotkey keyBindingShell_replace key, ':down', ''
unless prefix then return keyBindingShell_key
return "#{prefix}#{keyBindingShell_key}"
###* import('./keyBindingShell').KeyBindingShell['getListItem'] ###
getListItem: (keyMixed) ->
[keyBindingShell_key, keyBindingShell_name] = keyBindingShell_split (keyBindingShell_replace keyMixed, ':down', ''), '.'
keyBindingShell_list = [keyBindingShell_key]
unless keyBindingShell_list then return []
if keyBindingShell_name then keyBindingShell_list = keyBindingShell_filter keyBindingShell_list, (it) -> it[0] == keyBindingShell_name
return keyBindingShell_list
###* import('./keyBindingShell').KeyBindingShell['isPrevented'] ###
isPrevented: (key) -> [key] == true
###* import('./keyBindingShell').KeyBindingShell['prepare'] ###
prepare: (key) ->
if [key] then return
[key] = => key
[key] = []
[key] = false
return
###* import('./keyBindingShell').KeyBindingShell['prevent'] ###
prevent: (key, isPrevented) ->
key
[key] = isPrevented
keyBindingShell_callback = [key]
keyBindingShell_key = key, '~'
if isPrevented then keyBindingShell_key = key
keyBindingShell_noop keyBindingShell_callback, keyBindingShell_key
Native 'Hotkey, % keyBindingShell_key, % keyBindingShell_callback, On'
return
###* import('./keyBindingShell').KeyBindingShell['register'] ###
register: (key) ->
key
keyBindingShell_callback = [key]
keyBindingShell_key = key, '~'
if [key] then keyBindingShell_key = key
keyBindingShell_noop keyBindingShell_callback, keyBindingShell_key
Native 'Hotkey, % keyBindingShell_key, % keyBindingShell_callback, On'
return
###* import('./keyBindingShell').KeyBindingShell['remove'] ###
remove: (keyMixed) ->
[keyBindingShell_key, keyBindingShell_name] = keyBindingShell_split (keyBindingShell_replace keyMixed, ':down', ''), '.'
unless keyBindingShell_name
[keyBindingShell_key] = []
return
keyBindingShell_listNew = keyBindingShell_filter [keyBindingShell_key], (it) -> it[0] != keyBindingShell_name
[keyBindingShell_key] = keyBindingShell_listNew
return
keyBindingShell_noop KeyBindingShell