Commit 5287dc7a authored by Arthur Bogaart's avatar Arthur Bogaart

ESSENTIALS-1017 Move code into HippoEssentials namespace

- Expose an object named HippoEssentials to the global
namespace that wraps all other hippo-essentials related javascript
 code (currently only the GoogleMaps scripts).
parent 6e3b48d3
......@@ -7,12 +7,24 @@
<@hst.headContribution category="htmlBodyEnd">
<script type="text/javascript">
if (!window.HEGM) {
window.HEGM = [];
}
window.HEGM.push(function() {
initGoogleMap('map-canvas-${componentId}', '${cparam.address?html}', ${cparam.longitude}, ${cparam.latitude}, ${cparam.zoomFactor}, '${cparam.mapType}');
});
(function(win) {
var he, gm;
if (!win.HippoEssentials) {
win.HippoEssentials = {};
}
he = win.HippoEssentials;
if (!he.GoogleMaps) {
he.GoogleMaps = {
queue: []
};
}
gm = he.GoogleMaps;
gm.queue.push(function() {
gm.render('map-canvas-${componentId}', '${cparam.address?html}', ${cparam.longitude}, ${cparam.latitude}, ${cparam.zoomFactor}, '${cparam.mapType}');
});
})(window);
</script>
</@hst.headContribution>
......@@ -23,17 +35,17 @@
<@hst.headContribution keyHint="google-maps-api" category="htmlBodyEnd">
<#if cparam.apiKey?has_content>
<#assign mapsUrl = "https://maps.googleapis.com/maps/api/js?key=${cparam.apiKey?html}&callback=initGoogleMaps"/>
<#assign mapsUrl = "https://maps.googleapis.com/maps/api/js?key=${cparam.apiKey?html}&callback=HippoEssentials.GoogleMaps.init"/>
<#else>
<#assign mapsUrl = "https://maps.googleapis.com/maps/api/js?callback=initGoogleMaps"/>
<#assign mapsUrl = "https://maps.googleapis.com/maps/api/js?callback=HippoEssentials.GoogleMaps.init"/>
</#if>
<script type="text/javascript" src="${mapsUrl}" async="async" defer="defer"></script>
</@hst.headContribution>
<#if editMode>
<script type="text/javascript">
if (window.initGoogleMap) {
initGoogleMap('map-canvas-${componentId}', '${cparam.address?html}', ${cparam.longitude}, ${cparam.latitude}, ${cparam.zoomFactor}, '${cparam.mapType}');
if (window.HippoEssentials && window.HippoEssentials.GoogleMaps) {
window.HippoEssentials.GoogleMaps.render('map-canvas-${componentId}', '${cparam.address?html}', ${cparam.longitude}, ${cparam.latitude}, ${cparam.zoomFactor}, '${cparam.mapType}');
}
</script>
</#if>
......@@ -48,40 +48,55 @@
});
}
window.initGoogleMaps = function () {
if (window.HEGM && window.HEGM.length) {
for (var i = 0; i < window.HEGM.length; i++) {
window.HEGM[i]();
var he, gm;
if (!window.HippoEssentials) {
window.HippoEssentials = {};
}
he = window.HippoEssentials;
if (!he.GoogleMaps) {
he.GoogleMaps = {
queue: []
};
}
gm = he.GoogleMaps;
if (!gm.init) {
gm.init = function() {
for (var i = 0; i < gm.queue.length; i++) {
gm.queue[i]();
}
window.HEGM = [];
}
};
gm.queue = [];
};
}
window.initGoogleMap = function (elementId, address, longitude, latitude, zoomFactor, mapType) {
if (!google) {
if (console) {
console.warn('Could not find Google Maps API, please make sure it is loaded correctly before creating a ' +
'new Google Map instance. This is typically done by inserting ' +
'<script src="https://maps.googleapis.com/maps/api/js"></script> into the page.');
if (!gm.render) {
gm.render = function (elementId, address, longitude, latitude, zoomFactor, mapType) {
if (!google) {
if (window.console) {
console.warn('Could not find Google Maps API, please make sure it is loaded correctly before creating a ' +
'new Google Map instance. This is typically done by inserting ' +
'<script src="https://maps.googleapis.com/maps/api/js"></script> into the page.');
}
return;
}
return;
}
var latlng;
if (latitude !== '' || longitude !== '') {
latlng = new google.maps.LatLng(latitude, longitude);
} else {
latlng = new google.maps.LatLng(0, 0);
}
var el = document.getElementById(elementId);
var mapOptions = {
center: latlng,
zoom: zoomFactor,
mapTypeId: getMapType(mapType)
var latlng;
if (latitude !== '' || longitude !== '') {
latlng = new google.maps.LatLng(latitude, longitude);
} else {
latlng = new google.maps.LatLng(0, 0);
}
var el = document.getElementById(elementId);
var mapOptions = {
center: latlng,
zoom: zoomFactor,
mapTypeId: getMapType(mapType)
};
var map = new google.maps.Map(el, mapOptions);
if (address !== '') {
geoCodeAddress(address, map);
}
};
var map = new google.maps.Map(el, mapOptions);
if (address !== '') {
geoCodeAddress(address, map);
}
};
}
})();
......@@ -7,12 +7,24 @@
<hst:headContribution category="htmlBodyEnd">
<script type="text/javascript">
if (!window.HEGM) {
window.HEGM = [];
}
window.HEGM.push(function() {
initGoogleMap('map-canvas-${componentId}', '${fn:escapeXml(requestScope.cparam.address)}', ${requestScope.cparam.longitude}, ${requestScope.cparam.latitude}, ${requestScope.cparam.zoomFactor}, '${requestScope.cparam.mapType}');
});
(function(win) {
var he, gm;
if (!win.HippoEssentials) {
win.HippoEssentials = {};
}
he = win.HippoEssentials;
if (!he.GoogleMaps) {
he.GoogleMaps = {
queue: []
};
}
gm = he.GoogleMaps;
gm.queue.push(function() {
gm.render('map-canvas-${componentId}', '${fn:escapeXml(requestScope.cparam.address)}', ${requestScope.cparam.longitude}, ${requestScope.cparam.latitude}, ${requestScope.cparam.zoomFactor}, '${requestScope.cparam.mapType}');
});
})(window);
</script>
</hst:headContribution>
......@@ -24,10 +36,10 @@
<hst:headContribution keyHint="google-maps-api" category="htmlBodyEnd">
<c:choose>
<c:when test="${not empty requestScope.cparam.apiKey}">
<c:set var="mapsUrl">https://maps.googleapis.com/maps/api/js?key=${fn:escapeXml(requestScope.cparam.apiKey)}&callback=initGoogleMaps</c:set>
<c:set var="mapsUrl">https://maps.googleapis.com/maps/api/js?key=${fn:escapeXml(requestScope.cparam.apiKey)}&callback=HippoEssentials.GoogleMaps.init</c:set>
</c:when>
<c:otherwise>
<c:set var="mapsUrl">https://maps.googleapis.com/maps/api/js?callback=initGoogleMaps</c:set>
<c:set var="mapsUrl">https://maps.googleapis.com/maps/api/js?callback=HippoEssentials.GoogleMaps.init</c:set>
</c:otherwise>
</c:choose>
<script type="text/javascript" src="${mapsUrl}" async="async" defer="defer"></script>
......@@ -35,8 +47,8 @@
<c:if test="${editMode}">
<script type="text/javascript">
if (window.initGoogleMap) {
initGoogleMap('map-canvas-${componentId}', '${fn:escapeXml(requestScope.cparam.address)}', ${requestScope.cparam.longitude}, ${requestScope.cparam.latitude}, ${requestScope.cparam.zoomFactor}, '${requestScope.cparam.mapType}');
if (window.HippoEssentials && window.HippoEssentials.GoogleMaps) {
window.HippoEssentials.GoogleMaps.render('map-canvas-${componentId}', '${fn:escapeXml(requestScope.cparam.address)}', ${requestScope.cparam.longitude}, ${requestScope.cparam.latitude}, ${requestScope.cparam.zoomFactor}, '${requestScope.cparam.mapType}');
}
</script>
</c:if>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment