-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathgeometry.js
266 lines (251 loc) · 8.46 KB
/
geometry.js
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
export var Geometry = L.FeatureGroup.extend({
/**
* Initialize the feature group
* @param {M.Path[]} layers
* @param {Object} options
*/
initialize: function (layers, options) {
if (options.wrappers && options.wrappers.length > 0)
options = Object.assign(
M.Path.prototype._convertWrappers(options.wrappers),
options
);
L.LayerGroup.prototype.initialize.call(this, layers, options);
this._featureEl = this.options.mapmlFeature;
this.layerBounds = options.layerBounds;
this.zoomBounds = options.zoomBounds;
let firstLayer = layers[Object.keys(layers)[0]];
if (layers.length === 1 && firstLayer.options.link)
this.options.link = firstLayer.options.link;
if (
(this.options.onEachFeature && this.options.properties) ||
this.options.link
) {
L.DomUtil.addClass(this.options.group, 'leaflet-interactive');
if (this.options.link) {
M.Path.prototype.attachLinkHandler.call(
this,
this.options.group,
this.options.link,
this.options._leafletLayer
);
this.options.group.setAttribute('role', 'link');
} else {
this.options.group.setAttribute('aria-expanded', 'false');
this.options.group.setAttribute('role', 'button');
this.options.onEachFeature(this.options.properties, this);
this.off('click', this._openPopup);
}
}
L.DomEvent.on(this.options.group, 'keyup keydown', this._handleFocus, this);
this.options.group.setAttribute('aria-label', this.options.accessibleTitle);
if (this.options.featureID)
this.options.group.setAttribute('data-fid', this.options.featureID);
for (let feature of layers) {
feature._groupLayer = this;
}
},
onAdd: function (map) {
L.LayerGroup.prototype.onAdd.call(this, map);
this.updateInteraction();
},
updateInteraction: function () {
let map = this._map || this.options._leafletLayer._map;
if (this.options.onEachFeature || this.options.link)
map.featureIndex.addToIndex(
this,
this.getPCRSCenter(),
this.options.group
);
for (let layerID in this._layers) {
let layer = this._layers[layerID];
for (let part of layer._parts) {
if (layer.featureAttributes && layer.featureAttributes.tabindex)
map.featureIndex.addToIndex(layer, layer.getPCRSCenter(), part.path);
for (let subPart of part.subrings) {
if (subPart.attr && subPart.attr.tabindex)
map.featureIndex.addToIndex(layer, subPart.center, subPart.path);
}
}
}
},
/**
* Check whether the feature group should be rendered at current map zoom level
* @param {Number} zoom - current map zoom
* @param {Object} vectorMinZoom - the minimum zoom bound of vector layer
* @param {Object} vectorMaxZoom - the maximum zoom bound of vector layer
* @returns {Boolean}
* @private
*/
_checkRender: function (zoom, vectorMinZoom, vectorMaxZoom) {
let minZoom = this._featureEl.min,
maxZoom = this._featureEl.max;
// if the current map zoom falls below/above the zoom bounds of the vector layer
if (zoom > vectorMaxZoom || zoom < vectorMinZoom) return false;
// if the current map zoom falls below/above the [min, max] range
if (
(minZoom !== null && zoom < +minZoom) ||
(maxZoom !== null && zoom > +maxZoom)
) {
return false;
}
return true;
},
/**
* Handler for focus events
* @param {L.DOMEvent} e - Event that occurred
* @private
*/
_handleFocus: function (e) {
// tab, shift, cr, esc, up, left, down, right,
if (
[9, 16, 27, 37, 38, 39, 40].includes(e.keyCode) &&
e.type === 'keydown'
) {
let index = this._map.featureIndex.currentIndex;
// Down/right arrow keys replicate tabbing through the feature index
// Up/left arrow keys replicate shift-tabbing through the feature index
if (e.keyCode === 37 || e.keyCode === 38) {
L.DomEvent.stop(e);
this._map.featureIndex.inBoundFeatures[index].path.setAttribute(
'tabindex',
-1
);
if (index === 0) {
this._map.featureIndex.inBoundFeatures[
this._map.featureIndex.inBoundFeatures.length - 1
].path.focus();
this._map.featureIndex.currentIndex =
this._map.featureIndex.inBoundFeatures.length - 1;
} else {
this._map.featureIndex.inBoundFeatures[index - 1].path.focus();
this._map.featureIndex.currentIndex--;
}
} else if (e.keyCode === 39 || e.keyCode === 40) {
L.DomEvent.stop(e);
this._map.featureIndex.inBoundFeatures[index].path.setAttribute(
'tabindex',
-1
);
if (index === this._map.featureIndex.inBoundFeatures.length - 1) {
this._map.featureIndex.inBoundFeatures[0].path.focus();
this._map.featureIndex.currentIndex = 0;
} else {
this._map.featureIndex.inBoundFeatures[index + 1].path.focus();
this._map.featureIndex.currentIndex++;
}
} else if (e.keyCode === 27) {
let shadowRoot = this._map.options.mapEl.shadowRoot
? this._map.options.mapEl.shadowRoot
: this._map.options.mapEl.querySelector('.mapml-web-map').shadowRoot;
if (shadowRoot.activeElement.nodeName !== 'g') return;
this._map._container.focus();
} else if (e.keyCode === 9) {
let obj = this;
setTimeout(function () {
obj._map.featureIndex.inBoundFeatures[0].path.setAttribute(
'tabindex',
0
);
}, 0);
}
// tab, shift, cr, esc, right, left, up, down,
// 1, 2, 3, 4, 5, 6, 7 (featureIndexOverlay available index items
// [8, 9] being allocated to next, previous menu items).
} else if (
![9, 16, 13, 27, 37, 38, 39, 40, 49, 50, 51, 52, 53, 54, 55].includes(
e.keyCode
)
) {
this._map.featureIndex.currentIndex = 0;
this._map.featureIndex.inBoundFeatures[0].path.focus();
}
// 27 added so that the tooltip opens when dismissing popup with 'esc' key
if (e.target.tagName.toUpperCase() !== 'G') return;
if (
[9, 13, 16, 37, 38, 39, 40, 49, 50, 51, 52, 53, 54, 55, 27].includes(
e.keyCode
) &&
e.type === 'keyup'
) {
this.options.group.parentNode.appendChild(this.options.group);
this.options.group.focus();
this.openTooltip();
} else if (e.keyCode === 13 || e.keyCode === 32) {
this.closeTooltip();
if (!this.options.link && this.options.onEachFeature) {
L.DomEvent.stop(e);
this.openPopup();
}
} else {
this.closeTooltip();
}
},
/**
* Add a M.Path to the M.Geometry
* @param layer
*/
addLayer: function (layer) {
if (!layer.options.link && layer.options.interactive) {
this.options.onEachFeature(this.options.properties, layer);
}
L.FeatureGroup.prototype.addLayer.call(this, layer);
},
/**
* Focuses the previous function in the sequence on previous button press
* @param e
* @private
*/
_previousFeature: function (e) {
L.DomEvent.stop(e);
this._map.featureIndex.currentIndex = Math.max(
this._map.featureIndex.currentIndex - 1,
0
);
let prevFocus =
this._map.featureIndex.inBoundFeatures[
this._map.featureIndex.currentIndex
];
prevFocus.path.focus();
this._map.closePopup();
},
/**
* Focuses next feature in sequence
* @param e
* @private
*/
_nextFeature: function (e) {
L.DomEvent.stop(e);
this._map.featureIndex.currentIndex = Math.min(
this._map.featureIndex.currentIndex + 1,
this._map.featureIndex.inBoundFeatures.length - 1
);
let nextFocus =
this._map.featureIndex.inBoundFeatures[
this._map.featureIndex.currentIndex
];
nextFocus.path.focus();
this._map.closePopup();
},
getPCRSCenter: function () {
let bounds;
for (let l in this._layers) {
let layer = this._layers[l];
if (!bounds) {
bounds = L.bounds(layer.getPCRSCenter(), layer.getPCRSCenter());
} else {
bounds.extend(layer.getPCRSCenter());
}
}
return bounds.getCenter();
}
});
/**
* Returns new M.Geometry
* @param {M.Path[]} layers - Layers belonging to feature group
* @param {Object} options - Options for the feature group
* @returns {M.Geometry}
*/
export var geometry = function (layers, options) {
return new Geometry(layers, options);
};