MapControl abstract class
The abstract base class for map controls.
abstract class MapControl {
MapViewport _map;
/// the map this control is attached to, or null
MapViewport get map => _map;
/// the list of currently subscriptions
final List _subscriptions = [];
/// the container element for the control
Element _root;
/// the root DOM element for this map control
Element get root => _root;
/**
* Creates a new map control attached to [viewport].
*
* If [viewport] is missing, the control isn't attached yet.
* Use [attach] to attach it.
*/
MapControl([MapViewport viewport]) {
if (viewport != null) {
attach(viewport);
}
}
/**
* Detaches this control from the map viewport it is currently
* attached to (if any).
*/
void detach() {
if (_map == null) return;
if (_root == null) return;
if (_map._controlsPane == null) return;
_map.controlsPane.root.children.remove(root);
_subscriptions.forEach((s) => s.cancel());
_subscriptions.clear();
}
/**
* Attaches this control to the map [viewport].
*/
void attach(MapViewport viewport) {
_require(viewport != null, "viewport must not be null");
// already attached ?
if (_map != null) {
if (_map == viewport) {
return; // don't attach again
} else {
detach(); // detach the currently attached, then attach the new one
}
}
this._map = viewport;
var controlsPane = _map.controlsPane;
//TODO: log a warning?
if (controlsPane == null) return;
build();
controlsPane.controls.add(this);
controlsPane.root.children.add(root);
applyPosition();
}
_normalizePropertyValue(v) {
if (v is int) return v.toString();
else if (v is String) return v.trim();
else
throw new ArgumentError("Expected int or String, got $v");
}
/**
* the default position of the control if no position has been
* set explicitly using [placeAt].
*
* Override in subclasses.
*/
Point2D get defaultPosition;
Point2D _position;
/**
* Applies the current position to the appropriate DOM element.
*/
void applyPosition() {
if (root == null) return;
var pos = _position == null ? defaultPosition : _position;
if (pos == null) {
//TODO: log a warning ?
return;
}
root.style
..left = "${pos.x}px"
..top = "${pos.y}px";
}
/**
* Places the control at the position ([x], [y]).
*/
void placeAt(int x, int y) {
_position = new Point2D(x,y);
applyPosition();
}
/**
* Abstract method which builds the DOM tree for the control.
* Override in subclasses.
*/
void build();
/**
* Invoke this to force to (re-)layout the map control in the current
* map viewport.
*/
void layout() {}
}
Subclasses
LayerControl, PanControl, ScaleIndicatorControl, SimpleZoomControl, ZoomControl
Constructors
new MapControl([MapViewport viewport]) #
Creates a new map control attached to viewport.
If viewport is missing, the control isn't attached yet. Use attach to attach it.
MapControl([MapViewport viewport]) {
if (viewport != null) {
attach(viewport);
}
}
Properties
final Point2D defaultPosition #
the default position of the control if no position has been set explicitly using placeAt.
Override in subclasses.
Point2D get defaultPosition;
final MapViewport map #
the map this control is attached to, or null
MapViewport get map => _map;
Methods
void applyPosition() #
Applies the current position to the appropriate DOM element.
void applyPosition() {
if (root == null) return;
var pos = _position == null ? defaultPosition : _position;
if (pos == null) {
//TODO: log a warning ?
return;
}
root.style
..left = "${pos.x}px"
..top = "${pos.y}px";
}
void attach(MapViewport viewport) #
Attaches this control to the map viewport.
void attach(MapViewport viewport) {
_require(viewport != null, "viewport must not be null");
// already attached ?
if (_map != null) {
if (_map == viewport) {
return; // don't attach again
} else {
detach(); // detach the currently attached, then attach the new one
}
}
this._map = viewport;
var controlsPane = _map.controlsPane;
//TODO: log a warning?
if (controlsPane == null) return;
build();
controlsPane.controls.add(this);
controlsPane.root.children.add(root);
applyPosition();
}
abstract void build() #
Abstract method which builds the DOM tree for the control. Override in subclasses.
void detach() #
Detaches this control from the map viewport it is currently attached to (if any).
void detach() {
if (_map == null) return;
if (_root == null) return;
if (_map._controlsPane == null) return;
_map.controlsPane.root.children.remove(root);
_subscriptions.forEach((s) => s.cancel());
_subscriptions.clear();
}
void layout() #
Invoke this to force to (re-)layout the map control in the current map viewport.
void layout() {}