-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbing.js
324 lines (287 loc) · 8.91 KB
/
bing.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
// This module was written by milespratt (https://github.com/milespratt/bingmaps-react)
"use strict";
require("core-js/modules/web.dom-collections.iterator.js");
import { useRouter } from "next/navigation";
Object.defineProperty(exports, "__esModule", {
value: true,
});
//exports.default = BingMapsReact;
require("core-js/modules/es.symbol.description.js");
var _react = _interopRequireWildcard(require("react"));
function _getRequireWildcardCache(nodeInterop) {
if (typeof WeakMap !== "function") return null;
var cacheBabelInterop = new WeakMap();
var cacheNodeInterop = new WeakMap();
return (_getRequireWildcardCache = function _getRequireWildcardCache(
nodeInterop
) {
return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
})(nodeInterop);
}
function _interopRequireWildcard(obj, nodeInterop) {
if (!nodeInterop && obj && obj.__esModule) {
return obj;
}
if (obj === null || (typeof obj !== "object" && typeof obj !== "function")) {
return { default: obj };
}
var cache = _getRequireWildcardCache(nodeInterop);
if (cache && cache.has(obj)) {
return cache.get(obj);
}
var newObj = {};
var hasPropertyDescriptor =
Object.defineProperty && Object.getOwnPropertyDescriptor;
for (var key in obj) {
if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
var desc = hasPropertyDescriptor
? Object.getOwnPropertyDescriptor(obj, key)
: null;
if (desc && (desc.get || desc.set)) {
Object.defineProperty(newObj, key, desc);
} else {
newObj[key] = obj[key];
}
}
}
newObj.default = obj;
if (cache) {
cache.set(obj, newObj);
}
return newObj;
}
function ownKeys(object, enumerableOnly) {
var keys = Object.keys(object);
if (Object.getOwnPropertySymbols) {
var symbols = Object.getOwnPropertySymbols(object);
if (enumerableOnly) {
symbols = symbols.filter(function (sym) {
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
});
}
keys.push.apply(keys, symbols);
}
return keys;
}
function _objectSpread(target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i] != null ? arguments[i] : {};
if (i % 2) {
ownKeys(Object(source), true).forEach(function (key) {
_defineProperty(target, key, source[key]);
});
} else if (Object.getOwnPropertyDescriptors) {
Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
} else {
ownKeys(Object(source)).forEach(function (key) {
Object.defineProperty(
target,
key,
Object.getOwnPropertyDescriptor(source, key)
);
});
}
}
return target;
}
function _defineProperty(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true,
});
} else {
obj[key] = value;
}
return obj;
}
function BingMapsReact(_ref) {
let {
bingMapsKey = null,
height = "100%",
mapOptions = null,
onMapReady = null,
pushPins = null,
pushPinsWithInfoboxes = null,
viewOptions = null,
width = "100%",
} = _ref;
// refs
const mapContainer = (0, _react.useRef)(null);
const map = (0, _react.useRef)(null); // removes pushpins
const router = useRouter();
function removePushpins(map, Maps) {
for (var i = map.entities.getLength() - 1; i >= 0; i--) {
var pushpin = map.entities.get(i);
if (pushpin instanceof Maps.Pushpin) {
map.entities.removeAt(i);
}
}
} // add pushpins with infoboxes
const addPushpinsWithInfoboxes = (0, _react.useCallback)(
(pushPinsToAdd, infobox, map, Maps) => {
removePushpins(map, Maps);
pushPinsToAdd.forEach((pushPin) => {
if (pushPin === null) {
return;
}
const newPin = new Maps.Pushpin(pushPin.center, pushPin.options);
newPin.metadata = _objectSpread({}, pushPin.options);
Maps.Events.addHandler(newPin, "mouseover", (e) => {
var _pushPin$infobox;
infobox.setOptions(
_objectSpread(
{
title: e.target.metadata.candidateName,
description: e.target.metadata.id,
htmlContent:
((_pushPin$infobox = pushPin.infobox) === null ||
_pushPin$infobox === void 0
? void 0
: _pushPin$infobox.infoboxHtml) || pushPin.infoboxHtml,
location: newPin.getLocation(),
visible: true,
actions: [
{
label: `Go to exam page (${e.target.metadata.title})`,
eventHandler: () =>
router.push(`/exams/${e.target.metadata.id}`),
},
{
label: "Go to candidate's page",
eventHandler: () =>
router.push(
`/candidates/${
e.target.metadata.candidateId
}?name=${e.target.metadata.candidateName.replaceAll(
" ",
"+"
)}`
),
},
],
},
pushPin.infobox
)
);
});
Maps.Events.addHandler(newPin, "mouseout", (e) => {
infobox.setOptions({
visible: false,
});
});
Maps.Events.addHandler(newPin, "click", (e) => {
router.push(`/exams/${e.target.metadata.id}`);
});
map.entities.push(newPin);
});
},
[]
); // add pushpins
const addPushpins = (0, _react.useCallback)((pushPinsToAdd, map, Maps) => {
removePushpins(map, Maps);
pushPinsToAdd.forEach((pushPin) => {
if (pushPin === null) {
return;
}
const newPin = new Maps.Pushpin(pushPin.center, pushPin.options);
map.entities.push(newPin);
});
}, []); // set view options
function setMapViewOptions(map, viewOptions, Maps) {
const options = _objectSpread({}, viewOptions);
if (viewOptions.mapTypeId) {
options.mapTypeId = Maps.MapTypeId[viewOptions.mapTypeId];
}
if (viewOptions.hideRoadLabels) {
options.labelOverlay = Maps.LabelOverlay.hidden;
}
map.setView(options);
} // set map options
function setMapOptions(map, mapOptions, Maps) {
const options = _objectSpread({}, mapOptions); // some map options require values from the Maps class
// these conditional statements handle those cases
if (mapOptions.navigationBarMode) {
options.navigationBarMode =
Maps.NavigationBarMode[mapOptions.navigationBarMode];
}
if (mapOptions.navigationBarOrientation) {
options.navigationBarOrientation =
Maps.NavigationBarOrientation[mapOptions.navigationBarOrientation];
}
if (mapOptions.supportedMapTypes) {
options.supportedMapTypes = mapOptions.supportedMapTypes.map(
(type) => Maps.MapTypeId[type]
);
}
map.setOptions(options);
} // make map, set options, add pins
const makeMap = (0, _react.useCallback)(() => {
const { Maps } = window.Microsoft; // only make a new map if one doesn't already exist
if (!map.current) {
map.current = new Maps.Map(mapContainer.current, {
credentials: bingMapsKey,
});
} // set viewOptions, if any
if (viewOptions) {
setMapViewOptions(map.current, viewOptions, Maps);
} // set mapOptions, if any
if (mapOptions) {
setMapOptions(map.current, mapOptions, Maps);
} // add push pins, if any
if (pushPins) {
addPushpins(pushPins, map.current, Maps);
} // add infoboxes, if any
if (pushPinsWithInfoboxes) {
const infobox = new Maps.Infobox(map.current.getCenter(), {
visible: false,
});
infobox.setMap(map.current);
addPushpinsWithInfoboxes(
pushPinsWithInfoboxes,
infobox,
map.current,
Maps
);
}
onMapReady &&
onMapReady({
map,
});
}, [
addPushpinsWithInfoboxes,
addPushpins,
bingMapsKey,
mapOptions,
onMapReady,
pushPins,
pushPinsWithInfoboxes,
viewOptions,
]);
(0, _react.useEffect)(() => {
if (window.Microsoft && window.Microsoft.Maps) {
makeMap();
} else {
const scriptTag = document.createElement("script");
scriptTag.setAttribute("type", "text/javascript");
scriptTag.setAttribute(
"src",
"https://www.bing.com/api/maps/mapcontrol?callback=makeMap"
);
scriptTag.async = true;
scriptTag.defer = true;
document.body.appendChild(scriptTag);
window.makeMap = makeMap;
}
}, [makeMap]);
return /*#__PURE__*/ _react.default.createElement("div", {
ref: mapContainer,
style: {
height: height,
width: width,
},
});
}
export default BingMapsReact;