Add core:focus-next/focus-previous

Focus now cycles between elements with a positive tabindex on
tab and shift-tab.
This commit is contained in:
Kevin Sawicki & Nathan Sobo
2013-05-16 15:00:55 -07:00
parent 76e7161608
commit 001183245e
3 changed files with 134 additions and 2 deletions

View File

@@ -67,10 +67,10 @@
# allow standard input fields to work correctly
'input:not(.hidden-input)':
'tab': 'core:focus-next'
'shift-tab': 'core:focus-previous'
'left': 'native!'
'right': 'native!'
'tab': 'native!'
'shift-tab': 'native!'
'shift-left': 'native!'
'shift-right': 'native!'
'backspace': 'native!'
@@ -81,3 +81,7 @@
'meta-x': 'native!'
'meta-c': 'native!'
'meta-v': 'native!'
'button':
'tab': 'core:focus-next'
'shift-tab': 'core:focus-previous'

View File

@@ -15,6 +15,9 @@ class WindowEventHandler
window.close()
@subscribeToCommand $(window), 'window:reload', => reload()
@subscribeToCommand $(document), 'core:focus-next', @focusNext
@subscribeToCommand $(document), 'core:focus-previous', @focusPrevious
@subscribe $(document), 'keydown', keymap.handleKeyEvent
@subscribe $(document), 'drop', onDrop
@@ -31,4 +34,50 @@ class WindowEventHandler
require('child_process').spawn('open', [location])
false
eachTabIndexedElement: (callback) ->
for element in $('[tabindex]')
element = $(element)
continue if element.attr('disabled')
tabIndex = parseInt(element.attr('tabindex'))
continue unless tabIndex >= 0
callback(element, tabIndex)
focusNext: =>
focusedTabIndex = parseInt($(':focus').attr('tabindex')) or -Infinity
nextElement = null
nextTabIndex = Infinity
lowestElement = null
lowestTabIndex = Infinity
@eachTabIndexedElement (element, tabIndex) ->
if tabIndex < lowestTabIndex
lowestTabIndex = tabIndex
lowestElement = element
if focusedTabIndex < tabIndex < nextTabIndex
nextTabIndex = tabIndex
nextElement = element
(nextElement ? lowestElement).focus()
focusPrevious: =>
focusedTabIndex = parseInt($(':focus').attr('tabindex')) or Infinity
previousElement = null
previousTabIndex = -Infinity
highestElement = null
highestTabIndex = -Infinity
@eachTabIndexedElement (element, tabIndex) ->
if tabIndex > highestTabIndex
highestTabIndex = tabIndex
highestElement = element
if focusedTabIndex > tabIndex > previousTabIndex
previousTabIndex = tabIndex
previousElement = element
(previousElement ? highestElement).focus()
_.extend WindowEventHandler.prototype, Subscriber