-
Notifications
You must be signed in to change notification settings - Fork 0
/
scale.js
86 lines (66 loc) · 1.92 KB
/
scale.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// scale.js
// Martin Pravda
// Scale UI component for selecting and changing map scale
/*jslint browser */
import make_ui from "./ui.js";
import dom from "./dom.js";
const default_scales = [
500,
1000,
2500,
5000,
10000,
20000,
50000,
100000,
250000,
1000000
];
const scale_rx = /^[1:]?[:.,\-?"'\\]*(\d+)/g;
const scale_ui = make_ui("map-scale", function (element, {
on_scale_changed,
scales = default_scales
}) {
const shadow = element.attachShadow({mode: "closed"});
function parse_scale(scale) {
const [ignore, scale_nr] = scale.split(":");
return Number(scale_nr);
}
function scalify(scale_nr) {
return `1:${scale_nr}`;
}
function sanitize_scale(scale) {
if (scale_rx.test(scale) === false) {
return select_element.value;
}
return scale.replace(scale_rx, function (ignore, scale_group) {
return scalify(scale_group);
});
}
const scale_element = dom("input", {
onchange: function on_change(event) {
event.preventDefault();
const value = event.target.value?.trim();
const scale = sanitize_scale(value);
event.target.value = scale;
// since value is a string we need to cast it to the number type
on_scale_changed(parse_scale(scale));
},
type: "text"
});
const select_element = dom("select", {
onchange: function on_select_change(event) {
scale_element.value = event.target.value;
on_scale_changed(parse_scale(scale_element.value));
}
}, scales.map(function (scale) {
return dom("option", [scalify(scale)]);
}));
shadow.append(scale_element, select_element);
});
//demo import demo from "./demo.js";
//demo demo(scale_ui({
//demo on_scale_changed: function (scale) {
//demo console.log(scale);
//demo }
//demo }));