Skip to content

Commit

Permalink
refactor(FreehandMouseTool): Consistently access event element (#1032)
Browse files Browse the repository at this point in the history
* refactor(FreehandMouseTool): Consistently access `element` and other attributes on the eventData
  • Loading branch information
dlwire authored and JamesAPetts committed Aug 1, 2019
1 parent ab4867e commit e7099c3
Showing 1 changed file with 57 additions and 59 deletions.
116 changes: 57 additions & 59 deletions src/tools/annotation/FreehandMouseTool.js
Original file line number Diff line number Diff line change
Expand Up @@ -394,13 +394,9 @@ export default class FreehandMouseTool extends BaseAnnotationTool {
// The mouse location
lines.push(config.mouseLocation.handles.start);
}
drawJoinedLines(
context,
eventData.element,
data.handles.points[j],
lines,
{ color }
);
drawJoinedLines(context, element, data.handles.points[j], lines, {
color,
});
}
}

Expand Down Expand Up @@ -573,9 +569,8 @@ export default class FreehandMouseTool extends BaseAnnotationTool {
}

handleSelectedCallback(evt, toolData, handle, interactionType = 'mouse') {
const eventData = evt.detail;
const element = eventData.element;
const toolState = getToolState(eventData.element, this.name);
const { element } = evt.detail;
const toolState = getToolState(element, this.name);

if (handle.hasBoundingBox) {
// Use default move handler.
Expand Down Expand Up @@ -620,14 +615,14 @@ export default class FreehandMouseTool extends BaseAnnotationTool {
*/
_drawingMouseMoveCallback(evt) {
const eventData = evt.detail;
const element = eventData.element;
const toolState = getToolState(eventData.element, this.name);
const { currentPoints, element } = eventData;
const toolState = getToolState(element, this.name);

const config = this.configuration;
const currentTool = config.currentTool;

const data = toolState.data[currentTool];
const coords = eventData.currentPoints.canvas;
const coords = currentPoints.canvas;

// Set the mouseLocation handle
this._getMouseLocation(eventData);
Expand All @@ -649,7 +644,7 @@ export default class FreehandMouseTool extends BaseAnnotationTool {
}

// Force onImageRendered
external.cornerstone.updateImage(eventData.element);
external.cornerstone.updateImage(element);
}

/**
Expand Down Expand Up @@ -680,8 +675,9 @@ export default class FreehandMouseTool extends BaseAnnotationTool {

_drawingDrag(evt) {
const eventData = evt.detail;
const { element } = eventData;

const toolState = getToolState(eventData.element, this.name);
const toolState = getToolState(element, this.name);

const config = this.configuration;
const currentTool = config.currentTool;
Expand All @@ -695,7 +691,7 @@ export default class FreehandMouseTool extends BaseAnnotationTool {
this._dragging = true;

// Force onImageRendered
external.cornerstone.updateImage(eventData.element);
external.cornerstone.updateImage(element);
}

/**
Expand All @@ -706,19 +702,17 @@ export default class FreehandMouseTool extends BaseAnnotationTool {
* @returns {undefined}
*/
_drawingMouseUpCallback(evt) {
const eventData = evt.detail;
const { element } = evt.detail;

if (!this._dragging) {
return;
}

this._dragging = false;

const element = eventData.element;

const config = this.configuration;
const currentTool = config.currentTool;
const toolState = getToolState(eventData.element, this.name);
const toolState = getToolState(element, this.name);
const data = toolState.data[currentTool];

if (!freehandIntersect.end(data.handles.points) && data.canComplete) {
Expand All @@ -741,17 +735,17 @@ export default class FreehandMouseTool extends BaseAnnotationTool {
*/
_drawingMouseDownCallback(evt) {
const eventData = evt.detail;
const { buttons, currentPoints, element } = eventData;

if (!this.options.mouseButtonMask.includes(eventData.buttons)) {
if (!this.options.mouseButtonMask.includes(buttons)) {
return;
}

const element = eventData.element;
const coords = eventData.currentPoints.canvas;
const coords = currentPoints.canvas;

const config = this.configuration;
const currentTool = config.currentTool;
const toolState = getToolState(eventData.element, this.name);
const toolState = getToolState(element, this.name);
const data = toolState.data[currentTool];

const handleNearby = this._pointNearHandle(element, data, coords);
Expand All @@ -778,13 +772,13 @@ export default class FreehandMouseTool extends BaseAnnotationTool {
*/
_drawingTouchStartCallback(evt) {
const eventData = evt.detail;
const { currentPoints, element } = eventData;

const element = eventData.element;
const coords = eventData.currentPoints.canvas;
const coords = currentPoints.canvas;

const config = this.configuration;
const currentTool = config.currentTool;
const toolState = getToolState(eventData.element, this.name);
const toolState = getToolState(element, this.name);
const data = toolState.data[currentTool];

const handleNearby = this._pointNearHandle(element, data, coords);
Expand Down Expand Up @@ -835,9 +829,10 @@ export default class FreehandMouseTool extends BaseAnnotationTool {
* @returns {undefined}
*/
_drawingMouseDoubleClickCallback(evt) {
const eventData = evt.detail;
const { element } = evt.detail;

this.completeDrawing(element);

this.completeDrawing(eventData.element);
preventPropagation(evt);
}

Expand All @@ -849,9 +844,10 @@ export default class FreehandMouseTool extends BaseAnnotationTool {
* @returns {undefined}
*/
_drawingDoubleTapClickCallback(evt) {
const eventData = evt.detail;
const { element } = evt.detail;

this.completeDrawing(element);

this.completeDrawing(eventData.element);
preventPropagation(evt);
}

Expand All @@ -864,12 +860,13 @@ export default class FreehandMouseTool extends BaseAnnotationTool {
*/
_editMouseDragCallback(evt) {
const eventData = evt.detail;
const { element, buttons } = eventData;

if (!this.options.mouseButtonMask.includes(eventData.buttons)) {
if (!this.options.mouseButtonMask.includes(buttons)) {
return;
}

const toolState = getToolState(eventData.element, this.name);
const toolState = getToolState(element, this.name);

const config = this.configuration;
const data = toolState.data[config.currentTool];
Expand Down Expand Up @@ -900,7 +897,7 @@ export default class FreehandMouseTool extends BaseAnnotationTool {
}

// Update the image
external.cornerstone.updateImage(eventData.element);
external.cornerstone.updateImage(element);
}

/**
Expand All @@ -912,8 +909,9 @@ export default class FreehandMouseTool extends BaseAnnotationTool {
*/
_editTouchDragCallback(evt) {
const eventData = evt.detail;
const { element } = eventData;

const toolState = getToolState(eventData.element, this.name);
const toolState = getToolState(element, this.name);

const config = this.configuration;
const data = toolState.data[config.currentTool];
Expand Down Expand Up @@ -944,7 +942,7 @@ export default class FreehandMouseTool extends BaseAnnotationTool {
}

// Update the image
external.cornerstone.updateImage(eventData.element);
external.cornerstone.updateImage(element);
}

/**
Expand All @@ -970,16 +968,15 @@ export default class FreehandMouseTool extends BaseAnnotationTool {
*/
_editMouseUpCallback(evt) {
const eventData = evt.detail;

const element = eventData.element;
const toolState = getToolState(eventData.element, this.name);
const { element } = eventData;
const toolState = getToolState(element, this.name);

this._deactivateModify(element);

this._dropHandle(eventData, toolState);
this._endDrawing(element);

external.cornerstone.updateImage(eventData.element);
external.cornerstone.updateImage(element);
}

/**
Expand Down Expand Up @@ -1032,11 +1029,8 @@ export default class FreehandMouseTool extends BaseAnnotationTool {
_startDrawing(evt) {
const eventData = evt.detail;
const measurementData = this.createNewMeasurement(eventData);
const element = eventData.element;
const { element } = eventData;
const config = this.configuration;

this._referencedElement = element;

let interactionType;

if (evt.type === EVENTS.MOUSE_DOWN_ACTIVATE) {
Expand All @@ -1047,9 +1041,9 @@ export default class FreehandMouseTool extends BaseAnnotationTool {
this._activateDraw(element, interactionType);
this._getMouseLocation(eventData);

addToolState(eventData.element, this.name, measurementData);
addToolState(element, this.name, measurementData);

const toolState = getToolState(eventData.element, this.name);
const toolState = getToolState(element, this.name);

config.currentTool = toolState.data.length - 1;

Expand All @@ -1064,7 +1058,8 @@ export default class FreehandMouseTool extends BaseAnnotationTool {
* @returns {undefined}
*/
_addPoint(eventData) {
const toolState = getToolState(eventData.element, this.name);
const { currentPoints, element } = eventData;
const toolState = getToolState(element, this.name);

// Get the toolState from the last-drawn polygon
const config = this.configuration;
Expand All @@ -1074,13 +1069,13 @@ export default class FreehandMouseTool extends BaseAnnotationTool {
return;
}

const newHandleData = new FreehandHandleData(eventData.currentPoints.image);
const newHandleData = new FreehandHandleData(currentPoints.image);

// If this is not the first handle
if (data.handles.points.length) {
// Add the line from the current handle to the new handle
data.handles.points[config.currentHandle - 1].lines.push(
eventData.currentPoints.image
currentPoints.image
);
}

Expand All @@ -1091,8 +1086,8 @@ export default class FreehandMouseTool extends BaseAnnotationTool {
config.currentHandle += 1;

// Force onImageRendered to fire
external.cornerstone.updateImage(eventData.element);
this.fireModifiedEvent(eventData.element, data);
external.cornerstone.updateImage(element);
this.fireModifiedEvent(element, data);
}

/**
Expand All @@ -1106,7 +1101,7 @@ export default class FreehandMouseTool extends BaseAnnotationTool {
*/
_addPointPencilMode(eventData, points) {
const config = this.configuration;
const element = eventData.element;
const { element } = eventData;
const mousePoint = config.mouseLocation.handles.start;

const handleFurtherThanMinimumSpacing = handle =>
Expand Down Expand Up @@ -1206,8 +1201,8 @@ export default class FreehandMouseTool extends BaseAnnotationTool {
* @returns {Object}
*/
_pointNearHandleAllTools(eventData) {
const element = eventData.element;
const coords = eventData.currentPoints.canvas;
const { currentPoints, element } = eventData;
const coords = currentPoints.canvas;
const toolState = getToolState(element, this.name);

if (!toolState) {
Expand Down Expand Up @@ -1239,12 +1234,13 @@ export default class FreehandMouseTool extends BaseAnnotationTool {
* @returns {undefined}
*/
_getMouseLocation(eventData) {
const { currentPoints, image } = eventData;
// Set the mouseLocation handle
const config = this.configuration;

config.mouseLocation.handles.start.x = eventData.currentPoints.image.x;
config.mouseLocation.handles.start.y = eventData.currentPoints.image.y;
clipToBox(config.mouseLocation.handles.start, eventData.image);
config.mouseLocation.handles.start.x = currentPoints.image.x;
config.mouseLocation.handles.start.y = currentPoints.image.y;
clipToBox(config.mouseLocation.handles.start, image);
}

/**
Expand All @@ -1256,6 +1252,8 @@ export default class FreehandMouseTool extends BaseAnnotationTool {
* @returns {Boolean}
*/
_checkInvalidHandleLocation(data, eventData) {
const { element } = eventData;

if (data.handles.points.length < 2) {
return true;
}
Expand All @@ -1282,7 +1280,7 @@ export default class FreehandMouseTool extends BaseAnnotationTool {
*/
_checkHandlesPolygonMode(data, eventData) {
const config = this.configuration;
const element = eventData.element;
const { element } = eventData;
const mousePoint = config.mouseLocation.handles.start;
const points = data.handles.points;
let invalidHandlePlacement = false;
Expand Down Expand Up @@ -1343,7 +1341,7 @@ export default class FreehandMouseTool extends BaseAnnotationTool {
*/
_invalidHandlePencilMode(data, eventData) {
const config = this.configuration;
const element = eventData.element;
const { element } = eventData;
const mousePoint = config.mouseLocation.handles.start;
const points = data.handles.points;

Expand Down

0 comments on commit e7099c3

Please sign in to comment.