Add experimental controls infrastructure, thanks @elemoine

This commit is contained in:
Tom Payne
2012-07-18 13:32:22 +02:00
parent 1af79ede29
commit c6343ed2bc
11 changed files with 214 additions and 381 deletions
-103
View File
@@ -1,103 +0,0 @@
goog.provide('ol.control.Attribution');
goog.require('ol.event');
goog.require('ol.control.Control');
goog.require('goog.dom');
/**
* @constructor
* @extends {ol.control.Control}
* @param {boolean|undefined} opt_autoActivate
*/
ol.control.Attribution = function(opt_autoActivate) {
goog.base(this, opt_autoActivate);
/**
* @type {Node}
*/
this.container_ = null;
/**
* Activate this control when it is added to a map. Default is true.
*
* @type {boolean} autoActivate
*/
this.autoActivate_ =
goog.isDef(opt_autoActivate) ? opt_autoActivate : true;
};
goog.inherits(ol.control.Attribution, ol.control.Control);
/**
* @const {string}
*/
ol.control.Attribution.prototype.CLS = 'ol-control-attribution';
/**
* @param {ol.Map} map
*/
ol.control.Attribution.prototype.setMap = function(map) {
var staticOverlay = map.getStaticOverlay();
if (goog.isNull(this.container_)) {
this.container_ = goog.dom.createDom('div', this.CLS);
// This is not registered as priority listener, so priority listeners
// can still get the click event.
map.getEvents().register('click', this.stopLinkClick, this);
}
if (!goog.isNull(staticOverlay)) {
goog.dom.append(staticOverlay, this.container_);
}
goog.base(this, 'setMap', map);
};
/**
* Prevent clicks on links in the attribution from getting through to
* listeners.
*/
ol.control.Attribution.prototype.stopLinkClick = function(evt) {
var node = evt.target;
return node.nodeName !== 'A' ||
!goog.dom.getAncestorByClass(node, this.CLS);
};
/** @inheritDoc */
ol.control.Attribution.prototype.activate = function() {
var active = goog.base(this, 'activate');
if (active) {
this.map_.getEvents().register('layeradd', this.update, this);
this.update();
}
return active;
};
/** @inheritDoc */
ol.control.Attribution.prototype.deactivate = function() {
var inactive = goog.base(this, 'deactivate');
if (inactive) {
this.map_.getEvents().unregister('layeradd', this.update, this);
}
return inactive;
};
ol.control.Attribution.prototype.update = function() {
var attribution = [],
layers = this.map_.getLayers(), layerAttribution;
for (var i=0, ii=layers.length; i<ii; ++i) {
layerAttribution = layers[i].getAttribution();
if (layerAttribution &&
!~goog.array.indexOf(attribution, layerAttribution)) {
attribution.push(layerAttribution);
}
}
this.container_.innerHTML = attribution.join(', ');
};
ol.control.Attribution.prototype.destroy = function() {
goog.dom.removeNode(this.container_);
goog.base(this, 'destroy');
};
ol.control.addControl('attribution', ol.control.Attribution);
-85
View File
@@ -1,85 +0,0 @@
goog.provide('ol.control');
goog.provide('ol.control.Control');
goog.require('goog.object');
/**
* @enum {Object}
*/
ol.control.CONTROL_MAP = {};
/**
* @param {string} name
* @param {Function} Control
*/
ol.control.addControl = function(name, Control) {
ol.control.CONTROL_MAP[name] = Control;
};
/**
* @constructor
* @param {boolean|undefined} opt_autoActivate
*/
ol.control.Control = function(opt_autoActivate) {
/**
* @type {ol.Map} map
* @protected
*/
this.map_ = null;
/**
* @type {boolean} active
* @private
*/
this.active_ = false;
/**
* Activate this control when it is added to a map. Default is false.
*
* @type {boolean} autoActivate
*/
this.autoActivate_ =
goog.isDef(opt_autoActivate) ? opt_autoActivate : false;
};
/**
* @return {ol.Map} getMap
*/
ol.control.Control.prototype.getMap = function() {
return this.map_;
};
/**
* @param {ol.Map} map
*/
ol.control.Control.prototype.setMap = function(map) {
this.map_ = map;
if (this.autoActivate_) {
this.activate();
}
};
/**
* @return {boolean}
*/
ol.control.Control.prototype.activate = function() {
var returnValue = !this.active_;
this.active_ = true;
return returnValue;
};
/**
* @return {boolean}
*/
ol.control.Control.prototype.deactivate = function() {
var returnValue = this.active_;
this.active_ = false;
return returnValue;
};
ol.control.Control.prototype.destroy = function() {
this.deactivate();
goog.object.clear(this);
};
-71
View File
@@ -1,71 +0,0 @@
goog.provide('ol.control.Navigation');
goog.require('ol.control.Control');
/**
* @constructor
* @extends {ol.control.Control}
* @param {boolean|undefined} opt_autoActivate
*/
ol.control.Navigation = function(opt_autoActivate) {
goog.base(this, opt_autoActivate);
/**
* Activate this control when it is added to a map. Default is true.
*
* @type {boolean} autoActivate
*/
this.autoActivate_ =
goog.isDef(opt_autoActivate) ? opt_autoActivate : true;
};
goog.inherits(ol.control.Navigation, ol.control.Control);
/** @inheritDoc */
ol.control.Navigation.prototype.activate = function() {
var active = goog.base(this, 'activate');
if (active) {
var events = this.map_.getEvents();
events.register("drag", this.moveMap, this);
events.register("scroll", this.zoomMap, this);
}
return active;
};
/** @inheritDoc */
ol.control.Navigation.prototype.deactivate = function() {
var inactive = goog.base(this, 'deactivate');
if (inactive) {
var events = this.map_.getEvents();
events.unregister("drag", this.moveMap, this);
events.unregister("scroll", this.zoomMap, this);
}
return inactive;
};
/**
* @param {Object} evt
*/
ol.control.Navigation.prototype.moveMap = function(evt) {
this.map_.moveByPx(evt.deltaX, evt.deltaY);
return false;
};
/**
* @param {Event} evt
*/
ol.control.Navigation.prototype.zoomMap = function(evt) {
var map = this.map_,
delta = ((evt.deltaY / 3) | 0);
if (Math.abs(delta) === 0) {
return;
}
map.setZoom(map.getZoom()-delta, map.getEvents().getPointerPosition(evt));
// We don't want the page to scroll.
evt.preventDefault();
return false;
};
ol.control.addControl('navigation', ol.control.Navigation);
-122
View File
@@ -1,122 +0,0 @@
goog.provide('ol.control.Zoom');
goog.require('ol.event');
goog.require('ol.control.Control');
goog.require('goog.dom');
/**
* @constructor
* @extends {ol.control.Control}
* @param {boolean|undefined} opt_autoActivate
*/
ol.control.Zoom = function(opt_autoActivate) {
goog.base(this, opt_autoActivate);
/**
* Activate this control when it is added to a map. Default is true.
*
* @type {boolean} autoActivate
*/
this.autoActivate_ =
goog.isDef(opt_autoActivate) ? opt_autoActivate : true;
};
goog.inherits(ol.control.Zoom, ol.control.Control);
/**
* @param {ol.Map} map
*/
ol.control.Zoom.prototype.setMap = function(map) {
goog.base(this, 'setMap', map);
var container = goog.dom.createDom('div', ol.control.Zoom.RES.CLS);
var inButton = goog.dom.createDom(
'div', ol.control.Zoom.RES.IN_CLS,
goog.dom.createDom('a', {'href': '#zoomIn'})
);
goog.dom.setTextContent(
/** @type {Element} */ (inButton.firstChild),
ol.control.Zoom.RES.IN_TEXT
);
var outButton = goog.dom.createDom(
'div', ol.control.Zoom.RES.OUT_CLS,
goog.dom.createDom('a', {'href': '#zoomOut'})
);
goog.dom.setTextContent(
/** @type {Element} */ (outButton.firstChild),
ol.control.Zoom.RES.OUT_TEXT
);
goog.dom.append(container, inButton, outButton);
var overlay = map.getStaticOverlay();
if (goog.isDefAndNotNull(overlay)) {
goog.dom.append(overlay, container);
}
};
/** @inheritDoc */
ol.control.Zoom.prototype.activate = function() {
var active = goog.base(this, 'activate');
if (active) {
this.map_.getEvents().register('click', this.handle, this);
this.map_.getEvents().register('keypress', this.handle, this);
}
return active;
};
/** @inheritDoc */
ol.control.Zoom.prototype.deactivate = function() {
var inactive = goog.base(this, 'deactivate');
if (inactive) {
this.map_.getEvents().unregister('click', this.handle, this);
this.map_.getEvents().unregister('keypress', this.handle, this);
}
return inactive;
};
/**
* @param {Event} evt
* @return {boolean}
*/
ol.control.Zoom.prototype.handle = function(evt) {
var target = /** @type {Node} */ (evt.target),
handled = false;
if (evt.type === 'click' || ol.event.isEnterOrSpace(evt)) {
if (goog.dom.getAncestorByClass(target, ol.control.Zoom.RES.IN_CLS)) {
this.map_.zoomIn();
handled = true;
} else
if (goog.dom.getAncestorByClass(target, ol.control.Zoom.RES.OUT_CLS)) {
this.map_.zoomOut();
handled = true;
}
if (handled) {
// Stop default behavior (i.e. don't follow link to anchor)
evt.preventDefault();
// On Android 2.3, the above does not prevent the link from being
// followed, but stopPropagation does.
evt.stopPropagation();
}
}
return !handled;
};
ol.control.Zoom.prototype.destroy = function() {
goog.dom.removeNode(goog.dom.getElementByClass(
ol.control.Zoom.RES.CLS,
/** @type {Element} */ (this.map_.getStaticOverlay())
));
goog.base(this, 'destroy');
};
ol.control.addControl('zoom', ol.control.Zoom);
ol.control.Zoom.RES = {
CLS: 'ol-control-zoom',
IN_CLS: 'ol-control-zoom-in',
OUT_CLS: 'ol-control-zoom-out',
IN_TEXT: '+',
OUT_TEXT: '\u2013'
};
+17
View File
@@ -0,0 +1,17 @@
goog.provide('ol.Control');
goog.require('ol.MapBrowserEvent');
/**
* @constructor
*/
ol.Control = function() {
};
/**
* @param {ol.MapBrowserEvent} event Map browser event.
*/
ol.Control.prototype.handleMapBrowserEvent = goog.abstractMethod;
+34
View File
@@ -0,0 +1,34 @@
goog.provide('ol.control.DblClickZoom');
goog.require('goog.events.EventType');
goog.require('ol.Control');
goog.require('ol.MapBrowserEvent');
/**
* @constructor
* @extends {ol.Control}
*/
ol.control.DblClickZoom = function() {
goog.base(this);
};
goog.inherits(ol.control.DblClickZoom, ol.Control);
/**
* @inheritDoc
*/
ol.control.DblClickZoom.prototype.handleMapBrowserEvent = function(event) {
if (event.type == goog.events.EventType.DBLCLICK) {
var map = event.map;
map.whileFrozen(function() {
// FIXME compute correct center for zoom
map.setCenter(event.getCoordinate());
var browserEventObject = event.getBrowserEventObject();
var scale = browserEventObject.shiftKey ? 2 : 0.5;
map.setResolution(scale * map.getResolution());
});
event.preventDefault();
}
};