Skip to content

Commit

Permalink
refacto(Controls): switch context menu management in StateControl
Browse files Browse the repository at this point in the history
  • Loading branch information
mgermerie committed Sep 14, 2021
1 parent 825841c commit 5fa010b
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 11 deletions.
7 changes: 0 additions & 7 deletions src/Controls/GlobeControls.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,6 @@ class GlobeControls extends THREE.EventDispatcher {

this._onEndingMove = null;
this._onMouseWheel = this.onMouseWheel.bind(this);
this._onContextMenuListener = this.onContextMenuListener.bind(this);
this._onTravel = this.travel.bind(this);
this._onTouchStart = this.onTouchStart.bind(this);
this._onTouchEnd = this.onTouchEnd.bind(this);
Expand All @@ -257,7 +256,6 @@ class GlobeControls extends THREE.EventDispatcher {
this.states.addEventListener(this.states.PAN._event, this._onPan, false);
this.states.addEventListener(this.states.PANORAMIC._event, this._onPanoramic, false);

this.view.domElement.addEventListener('contextmenu', this._onContextMenuListener, false);
this.view.domElement.addEventListener('wheel', this._onMouseWheel, false);
this.view.domElement.addEventListener('touchstart', this._onTouchStart, false);
this.view.domElement.addEventListener('touchend', this._onTouchEnd, false);
Expand Down Expand Up @@ -873,17 +871,12 @@ class GlobeControls extends THREE.EventDispatcher {
}
}

onContextMenuListener(event) {
event.preventDefault();
}

onTouchEnd() {
this.handleEndMovement({ previous: this.state });
this.state = this.states.NONE;
}

dispose() {
this.view.domElement.removeEventListener('contextmenu', this._onContextMenuListener, false);
this.view.domElement.removeEventListener('wheel', this._onMouseWheel, false);
this.view.domElement.removeEventListener('touchstart', this._onTouchStart, false);
this.view.domElement.removeEventListener('touchend', this._onTouchEnd, false);
Expand Down
8 changes: 8 additions & 0 deletions src/Controls/StateControl.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ class StateControl extends THREE.EventDispatcher {
this._onKeyUp = this.onKeyUp.bind(this);

this._onBlur = this.onBlur.bind(this);
this._onContextMenu = this.onContextMenu.bind(this);

this._domElement.addEventListener('pointerdown', this._onPointerDown, false);

Expand All @@ -164,6 +165,8 @@ class StateControl extends THREE.EventDispatcher {

// Reset key/mouse when window loose focus
window.addEventListener('blur', this._onBlur);
// disable context menu when right-clicking
this._domElement.addEventListener('contextmenu', this._onContextMenu, false);

// TODO : this shall be removed when switching keyboard management form Controls to StateControls
this._handleTravelInEvent = (event) => {
Expand Down Expand Up @@ -350,6 +353,10 @@ class StateControl extends THREE.EventDispatcher {
this.onPointerUp();
}

onContextMenu(event) {
event.preventDefault();
}

/**
* Remove all event listeners created within this instance of `StateControl`
*/
Expand All @@ -366,6 +373,7 @@ class StateControl extends THREE.EventDispatcher {
this._domElement.removeEventListener('keyup', this._onKeyUp, false);

window.removeEventListener('blur', this._onBlur);
this._domElement.removeEventListener('contextmenu', this._onContextMenu, false);

this._domElement.removeEventListener(this.TRAVEL_IN.trigger, this._handleTravelInEvent, false);
this._domElement.removeEventListener(this.TRAVEL_OUT.trigger, this._handleTravelInEvent, false);
Expand Down
4 changes: 0 additions & 4 deletions test/unit/globecontrol.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,6 @@ describe('GlobeControls', function () {
controls.onTouchMove(event);
});

it('onContextMenuListener', function () {
controls.onContextMenuListener(event);
});

it('lookAtCoordinate with animation', function (done) {
const rig = getRig(viewer.camera.camera3D);
let i;
Expand Down
4 changes: 4 additions & 0 deletions test/unit/statecontrol.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,10 @@ describe('StateControl', function () {
assert.ok(states.NONE === states.currentState);
});

it('context menu should not appear', function () {
states.onContextMenu(event);
});

it('should dispose event listeners', function () {
states.dispose();
});
Expand Down

0 comments on commit 5fa010b

Please sign in to comment.