import { correctNodes } from './utils/correctFocus' ; import { pickFocusable } from './utils/firstFocus' ; import { isGuard } from './utils/is' ; export const NEW_FOCUS = 'NEW_FOCUS' ; param innerNodes param outerNodes param activeElement param lastNode returns { number | string | undefined | * } export const newFocus = ( innerNodes , outerNodes , activeElement , lastNode ) : number | undefined | typeof NEW_FOCUS => { const cnt = innerNodes . length ; const firstFocus = innerNodes [ 0 ] ; const lastFocus = innerNodes [ cnt - 1 ] ; const isOnGuard = isGuard ( activeElement ) ; if ( innerNodes . indexOf ( activeElement ) >= 0 ) { return undefined ; } const activeIndex = outerNodes . indexOf ( activeElement ) ; const lastIndex = lastNode ? outerNodes . indexOf ( lastNode ) : activeIndex ; const lastNodeInside = lastNode ? innerNodes . indexOf ( lastNode ) : - 1 ; const indexDiff = activeIndex - lastIndex ; const firstNodeIndex = outerNodes . indexOf ( firstFocus ) ; const lastNodeIndex = outerNodes . indexOf ( lastFocus ) ; const correctedNodes = correctNodes ( outerNodes ) ; const correctedIndexDiff = correctedNodes . indexOf ( activeElement ) - ( lastNode ? correctedNodes . indexOf ( lastNode ) : activeIndex ) ; const returnFirstNode = pickFocusable ( innerNodes , 0 ) ; const returnLastNode = pickFocusable ( innerNodes , cnt - 1 ) ; if ( activeIndex === - 1 || lastNodeInside === - 1 ) { return NEW_FOCUS ; } if ( ! indexDiff && lastNodeInside >= 0 ) { return lastNodeInside ; } if ( activeIndex <= firstNodeIndex && isOnGuard && Math . abs ( indexDiff ) > 1 ) { return returnLastNode ; } if ( activeIndex >= lastNodeIndex && isOnGuard && Math . abs ( indexDiff ) > 1 ) { return returnFirstNode ; } if ( indexDiff && Math . abs ( correctedIndexDiff ) > 1 ) { return lastNodeInside ; } if ( activeIndex <= firstNodeIndex ) { return returnLastNode ; } if ( activeIndex > lastNodeIndex ) { return returnFirstNode ; } if ( indexDiff ) { if ( Math . abs ( indexDiff ) > 1 ) { return lastNodeInside ; } return ( cnt + lastNodeInside + indexDiff ) % cnt ; } return undefined ; } ;