Skip to content

Commit d92c958

Browse files
authored
Simplify hover_text, display_text and link_url interaction (#36)
* Simplify `hover_text`, `display_text` and `link_url` interaction Closes: #30 * Extract links layer out from hovertext
1 parent 23c1b36 commit d92c958

File tree

3 files changed

+50
-65
lines changed

3 files changed

+50
-65
lines changed

src/globe/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import { GlobalZoomLayer } from "./layers/globalzoom";
3030
import { AmbientLightLayer } from "./layers/ambientlight";
3131
import { OrbitControlsLayer } from "./layers/orbitcontrols";
3232
import { NewCameraPositionsLayer } from "./layers/newcamerapositions";
33+
import { LinkDataObjectsLayer } from "./layers/links";
3334

3435
// Configures the registry
3536
// WARN: All the layers should be added here!
@@ -54,6 +55,7 @@ for (const layer of [
5455
new ArcsLayer(),
5556
new CustomObjectLayerGroup(),
5657
new HoverTextObjectsLayer(),
58+
new LinkDataObjectsLayer(),
5759
new LabeledObjectsLayer(),
5860
new CirclesLayer(),
5961
new PointersLayer(),

src/globe/layers/hovertext.ts

Lines changed: 22 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,13 @@ import { registerDialogContainer } from "../../components";
66
import { PointData } from "../../data";
77
// eslint-disable-next-line @typescript-eslint/no-unused-vars
88
import { isHoverTextData, HoverTextData } from "../../data/hover";
9-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
10-
import { isLinkData, LinkData } from "../../data/link";
119
import { GlobeLayerAttachHook } from "../layer";
12-
import { isLabelsData } from "../../data/label";
13-
import {
14-
addClickListener,
15-
addMouseEventListener,
16-
MouseInteractionLayer,
17-
} from "./mouseevents";
10+
import { addMouseEventListener, MouseInteractionLayer } from "./mouseevents";
1811

1912
/**
20-
* Globe layer for all objects that implement {@link HoverTextData} or {@link LinkData}.
21-
*
22-
* If an object also has a label attached, then hover window is opened on click only.
23-
* If both hover text and link are provided, link is available when clicking on the opened hover window.
13+
* Globe layer for all objects that implement {@link HoverTextData}.
2414
*
25-
* Implements {@link MouseInteractionLayer} to provide hover and click functionality.
15+
* Implements {@link MouseInteractionLayer} to provide hover.
2616
*/
2717
export class HoverTextObjectsLayer
2818
implements
@@ -37,8 +27,6 @@ export class HoverTextObjectsLayer
3727
private hoverWindow: HTMLDivElement | null = null;
3828
private hoverWindowPosition = new THREE.Vector3();
3929
private hoverLabelText: HTMLParagraphElement | null = null;
40-
private urlToOpen: string | null = null;
41-
private openUrlInNewWindow: boolean = true;
4230

4331
attachToGlobe(
4432
_globe: ThreeGlobe,
@@ -62,19 +50,6 @@ export class HoverTextObjectsLayer
6250
`;
6351
this.hoverWindow =
6452
hoverDialogContainer.querySelector<HTMLDivElement>("#hoverDialog");
65-
const dialogArea =
66-
hoverDialogContainer.querySelector<HTMLDivElement>("#hoverDialogArea")!;
67-
dialogArea.addEventListener("click", (_event: Event) => {
68-
if (this.urlToOpen) {
69-
window.open(
70-
this.urlToOpen,
71-
this.openUrlInNewWindow ? "_blank" : "_self",
72-
);
73-
}
74-
if (this.hoverWindow) {
75-
this.hoverWindow.hidden = true;
76-
}
77-
});
7853
this.hoverLabelText =
7954
hoverDialogContainer.querySelector<HTMLParagraphElement>(
8055
"#hoverDialogText",
@@ -83,43 +58,25 @@ export class HoverTextObjectsLayer
8358

8459
buildObject(parent: Object3D, object: PointData): void {
8560
if (isHoverTextData(object) && object.hover_text) {
86-
addMouseEventListener(
87-
isLabelsData(object) && object.display_text ? "click" : "hover",
88-
parent,
89-
() => {
90-
this.hadMouseHits = true;
91-
if (this.hoverLabelText) {
92-
this.hoverLabelText.innerText = object.hover_text ?? "";
93-
}
94-
if (this.hoverWindow) {
95-
parent.getWorldPosition(this.hoverWindowPosition);
96-
this.hoverWindow.hidden = false;
97-
this.hoverWindowPosition.project(this.cachedCamera!);
98-
this.hoverWindowPosition.x =
99-
(this.hoverWindowPosition.x * window.innerWidth) / 2 +
100-
window.innerWidth / 2;
101-
this.hoverWindowPosition.y =
102-
-((this.hoverWindowPosition.y * window.innerHeight) / 2) +
103-
window.innerHeight / 2;
104-
this.hoverWindow.style.top =
105-
this.hoverWindowPosition.y.toString() + "px";
106-
this.hoverWindow.style.left =
107-
this.hoverWindowPosition.x.toString() + "px";
108-
if (isLinkData(object) && object.link_url) {
109-
this.urlToOpen = object.link_url;
110-
this.openUrlInNewWindow =
111-
object.new_window == true || object.new_window == null;
112-
}
113-
}
114-
},
115-
);
116-
} else if (isLinkData(object) && object.link_url) {
117-
addClickListener(parent, () => {
118-
if (object.link_url) {
119-
window.open(
120-
object.link_url,
121-
object.new_window === false ? "_self" : "_blank",
122-
);
61+
addMouseEventListener("hover", parent, () => {
62+
this.hadMouseHits = true;
63+
if (this.hoverLabelText) {
64+
this.hoverLabelText.innerText = object.hover_text ?? "";
65+
}
66+
if (this.hoverWindow) {
67+
parent.getWorldPosition(this.hoverWindowPosition);
68+
this.hoverWindow.hidden = false;
69+
this.hoverWindowPosition.project(this.cachedCamera!);
70+
this.hoverWindowPosition.x =
71+
(this.hoverWindowPosition.x * window.innerWidth) / 2 +
72+
window.innerWidth / 2;
73+
this.hoverWindowPosition.y =
74+
-((this.hoverWindowPosition.y * window.innerHeight) / 2) +
75+
window.innerHeight / 2;
76+
this.hoverWindow.style.top =
77+
this.hoverWindowPosition.y.toString() + "px";
78+
this.hoverWindow.style.left =
79+
this.hoverWindowPosition.x.toString() + "px";
12380
}
12481
});
12582
}

src/globe/layers/links.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { Object3D } from "three";
2+
import { CustomObjectLayerBuildHook } from "./customobject";
3+
import { PointData } from "../../data";
4+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
5+
import { isLinkData, LinkData } from "../../data/link";
6+
import { addClickListener } from "./mouseevents";
7+
8+
/**
9+
* Globe layer for all objects that implement {@link LinkData}.
10+
*/
11+
export class LinkDataObjectsLayer implements CustomObjectLayerBuildHook {
12+
readonly layerName: string = "LinkData";
13+
14+
buildObject(parent: Object3D, object: PointData): void {
15+
if (isLinkData(object) && object.link_url) {
16+
addClickListener(parent, () => {
17+
if (object.link_url) {
18+
window.open(
19+
object.link_url,
20+
object.new_window === false ? "_self" : "_blank",
21+
);
22+
}
23+
});
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)