Skip to content

Commit

Permalink
Address issue chrisrzhou#44
Browse files Browse the repository at this point in the history
  • Loading branch information
tgmof committed Dec 31, 2020
1 parent 9d8e913 commit 02e27bc
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 8 deletions.
8 changes: 8 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Change Log

## [5.0.3](https://github.com/chrisrzhou/react-globe/compare/v5.0.3...v5.0.2) (2020-12-31)

Small enhancement.
Add onTouch event for mobile use.
| Status | Old | New |
| --- | --- | --- |
| added | | `onTouchMarker` |

## [5.0.2](https://github.com/chrisrzhou/react-globe/compare/v5.0.2...v5.0.1) (2020-08-08)

Various bugfixes.
Expand Down
2 changes: 2 additions & 0 deletions docs/props.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ interface Props {
width?: string | number;
/** Callback to handle click events of a marker. Captures the clicked marker, ThreeJS object and pointer event. */
onClickMarker?: MarkerCallback;
/** Callback to handle touch events of a marker. Captures the touched marker, ThreeJS object and pointer event. */
onTouchMarker?: MarkerCallback;
/** Callback to handle defocus events (i.e. clicking the globe after a focus has been applied). Captures the previously focused coordinates. */
onDefocus?: (previousFocus: Coordinates) => void;
/** Capture the initialized globe instance */
Expand Down
11 changes: 11 additions & 0 deletions docs/usage/callbacks.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ import ReactGlobe from './react-globe';
setDetails(`City: ${marker.city} (Sales: ${marker.value}.0M)`);
}

function onTouchMarker(marker, markerObject, event) {
setEvent({
type: 'TOUCH',
marker,
markerObjectID: markerObject.uuid,
pointerEventPosition: { x: event.clientX, y: event.clientY },
});
setDetails(`City: ${marker.city} (Sales: ${marker.value}.0M)`);
}

function onDefocus(previousCoordinates) {
setEvent({
type: 'DEFOCUS',
Expand Down Expand Up @@ -59,6 +69,7 @@ import ReactGlobe from './react-globe';
<div style={{ position: 'relative' }}>
<ReactGlobe
onClickMarker={onClickMarker}
onTouchMarker={onTouchMarker}
onDefocus={onDefocus}
onMouseOutMarker={onMouseOutMarker}
onMouseOverMarker={onMouseOverMarker}
Expand Down
3 changes: 3 additions & 0 deletions lib/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export default function ReactGlobe({
options = defaultOptions,
width = '100%',
onClickMarker,
onTouchMarker,
onDefocus,
onGetGlobe,
onGlobeBackgroundTextureLoaded,
Expand Down Expand Up @@ -73,6 +74,7 @@ export default function ReactGlobe({
const globe = globeRef.current;
globe.updateCallbacks({
onClickMarker,
onTouchMarker,
onDefocus,
onGlobeBackgroundTextureLoaded,
onGlobeCloudsTextureLoaded,
Expand All @@ -82,6 +84,7 @@ export default function ReactGlobe({
});
}, [
onClickMarker,
onTouchMarker,
onDefocus,
onGlobeBackgroundTextureLoaded,
onGlobeCloudsTextureLoaded,
Expand Down
15 changes: 8 additions & 7 deletions lib/defaults.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { BACKGROUND_RADIUS_SCALE, MarkerTypes } from './enums';

export const defaultCallbacks = {
onClickMarker: (_marker, _markerObject, _event) => {},
onDefocus: _previousFocus => {},
onGlobeBackgroundTextureLoaded: () => {},
onGlobeCloudsTextureLoaded: () => {},
onGlobeTextureLoaded: () => {},
onMouseOutMarker: (_marker, _markerObject, _event) => {},
onMouseOverMarker: (_marker, _markerObject, _event) => {},
onClickMarker: (_marker, _markerObject, _event) => { },
onTouchMarker: (_marker, _markerObject, _event) => { },
onDefocus: _previousFocus => { },
onGlobeBackgroundTextureLoaded: () => { },
onGlobeCloudsTextureLoaded: () => { },
onGlobeTextureLoaded: () => { },
onMouseOutMarker: (_marker, _markerObject, _event) => { },
onMouseOverMarker: (_marker, _markerObject, _event) => { },
};

export const defaultGlobeBackgroundTexture =
Expand Down
11 changes: 11 additions & 0 deletions lib/globe.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,17 @@ export default class Globe {
this.updateFocus(marker.coordinates);
this.callbacks.onClickMarker(marker, markerObject, event);
},
onTouchMarker: (marker, markerObject, event) => {
this.updateFocus(marker.coordinates);
if (this.options.enableMarkerTooltip) {
this.tooltip.show(
event.clientX,
event.clientY,
this.options.markerTooltipRenderer(markerObject.marker),
);
}
this.callbacks.onTouchMarker(marker, markerObject, event);
},
onMouseOutMarker: (marker, markerObject, event) => {
this.tooltip.hide();
this.callbacks.onMouseOutMarker(marker, markerObject, event);
Expand Down
4 changes: 4 additions & 0 deletions lib/markers.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ function applyCallbacks(markerObject, callbacks) {
const event = interaction.data.originalEvent;
callbacks.onClickMarker(marker, markerObject, event);
});
markerObject.on('touch', interaction => {
const event = interaction.data.originalEvent;
callbacks.onTouchMarker(marker, markerObject, event);
});
markerObject.on('mousemove', interaction => {
const event = interaction.data.originalEvent;
callbacks.onMouseOverMarker(marker, markerObject, event);
Expand Down
3 changes: 2 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,12 @@ function MyGlobe() {
options={options}
width="100%"
onClickMarker={(marker, markerObject, event) => console.log(marker, markerObject, event)}
onTouchMarker={(marker, markerObject, event) => console.log(marker, markerObject, event)}
onGetGlobe={setGlobe}
onMouseOutMarker={(marker, markerObject, event) => console.log(marker, markerObject, event)}
onGlobeTextureLoaded={() => console.log('globe loaded')}
onMouseOverMarker={(marker, markerObject, event) => console.log(marker, markerObject, event)}
>
/>
)
}
```
Expand Down
3 changes: 3 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface Animation {

export interface Callbacks {
onClickMarker: MarkerCallback;
onTouchMarker: MarkerCallback;
onDefocus: (previousFocus: Coordinates | null) => void;
onGlobeBackgroundTextureLoaded: () => void;
onGlobeCloudsTextureLoaded: () => void;
Expand Down Expand Up @@ -211,6 +212,8 @@ export interface Props {
width?: string | number;
/** Callback to handle click events of a marker. Captures the clicked marker, ThreeJS object and pointer event. */
onClickMarker?: MarkerCallback;
/** Callback to handle touch events of a marker. Captures the touched marker, ThreeJS object and pointer event. */
onTouchMarker?: MarkerCallback;
/** Callback to handle defocus events (i.e. clicking the globe after a focus has been applied). Captures the previously focused coordinates. */
onDefocus?: (previousFocus: Coordinates) => void;
/** Capture the initialized globe instance */
Expand Down

0 comments on commit 02e27bc

Please sign in to comment.