-
Notifications
You must be signed in to change notification settings - Fork 0
/
requirejs-gmaps.js
executable file
·65 lines (51 loc) · 1.88 KB
/
requirejs-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
55
56
57
58
59
60
61
62
63
64
65
/**
* This helper returns a functional google object
*/
define(['./es6-promise'], function (ES6Promise) {
ES6Promise.polyfill();
function appendScriptTag(src) {
if (typeof window.google === "object" && typeof window.google.maps !== "undefined") {
return Promise.resolve(window.google.maps);
}
return 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;
return document.body.appendChild(script);
});
}
return {
load: function (name, parentRequire, onload, opt_config) {
var config = opt_config || {},
scriptUrl = parentRequire.toUrl(name).split('?')[0],
parameters = config.gmaps.parameters,
paramArray = ['callback=__google_maps_callback__'];
if (config.isBuild) {
onload(null);
return;
}
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 onload(gmaps);
}).catch(function (e) {
return onload.error(e);
});
}
};
});