utils.js 2.08 KB
Newer Older
Eric Duminil's avatar
Eric Duminil committed
1
2
var utils = {};

3
4
5
6
utils.groupBy = function (xs, key) {
	return xs.reduce(function (rv, x) {
		(rv[x[key]] = rv[x[key]] || []).push(x);
		return rv;
Eric Duminil's avatar
Eric Duminil committed
7
8
	}, {});
}
9

Eric Duminil's avatar
Eric Duminil committed
10
11
12
13
14
15
16
17
// 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
18
utils.copyToClipboard = function (text, log) {
Eric Duminil's avatar
Eric Duminil committed
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
	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);
Eric Duminil's avatar
Eric Duminil committed
37
		}
Eric Duminil's avatar
Eric Duminil committed
38
39
		finally {
			document.body.removeChild(textarea);
Eric Duminil's avatar
Eric Duminil committed
40
		}
Eric Duminil's avatar
Eric Duminil committed
41
42
	}
}
43
44

utils.read_kml = function (url) {
Eric Duminil's avatar
Eric Duminil committed
45
	return new ol.source.KML({
46
47
48
49
		projection: ol.proj.get('EPSG:3857'),
		url: url,
		extractAttributes: false,
		extractStyles: false
Eric Duminil's avatar
Eric Duminil committed
50
51
	});
}
52
53
54
55
56
57

utils.is_polygon = function (geom) {
	return ('getType' in geom) && (geom.getType() === 'Polygon');
}

utils.polygon_style = function (color, alpha) {
Eric Duminil's avatar
Eric Duminil committed
58
	return new ol.style.Style({
59
60
		fill: new ol.style.Fill({
			color: 'rgba(255, 255, 255,' + alpha + ')'
Eric Duminil's avatar
Eric Duminil committed
61
		}),
62
63
64
65
		stroke: new ol.style.Stroke({
			color: color,
			width: 2,
			lineDash: [5, 10]
Eric Duminil's avatar
Eric Duminil committed
66
67
68
		}),
	});
}