@@ -19,12 +19,10 @@ export var FeatureIndex = L.Layer.extend({
19
19
20
20
this . _output = L . DomUtil . create ( "output" , "mapml-feature-index" , map . _container ) ;
21
21
this . _body = L . DomUtil . create ( "span" , "mapml-feature-index-content" , this . _output ) ;
22
- this . _moreContent = L . DomUtil . create ( "span" , "mapml-feature-index-more-content" , this . _output ) ;
23
- this . _moreContent . style . display = "none" ;
24
22
25
23
map . on ( "layerchange layeradd layerremove overlayremove" , this . _toggleEvents , this ) ;
26
24
map . on ( 'moveend focus' , this . _checkOverlap , this ) ;
27
- map . on ( "keydown" , this . _toggleMoreContent , this ) ;
25
+ map . on ( "keydown" , this . _toggleContent , this ) ;
28
26
this . _addOrRemoveFeatureIndex ( ) ;
29
27
} ,
30
28
@@ -48,47 +46,71 @@ export var FeatureIndex = L.Layer.extend({
48
46
49
47
body . innerHTML = "" ;
50
48
51
- let moreContent = this . _moreContent ;
52
- moreContent . innerHTML = "" ;
53
-
49
+ body . allFeatures = [ ] ;
54
50
keys . forEach ( i => {
55
51
if ( layers [ i ] . featureAttributes && featureIndexBounds . overlaps ( layers [ i ] . _bounds ) ) {
56
52
let label = layers [ i ] . group . getAttribute ( "aria-label" ) ;
57
53
58
- if ( index % 9 === 0 ) {
59
- let span = document . createElement ( "span" ) ;
60
- span . setAttribute ( "id" , index / 9 ) ;
61
- moreContent . appendChild ( span ) ;
62
- if ( index === 9 ) {
63
- body . appendChild ( this . _updateOutput ( "More results" , 9 ) ) ;
64
- index += 1 ;
65
- }
54
+ if ( index < 8 ) {
55
+ body . appendChild ( this . _updateOutput ( label , index , index ) ) ;
66
56
}
67
-
68
- if ( index > 9 ) {
69
- let value = Math . floor ( ( index - 1 ) / 9 ) ;
70
- let span = moreContent . querySelector ( `[id=${ CSS . escape ( value ) } ]` ) ;
71
- span . appendChild ( this . _updateOutput ( label , index ) ) ;
72
- } else {
73
- body . appendChild ( this . _updateOutput ( label , index ) ) ;
57
+ if ( index % 7 === 0 || index === 1 ) {
58
+ body . allFeatures . push ( [ ] ) ;
59
+ }
60
+ body . allFeatures [ Math . floor ( ( index - 1 ) / 7 ) ] . push ( { label, index} ) ;
61
+ if ( body . allFeatures [ 1 ] && body . allFeatures [ 1 ] . length === 1 ) {
62
+ body . appendChild ( this . _updateOutput ( "More results" , 0 , 9 ) ) ;
74
63
}
75
64
index += 1 ;
76
-
77
65
}
78
66
} ) ;
67
+ this . _addToggleKeys ( ) ;
79
68
} ,
80
- _updateOutput : function ( label , index ) {
69
+
70
+ _updateOutput : function ( label , index , key ) {
81
71
let span = document . createElement ( "span" ) ;
82
- span . innerHTML = `<kbd>${ index } </kbd>` + " " + label ;
72
+ span . setAttribute ( "index" , index ) ;
73
+ span . innerHTML = `<kbd>${ key } </kbd>` + " " + label ;
83
74
return span ;
84
75
} ,
85
- _toggleMoreContent : function ( e ) {
86
- let display = this . _moreContent . style . display ;
76
+
77
+ _addToggleKeys : function ( ) {
78
+ let allFeatures = this . _body . allFeatures ;
79
+ for ( let i = 0 ; i < allFeatures . length ; i ++ ) {
80
+ if ( allFeatures [ i ] . length === 0 ) return ;
81
+ if ( allFeatures [ i - 1 ] ) {
82
+ let label = "Previous results" ;
83
+ allFeatures [ i ] . push ( { label} ) ;
84
+ }
85
+
86
+ if ( allFeatures [ i + 1 ] && allFeatures [ i + 1 ] . length > 0 ) {
87
+ let label = "More results" ;
88
+ allFeatures [ i ] . push ( { label} ) ;
89
+ }
90
+ }
91
+ } ,
92
+
93
+ _toggleContent : function ( e ) {
94
+ let body = this . _body ;
87
95
if ( e . originalEvent . keyCode === 57 ) {
88
- if ( display === "none" ) {
89
- this . _moreContent . style . display = "inline-block" ;
90
- } else {
91
- this . _moreContent . style . display = "none" ;
96
+ this . _newContent ( body , 1 ) ;
97
+ } else if ( e . originalEvent . keyCode === 56 ) {
98
+ this . _newContent ( body , - 1 ) ;
99
+ }
100
+ } ,
101
+
102
+ _newContent : function ( body , direction ) {
103
+ let index = body . firstChild . getAttribute ( "index" ) ;
104
+ let newContent = body . allFeatures [ Math . floor ( ( ( index - 1 ) / 7 ) + direction ) ] ;
105
+ if ( newContent && newContent . length > 0 ) {
106
+ body . innerHTML = "" ;
107
+ for ( let i = 0 ; i < newContent . length ; i ++ ) {
108
+ let feature = newContent [ i ] ;
109
+ let index = feature . index ? feature . index : 0 ;
110
+ let key = i + 1 ;
111
+ if ( feature . label === "More results" ) key = 9 ;
112
+ if ( feature . label === "Previous results" ) key = 8 ;
113
+ body . appendChild ( this . _updateOutput ( feature . label , index , key ) ) ;
92
114
}
93
115
}
94
116
} ,
0 commit comments