From aec1c2c138ce1f5b8b5a65589ef88bef1d1436e0 Mon Sep 17 00:00:00 2001 From: Samuel Asensi Date: Fri, 12 Jul 2019 12:06:26 +0200 Subject: [PATCH] Fix SymbolItem#hitTestAll() Closes #1680 --- src/item/SymbolItem.js | 9 +++++++++ test/tests/SymbolItem.js | 15 +++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/item/SymbolItem.js b/src/item/SymbolItem.js index 437789f2..1a95a3a8 100644 --- a/src/item/SymbolItem.js +++ b/src/item/SymbolItem.js @@ -121,7 +121,16 @@ var SymbolItem = Item.extend(/** @lends SymbolItem# */{ }, _hitTestSelf: function(point, options, viewMatrix) { + // We need to call definition item hit test with `options.all` + // disabled, otherwise it would populate the array with its own + // matches. What we want instead is only returning one match per symbol + // item (#1680). So we store original matches array... + var all = options.all; + // ...we temporarily disable `options.all`... + delete options.all; var res = this._definition._item._hitTest(point, options, viewMatrix); + // ...then after hit testing, we restore the original matches array. + options.all = all; // TODO: When the symbol's definition is a path, should hitResult // contain information like HitResult#curve? if (res) diff --git a/test/tests/SymbolItem.js b/test/tests/SymbolItem.js index 9e3ba191..7eb82ba1 100644 --- a/test/tests/SymbolItem.js +++ b/test/tests/SymbolItem.js @@ -143,3 +143,18 @@ test('SymbolItem#bounds with #applyMatrix = false', function() { equals(function() { return placed.bounds; }, { x: 150, y: 150, width: 100, height: 100 }); }); + +test('SymbolItem#hitTestAll', function() { + var symbol = new SymbolDefinition( + new Path.Circle({ + center: [0, 0], + radius: 10, + fillColor: 'orange' + }) + ); + var symbolItem = symbol.place([50, 50]); + + var hitTestAll = symbolItem.hitTestAll([50, 50]); + equals(hitTestAll.length, 1); + equals(hitTestAll[0].item.id, symbolItem.id); +});