Commit 582f24aa authored by Eric Duminil's avatar Eric Duminil
Browse files

Moving some code to utils.

parent 9df8543d
......@@ -31,6 +31,7 @@
<div id="side">
<div id="dataPanel" ></div>
</div>
<script src="script/utils.js" type="text/javascript"></script>
<script src="script/simstadt_openlayers.js" type="text/javascript"></script>
</body>
</html>
......@@ -102,7 +102,7 @@ var regionChooser = (function(){
var features = Array.from(kml_source.getFeatures());
// Sort projects
features.sort((a, b) => a.project.localeCompare(b.project));
features_by_project = groupBy(features, "project");
features_by_project = utils.groupBy(features, "project");
// Sort CityGMLs inside each project
Object.values(features_by_project).forEach(features => features.sort((a, b) => a.name.localeCompare(b.name)));
}
......@@ -370,13 +370,6 @@ var regionChooser = (function(){
}
}
groupBy = function(xs, key) {
return xs.reduce(function(rv, x) {
(rv[x[key]] = rv[x[key]] || []).push(x);
return rv;
}, {});
};
function displayHelp(){
dataPanel.empty();
dataPanel.append("<h2 class='info'>Welcome to Region Chooser!<br><br>\n");
......@@ -427,42 +420,8 @@ var regionChooser = (function(){
var wgs84Coords = geom.getLinearRing(0).getCoordinates();
var wktPolygon = "POLYGON((";
wktPolygon += wgs84Coords.map(lonLat => lonLat.join(" ")).join(", ");
publicScope.copyToClipboard(wktPolygon + "))");
}
// Copies a string to the clipboard. Must be called from within an
// event handler such as click. May return false if it failed, but
// this is not always possible. Browser support for Chrome 43+,
// Firefox 42+, Safari 10+, Edge and Internet Explorer 10+.
// Internet Explorer: The clipboard feature may be disabled by
// an administrator. By default a prompt is shown the first
// time the clipboard is used (per session).
// https://stackoverflow.com/a/33928558/6419007
publicScope.copyToClipboard = function(text) {
if (window.clipboardData && window.clipboardData.setData) {
// Internet Explorer-specific code path to prevent textarea being shown while dialog is visible.
return window.clipboardData.setData("Text", text);
}
else if (document.queryCommandSupported && document.queryCommandSupported("copy")) {
var textarea = document.createElement("textarea");
textarea.textContent = text;
textarea.style.position = "fixed"; // Prevent scrolling to bottom of page in Microsoft Edge.
document.body.appendChild(textarea);
textarea.select();
try {
document.execCommand("copy"); // Security exception may be thrown by some browsers.
dataPanel.append("<h2 class='ok'>Coordinates copied to clipboard!</h2><br/>\n");
return;
}
catch (ex) {
console.warn("Copy to clipboard failed.", ex);
return prompt("Copy to clipboard: Ctrl+C, Enter", text);
}
finally {
document.body.removeChild(textarea);
}
utils.copyToClipboard(wktPolygon + "))", dataPanel);
}
}
publicScope.showRepositoryName = function(path) {
document.getElementById("repo_path").textContent = path;
......
var utils = {
groupBy : function(xs, key) {
return xs.reduce(function(rv, x) {
(rv[x[key]] = rv[x[key]] || []).push(x);
return rv;
}, {});
},
// Copies a string to the clipboard. Must be called from within an
// event handler such as click. May return false if it failed, but
// this is not always possible. Browser support for Chrome 43+,
// Firefox 42+, Safari 10+, Edge and Internet Explorer 10+.
// Internet Explorer: The clipboard feature may be disabled by
// an administrator. By default a prompt is shown the first
// time the clipboard is used (per session).
// https://stackoverflow.com/a/33928558/6419007
copyToClipboard : function(text, log) {
if (window.clipboardData && window.clipboardData.setData) {
// Internet Explorer-specific code path to prevent textarea being shown while dialog is visible.
return window.clipboardData.setData("Text", text);
}
else if (document.queryCommandSupported && document.queryCommandSupported("copy")) {
var textarea = document.createElement("textarea");
textarea.textContent = text;
textarea.style.position = "fixed"; // Prevent scrolling to bottom of page in Microsoft Edge.
document.body.appendChild(textarea);
textarea.select();
try {
document.execCommand("copy"); // Security exception may be thrown by some browsers.
log.append("<h2 class='ok'>Coordinates copied to clipboard!</h2><br/>\n");
return;
}
catch (ex) {
console.warn("Copy to clipboard failed.", ex);
return prompt("Copy to clipboard: Ctrl+C, Enter", text);
}
finally {
document.body.removeChild(textarea);
}
}
}
};
\ No newline at end of file
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