| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
ve.ui.TabIndexScope = function VeUiTabIndexScope( config ) { |
| 18 |
config = ve.extendObject( { |
| 19 |
root: false, |
| 20 |
skipAriaDisabled: true, |
| 21 |
skipAriaHidden: true |
| 22 |
}, config ); |
| 23 |
|
| 24 |
this.skipAriaDisabled = config.skipAriaDisabled; |
| 25 |
this.skipAriaHidden = config.skipAriaHidden; |
| 26 |
|
| 27 |
this.onRootKeyDownBound = this.onRootKeyDown.bind( this ); |
| 28 |
|
| 29 |
if ( config.root ) { |
| 30 |
this.setTabRoot( config.root ); |
| 31 |
} |
| 32 |
}; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
OO.initClass( ve.ui.TabIndexScope ); |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
ve.ui.TabIndexScope.prototype.setTabRoot = function ( $root ) { |
| 46 |
if ( this.$root ) { |
| 47 |
this.$root.off( 'keydown', this.onRootKeyDownBound ); |
| 48 |
} |
| 49 |
|
| 50 |
this.$root = $( $root ).on( 'keydown', this.onRootKeyDownBound ); |
| 51 |
}; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
ve.ui.TabIndexScope.prototype.getElementsInRoot = function () { |
| 61 |
const elements = this.$root.find( '*' ) |
| 62 |
.filter( ( index, element ) => { |
| 63 |
if ( element.tabIndex === -1 ) { |
| 64 |
|
| 65 |
return false; |
| 66 |
} |
| 67 |
if ( this.skipAriaDisabled && element.getAttribute( 'aria-disabled' ) === 'true' ) { |
| 68 |
return false; |
| 69 |
} |
| 70 |
if ( this.skipAriaHidden && $( element ).closest( '[aria-hidden="true"]', this.$root[ 0 ] ).length ) { |
| 71 |
return false; |
| 72 |
} |
| 73 |
if ( element.isContentEditable && element.contentEditable !== 'true' ) { |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
return false; |
| 78 |
} |
| 79 |
return OO.ui.isFocusableElement( $( element ) ); |
| 80 |
} ) |
| 81 |
.map( ( index, element ) => ( { element: element, index: index } ) ) |
| 82 |
.get(); |
| 83 |
elements.sort( ( a, b ) => { |
| 84 |
if ( a.element.tabIndex < b.element.tabIndex ) { |
| 85 |
return -1; |
| 86 |
} |
| 87 |
if ( a.element.tabIndex > b.element.tabIndex ) { |
| 88 |
return 1; |
| 89 |
} |
| 90 |
return a.index - b.index; |
| 91 |
} ); |
| 92 |
return elements.map( ( data ) => data.element ); |
| 93 |
}; |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
ve.ui.TabIndexScope.prototype.onRootKeyDown = function ( e ) { |
| 102 |
if ( e.which !== OO.ui.Keys.TAB ) { |
| 103 |
return; |
| 104 |
} |
| 105 |
|
| 106 |
const elements = this.getElementsInRoot(); |
| 107 |
let index = elements.indexOf( e.target ); |
| 108 |
|
| 109 |
if ( index === -1 ) { |
| 110 |
return; |
| 111 |
} |
| 112 |
|
| 113 |
index += e.shiftKey ? -1 : 1; |
| 114 |
|
| 115 |
if ( ( index < 0 || index >= elements.length ) ) { |
| 116 |
return; |
| 117 |
} |
| 118 |
|
| 119 |
e.preventDefault(); |
| 120 |
elements[ index ].focus(); |
| 121 |
}; |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
ve.ui.TabIndexScope.prototype.teardown = function () { |
| 127 |
this.setRoot( [] ); |
| 128 |
}; |