Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Track tilemap event handlers and unbind at destroy #3774

Merged
merged 2 commits into from
May 7, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 36 additions & 21 deletions src/kibana/components/vislib/visualizations/tile_map.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ define(function (require) {

// create a new maps array
self.maps = [];
self.popups = [];

var worldBounds = L.latLngBounds([-90, -220], [90, 220]);

Expand Down Expand Up @@ -158,23 +159,29 @@ define(function (require) {
self.addLabel(mapData.properties.label, map);
}

// 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('<a class="leaflet-control-zoom fa fa-crop" title="Fit Data Bounds"></a>');
$(container).on('click', function () {
self.fitBounds(map, featureLayer);
});
return container;
}
});
var fitContainer = L.DomUtil.create('div', 'leaflet-control leaflet-bar leaflet-control-zoom leaflet-control-fit');

if (mapData && mapData.features.length > 0) {
map.addControl(new FitControl());
// Add button to fit container to points
var FitControl = L.Control.extend({
options: {
position: 'topleft'
},
onAdd: function (map) {
$(fitContainer).html('<a class="leaflet-control-zoom fa fa-crop" title="Fit Data Bounds"></a>');
$(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);
Expand Down Expand Up @@ -517,6 +524,8 @@ define(function (require) {
.on('mouseout', function (e) {
layer.closePopup();
});

this.popups.push({elem: popup, layer: layer});
};

/**
Expand Down Expand Up @@ -617,16 +626,22 @@ 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('*');
if (map.fitControl) {
map.fitControl.removeFrom(map);
}
map.remove();
children.remove();
});
}

};

return TileMap;
Expand Down