Move postrender event listening into base class

This commit is contained in:
Tom Payne
2013-04-05 20:33:50 +02:00
parent da1e5aadd3
commit 06bcab8374
6 changed files with 31 additions and 118 deletions
+1 -26
View File
@@ -5,13 +5,10 @@ goog.provide('ol.control.Attribution');
goog.require('goog.array'); goog.require('goog.array');
goog.require('goog.dom'); goog.require('goog.dom');
goog.require('goog.dom.TagName'); goog.require('goog.dom.TagName');
goog.require('goog.events');
goog.require('goog.object'); goog.require('goog.object');
goog.require('goog.style'); goog.require('goog.style');
goog.require('ol.Attribution'); goog.require('ol.Attribution');
goog.require('ol.FrameState'); goog.require('ol.FrameState');
goog.require('ol.MapEvent');
goog.require('ol.MapEventType');
goog.require('ol.TileRange'); goog.require('ol.TileRange');
goog.require('ol.control.Control'); goog.require('ol.control.Control');
goog.require('ol.css'); goog.require('ol.css');
@@ -62,12 +59,6 @@ ol.control.Attribution = function(opt_options) {
*/ */
this.attributionElementRenderedVisible_ = {}; this.attributionElementRenderedVisible_ = {};
/**
* @private
* @type {?number}
*/
this.postrenderListenKey_ = null;
}; };
goog.inherits(ol.control.Attribution, ol.control.Control); goog.inherits(ol.control.Attribution, ol.control.Control);
@@ -108,29 +99,13 @@ ol.control.Attribution.prototype.getTileSourceAttributions =
/** /**
* @param {ol.MapEvent} mapEvent Map event. * @inheritDoc
*/ */
ol.control.Attribution.prototype.handleMapPostrender = function(mapEvent) { ol.control.Attribution.prototype.handleMapPostrender = function(mapEvent) {
this.updateElement_(mapEvent.frameState); this.updateElement_(mapEvent.frameState);
}; };
/**
* @inheritDoc
*/
ol.control.Attribution.prototype.setMap = function(map) {
if (!goog.isNull(this.postrenderListenKey_)) {
goog.events.unlistenByKey(this.postrenderListenKey_);
this.postrenderListenKey_ = null;
}
goog.base(this, 'setMap', map);
if (!goog.isNull(map)) {
this.postrenderListenKey_ = goog.events.listen(
map, ol.MapEventType.POSTRENDER, this.handleMapPostrender, false, this);
}
};
/** /**
* @private * @private
* @param {?ol.FrameState} frameState Frame state. * @param {?ol.FrameState} frameState Frame state.
+23
View File
@@ -2,6 +2,9 @@ goog.provide('ol.control.Control');
goog.provide('ol.control.ControlOptions'); goog.provide('ol.control.ControlOptions');
goog.require('goog.Disposable'); goog.require('goog.Disposable');
goog.require('goog.events');
goog.require('ol.MapEvent');
goog.require('ol.MapEventType');
/** /**
@@ -44,6 +47,12 @@ ol.control.Control = function(controlOptions) {
*/ */
this.map_ = null; this.map_ = null;
/**
* @private
* @type {?number}
*/
this.postrenderListenKey_ = null;
if (goog.isDef(controlOptions.map)) { if (goog.isDef(controlOptions.map)) {
this.setMap(controlOptions.map); this.setMap(controlOptions.map);
} }
@@ -69,6 +78,12 @@ ol.control.Control.prototype.getMap = function() {
}; };
/**
* @param {ol.MapEvent} mapEvent Map event.
*/
ol.control.Control.prototype.handleMapPostrender = goog.nullFunction;
/** /**
* Removes the control from its current map and attaches it to the new map. * Removes the control from its current map and attaches it to the new map.
* Subtypes might also wish set up event handlers to get notified about changes * Subtypes might also wish set up event handlers to get notified about changes
@@ -80,10 +95,18 @@ ol.control.Control.prototype.setMap = function(map) {
if (!goog.isNull(this.map_)) { if (!goog.isNull(this.map_)) {
goog.dom.removeNode(this.element); goog.dom.removeNode(this.element);
} }
if (!goog.isNull(this.postrenderListenKey_)) {
goog.events.unlistenByKey(this.postrenderListenKey_);
this.postrenderListenKey_ = null;
}
this.map_ = map; this.map_ = map;
if (!goog.isNull(this.map_)) { if (!goog.isNull(this.map_)) {
var target = goog.isDef(this.target_) ? var target = goog.isDef(this.target_) ?
this.target_ : map.getOverlayContainer(); this.target_ : map.getOverlayContainer();
goog.dom.appendChild(target, this.element); goog.dom.appendChild(target, this.element);
if (this.handleMapPostrender !== goog.nullFunction) {
this.postrenderListenKey_ = goog.events.listen(map,
ol.MapEventType.POSTRENDER, this.handleMapPostrender, false, this);
}
} }
}; };
-25
View File
@@ -2,12 +2,9 @@ goog.provide('ol.control.Logo');
goog.require('goog.dom'); goog.require('goog.dom');
goog.require('goog.dom.TagName'); goog.require('goog.dom.TagName');
goog.require('goog.events');
goog.require('goog.object'); goog.require('goog.object');
goog.require('goog.style'); goog.require('goog.style');
goog.require('ol.FrameState'); goog.require('ol.FrameState');
goog.require('ol.MapEvent');
goog.require('ol.MapEventType');
goog.require('ol.control.Control'); goog.require('ol.control.Control');
goog.require('ol.css'); goog.require('ol.css');
@@ -50,12 +47,6 @@ ol.control.Logo = function(opt_options) {
*/ */
this.logoElements_ = {}; this.logoElements_ = {};
/**
* @private
* @type {?number}
*/
this.postrenderListenKey_ = null;
}; };
goog.inherits(ol.control.Logo, ol.control.Control); goog.inherits(ol.control.Logo, ol.control.Control);
@@ -68,22 +59,6 @@ ol.control.Logo.prototype.handleMapPostrender = function(mapEvent) {
}; };
/**
* @inheritDoc
*/
ol.control.Logo.prototype.setMap = function(map) {
if (!goog.isNull(this.postrenderListenKey_)) {
goog.events.unlistenByKey(this.postrenderListenKey_);
this.postrenderListenKey_ = null;
}
goog.base(this, 'setMap', map);
if (!goog.isNull(map)) {
this.postrenderListenKey_ = goog.events.listen(
map, ol.MapEventType.POSTRENDER, this.handleMapPostrender, false, this);
}
};
/** /**
* @param {?ol.FrameState} frameState Frame state. * @param {?ol.FrameState} frameState Frame state.
* @private * @private
+2 -7
View File
@@ -9,8 +9,6 @@ goog.require('goog.events');
goog.require('goog.events.EventType'); goog.require('goog.events.EventType');
goog.require('goog.style'); goog.require('goog.style');
goog.require('ol.CoordinateFormatType'); goog.require('ol.CoordinateFormatType');
goog.require('ol.MapEvent');
goog.require('ol.MapEventType');
goog.require('ol.Pixel'); goog.require('ol.Pixel');
goog.require('ol.Projection'); goog.require('ol.Projection');
goog.require('ol.TransformFunction'); goog.require('ol.TransformFunction');
@@ -98,8 +96,7 @@ goog.inherits(ol.control.MousePosition, ol.control.Control);
/** /**
* @param {ol.MapEvent} mapEvent Map event. * @inheritDoc
* @protected
*/ */
ol.control.MousePosition.prototype.handleMapPostrender = function(mapEvent) { ol.control.MousePosition.prototype.handleMapPostrender = function(mapEvent) {
var frameState = mapEvent.frameState; var frameState = mapEvent.frameState;
@@ -151,9 +148,7 @@ ol.control.MousePosition.prototype.setMap = function(map) {
goog.events.listen(viewport, goog.events.EventType.MOUSEMOVE, goog.events.listen(viewport, goog.events.EventType.MOUSEMOVE,
this.handleMouseMove, false, this), this.handleMouseMove, false, this),
goog.events.listen(viewport, goog.events.EventType.MOUSEOUT, goog.events.listen(viewport, goog.events.EventType.MOUSEOUT,
this.handleMouseOut, false, this), this.handleMouseOut, false, this)
goog.events.listen(map, ol.MapEventType.POSTRENDER,
this.handleMapPostrender, false, this)
]; ];
} }
}; };
+2 -29
View File
@@ -4,8 +4,6 @@ goog.provide('ol.control.ScaleLineUnits');
goog.require('goog.dom'); goog.require('goog.dom');
goog.require('goog.style'); goog.require('goog.style');
goog.require('ol.FrameState'); goog.require('ol.FrameState');
goog.require('ol.MapEvent');
goog.require('ol.MapEventType');
goog.require('ol.ProjectionUnits'); goog.require('ol.ProjectionUnits');
goog.require('ol.TransformFunction'); goog.require('ol.TransformFunction');
goog.require('ol.control.Control'); goog.require('ol.control.Control');
@@ -65,12 +63,6 @@ ol.control.ScaleLine = function(opt_options) {
this.units_ = goog.isDef(options.units) ? this.units_ = goog.isDef(options.units) ?
options.units : ol.control.ScaleLineUnits.METRIC; options.units : ol.control.ScaleLineUnits.METRIC;
/**
* @private
* @type {Array.<?number>}
*/
this.listenerKeys_ = null;
/** /**
* @private * @private
* @type {boolean} * @type {boolean}
@@ -112,30 +104,11 @@ goog.inherits(ol.control.ScaleLine, ol.control.Control);
ol.control.ScaleLine.LEADING_DIGITS = [1, 2, 5]; ol.control.ScaleLine.LEADING_DIGITS = [1, 2, 5];
/**
* @param {ol.MapEvent} mapEvent Map event.
*/
ol.control.ScaleLine.prototype.handleMapPostrender = function(mapEvent) {
var frameState = mapEvent.frameState;
this.updateElement_(mapEvent.frameState);
};
/** /**
* @inheritDoc * @inheritDoc
*/ */
ol.control.ScaleLine.prototype.setMap = function(map) { ol.control.ScaleLine.prototype.handleMapPostrender = function(mapEvent) {
if (!goog.isNull(this.listenerKeys_)) { this.updateElement_(mapEvent.frameState);
goog.array.forEach(this.listenerKeys_, goog.events.unlistenByKey);
this.listenerKeys_ = null;
}
goog.base(this, 'setMap', map);
if (!goog.isNull(map)) {
this.listenerKeys_ = [
goog.events.listen(map, ol.MapEventType.POSTRENDER,
this.handleMapPostrender, false, this)
];
}
}; };
+3 -31
View File
@@ -10,7 +10,6 @@ goog.require('goog.dom.TagName');
goog.require('goog.events'); goog.require('goog.events');
goog.require('goog.fx.Dragger'); goog.require('goog.fx.Dragger');
goog.require('goog.style'); goog.require('goog.style');
goog.require('ol.MapEventType');
goog.require('ol.control.Control'); goog.require('ol.control.Control');
goog.require('ol.css'); goog.require('ol.css');
@@ -74,12 +73,6 @@ ol.control.ZoomSlider = function(zoomSliderOptions) {
*/ */
this.direction_ = ol.control.ZoomSlider.direction.VERTICAL; this.direction_ = ol.control.ZoomSlider.direction.VERTICAL;
/**
* @private
* @type {Array.<?number>}
*/
this.mapListenerKeys_ = null;
/** /**
* @private * @private
* @type {Array.<?number>} * @type {Array.<?number>}
@@ -157,31 +150,11 @@ ol.control.ZoomSlider.DEFAULT_MAX_RESOLUTION = 156543.0339;
ol.control.ZoomSlider.prototype.setMap = function(map) { ol.control.ZoomSlider.prototype.setMap = function(map) {
goog.base(this, 'setMap', map); goog.base(this, 'setMap', map);
this.currentResolution_ = map.getView().getResolution(); this.currentResolution_ = map.getView().getResolution();
this.initMapEventListeners_();
this.initSlider_(); this.initSlider_();
this.positionThumbForResolution_(this.currentResolution_); this.positionThumbForResolution_(this.currentResolution_);
}; };
/**
* Initializes the event listeners for map events.
*
* @private
*/
ol.control.ZoomSlider.prototype.initMapEventListeners_ = function() {
if (!goog.isNull(this.mapListenerKeys_)) {
goog.array.forEach(this.mapListenerKeys_, goog.events.unlistenByKey);
this.mapListenerKeys_ = null;
}
if (!goog.isNull(this.getMap())) {
this.mapListenerKeys_ = [
goog.events.listen(this.getMap(), ol.MapEventType.POSTRENDER,
this.handleMapPostRender_, undefined, this)
];
}
};
/** /**
* Initializes the slider element. This will determine and set this controls * Initializes the slider element. This will determine and set this controls
* direction_ and also constrain the dragging of the thumb to always be within * direction_ and also constrain the dragging of the thumb to always be within
@@ -217,11 +190,10 @@ ol.control.ZoomSlider.prototype.initSlider_ = function() {
/** /**
* @param {ol.MapEvent} mapEvtObj The ol.MapEvent object. * @inheritDoc
* @private
*/ */
ol.control.ZoomSlider.prototype.handleMapPostRender_ = function(mapEvtObj) { ol.control.ZoomSlider.prototype.handleMapPostrender = function(mapEvent) {
var res = mapEvtObj.frameState.view2DState.resolution; var res = mapEvent.frameState.view2DState.resolution;
if (res !== this.currentResolution_) { if (res !== this.currentResolution_) {
this.currentResolution_ = res; this.currentResolution_ = res;
this.positionThumbForResolution_(res); this.positionThumbForResolution_(res);