-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.html
More file actions
171 lines (151 loc) · 5.38 KB
/
index.html
File metadata and controls
171 lines (151 loc) · 5.38 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<html>
<head>
<script src="./node_modules/maplibre-gl/dist/maplibre-gl.js"></script>
<link href="./node_modules/maplibre-gl/dist/maplibre-gl.css" rel="stylesheet" />
<link href="./index.css" rel="stylesheet" />
</head>
<body>
<div id="map"></div>
<div id="popup">
<div><strong>Style:</strong> <span id="s-name"></span></div>
<div><strong>Style URL:</strong> <span id="s-url"></span></div>
<hr/>
<div id="layers"></div>
<button id="close" class="close">Close</button>
</div>
<script type="module">
import StyleFlipperControl from "./node_modules/maplibre-gl-style-flipper/index.js";
const mapStyles = {
"historical": {
code: "historical",
image: "./img/historical.png",
url: "./historical/historical.json"
},
"japanese_scroll": {
code: "japanese_scroll",
image: "./img/japanese_scroll.png",
url: "./japanese_scroll/japanese_scroll.json"
},
"railway": {
code: "railway",
image: "./img/railway.png",
url: "./railway/railway.json"
},
"woodblock": {
code: "woodblock",
image: "./img/woodblock.png",
url: "./woodblock/woodblock.json"
},
};
let map = new maplibregl.Map({
container: 'map',
style: mapStyles["historical"].url,
center: [-74.033, 40.6259],
zoom: 11.73,
hash: true
})
// Create an instance of StyleFlipperControl
const flipper = new StyleFlipperControl(mapStyles);
// Set the initial style code
flipper.setCurrentStyleCode("historical");
// Add the control to the map
map.addControl(flipper, "bottom-right");
const
p = document.getElementById("popup"),
sName = document.getElementById("s-name"),
sUrl = document.getElementById("s-url"),
layersEl = document.getElementById("layers"),
closeBtn = document.getElementById("close")
;
map.on("load", () => {
map.on("click", (e) => {
e.originalEvent.stopPropagation();
const cur = flipper.currentStyleCode || "historical";
sName.textContent = cur;
sUrl.textContent = mapStyles[cur].url || "";
layersEl.innerHTML = "";
const tol = 2;
const bbox = [[e.point.x - tol, e.point.y - tol], [e.point.x + tol, e.point.y + tol]];
const features = map.queryRenderedFeatures(bbox);
if (!features || features.length === 0) {
layersEl.textContent = "No rendered features at this point.";
} else {
const byLayer = {};
features.forEach(f => {
const id = (f.layer && f.layer.id) ? f.layer.id : "(no-layer)";
(byLayer[id] || (byLayer[id] = [])).push(f);
});
for (const lid of Object.keys(byLayer)) {
const wrapper = document.createElement("div");
const title = document.createElement("div");
title.className = "layer-title";
const left = document.createElement("div");
left.className = "layer-left";
const nameSpan = document.createElement("span");
nameSpan.textContent = `${lid} — ${byLayer[lid].length} feature(s)`;
left.appendChild(nameSpan);
const copyBtn = document.createElement("button");
copyBtn.className = "copy-btn";
copyBtn.type = "button";
copyBtn.innerText = "📋";
copyBtn.title = "Copy layer id";
copyBtn.dataset.layer = lid;
// copy handler
copyBtn.addEventListener("click", async (ev) => {
ev.stopPropagation(); // avoid closing popup
try {
await navigator.clipboard.writeText(lid);
const prev = copyBtn.innerText;
copyBtn.innerText = "Copied!";
setTimeout(() => copyBtn.innerText = prev, 1200);
} catch (err) {
copyBtn.innerText = "Err";
setTimeout(() => copyBtn.innerText = "📋", 1200);
}
});
left.appendChild(copyBtn);
title.appendChild(left);
// append features props (only properties) below
const propsDiv = document.createElement("div");
byLayer[lid].forEach((feat, i) => {
const pre = document.createElement("pre");
pre.textContent = `feature ${i} id:${feat.id ?? "n/a"}\n` + JSON.stringify(feat.properties || {}, null, 2);
propsDiv.appendChild(pre);
});
title.style.marginBottom = "6px";
wrapper.appendChild(title);
wrapper.appendChild(propsDiv);
layersEl.appendChild(wrapper);
}
}
p.style.left = (e.originalEvent.clientX + 10) + "px";
p.style.top = (e.originalEvent.clientY + 10) + "px";
p.style.display = "block";
});
});
// close handling: ignore clicks inside popup
document.addEventListener("click", (ev) => {
if (!p.contains(ev.target)) p.style.display = "none";
});
closeBtn.addEventListener("click", () => p.style.display = "none");
</script>
<script id="__bs_script__">//<![CDATA[
(function() {
try {
var script = document.createElement('script');
if ('async') {
script.async = true;
}
script.src = 'http://HOST:3001/browser-sync/browser-sync-client.js?v=3.0.4'.replace("HOST", location.hostname);
if (document.body) {
document.body.appendChild(script);
} else if (document.head) {
document.head.appendChild(script);
}
} catch (e) {
console.error("Browsersync: could not append script tag", e);
}
})()
//]]></script>
</body>
</html>