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

View File

@@ -5,13 +5,10 @@ goog.provide('ol.control.Attribution');
goog.require('goog.array');
goog.require('goog.dom');
goog.require('goog.dom.TagName');
goog.require('goog.events');
goog.require('goog.object');
goog.require('goog.style');
goog.require('ol.Attribution');
goog.require('ol.FrameState');
goog.require('ol.MapEvent');
goog.require('ol.MapEventType');
goog.require('ol.TileRange');
goog.require('ol.control.Control');
goog.require('ol.css');
@@ -62,12 +59,6 @@ ol.control.Attribution = function(opt_options) {
*/
this.attributionElementRenderedVisible_ = {};
/**
* @private
* @type {?number}
*/
this.postrenderListenKey_ = null;
};
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) {
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
* @param {?ol.FrameState} frameState Frame state.

View File

@@ -2,6 +2,9 @@ goog.provide('ol.control.Control');
goog.provide('ol.control.ControlOptions');
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;
/**
* @private
* @type {?number}
*/
this.postrenderListenKey_ = null;
if (goog.isDef(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.
* 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_)) {
goog.dom.removeNode(this.element);
}
if (!goog.isNull(this.postrenderListenKey_)) {
goog.events.unlistenByKey(this.postrenderListenKey_);
this.postrenderListenKey_ = null;
}
this.map_ = map;
if (!goog.isNull(this.map_)) {
var target = goog.isDef(this.target_) ?
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);
}
}
};

View File

@@ -2,12 +2,9 @@ goog.provide('ol.control.Logo');
goog.require('goog.dom');
goog.require('goog.dom.TagName');
goog.require('goog.events');
goog.require('goog.object');
goog.require('goog.style');
goog.require('ol.FrameState');
goog.require('ol.MapEvent');
goog.require('ol.MapEventType');
goog.require('ol.control.Control');
goog.require('ol.css');
@@ -50,12 +47,6 @@ ol.control.Logo = function(opt_options) {
*/
this.logoElements_ = {};
/**
* @private
* @type {?number}
*/
this.postrenderListenKey_ = null;
};
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.
* @private

View File

@@ -9,8 +9,6 @@ goog.require('goog.events');
goog.require('goog.events.EventType');
goog.require('goog.style');
goog.require('ol.CoordinateFormatType');
goog.require('ol.MapEvent');
goog.require('ol.MapEventType');
goog.require('ol.Pixel');
goog.require('ol.Projection');
goog.require('ol.TransformFunction');
@@ -98,8 +96,7 @@ goog.inherits(ol.control.MousePosition, ol.control.Control);
/**
* @param {ol.MapEvent} mapEvent Map event.
* @protected
* @inheritDoc
*/
ol.control.MousePosition.prototype.handleMapPostrender = function(mapEvent) {
var frameState = mapEvent.frameState;
@@ -151,9 +148,7 @@ ol.control.MousePosition.prototype.setMap = function(map) {
goog.events.listen(viewport, goog.events.EventType.MOUSEMOVE,
this.handleMouseMove, false, this),
goog.events.listen(viewport, goog.events.EventType.MOUSEOUT,
this.handleMouseOut, false, this),
goog.events.listen(map, ol.MapEventType.POSTRENDER,
this.handleMapPostrender, false, this)
this.handleMouseOut, false, this)
];
}
};

View File

@@ -4,8 +4,6 @@ goog.provide('ol.control.ScaleLineUnits');
goog.require('goog.dom');
goog.require('goog.style');
goog.require('ol.FrameState');
goog.require('ol.MapEvent');
goog.require('ol.MapEventType');
goog.require('ol.ProjectionUnits');
goog.require('ol.TransformFunction');
goog.require('ol.control.Control');
@@ -65,12 +63,6 @@ ol.control.ScaleLine = function(opt_options) {
this.units_ = goog.isDef(options.units) ?
options.units : ol.control.ScaleLineUnits.METRIC;
/**
* @private
* @type {Array.<?number>}
*/
this.listenerKeys_ = null;
/**
* @private
* @type {boolean}
@@ -112,30 +104,11 @@ goog.inherits(ol.control.ScaleLine, ol.control.Control);
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
*/
ol.control.ScaleLine.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)) {
this.listenerKeys_ = [
goog.events.listen(map, ol.MapEventType.POSTRENDER,
this.handleMapPostrender, false, this)
];
}
ol.control.ScaleLine.prototype.handleMapPostrender = function(mapEvent) {
this.updateElement_(mapEvent.frameState);
};

View File

@@ -10,7 +10,6 @@ goog.require('goog.dom.TagName');
goog.require('goog.events');
goog.require('goog.fx.Dragger');
goog.require('goog.style');
goog.require('ol.MapEventType');
goog.require('ol.control.Control');
goog.require('ol.css');
@@ -74,12 +73,6 @@ ol.control.ZoomSlider = function(zoomSliderOptions) {
*/
this.direction_ = ol.control.ZoomSlider.direction.VERTICAL;
/**
* @private
* @type {Array.<?number>}
*/
this.mapListenerKeys_ = null;
/**
* @private
* @type {Array.<?number>}
@@ -157,31 +150,11 @@ ol.control.ZoomSlider.DEFAULT_MAX_RESOLUTION = 156543.0339;
ol.control.ZoomSlider.prototype.setMap = function(map) {
goog.base(this, 'setMap', map);
this.currentResolution_ = map.getView().getResolution();
this.initMapEventListeners_();
this.initSlider_();
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
* 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.
* @private
* @inheritDoc
*/
ol.control.ZoomSlider.prototype.handleMapPostRender_ = function(mapEvtObj) {
var res = mapEvtObj.frameState.view2DState.resolution;
ol.control.ZoomSlider.prototype.handleMapPostrender = function(mapEvent) {
var res = mapEvent.frameState.view2DState.resolution;
if (res !== this.currentResolution_) {
this.currentResolution_ = res;
this.positionThumbForResolution_(res);