Skip to content

Commit ab796a5

Browse files
committed
Remove event handlers when not using ruler
1 parent 25ac708 commit ab796a5

File tree

1 file changed

+35
-26
lines changed

1 file changed

+35
-26
lines changed

client/src/components/geoJS/layers/measureToolLayer.ts

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ export default class MeasureToolLayer extends BaseTextLayer<TextData> {
2222
dragging: boolean;
2323
yValue: number;
2424

25+
moveHandler: (e: GeoEvent) => void;
26+
mousedownHandler: (e: GeoEvent) => void;
27+
mouseupHandler: () => void;
28+
2529
constructor(
2630
// eslint-disable-next-line @typescript-eslint/no-explicit-any
2731
geoViewerRef: any,
@@ -60,26 +64,17 @@ export default class MeasureToolLayer extends BaseTextLayer<TextData> {
6064
if (this.rulerOn) {
6165
this.enableDrawing();
6266
}
63-
}
6467

65-
enableDrawing() {
66-
this.rulerOn = true;
67-
// Frequency ruler
68-
this.lineAnnotation = this.frequencyRulerLayer.createFeature('line')
69-
.data([[
70-
{x: 0, y: this.yValue},
71-
{x: this.spectroInfo.width, y: this.yValue},
72-
]])
73-
.style(this.createLineStyle());
74-
this.pointAnnotation = this.frequencyRulerLayer.createFeature('point')
75-
.data([{x: 0, y: this.yValue}])
76-
.style(this.createPointStyle());
77-
this.geoViewerRef.geoOn(geo.event.mousedown, (e: GeoEvent) => {
68+
this.moveHandler = (e: GeoEvent) => {
69+
if (e && this.dragging) {
70+
this.updateRuler(e.mouse.geo.y);
71+
}
72+
};
73+
this.mousedownHandler = (e: GeoEvent) => {
7874
const gcs = this.geoViewerRef.displayToGcs(e.map);
7975
const p = this.pointAnnotation.data()[0];
80-
const dx = gcs.x - p.x;
81-
const dy = gcs.y - p.y;
82-
// TODO figure out how to do screen pixels for this
76+
const dx = Math.abs(gcs.x - p.x);
77+
const dy = Math.abs(gcs.y - p.y);
8378
if (Math.sqrt(dx*dx + dy*dy) < 20 || dy < 10) {
8479
this.geoViewerRef.interactor().addAction({
8580
action: 'dragpoint',
@@ -89,17 +84,29 @@ export default class MeasureToolLayer extends BaseTextLayer<TextData> {
8984
});
9085
this.dragging = true;
9186
}
92-
});
93-
this.geoViewerRef.geoOn(geo.event.actionmove, (e: GeoEvent) => {
94-
if (this.dragging) {
95-
this.updateRuler(e.mouse.geo.y);
96-
}
97-
});
98-
this.geoViewerRef.geoOn(geo.event.mouseup, () => {
87+
};
88+
this.mouseupHandler = () => {
9989
this.dragging = false;
10090
this.geoViewerRef.interactor().removeAction(undefined, undefined, 'MeasureToolLayer');
10191
this.updateRuler(this.yValue);
102-
});
92+
};
93+
}
94+
95+
enableDrawing() {
96+
this.rulerOn = true;
97+
// Frequency ruler
98+
this.lineAnnotation = this.frequencyRulerLayer.createFeature('line')
99+
.data([[
100+
{x: 0, y: this.yValue},
101+
{x: this.spectroInfo.width, y: this.yValue},
102+
]])
103+
.style(this.createLineStyle());
104+
this.pointAnnotation = this.frequencyRulerLayer.createFeature('point')
105+
.data([{x: 0, y: this.yValue}])
106+
.style(this.createPointStyle());
107+
this.geoViewerRef.geoOn(geo.event.mousedown, this.mousedownHandler);
108+
this.geoViewerRef.geoOn(geo.event.actionmove, this.moveHandler);
109+
this.geoViewerRef.geoOn(geo.event.mouseup, this.mouseupHandler);
103110
this.frequencyRulerLayer.draw();
104111
this.updateRuler(this.yValue);
105112
}
@@ -117,7 +124,6 @@ export default class MeasureToolLayer extends BaseTextLayer<TextData> {
117124
}
118125

119126
updateRuler(newY: number) {
120-
// this.setTextColor();
121127
if (newY < 0) {
122128
return;
123129
}
@@ -155,6 +161,9 @@ export default class MeasureToolLayer extends BaseTextLayer<TextData> {
155161
this.textData = [];
156162
this.textLayer.data(this.textData).draw();
157163
this.clearRulerLayer();
164+
this.geoViewerRef.geoOff(geo.event.mousedown, this.mousedownHandler);
165+
this.geoViewerRef.geoOff(geo.event.mouseup, this.mouseupHandler);
166+
this.geoViewerRef.geoOff(geo.event.actionmove, this.moveHandler);
158167
}
159168

160169
clearRulerLayer() {

0 commit comments

Comments
 (0)