Add view abstraction

This commit is contained in:
Éric Lemoine
2013-01-04 17:07:20 +01:00
parent 21331d1065
commit 927cffb2b7
34 changed files with 934 additions and 690 deletions

View File

@@ -2,6 +2,7 @@
// FIXME handle date line wrap
// FIXME handle layer order
// FIXME check clean-up code
// FIXME works for View2D only
goog.provide('ol.control.Attribution');
@@ -14,6 +15,8 @@ goog.require('goog.style');
goog.require('ol.Collection');
goog.require('ol.CoverageArea');
goog.require('ol.TileCoverageArea');
goog.require('ol.View2D');
goog.require('ol.View2DProperty');
goog.require('ol.control.Control');
goog.require('ol.layer.Layer');
@@ -33,12 +36,6 @@ ol.control.Attribution = function(attributionOptions) {
'class': 'ol-attribution'
}, this.ulElement_);
/**
* @private
* @type {Array.<number>}
*/
this.layersListenerKeys_ = null;
/**
* @private
* @type {Object.<number, ?number>}
@@ -63,6 +60,18 @@ ol.control.Attribution = function(attributionOptions) {
*/
this.mapListenerKeys_ = null;
/**
* @private
* @type {Array.<number>}
*/
this.layersListenerKeys_ = null;
/**
* @private
* @type {Array.<number>}
*/
this.viewListenerKeys_ = null;
goog.base(this, {
element: element,
map: attributionOptions.map,
@@ -110,14 +119,17 @@ ol.control.Attribution.prototype.createAttributionElementsForLayer_ =
var map = this.getMap();
var mapIsDef = map.isDef();
var mapExtent = /** @type {ol.Extent} */ map.getExtent();
var mapProjection = /** @type {ol.Projection} */ map.getProjection();
var mapResolution = /** @type {number} */ map.getResolution();
var layerVisible = layer.getVisible();
var attributionVisibilities;
if (mapIsDef && layerVisible) {
var mapSize = /** @type {ol.Size} */ (map.getSize());
// FIXME works for View2D only
var view = map.getView();
goog.asserts.assert(view instanceof ol.View2D);
var mapExtent = view.getExtent(mapSize);
var mapProjection = /** @type {ol.Projection} */ (view.getProjection());
var mapResolution = /** @type {number} */ (view.getResolution());
attributionVisibilities = this.getLayerAttributionVisiblities_(
layer, mapExtent, mapResolution, mapProjection);
} else {
@@ -131,7 +143,7 @@ ol.control.Attribution.prototype.createAttributionElementsForLayer_ =
var attributionElement = goog.dom.createElement(goog.dom.TagName.LI);
attributionElement.innerHTML = attribution.getHtml();
if (!map.isDef ||
if (!mapIsDef ||
!layerVisible ||
goog.isNull(attributionVisibilities) ||
!attributionVisibilities[attributionKey]) {
@@ -151,8 +163,8 @@ ol.control.Attribution.prototype.createAttributionElementsForLayer_ =
/**
* @param {ol.layer.Layer} layer Layer.
* @param {ol.Extent} mapExtent Map extent.
* @param {number} mapResolution Map resolution.
* @param {ol.Extent} mapExtent View extent.
* @param {number} mapResolution View resolution.
* @param {ol.Projection} mapProjection Map projection.
* @return {Object.<number, boolean>} Attribution visibilities.
* @private
@@ -240,17 +252,8 @@ ol.control.Attribution.prototype.handleLayerLoad = function(event) {
* @protected
*/
ol.control.Attribution.prototype.handleLayerVisibleChanged = function(event) {
var map = this.getMap();
var mapIsDef = map.isDef();
var mapExtent = /** @type {ol.Extent} */ map.getExtent();
var mapProjection = /** @type {ol.Projection} */ map.getProjection();
var mapResolution = /** @type {number} */ map.getResolution();
var layer = /** @type {ol.layer.Layer} */ (event.target);
this.updateLayerAttributionsVisibility_(
layer, mapIsDef, mapExtent, mapResolution, mapProjection);
this.updateLayerAttributionsVisibility_(layer);
};
@@ -279,20 +282,26 @@ ol.control.Attribution.prototype.handleLayersRemove =
/**
* @protected
*/
ol.control.Attribution.prototype.handleMapChanged = function() {
ol.control.Attribution.prototype.handleMapViewChanged = function() {
if (!goog.isNull(this.viewListenerKeys_)) {
goog.array.forEach(this.viewListenerKeys_, goog.events.unlistenByKey);
this.viewListenerKeys_ = null;
}
var map = this.getMap();
var mapIsDef = map.isDef();
var mapExtent = /** @type {ol.Extent} */ map.getExtent();
var mapProjection = /** @type {ol.Projection} */ map.getProjection();
var mapResolution = map.getResolution();
var layers = map.getLayers();
layers.forEach(function(layer) {
this.updateLayerAttributionsVisibility_(
layer, mapIsDef, mapExtent, mapResolution, mapProjection);
}, this);
goog.asserts.assert(!goog.isNull(map));
var view = map.getView();
if (!goog.isNull(view)) {
// FIXME works for View2D only
goog.asserts.assert(view instanceof ol.View2D);
this.viewListenerKeys_ = [
goog.events.listen(
view, ol.Object.getChangedEventType(ol.View2DProperty.CENTER),
this.updateAttributions, false, this),
goog.events.listen(
view, ol.Object.getChangedEventType(ol.View2DProperty.RESOLUTION),
this.updateAttributions, false, this)
];
}
};
@@ -359,35 +368,51 @@ ol.control.Attribution.prototype.setMap = function(map) {
goog.base(this, 'setMap', map);
if (!goog.isNull(map)) {
this.mapListenerKeys_ = [
goog.events.listen(
map, ol.Object.getChangedEventType(ol.MapProperty.CENTER),
this.handleMapChanged, false, this),
goog.events.listen(
map, ol.Object.getChangedEventType(ol.MapProperty.LAYERS),
this.handleMapLayersChanged, false, this),
goog.events.listen(
map, ol.Object.getChangedEventType(ol.MapProperty.RESOLUTION),
this.handleMapChanged, false, this),
goog.events.listen(
map, ol.Object.getChangedEventType(ol.MapProperty.SIZE),
this.handleMapChanged, false, this)
this.updateAttributions, false, this),
goog.events.listen(
map, ol.Object.getChangedEventType(ol.MapProperty.VIEW),
this.updateAttributions, false, this)
];
this.handleMapViewChanged();
this.handleMapLayersChanged();
}
};
/**
* @protected
*/
ol.control.Attribution.prototype.updateAttributions = function() {
var map = this.getMap();
var layers = map.getLayers();
layers.forEach(function(layer) {
this.updateLayerAttributionsVisibility_(layer);
}, this);
};
/**
* @param {ol.layer.Layer} layer Layer.
* @param {boolean} mapIsDef Map is defined.
* @param {ol.Extent} mapExtent Map extent.
* @param {number} mapResolution Map resolution.
* @param {ol.Projection} mapProjection Map projection.
* @private
*/
ol.control.Attribution.prototype.updateLayerAttributionsVisibility_ =
function(layer, mapIsDef, mapExtent, mapResolution, mapProjection) {
if (mapIsDef && layer.getVisible()) {
function(layer) {
var map = this.getMap();
if (map.isDef() && layer.getVisible()) {
var mapSize = /** @type {ol.Size} */ (map.getSize());
var view = map.getView();
// FIXME works for View2D only
goog.asserts.assert(view instanceof ol.View2D);
var mapExtent = view.getExtent(mapSize);
var mapProjection = /** @type {ol.Projection} */ (view.getProjection());
var mapResolution = /** @type {number} */ (view.getResolution());
var attributionVisibilities = this.getLayerAttributionVisiblities_(
layer, mapExtent, mapResolution, mapProjection);
goog.object.forEach(

View File

@@ -1,4 +1,5 @@
// FIXME should listen on appropriate pane, once it is defined
// FIXME works for View2D only
goog.provide('ol.control.MousePosition');
@@ -60,21 +61,40 @@ ol.control.MousePosition = function(mousePositionOptions) {
* @private
* @type {Array.<number>}
*/
this.listenerKeys_ = [];
this.mapListenerKeys_ = null;
this.handleMapProjectionChanged();
/**
* @private
* @type {Array.<number>}
*/
this.viewListenerKeys_ = null;
this.handleViewProjectionChanged_();
};
goog.inherits(ol.control.MousePosition, ol.control.Control);
/**
* @protected
* @private
*/
ol.control.MousePosition.prototype.handleMapProjectionChanged = function() {
this.updateTransform_();
// FIXME should we instead re-calculate using the last known mouse position?
this.element.innerHTML = this.undefinedHtml_;
ol.control.MousePosition.prototype.handleMapViewChanged_ = function() {
var map = this.getMap();
goog.asserts.assert(!goog.isNull(map));
if (!goog.isNull(this.viewListenerKeys_)) {
goog.array.forEach(this.viewListenerKeys_, goog.events.unlistenByKey);
this.viewListenerKeys_ = null;
}
var view = map.getView();
if (goog.isDefAndNotNull(view)) {
// FIXME works for View2D only
goog.asserts.assert(view instanceof ol.View2D);
this.viewListenerKeys_ = [
goog.events.listen(
view, ol.Object.getChangedEventType(ol.View2DProperty.ROTATION),
this.handleViewProjectionChanged_, false, this)
];
this.handleViewProjectionChanged_();
}
};
@@ -112,25 +132,36 @@ ol.control.MousePosition.prototype.handleMouseOut = function(browserEvent) {
};
/**
* @private
*/
ol.control.MousePosition.prototype.handleViewProjectionChanged_ = function() {
this.updateTransform_();
// FIXME should we instead re-calculate using the last known
// mouse position?
this.element.innerHTML = this.undefinedHtml_;
};
/**
* @inheritDoc
*/
ol.control.MousePosition.prototype.setMap = function(map) {
if (!goog.isNull(this.listenerKeys_)) {
goog.array.forEach(this.listenerKeys_, goog.events.unlistenByKey);
this.listenerKeys_ = null;
if (!goog.isNull(this.mapListenerKeys_)) {
goog.array.forEach(this.mapListenerKeys_, goog.events.unlistenByKey);
this.mapListenerKeys_ = null;
}
goog.base(this, 'setMap', map);
if (!goog.isNull(map)) {
var viewport = map.getViewport();
this.listenerKeys = [
goog.events.listen(map,
ol.Object.getChangedEventType(ol.MapProperty.PROJECTION),
this.handleMapProjectionChanged, false, this),
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.handleMouseOut, false, this),
goog.events.listen(map,
ol.Object.getChangedEventType(ol.MapProperty.VIEW),
this.handleMapViewChanged_, false, this)
];
this.updateTransform_();
}
@@ -141,16 +172,19 @@ ol.control.MousePosition.prototype.setMap = function(map) {
* @private
*/
ol.control.MousePosition.prototype.updateTransform_ = function() {
var map = this.getMap();
if (goog.isNull(map)) {
this.transform_ = ol.Projection.identityTransform;
} else {
var mapProjection = map.getProjection();
if (!goog.isDef(mapProjection) || !goog.isDef(this.projection_)) {
var map, view;
if (!goog.isNull(map = this.getMap()) &&
!goog.isNull(view = map.getView())) {
// FIXME works for View2D only
goog.asserts.assert(view instanceof ol.View2D);
var viewProjection = view.getProjection();
if (!goog.isDef(viewProjection) || !goog.isDef(this.projection_)) {
this.transform_ = ol.Projection.identityTransform;
} else {
this.transform_ =
ol.Projection.getTransform(mapProjection, this.projection_);
ol.Projection.getTransform(viewProjection, this.projection_);
}
} else {
this.transform_ = ol.Projection.identityTransform;
}
};

View File

@@ -1,3 +1,5 @@
// FIXME works for View2D only
goog.provide('ol.control.Zoom');
goog.require('goog.dom');
@@ -58,7 +60,9 @@ goog.inherits(ol.control.Zoom, ol.control.Control);
ol.control.Zoom.prototype.handleIn_ = function(browserEvent) {
// prevent #zoomIn anchor from getting appended to the url
browserEvent.preventDefault();
this.getMap().zoom(this.delta_);
var map = this.getMap();
// FIXME works for View2D only
map.getView().zoom(map, this.delta_);
};
@@ -69,5 +73,7 @@ ol.control.Zoom.prototype.handleIn_ = function(browserEvent) {
ol.control.Zoom.prototype.handleOut_ = function(browserEvent) {
// prevent #zoomOut anchor from getting appended to the url
browserEvent.preventDefault();
this.getMap().zoom(-this.delta_);
var map = this.getMap();
// FIXME works for View2D only
map.getView().zoom(map, -this.delta_);
};