From 9873587ab37ea1463f767acc0dbc184985bd71d5 Mon Sep 17 00:00:00 2001 From: Rashid Khan Date: Wed, 6 May 2015 13:47:14 -0700 Subject: [PATCH 1/2] Track event handlers and unbind at destroy --- .../vislib/visualizations/tile_map.js | 33 +++++++++++++------ 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/src/kibana/components/vislib/visualizations/tile_map.js b/src/kibana/components/vislib/visualizations/tile_map.js index e4e17717756dd..22fd3300b07b3 100644 --- a/src/kibana/components/vislib/visualizations/tile_map.js +++ b/src/kibana/components/vislib/visualizations/tile_map.js @@ -56,6 +56,7 @@ define(function (require) { // create a new maps array self.maps = []; + self.popups = []; var worldBounds = L.latLngBounds([-90, -220], [90, 220]); @@ -158,23 +159,28 @@ define(function (require) { self.addLabel(mapData.properties.label, map); } + var fitContainer = L.DomUtil.create('div', 'leaflet-control leaflet-bar leaflet-control-zoom leaflet-control-fit'); + // Add button to fit container to points var FitControl = L.Control.extend({ options: { position: 'topleft' }, onAdd: function (map) { - var container = L.DomUtil.create('div', 'leaflet-control leaflet-bar leaflet-control-zoom leaflet-control-fit'); - $(container).html(''); - $(container).on('click', function () { + $(fitContainer).html(''); + $(fitContainer).on('click', function () { self.fitBounds(map, featureLayer); }); - return container; + return fitContainer; + }, + onRemove: function (map) { + $(fitContainer).off('click'); } }); + map.fitControl = new FitControl(); if (mapData && mapData.features.length > 0) { - map.addControl(new FitControl()); + map.addControl(map.fitControl); } self.maps.push(map); @@ -517,6 +523,8 @@ define(function (require) { .on('mouseout', function (e) { layer.closePopup(); }); + + this.popups.push({elem: popup, layer: layer}); }; /** @@ -617,13 +625,18 @@ define(function (require) { * @return {undefined} */ TileMap.prototype.destroy = function () { - if (this.maps && this.maps.length) { + if (this.popups) { + this.popups.forEach(function (popup) { + popup.elem.off('mouseover').off('mouseout'); + popup.layer.unbindPopup(popup.elem); + }); + this.popups = []; + } + + if (this.maps) { this.maps.forEach(function (map) { - // Cleanup hanging DOM nodes - // TODO: The correct way to handle this is to ensure all listeners are properly removed - var children = $(map._container).find('*'); + map.removeControl(map.fitControl); map.remove(); - children.remove(); }); } From cd232be39ebdb449852d0ff49b8b09df464ae0f7 Mon Sep 17 00:00:00 2001 From: Rashid Khan Date: Thu, 7 May 2015 11:22:31 -0700 Subject: [PATCH 2/2] Check for existence of control before destroying it --- .../vislib/visualizations/tile_map.js | 42 ++++++++++--------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/src/kibana/components/vislib/visualizations/tile_map.js b/src/kibana/components/vislib/visualizations/tile_map.js index 22fd3300b07b3..84c07ac9eea3f 100644 --- a/src/kibana/components/vislib/visualizations/tile_map.js +++ b/src/kibana/components/vislib/visualizations/tile_map.js @@ -161,26 +161,27 @@ define(function (require) { var fitContainer = L.DomUtil.create('div', 'leaflet-control leaflet-bar leaflet-control-zoom leaflet-control-fit'); - // Add button to fit container to points - var FitControl = L.Control.extend({ - options: { - position: 'topleft' - }, - onAdd: function (map) { - $(fitContainer).html(''); - $(fitContainer).on('click', function () { - self.fitBounds(map, featureLayer); - }); - return fitContainer; - }, - onRemove: function (map) { - $(fitContainer).off('click'); - } - }); - map.fitControl = new FitControl(); - if (mapData && mapData.features.length > 0) { + // Add button to fit container to points + var FitControl = L.Control.extend({ + options: { + position: 'topleft' + }, + onAdd: function (map) { + $(fitContainer).html(''); + $(fitContainer).on('click', function () { + self.fitBounds(map, featureLayer); + }); + return fitContainer; + }, + onRemove: function (map) { + $(fitContainer).off('click'); + } + }); + map.fitControl = new FitControl(); map.addControl(map.fitControl); + } else { + map.fitControl = undefined; } self.maps.push(map); @@ -635,11 +636,12 @@ define(function (require) { if (this.maps) { this.maps.forEach(function (map) { - map.removeControl(map.fitControl); + if (map.fitControl) { + map.fitControl.removeFrom(map); + } map.remove(); }); } - }; return TileMap;