Improved: Move page-specific script links to html template (OFBIZ-11799)

Created new importLibrary js function to load OpenLayer js and css
Tested on
https://localhost:8443/example/control/ExampleGeoLocationPointSet4
https://localhost:8443/example/control/ExampleOsmGeoLocationPointSet4
This commit is contained in:
James Yong
2020-08-15 11:57:58 +08:00
parent aaa5d9e9a5
commit c4b5407eb2
2 changed files with 89 additions and 54 deletions
@@ -16,11 +16,6 @@ KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<#-- Define OpenLayers js and css tags to be added to html head tag when multi-block=true. -->
<script data-import="head" type="application/javascript"
src="/common/js/plugins/OpenLayers-5.3.0.js"></script>
<link rel="stylesheet" type="text/css"
href="/common/js/plugins/OpenLayers-5.3.0.css"/>
<#if geoChart?has_content>
<#-- ================================= Golbal Init ======================================-->
<#if geoChart.id?has_content>
@@ -120,62 +115,65 @@ under the License.
<#elseif "GEOPT_OSM" == geoChart.dataSourceId>
<div id="${id}" class="map" style="border:1px solid #979797; background-color:#e5e3df; width:${geoChart.width}; height:${geoChart.height}; margin:2em auto;"></div>
<script type="application/javascript">
var iconFeatures=[];
importLibrary(["/common/js/plugins/OpenLayers-5.3.0.js", "/common/js/plugins/OpenLayers-5.3.0.css"],
function() {
var iconFeatures=[];
<#if geoChart.points?has_content>
<#list geoChart.points as point>
iconFeatures.push(
new ol.Feature({
geometry: new ol.geom.Point(ol.proj.transform([${point.lon},${point.lat}],
'EPSG:4326', 'EPSG:900913'))
})
);
</#list>
</#if>
<#if geoChart.points?has_content>
<#list geoChart.points as point>
iconFeatures.push(
new ol.Feature({
geometry: new ol.geom.Point(ol.proj.transform([${point.lon},${point.lat}],
'EPSG:4326', 'EPSG:900913'))
})
);
</#list>
</#if>
var vectorSource = new ol.source.Vector({
features: iconFeatures
});
var vectorSource = new ol.source.Vector({
features: iconFeatures
});
var iconStyle = new ol.style.Style({
image: new ol.style.Icon(({
anchor: [0.5, 25],
anchorXUnits: 'fraction',
anchorYUnits: 'pixels',
opacity: 0.75,
src: '<@ofbizContentUrl>/images/img/marker.png</@ofbizContentUrl>'
}))
});
var iconStyle = new ol.style.Style({
image: new ol.style.Icon(({
anchor: [0.5, 25],
anchorXUnits: 'fraction',
anchorYUnits: 'pixels',
opacity: 0.75,
src: '<@ofbizContentUrl>/images/img/marker.png</@ofbizContentUrl>'
}))
});
var vectorLayer = new ol.layer.Vector({
source: vectorSource,
style: iconStyle
});
var vectorLayer = new ol.layer.Vector({
source: vectorSource,
style: iconStyle
});
var map = new ol.Map({
target: '${id}',
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
var map = new ol.Map({
target: '${id}',
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
}),
vectorLayer
],
view: new ol.View({
center: ol.proj.fromLonLat([${center.lon}, ${center.lat}]),
zoom: ${zoom}
}),
vectorLayer
],
view: new ol.View({
center: ol.proj.fromLonLat([${center.lon}, ${center.lat}]),
zoom: ${zoom}
}),
controls: [
new ol.control.Zoom(),
new ol.control.ZoomSlider(),
new ol.control.ZoomToExtent(),
new ol.control.OverviewMap(),
new ol.control.ScaleLine(),
new ol.control.FullScreen()
]
});
controls: [
new ol.control.Zoom(),
new ol.control.ZoomSlider(),
new ol.control.ZoomToExtent(),
new ol.control.OverviewMap(),
new ol.control.ScaleLine(),
new ol.control.FullScreen()
]
});
// fit to show all markers (optional)
map.getView().fit(vectorSource.getExtent(), map.getSize());
// fit to show all markers (optional)
map.getView().fit(vectorSource.getExtent(), map.getSize());
});
</script>
</#if>
@@ -1418,3 +1418,40 @@ function sendJWT(targetUrl) {
});
}
}
var importLibrary = function() {
var importLibraryFiles = new Map();
return function (urls, onSuccessFn, onErrorFn) {
function cachedScript(url, options) {
// Allow user to set any option except for dataType, cache, and url
options = $.extend(options || {}, {
dataType: "script",
cache: true,
url: url
});
// Use $.ajax() since it is more flexible than $.getScript
// Return the jqXHR object so we can chain callbacks
return jQuery.ajax(options);
};
jQuery.when.apply(jQuery,
jQuery.map(urls, function (url) {
if (!importLibraryFiles.has(url)) {
var deferObj = (url.endsWith(".css") ?
jQuery.get(url, function (css) {
jQuery("<style>" + css + "</style>").appendTo("head");
}
) :
cachedScript(url));
importLibraryFiles.set(url, deferObj);
return deferObj;
} else {
return importLibraryFiles.get(url);
}
})
).then(onSuccessFn).catch(onErrorFn || function () {
alert('Error loading one of the files: \n' + urls.join('\n'))
});
}
}();