Skip to content

Commit 4af79bb

Browse files
committed
Remove dead tooltip code
1 parent 169f395 commit 4af79bb

File tree

7 files changed

+146
-144
lines changed

7 files changed

+146
-144
lines changed

examples/standalone/gpd_polygons.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@
88

99
m.set_opacity("geopandas", 0.5)
1010
# m.set_visible("geopandas", False)
11-
11+
m.add_call("addOverlay", (9.5, 51.31667), "Kassel", "background-color: white; padding: 5px; border-radius: 4px;")
12+
m.set_center((9.5, 51.31667))
1213
m.save()

src/openlayers/js/openlayers.anywidget.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/openlayers/js/openlayers.standalone.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// See also https://openlayers.org/en/latest/examples/tooltip-on-hover.html
2+
3+
import { type Map } from "ol";
4+
import { type FeatureLike } from "ol/Feature";
5+
import Overlay from "ol/Overlay";
6+
7+
type FeatureProps = {
8+
[x: string]: any;
9+
}
10+
11+
function createElement(cssText?: string | undefined): HTMLElement {
12+
const el = document.createElement("div");
13+
el.id = "ol-tooltip";
14+
el.style.cssText = cssText || "padding: 5px; background-color: #333; color: #fff; border-radius: 4px; z-index: 100;"
15+
el.style.visibility = "hidden";
16+
return el;
17+
}
18+
19+
function getFeatureProperties(feature: FeatureLike): FeatureProps {
20+
let { geometry, ...props } = feature.getProperties();
21+
return props;
22+
}
23+
24+
function addTooltipTo(map: Map, prop: string): void {
25+
let el = createElement();
26+
const overlay = new Overlay({ element: el });
27+
map.addOverlay(overlay);
28+
let currentFeature: FeatureLike | undefined;
29+
map.on('pointermove', (e) => {
30+
if (e.dragging)
31+
return;
32+
const feature = map.forEachFeatureAtPixel(e.pixel, (feature) => {
33+
return feature;
34+
});
35+
if (feature) {
36+
el.style.visibility = "visible";
37+
overlay.setPosition(e.coordinate);
38+
if (feature !== currentFeature) {
39+
console.log("feature props", getFeatureProperties(feature));
40+
el.innerHTML = feature.get(prop)?.toString() || "";
41+
}
42+
} else {
43+
el.style.visibility = "hidden";
44+
}
45+
currentFeature = feature;
46+
});
47+
48+
map.getTargetElement().addEventListener("pointerleave", () => {
49+
el.style.visibility = "hidden";
50+
currentFeature = undefined;
51+
});
52+
}
53+
54+
export { addTooltipTo }

srcjs/ipywidget-ts/map.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import GeoJSON from "ol/format/GeoJSON";
44
import Overlay from "ol/Overlay";
55
import { fromLonLat, transformExtent, useGeographic } from "ol/proj";
66
import { JSONConverter } from "./json";
7-
import { addTooltip2 } from "./tooltip2";
7+
import { addTooltipToMap } from "./tooltip";
88

99
// --- Types
1010
import type Layer from "ol/layer/Layer";
@@ -224,17 +224,18 @@ export default class MapWidget {
224224
}
225225
}
226226

227-
// TODO: Test only at the moment
228-
addOverlay(position: Coordinate | undefined): void {
227+
// ...
228+
addOverlay(position: Coordinate | undefined, html: string, cssText: string | undefined, id: string = "ol-overlay"): void {
229229
const el = document.createElement("div");
230-
el.style.cssText = "";
231-
el.innerHTML = "We are out here."
230+
el.id = id;
231+
el.style.cssText = cssText || "";
232+
el.innerHTML = html;
232233
const overlay = new Overlay({ element: el, position: position });
233234
this._map.addOverlay(overlay);
234235
}
235236

236237
// ...
237238
addTooltip(template: string | null): void {
238-
addTooltip2(this._map, template);
239+
addTooltipToMap(this._map, template);
239240
}
240241
}

srcjs/ipywidget-ts/tooltip.ts

Lines changed: 81 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,101 @@
1-
// See also https://openlayers.org/en/latest/examples/tooltip-on-hover.html
1+
/*
2+
taken from https://openlayers.org/en/latest/examples/tooltip-on-hover.html
3+
*/
4+
import { Map } from "ol";
5+
import type { FeatureLike } from "ol/Feature";
6+
import type { Pixel } from "ol/pixel";
27

3-
import { type Map } from "ol";
4-
import { type FeatureLike } from "ol/Feature";
5-
import Overlay from "ol/Overlay";
8+
import mustache from "mustache";
69

7-
type FeatureProps = {
8-
[x: string]: any;
9-
}
10+
import { getFeatureProperties } from "./utils";
1011

11-
function createElement(cssText?: string | undefined): HTMLElement {
12-
const el = document.createElement("div");
13-
el.id = "ol-tooltip";
14-
el.style.cssText = cssText || "padding: 5px; background-color: #333; color: #fff; border-radius: 4px; z-index: 100;"
15-
el.style.visibility = "hidden";
16-
return el;
12+
/*
13+
#info {
14+
position: absolute;
15+
display: inline-block;
16+
height: auto;
17+
width: auto;
18+
z-index: 100;
19+
background-color: #333;
20+
color: #fff;
21+
text-align: center;
22+
border-radius: 4px;
23+
padding: 5px;
24+
left: 50%;
25+
transform: translateX(3%);
26+
visibility: hidden;
27+
pointer-events: none;
1728
}
29+
*/
30+
31+
const info = document.createElement("div");
32+
info.style.position = "absolute";
33+
info.style.display = "inline-block";
34+
info.style.height = "auto";
35+
info.style.width = "auto";
36+
info.style.zIndex = "100";
37+
info.style.backgroundColor = "#333";
38+
info.style.color = "#fff";
39+
// info.style.textAlign = "center";
40+
info.style.borderRadius = "4px";
41+
info.style.padding = "7px";
42+
info.style.left = "50%";
43+
// info.style.transform = "translateX(3%)";
44+
info.style.visibility = "hidden";
45+
info.style.pointerEvents = "none";
46+
47+
function renderFeatureProperties(feature: FeatureLike, template: string | null): string {
48+
const properties = getFeatureProperties(feature);
49+
if (template)
50+
return mustache.render(template, properties);
1851

19-
function getFeatureProperties(feature: FeatureLike): FeatureProps {
20-
let { geometry, ...props } = feature.getProperties();
21-
return props;
52+
return Object.keys(properties).map((key) => `${key}: ${properties[key]}`).join("</br>");
2253
}
2354

24-
function addTooltipTo(map: Map, prop: string): void {
25-
let el = createElement();
26-
const overlay = new Overlay({ element: el });
27-
map.addOverlay(overlay);
55+
function addTooltipToMap(map: Map, template: string | null): void {
56+
info.id = "ol-tooltip";
57+
map.getTargetElement().appendChild(info);
58+
console.log("tooltip element added", info);
59+
2860
let currentFeature: FeatureLike | undefined;
29-
map.on('pointermove', (e) => {
30-
if (e.dragging)
31-
return;
32-
const feature = map.forEachFeatureAtPixel(e.pixel, (feature) => {
33-
return feature;
34-
});
61+
const displayFeatureInfo = function (pixel: Pixel, target: EventTarget | null) {
62+
// @ts-expect-error
63+
const feature = target.closest('.ol-control')
64+
? undefined
65+
: map.forEachFeatureAtPixel(pixel, function (feature) {
66+
return feature;
67+
});
3568
if (feature) {
36-
el.style.visibility = "visible";
37-
overlay.setPosition(e.coordinate);
69+
// console.log("feature props", getFeatureProperties(feature));
70+
info.style.left = (pixel[0] + 15) + 'px';
71+
info.style.top = pixel[1] + 'px';
3872
if (feature !== currentFeature) {
39-
console.log("feature props", getFeatureProperties(feature));
40-
el.innerHTML = feature.get(prop)?.toString() || "";
73+
info.style.visibility = 'visible';
74+
info.innerHTML = renderFeatureProperties(feature, template);
4175
}
4276
} else {
43-
el.style.visibility = "hidden";
77+
info.style.visibility = 'hidden';
4478
}
4579
currentFeature = feature;
80+
};
81+
82+
map.on('pointermove', function (evt) {
83+
if (evt.dragging) {
84+
info.style.visibility = 'hidden';
85+
currentFeature = undefined;
86+
return;
87+
}
88+
displayFeatureInfo(evt.pixel, evt.originalEvent.target);
89+
});
90+
91+
map.on('click', function (evt) {
92+
displayFeatureInfo(evt.pixel, evt.originalEvent.target);
4693
});
4794

48-
map.getTargetElement().addEventListener("pointerleave", () => {
49-
el.style.visibility = "hidden";
95+
map.getTargetElement().addEventListener('pointerleave', function () {
5096
currentFeature = undefined;
97+
info.style.visibility = 'hidden';
5198
});
5299
}
53100

54-
export { addTooltipTo }
101+
export { addTooltipToMap };

srcjs/ipywidget-ts/tooltip2.ts

Lines changed: 0 additions & 101 deletions
This file was deleted.

0 commit comments

Comments
 (0)