Debian Codegraph @

extensions/VisualEditor/lib/ve/src/ui/ve.ui.TabIndexScope.js

raw symbols SCIP
1 /*!
2 * VisualEditor TabIndexScope class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * TabIndex Scope container constructor
9 *
10 * @class
11 * @constructor
12 *
13 * @param {Object} [config] Configuration options
14 * @param {jQuery} [config.root] Initial root element to scope tabIndex within
15 * @param {boolean} [config.skipAriaDisabled] Whether to skip elements that are just aria-disabled from the order
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 /* Setup */
35
36 OO.initClass( ve.ui.TabIndexScope );
37
38 /* Methods */
39
40 /**
41 * Set the current root element for tabbing
42 *
43 * @param {jQuery} $root root element to scope tabIndex within
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 * Build a list of elements in the current root, in tab order
55 *
56 * This mimics browser behavior: fetch focusable elements, sort by [tabIndex, DOM order]
57 *
58 * @return {HTMLElement[]} list of elements in the order they should be tabbed through
59 */
60 ve.ui.TabIndexScope.prototype.getElementsInRoot = function () {
61 const elements = this.$root.find( '*' )
62 .filter( ( index, element ) => {
63 if ( element.tabIndex === -1 ) {
64 // tabIndex -1 is focusable, but shouldn't appear to keyboard-navigation
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 // Skip nodes within contentEditable nodes (but not the root contentEditable nodes),
75 // which would be focusable if they weren't editable, e.g. links.
76 // This matches browser behavior.
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 * Handle keydown events on elements
97 *
98 * @private
99 * @param {jQuery.Event} e
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 * Teardown tabbable elements manager
125 */
126 ve.ui.TabIndexScope.prototype.teardown = function () {
127 this.setRoot( [] );
128 };