Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
283 lines (231 loc) · 9.04 KB
/
Copy pathindex.js
File metadata and controls
283 lines (231 loc) · 9.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/**
* @name Marker Tab and Enter Key Navigation for Google Maps API v3
* @version 0.1
* @author riotrah with NYU STIT
* @fileoverview
*
* This library allows simple tab navigation of the markers in Google Maps API.
* It also allows 'clicking' the markers with the enter key.
*/
/* global google */
/* eslint-disable no-unused-vars */
/**
* Initializer function.
* It attaches a live object to the Google Maps object.
* As such it needs to be loaded after Google Maps has fully loaded
*
* @param {DOM Element} mapDiv The Div to which Google Maps Map
* was attached
* @param {Map } map The actual Map object
* @param {Marker []]} markers An array of Marker objects
* @param {Object} icons Object holding string paths for
* .selected and .deselected
* @param {Boolean} distinctMarkerIconFlag A boolean describing whether
* markers have distinct icons or each
* follows a default
*/
function keyNavInit(mapDiv, map, markers, icons, distinctMarkerIconFlag) {
let internalMapDiv;
internalMapDiv = mapDiv
.children[0]
.children[0]
.children[0];
// Create static vars for this maps extension
google.maps.markerNavigation = {};
// Keep passed markers in object
google.maps.markerNavigation.markers = markers;
// Currently selected marker's index in Markers
google.maps.markerNavigation.currentMarker = -1;
google.maps.markerNavigation.markerIcons = [];
google.maps.markerNavigation.updateMarkerCount = updateMarkerCount;
__addTabNavToMapMarkers(map, internalMapDiv);
if (icons) {
if (icons.selected) {
google.maps.markerNavigation.selectedMarkerIcon = icons.selected;
}
if (icons.deselected) {
google.maps.markerNavigation.deselectedMarkerIcon = icons.deselected;
}
}
if (distinctMarkerIconFlag) {
google.maps.markerNavigation.markerIcons = __getMarkerIconsFromMarkers(markers);
}
/**
* Resets current marker and eventListener if map is out of focus
*/
internalMapDiv.addEventListener('focusout', (e) => {
__deselectMarker(
google.maps.markerNavigation.previousMarkerListener,
google.maps.markerNavigation.markers[google.maps.markerNavigation.currentMarker]
);
google.maps.markerNavigation.currentMarker = -1;
});
}
/**
* Adds an event listener to google maps to capture Tab keypresses
* It tracks every time the button is pressed to see what 'direction'
* the current activeElement is travelling in in order to smoothly
* iterate through the markers in an intuitive fashion.
* If tab is pressed and the internal map div is now the active element,
* it starts capturing tab presses until all markers have been iterated thru,
* returning tab control to the browser to tab thru the other elements
* in the direction of entry.
*
* Basically it make Markers behave like DOM Elements as far as tab
* navigation is concerned.
*
* @param {Map} map Google Maps API Object
* @param {DOM Element} mapDiv Internal DOM Element whose keypresses we are capturing
*/
function __addTabNavToMapMarkers(map, mapDiv) {
const markers = google.maps.markerNavigation.markers || [];
google.maps.event.addDomListener(document, 'keydown', (e) => {
let direction;
if (e.key === 'Tab'
&& document.activeElement !== mapDiv) {
direction = !e.shiftKey;
if (direction) {
google.maps.markerNavigation.currentMarker = -1;
} else {
google.maps.markerNavigation.currentMarker = markers.length;
}
} else if (document.activeElement === mapDiv
&& e.key === 'Tab') {
const shift = e.shiftKey;
/**
* This grabs the event listener from the current selected marker,
* saves it to a prop of this object and passes that very listener
* to the function upon next call. This ensures that the previous
* listener is deactivated to save some mem.
*/
google.maps.markerNavigation.previousMarkerListener =
__iterateMarkers(
google.maps.markerNavigation.previousMarkerListener,
map,
mapDiv,
shift,
markers
);
if (google.maps.markerNavigation.currentMarker >= markers.length
&& !shift) {
google.maps.markerNavigation.currentMarker = markers.length;
} else if (google.maps.markerNavigation.currentMarker <= -1 && shift) {
google.maps.markerNavigation.currentMarker = -1;
} else {
e.preventDefault();
}
}
});
}
/**
* Step through each marker in the appropriate direction.
* Returns the added 'Enter' keypress event listener that gets attached to the
* newly selected Marker.
*
* @param {EventListener} listener Google Maps API EventListener of previous
* iteration
* @param {Map} map Map Object
* @param {DOM Element} mapDiv Internal Map element whose keypresses we
* are capturing
* @param {Boolean} shift Whether or not shift key is being pressed
* @return {EventListener} Google Maps API EventListener of
* just-selected Marker
*/
function __iterateMarkers(listener, map, mapDiv, shift) {
const markers = google.maps.markerNavigation.markers || [];
if (!markers.length) { google.maps.markerNavigation.currentMarker = 0; }
const mod = shift
? -1
: 1;
const newListener = __selectMarker(
map,
mapDiv,
markers[google.maps.markerNavigation.currentMarker + mod]
);
__deselectMarker(listener, markers[google.maps.markerNavigation.currentMarker]);
google.maps.markerNavigation.currentMarker += mod;
return newListener;
}
/**
* Deselects the given marker.
* Sets icon to either default deselected icon or its previous icon if given.
* Then it removes the EventListener attached to this Marker.
* The listener needs to be passed in by the function calling this one.
*
* @param {EventListener} enterListener Google Maps API EventListener to
* remove from previous marker
* @param {Marker} marker Google Maps API Marker to deselect
*/
function __deselectMarker(enterListener, marker) {
if (!marker) { return; }
const icon = google.maps.markerNavigation.markerIcons.length
? google.maps.markerNavigation.markerIcons[google.maps.markerNavigation.currentMarker]
: google.maps.markerNavigation.deselectedMarkerIcon;
marker.setIcon(icon);
if (enterListener) { enterListener.remove(); }
}
/**
* Selects the given Marker.
* Sets icon to default selected icon.
* Pans map to this marker.
* Attaches an enter key event listener to this marker.
*
* @param {Map} map Google Maps API Map Object
* @param {DOM Element} mapDiv Map Div to check for the enter key Listener
* @param {Marker} marker Google Maps API Marker Object
* @return {EventListener} Google Maps API EventListener returned by
* the newly attached listener
*/
function __selectMarker(map, mapDiv, marker) {
if (!marker) { return; }
marker.setIcon(google.maps.markerNavigation.selectedMarkerIcon);
// Should pan as well
map.panTo(marker.getPosition());
return __addClickMarkerViaEnterKey(mapDiv, marker);
}
/**
* Attaches a listener to given Marker and returns its reference.
* Simply triggers a click event on the given Marker, so if there is
* no click listener set then it does nothing.
* @param {DOM Element} mapDiv Map Div to capture enter buttons in
* @param {Marker} marker Google Maps API Marker Object
* @return {EventListener} Google Maps API EventListener returned by
* the newly attached listener
*/
function __addClickMarkerViaEnterKey(mapDiv, marker) {
if (!marker) { return; }
return google.maps.event.addDomListener(
document,
'keydown',
(e) => {
// const code = (e.keyCode ? e.keyCode : e.which);
if (document.activeElement === mapDiv
&& e.key === 'Enter') {
google.maps.event.trigger(marker, 'click');
}
}
);
}
/**
* Simply return the icons as an array from an array of Markers
* @param {Marker []} markers Array of Google Maps API Marker Object
* @return {String []} Array of String filepaths to icons
*/
function __getMarkerIconsFromMarkers(markers) {
if (markers) {
return markers.map((marker) => marker.icon);
}
}
/**
* Update the markers and their icons.
* This is if anything causes the number of Markers to change
* Or if they've been altered.
* @param {Marker []} markers Array of Google Maps API Marker Object
*/
const updateMarkerCount = (markers) => {
// Set markers
google.maps.markerNavigation.markers = markers;
google.maps.markerNavigation.markerIcons = __getMarkerIconsFromMarkers(markers);
// Clear previous marker enter listener
google.maps.markerNavigation.previousMarkerListener = undefined;
};