Skip to content

Commit

Permalink
Merge branch 'permalink2'
Browse files Browse the repository at this point in the history
  • Loading branch information
shramov committed Jun 23, 2012
2 parents 7ec7fd2 + 0d1d360 commit 693f716
Show file tree
Hide file tree
Showing 7 changed files with 359 additions and 230 deletions.
29 changes: 19 additions & 10 deletions control/Distance.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ L.Control.Distance = L.Control.extend({
this._active = false;
},

getLine: function() { return this._line; },

onAdd: function(map) {
var className = 'leaflet-control-distance',
container = this._container = L.DomUtil.create('div', className);
Expand All @@ -30,6 +32,7 @@ L.Control.Distance = L.Control.extend({
//text.style.display = 'inline';
//text.style.float = 'right';

this._map.addLayer(this._line);
this._calc_disable();
return container;
},
Expand All @@ -55,39 +58,44 @@ L.Control.Distance = L.Control.extend({
this._map.on('click', this._add_point, this);

this._map.getContainer().style.cursor = 'crosshair';
this._map.addLayer(this._line);
//this._map.addLayer(this._line);
L.DomUtil.addClass(this._link, 'leaflet-control-distance-active');
this._container.appendChild(this._link_delete);
this._container.appendChild(this._text);
this._active = true;
this._line.editing.enable();
if (!this._map.hasLayer(this._line))
this._map.addLayer(this._line);
this._update();
},

_calc_disable: function() {
this._map.off('click', this._add_point, this);
this._map.removeLayer(this._line);
//this._map.removeLayer(this._line);
this._map.getContainer().style.cursor = 'default';
this._container.removeChild(this._link_delete);
this._container.removeChild(this._text);
L.DomUtil.removeClass(this._link, 'leaflet-control-distance-active');
this._active = false;
this._line.editing.disable();
},

_add_point: function (e) {
var len = this._line.getLatLngs().length;
this._line.addLatLng(e.latlng);
this._map.removeLayer(this._line);
this._map.addLayer(this._line);
this._line.editing.updateMarkers();
this._line.fire('edit', {});
},

_reset: function(e) {
this._line.setLatLngs([]);
this._line.fire('edit', {});
this._map.removeLayer(this._line);
this._line.redraw();
this._line.editing.updateMarkers();
},

_update: function(e) {
console.info("Update");
this._text.textContent = this._d2txt(this._distance_calc());
},

Expand All @@ -106,13 +114,14 @@ L.Control.Distance = L.Control.extend({
d += p.distanceTo(ll[i]);
if (this.options.popups) {
var m = this._line.editing._markers[i];
m.bindPopup(this._d2txt(d));
m.on('mouseover', m.openPopup, m);
m.on('mouseout', m.closePopup, m);
}
if (m) {
m.bindPopup(this._d2txt(d));
m.on('mouseover', m.openPopup, m);
m.on('mouseout', m.closePopup, m);
}
}
p = ll[i];
}
return d;
}
});

74 changes: 74 additions & 0 deletions control/Permalink.Layer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
L.Control.Permalink.include({
/*
options: {
useMarker: true,
markerOptions: {}
},
*/

initialize_layer: function() {
//console.info("Initialize layer");
this.on('update', this._set_layer, this);
this.on('add', this._onadd_layer, this);
},

_onadd_layer: function(e) {
//console.info("onAdd::layer", e);
this._map.on('layeradd', this._update_layer, this);
this._map.on('layerremove', this._update_layer, this);
this._update_layer();
},

_update_layer: function() {
if (!this.options.layers) return;
//console.info(this.options.layers);
var layer = this.options.layers.currentBaseLayer();
if (layer)
this._update({layer: layer.name});
},

_set_layer: function(e) {
//console.info("Set layer", e);
var p = e.params;
if (!this.options.layers || !p.layer) return;
this.options.layers.chooseBaseLayer(p.layer);
}
});

L.Control.Layers.include({
chooseBaseLayer: function(name) {
var layer, obj;
for (var i in this._layers) {
if (!this._layers.hasOwnProperty(i))
continue;
obj = this._layers[i];
if (!obj.overlay && obj.name == name)
layer = obj.layer;
}
if (!layer || this._map.hasLayer(layer))
return;

for (var i in this._layers) {
if (!this._layers.hasOwnProperty(i))
continue;
obj = this._layers[i];
if (!obj.overlay && this._map.hasLayer(obj.layer))
this._map.removeLayer(obj.layer)
}
this._map.addLayer(layer)
this._update();
},

currentBaseLayer: function() {
for (var i in this._layers) {
if (!this._layers.hasOwnProperty(i))
continue;
var obj = this._layers[i];
//console.info("Layer: ", obj.name, obj);
if (obj.overlay) continue;
if (!obj.overlay && this._map.hasLayer(obj.layer))
return obj;
}
}
});

47 changes: 47 additions & 0 deletions control/Permalink.Line.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
L.Control.Permalink.include({
/*
options: {
line: null
},
*/

initialize_line: function() {
this.on('update', this._set_line, this);
this.on('add', this._onadd_line, this);
},

_onadd_line: function(e) {
//console.info("onAdd::line", e);
if (!this.options.line) return;
this.options.line.on('edit', this._update_line, this);
this._update_line()
},

_update_line: function() {
if (!this.options.line) return;
var line = this.options.line;
if (!line) return;
var text = [], coords = line.getLatLngs();
if (!coords.length)
return this._update({line: null});
for (var i in coords)
text.push(coords[i].lat.toFixed(4) + "," + coords[i].lng.toFixed(4))
this._update({line: text.join(';')});
},

_set_line: function(e) {
//console.info("Set line", e.params.line);
var p = e.params, l = this.options.line;
if (!l || !p.line) return;
var coords = [], text = p.line.split(';');
for (var i in text) {
var ll = text[i].split(',');
if (ll.length != 2) continue;
coords.push(new L.LatLng(ll[0], ll[1]));
}
if (!coords.length) return;
l.setLatLngs(coords);
if (!this._map.hasLayer(l))
this._map.addLayer(l);
}
});
29 changes: 29 additions & 0 deletions control/Permalink.Marker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
L.Control.Permalink.include({
/*
options: {
useMarker: true,
markerOptions: {}
},
*/

initialize_marker: function() {
//console.info("Initialize marker");
this.on('update', this._set_marker, this);
},

_set_marker: function(e) {
//console.info("Set marker", e);
var p = e.params;
//if (!this.options.useMarker) return;
if (this._marker) return;
if (p.marker != 1) return;
if (p.mlat !== undefined && p.mlon !== undefined)
return this._update({mlat: null, mlon: null,
lat: p.mlat, lon: p.mlon, marker: 1});
this._marker = new L.Marker(new L.LatLng(p.lat, p.lon),
this.options.markerOptions);
this._marker.bindPopup("<a href='" + this._update_href() + "'>" + this.options.text + "</a>");
this._map.addLayer(this._marker);
this._update({marker: null});
}
});
Loading

0 comments on commit 693f716

Please sign in to comment.