Handle unlistening in base class

This commit is contained in:
Tom Payne
2013-04-05 20:54:27 +02:00
parent 06bcab8374
commit 25eb18d2c8
3 changed files with 17 additions and 37 deletions

View File

@@ -48,10 +48,10 @@ ol.control.Control = function(controlOptions) {
this.map_ = null;
/**
* @private
* @type {?number}
* @protected
* @type {!Array.<?number>}
*/
this.postrenderListenKey_ = null;
this.listenerKeys = [];
if (goog.isDef(controlOptions.map)) {
this.setMap(controlOptions.map);
@@ -95,9 +95,9 @@ ol.control.Control.prototype.setMap = function(map) {
if (!goog.isNull(this.map_)) {
goog.dom.removeNode(this.element);
}
if (!goog.isNull(this.postrenderListenKey_)) {
goog.events.unlistenByKey(this.postrenderListenKey_);
this.postrenderListenKey_ = null;
if (!goog.array.isEmpty(this.listenerKeys)) {
goog.array.forEach(this.listenerKeys, goog.events.unlistenByKey);
this.listenerKeys.length = 0;
}
this.map_ = map;
if (!goog.isNull(this.map_)) {
@@ -105,8 +105,8 @@ ol.control.Control.prototype.setMap = function(map) {
this.target_ : map.getOverlayContainer();
goog.dom.appendChild(target, this.element);
if (this.handleMapPostrender !== goog.nullFunction) {
this.postrenderListenKey_ = goog.events.listen(map,
ol.MapEventType.POSTRENDER, this.handleMapPostrender, false, this);
this.listenerKeys.push(goog.events.listen(map,
ol.MapEventType.POSTRENDER, this.handleMapPostrender, false, this));
}
}
};

View File

@@ -42,12 +42,6 @@ ol.control.DragBox = function(dragBoxOptions) {
*/
this.startCoordinate_ = dragBoxOptions.startCoordinate;
/**
* @private
* @type {?number}
*/
this.dragListenKey_ = null;
goog.base(this, {
element: element,
map: dragBoxOptions.map
@@ -61,19 +55,15 @@ goog.inherits(ol.control.DragBox, ol.control.Control);
* @inheritDoc
*/
ol.control.DragBox.prototype.setMap = function(map) {
if (!goog.isNull(this.dragListenKey_)) {
goog.events.unlistenByKey(this.dragListenKey_);
this.dragListenKey_ = null;
}
goog.base(this, 'setMap', map);
if (!goog.isNull(map)) {
this.startPixel_ = map.getPixelFromCoordinate(this.startCoordinate_);
goog.asserts.assert(goog.isDef(this.startPixel_));
goog.style.setPosition(this.element, this.startPixel_);
goog.style.setBorderBoxSize(this.element, new ol.Size(0, 0));
this.dragListenKey_ = goog.events.listen(
map, ol.MapBrowserEvent.EventType.DRAG, this.updateBox_, false, this);
this.listenerKeys.push(goog.events.listen(
map, ol.MapBrowserEvent.EventType.DRAG, this.updateBox_, false, this));
}
goog.base(this, 'setMap', map);
};

View File

@@ -85,12 +85,6 @@ ol.control.MousePosition = function(opt_options) {
*/
this.lastMouseMovePixel_ = null;
/**
* @private
* @type {Array.<?number>}
*/
this.listenerKeys_ = null;
};
goog.inherits(ol.control.MousePosition, ol.control.Control);
@@ -137,19 +131,15 @@ ol.control.MousePosition.prototype.handleMouseOut = function(browserEvent) {
* @inheritDoc
*/
ol.control.MousePosition.prototype.setMap = function(map) {
if (!goog.isNull(this.listenerKeys_)) {
goog.array.forEach(this.listenerKeys_, goog.events.unlistenByKey);
this.listenerKeys_ = null;
}
goog.base(this, 'setMap', map);
if (!goog.isNull(map)) {
var viewport = map.getViewport();
this.listenerKeys_ = [
goog.events.listen(viewport, goog.events.EventType.MOUSEMOVE,
this.handleMouseMove, false, this),
goog.events.listen(viewport, goog.events.EventType.MOUSEOUT,
this.handleMouseOut, false, this)
];
this.listenerKeys.push(
goog.events.listen(viewport, goog.events.EventType.MOUSEMOVE,
this.handleMouseMove, false, this),
goog.events.listen(viewport, goog.events.EventType.MOUSEOUT,
this.handleMouseOut, false, this)
);
}
};