-
Notifications
You must be signed in to change notification settings - Fork 0
/
systemjs-gmaps.js
54 lines (41 loc) · 1.38 KB
/
systemjs-gmaps.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
var thePromise = null;
function appendScriptTag(src) {
if (typeof window.google === "object" && typeof window.google.maps !== "undefined") {
return Promise.resolve(window.google.maps);
}
if (thePromise !== null) {
return thePromise;
}
thePromise = new Promise(function (resolve, reject) {
window.__google_maps_callback__ = function () {
if (window.google.maps) {
var gmaps = window.google.maps;
resolve(gmaps);
return gmaps;
} else {
return reject('no gmaps object!');
}
};
});
var script = document.createElement("script");
script.type = "text/javascript";
script.async = true;
script.src = src;
document.body.appendChild(script);
return thePromise;
}
exports.fetch = function (load) {
var scriptUrl = load.address.split(/(\?|\.js)+/)[0],
parameters = load.metadata.parameters,
paramArray = ['callback=__google_maps_callback__'];
for (var key in parameters) {
if (parameters.hasOwnProperty(key) && parameters[key] !== null) {
paramArray.push(key + '=' + parameters[key]);
}
}
scriptUrl += '?' + paramArray.join('&');
return appendScriptTag(scriptUrl)
.then(function (gmaps) {
return 'module.exports = google.maps';
});
};