@@ -12,7 +12,6 @@ export var FeatureGroup = L.FeatureGroup.extend({
1212 L . LayerGroup . prototype . initialize . call ( this , layers , options ) ;
1313
1414 if ( ( this . options . onEachFeature && this . options . properties ) || this . options . link ) {
15- this . options . group . setAttribute ( 'tabindex' , '0' ) ;
1615 L . DomUtil . addClass ( this . options . group , "leaflet-interactive" ) ;
1716 L . DomEvent . on ( this . options . group , "keyup keydown mousedown" , this . _handleFocus , this ) ;
1817 let firstLayer = layers [ Object . keys ( layers ) [ 0 ] ] ;
@@ -32,12 +31,59 @@ export var FeatureGroup = L.FeatureGroup.extend({
3231 if ( this . options . featureID ) this . options . group . setAttribute ( "data-fid" , this . options . featureID ) ;
3332 } ,
3433
34+ onAdd : function ( map ) {
35+ L . LayerGroup . prototype . onAdd . call ( this , map ) ;
36+ this . updateInteraction ( ) ;
37+ } ,
38+
39+ updateInteraction : function ( ) {
40+ let map = this . _map || this . options . _leafletLayer . _map ;
41+ if ( ( this . options . onEachFeature && this . options . properties ) || this . options . link )
42+ map . featureIndex . addToIndex ( this , this . getPCRSCenter ( ) , this . options . group ) ;
43+
44+ for ( let layerID in this . _layers ) {
45+ let layer = this . _layers [ layerID ] ;
46+ for ( let part of layer . _parts ) {
47+ if ( layer . featureAttributes && layer . featureAttributes . tabindex )
48+ map . featureIndex . addToIndex ( layer , layer . getPCRSCenter ( ) , part . path ) ;
49+ for ( let subPart of part . subrings ) {
50+ if ( subPart . attr && subPart . attr . tabindex ) map . featureIndex . addToIndex ( layer , subPart . center , subPart . path ) ;
51+ }
52+ }
53+ }
54+ } ,
55+
3556 /**
3657 * Handler for focus events
3758 * @param {L.DOMEvent } e - Event that occurred
3859 * @private
3960 */
4061 _handleFocus : function ( e ) {
62+ if ( ( e . keyCode === 9 || e . keyCode === 16 ) && e . type === "keydown" ) {
63+ let index = this . _map . featureIndex . currentIndex ;
64+ if ( e . keyCode === 9 && e . shiftKey ) {
65+ if ( index === this . _map . featureIndex . inBoundFeatures . length - 1 )
66+ this . _map . featureIndex . inBoundFeatures [ index ] . path . setAttribute ( "tabindex" , - 1 ) ;
67+ if ( index !== 0 ) {
68+ L . DomEvent . stop ( e ) ;
69+ this . _map . featureIndex . inBoundFeatures [ index - 1 ] . path . focus ( ) ;
70+ this . _map . featureIndex . currentIndex -- ;
71+ }
72+ } else if ( e . keyCode === 9 ) {
73+ if ( index !== this . _map . featureIndex . inBoundFeatures . length - 1 ) {
74+ L . DomEvent . stop ( e ) ;
75+ this . _map . featureIndex . inBoundFeatures [ index + 1 ] . path . focus ( ) ;
76+ this . _map . featureIndex . currentIndex ++ ;
77+ } else {
78+ this . _map . featureIndex . inBoundFeatures [ 0 ] . path . setAttribute ( "tabindex" , - 1 ) ;
79+ this . _map . featureIndex . inBoundFeatures [ index ] . path . setAttribute ( "tabindex" , 0 ) ;
80+ }
81+ }
82+ } else if ( ! ( e . keyCode === 9 || e . keyCode === 16 || e . keyCode === 13 ) ) {
83+ this . _map . featureIndex . currentIndex = 0 ;
84+ this . _map . featureIndex . inBoundFeatures [ 0 ] . path . focus ( ) ;
85+ }
86+
4187 if ( e . target . tagName . toUpperCase ( ) !== "G" ) return ;
4288 if ( ( e . keyCode === 9 || e . keyCode === 16 || e . keyCode === 13 ) && e . type === "keyup" ) {
4389 this . openTooltip ( ) ;
@@ -69,22 +115,10 @@ export var FeatureGroup = L.FeatureGroup.extend({
69115 * @private
70116 */
71117 _previousFeature : function ( e ) {
72- let group = this . _source . group . previousSibling ;
73- if ( ! group ) {
74- let currentIndex = this . _source . group . closest ( "div.mapml-layer" ) . style . zIndex ;
75- let overlays = this . _map . getPane ( "overlayPane" ) . children ;
76- for ( let i = overlays . length - 1 ; i >= 0 ; i -- ) {
77- let layer = overlays [ i ] ;
78- if ( layer . style . zIndex >= currentIndex ) continue ;
79- group = layer . querySelector ( "g.leaflet-interactive" ) ;
80- if ( group ) {
81- group = group . parentNode . lastChild ;
82- break ;
83- }
84- }
85- if ( ! group ) group = this . _source . group ;
86- }
87- group . focus ( ) ;
118+ L . DomEvent . stop ( e ) ;
119+ this . _map . featureIndex . currentIndex = Math . max ( this . _map . featureIndex . currentIndex - 1 , 0 ) ;
120+ let prevFocus = this . _map . featureIndex . inBoundFeatures [ this . _map . featureIndex . currentIndex ] ;
121+ prevFocus . path . focus ( ) ;
88122 this . _map . closePopup ( ) ;
89123 } ,
90124
@@ -94,19 +128,24 @@ export var FeatureGroup = L.FeatureGroup.extend({
94128 * @private
95129 */
96130 _nextFeature : function ( e ) {
97- let group = this . _source . group . nextSibling ;
98- if ( ! group ) {
99- let currentIndex = this . _source . group . closest ( "div.mapml-layer" ) . style . zIndex ;
131+ L . DomEvent . stop ( e ) ;
132+ this . _map . featureIndex . currentIndex = Math . min ( this . _map . featureIndex . currentIndex + 1 , this . _map . featureIndex . inBoundFeatures . length - 1 ) ;
133+ let nextFocus = this . _map . featureIndex . inBoundFeatures [ this . _map . featureIndex . currentIndex ] ;
134+ nextFocus . path . focus ( ) ;
135+ this . _map . closePopup ( ) ;
136+ } ,
100137
101- for ( let layer of this . _map . getPane ( "overlayPane" ) . children ) {
102- if ( layer . style . zIndex <= currentIndex ) continue ;
103- group = layer . querySelectorAll ( "g.leaflet-interactive" ) ;
104- if ( group . length > 0 ) break ;
138+ getPCRSCenter : function ( ) {
139+ let bounds ;
140+ for ( let l in this . _layers ) {
141+ let layer = this . _layers [ l ] ;
142+ if ( ! bounds ) {
143+ bounds = L . bounds ( layer . getPCRSCenter ( ) , layer . getPCRSCenter ( ) ) ;
144+ } else {
145+ bounds . extend ( layer . getPCRSCenter ( ) ) ;
105146 }
106- group = group && group . length > 0 ? group [ 0 ] : this . _source . group ;
107147 }
108- group . focus ( ) ;
109- this . _map . closePopup ( ) ;
148+ return bounds . getCenter ( ) ;
110149 } ,
111150} ) ;
112151
0 commit comments