Provide the hooks needed for writing plugins / extensions to scale control

Separate the creation of scales as well as the updating of them into
separate methods, which allows for plugins to overwrite these methods
in order to add an extra scale and update it.

Update style to support n scales instead of exactly one or two.
This commit is contained in:
Juergen Treml
2012-07-29 14:10:03 +02:00
parent 6437e653ee
commit 1a1205ea6e
2 changed files with 19 additions and 7 deletions

5
dist/leaflet.css vendored
View File

@ -227,12 +227,15 @@
text-shadow: 1px 1px 1px #fff;
background-color: rgba(255, 255, 255, 0.5);
}
.leaflet-control-scale-line:nth-child(2) {
.leaflet-control-scale-line:not(:first-child) {
border-top: 2px solid #777;
padding-top: 1px;
border-bottom: none;
margin-top: -2px;
}
.leaflet-control-scale-line:not(:first-child):not(:last-child) {
border-bottom: 2px solid #777;
}
.leaflet-touch .leaflet-control-attribution, .leaflet-touch .leaflet-control-layers {
box-shadow: none;

View File

@ -14,12 +14,7 @@ L.Control.Scale = L.Control.extend({
container = L.DomUtil.create('div', className),
options = this.options;
if (options.metric) {
this._mScale = L.DomUtil.create('div', className + '-line', container);
}
if (options.imperial) {
this._iScale = L.DomUtil.create('div', className + '-line', container);
}
this._addScales(options, className, container);
map.on(options.updateWhenIdle ? 'moveend' : 'move', this._update, this);
this._update();
@ -31,6 +26,16 @@ L.Control.Scale = L.Control.extend({
map.off(this.options.updateWhenIdle ? 'moveend' : 'move', this._update, this);
},
_addScales: function(options, className, container) {
if (options.metric) {
this._mScale = L.DomUtil.create('div', className + '-line', container);
}
if (options.imperial) {
this._iScale = L.DomUtil.create('div', className + '-line', container);
}
},
_update: function () {
var bounds = this._map.getBounds(),
centerLat = bounds.getCenter().lat,
@ -45,6 +50,10 @@ L.Control.Scale = L.Control.extend({
maxMeters = dist * (options.maxWidth / size.x);
}
this._updateScales(options, maxMeters);
},
_updateScales: function (options, maxMeters) {
if (options.metric && maxMeters) {
this._updateMetric(maxMeters);
}