* Add event handlers for the spatial navigation behavior. * This function defines which input methods trigger the spatial navigation behavior. * @function spatialNavigationHandler
()
| 81 | * @function spatialNavigationHandler |
| 82 | */ |
| 83 | function spatialNavigationHandler() { |
| 84 | /* |
| 85 | * keydown EventListener : |
| 86 | * If arrow key pressed, get the next focusing element and send it to focusing controller |
| 87 | */ |
| 88 | window.addEventListener('keydown', (e) => { |
| 89 | const currentKeyMode = (parent && parent.__spatialNavigation__.keyMode) || window.__spatialNavigation__.keyMode; |
| 90 | const eventTarget = document.activeElement; |
| 91 | const dir = ARROW_KEY_CODE[e.keyCode]; |
| 92 | |
| 93 | if (e.keyCode === TAB_KEY_CODE) { |
| 94 | startingPoint = null; |
| 95 | } |
| 96 | |
| 97 | if (!currentKeyMode || |
| 98 | (currentKeyMode === 'NONE') || |
| 99 | ((currentKeyMode === 'SHIFTARROW') && !e.shiftKey) || |
| 100 | ((currentKeyMode === 'ARROW') && e.shiftKey)) |
| 101 | return; |
| 102 | |
| 103 | if (!e.defaultPrevented) { |
| 104 | let focusNavigableArrowKey = {left: true, up: true, right: true, down: true}; |
| 105 | |
| 106 | // Edge case (text input, area) : Don't move focus, just navigate cursor in text area |
| 107 | if ((eventTarget.nodeName === 'INPUT') || eventTarget.nodeName === 'TEXTAREA') { |
| 108 | focusNavigableArrowKey = handlingEditableElement(e); |
| 109 | } |
| 110 | |
| 111 | if (focusNavigableArrowKey[dir]) { |
| 112 | e.preventDefault(); |
| 113 | mapOfBoundRect = new Map(); |
| 114 | |
| 115 | navigate(dir); |
| 116 | |
| 117 | mapOfBoundRect = null; |
| 118 | startingPoint = null; |
| 119 | } |
| 120 | } |
| 121 | }); |
| 122 | |
| 123 | /* |
| 124 | * mouseup EventListener : |
| 125 | * If the mouse click a point in the page, the point will be the starting point. |
| 126 | * NOTE: Let UA set the spatial navigation starting point based on click |
| 127 | */ |
| 128 | document.addEventListener('mouseup', (e) => { |
| 129 | startingPoint = {x: e.clientX, y: e.clientY}; |
| 130 | }); |
| 131 | |
| 132 | /* |
| 133 | * focusin EventListener : |
| 134 | * When the element get the focus, save it and its DOMRect for resetting the search origin |
| 135 | * if it disappears. |
| 136 | */ |
| 137 | window.addEventListener('focusin', (e) => { |
| 138 | if (e.target !== window) { |
| 139 | savedSearchOrigin.element = e.target; |
| 140 | savedSearchOrigin.rect = e.target.getBoundingClientRect(); |
no test coverage detected