Advertisement
Guest User

Untitled

a guest
Aug 2nd, 2015
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1.  
  2. class @TabHistory extends EventDispatcher
  3.  
  4. @TAB = 'tab'
  5. @REVERSE_TAB = 'reverseTab'
  6. @TAB_ENTER = 'tabEnter'
  7. @REVERSE_TAB_ENTER = 'reverseTabEnter'
  8.  
  9.  
  10. constructor: ( @target, @deep=true ) ->
  11. super
  12.  
  13. @previousActiveElement = document.activeElement
  14.  
  15. window.addEventListener 'focusin', @detectTab
  16. window.addEventListener 'mousedown', @trackMouseButton
  17. window.addEventListener 'mouseup', @trackMouseButton
  18.  
  19.  
  20. detectTab: ( event ) =>
  21. previous = @previousActiveElement
  22. current = event.target
  23.  
  24. @previousActiveElement = current
  25.  
  26. return if @mouseDown
  27. return if current == previous # no change in focus
  28.  
  29. hasFocus = current == @target
  30. hasFocus ||= DOMUtil.isDescendant current, @target if @deep
  31.  
  32. return unless hasFocus
  33.  
  34. focusables = document.querySelectorAll FocusUtil.FOCUSABLE
  35. focusables = Array.prototype.slice.call focusables # convert from NodeList to Array so we can use filter() & indexOf()
  36. focusables = focusables.filter ( element ) -> element.getAttribute( 'tabindex' ) != '-1' # remove tabindex=-1 elements
  37.  
  38. previousIndex = focusables.indexOf previous
  39. currentIndex = focusables.indexOf current
  40.  
  41. hadFocus = previous == @target
  42. hadFocus ||= DOMUtil.isDescendant previous, @target
  43.  
  44. if currentIndex - previousIndex == 1
  45. @dispatchEvent new Event TabHistory.TAB_ENTER if !hadFocus
  46. @dispatchEvent new Event TabHistory.TAB if @deep
  47.  
  48. else if currentIndex - previousIndex == -1
  49. @dispatchEvent new Event TabHistory.REVERSE_TAB_ENTER if !hadFocus
  50. @dispatchEvent new Event TabHistory.REVERSE_TAB if @deep
  51.  
  52.  
  53. trackMouseButton: ( event ) =>
  54. @mouseDown = event.type == 'mousedown'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement