Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class @TabHistory extends EventDispatcher
- @TAB = 'tab'
- @REVERSE_TAB = 'reverseTab'
- @TAB_ENTER = 'tabEnter'
- @REVERSE_TAB_ENTER = 'reverseTabEnter'
- constructor: ( @target, @deep=true ) ->
- super
- @previousActiveElement = document.activeElement
- window.addEventListener 'focusin', @detectTab
- window.addEventListener 'mousedown', @trackMouseButton
- window.addEventListener 'mouseup', @trackMouseButton
- detectTab: ( event ) =>
- previous = @previousActiveElement
- current = event.target
- @previousActiveElement = current
- return if @mouseDown
- return if current == previous # no change in focus
- hasFocus = current == @target
- hasFocus ||= DOMUtil.isDescendant current, @target if @deep
- return unless hasFocus
- focusables = document.querySelectorAll FocusUtil.FOCUSABLE
- focusables = Array.prototype.slice.call focusables # convert from NodeList to Array so we can use filter() & indexOf()
- focusables = focusables.filter ( element ) -> element.getAttribute( 'tabindex' ) != '-1' # remove tabindex=-1 elements
- previousIndex = focusables.indexOf previous
- currentIndex = focusables.indexOf current
- hadFocus = previous == @target
- hadFocus ||= DOMUtil.isDescendant previous, @target
- if currentIndex - previousIndex == 1
- @dispatchEvent new Event TabHistory.TAB_ENTER if !hadFocus
- @dispatchEvent new Event TabHistory.TAB if @deep
- else if currentIndex - previousIndex == -1
- @dispatchEvent new Event TabHistory.REVERSE_TAB_ENTER if !hadFocus
- @dispatchEvent new Event TabHistory.REVERSE_TAB if @deep
- trackMouseButton: ( event ) =>
- @mouseDown = event.type == 'mousedown'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement