Skip to content

Commit

Permalink
cleanup: Fix JS code warning suggested by lgtm.com (projectmesa#1554)
Browse files Browse the repository at this point in the history
* Format js code

* Update InteractionHandler.js
  • Loading branch information
Tortar authored Dec 9, 2022
1 parent 336ae59 commit b09b608
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 18 deletions.
10 changes: 3 additions & 7 deletions mesa/visualization/templates/js/GridDraw.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const GridVisualization = function (
// Calls the appropriate shape(agent)
this.drawLayer = function (portrayalLayer) {
// Re-initialize the lookup table
interactionHandler ? interactionHandler.mouseoverLookupTable.init() : null;
if (interactionHandler) interactionHandler.mouseoverLookupTable.init();

for (const i in portrayalLayer) {
const p = portrayalLayer[i];
Expand All @@ -72,9 +72,7 @@ const GridVisualization = function (
p.y = gridHeight - p.y - 1;

// if a handler exists, add coordinates for the portrayalLayer index
interactionHandler
? interactionHandler.mouseoverLookupTable.set(p.x, p.y, i)
: null;
if (interactionHandler) interactionHandler.mouseoverLookupTable.set(p.x, p.y, i);

// If the stroke color is not defined, then the first color in the colors array is the stroke color.
if (!p.stroke_color) p.stroke_color = p.Color[0];
Expand Down Expand Up @@ -127,9 +125,7 @@ const GridVisualization = function (
this.drawCustomImage(p.Shape, p.x, p.y, p.scale, p.text, p.text_color);
}
// if a handler exists, update its mouse listeners with the new data
interactionHandler
? interactionHandler.updateMouseListeners(portrayalLayer)
: null;
if (interactionHandler) interactionHandler.updateMouseListeners(portrayalLayer);
};

// DRAWING METHODS
Expand Down
14 changes: 5 additions & 9 deletions mesa/visualization/templates/js/HexDraw.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ const HexVisualization = function (
const maxR = Math.min(cellHeight, cellWidth) / 2 - 1;

// Configure the interaction handler to use a hex coordinate mapper
interactionHandler ? interactionHandler.setCoordinateMapper("hex") : null;
if (interactionHandler) interactionHandler.setCoordinateMapper("hex");

// Calls the appropriate shape(agent)
this.drawLayer = function (portrayalLayer) {
// Re-initialize the lookup table
interactionHandler ? interactionHandler.mouseoverLookupTable.init() : null;
if (interactionHandler) interactionHandler.mouseoverLookupTable.init();
for (const i in portrayalLayer) {
const p = portrayalLayer[i];
// Does the inversion of y positioning because of html5
Expand All @@ -69,9 +69,7 @@ const HexVisualization = function (
p.y = gridHeight - p.y - 1;

// if a handler exists, add coordinates for the portrayalLayer index
interactionHandler
? interactionHandler.mouseoverLookupTable.set(p.x, p.y, i)
: null;
if (interactionHandler) interactionHandler.mouseoverLookupTable.set(p.x, p.y, i);

if (p.Shape == "hex")
this.drawHex(p.x, p.y, p.r, p.Color, p.Filled, p.text, p.text_color);
Expand All @@ -93,9 +91,7 @@ const HexVisualization = function (
this.drawCustomImage(p.Shape, p.x, p.y, p.scale, p.text, p.text_color);
}
// if a handler exists, update its mouse listeners with the new data
interactionHandler
? interactionHandler.updateMouseListeners(portrayalLayer)
: null;
if (interactionHandler) interactionHandler.updateMouseListeners(portrayalLayer);
};

// DRAWING METHODS
Expand Down Expand Up @@ -158,7 +154,7 @@ const HexVisualization = function (
} else {
cy = (y + 0.5) * cellHeight + cellHeight / 2;
}
maxHexRadius = cellHeight / Math.sqrt(3);
const maxHexRadius = cellHeight / Math.sqrt(3);
const r = radius * maxHexRadius;

function hex_corner(x, y, size, i) {
Expand Down
2 changes: 0 additions & 2 deletions mesa/visualization/templates/js/InteractionHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,6 @@ const InteractionHandler = function (width, height, gridWidth, gridHeight, ctx)

// map the event to x,y coordinates
const position = coordinateMapper(event);
const yPosition = Math.floor(event.offsetY / cellHeight);
const xPosition = Math.floor(event.offsetX / cellWidth);

// look up the portrayal items the coordinates refer to and draw a tooltip
mouseoverLookupTable
Expand Down

0 comments on commit b09b608

Please sign in to comment.