-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathPermalinkAttribution.js
More file actions
65 lines (55 loc) · 2.16 KB
/
Copy pathPermalinkAttribution.js
File metadata and controls
65 lines (55 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
* L.Control.Attribution that replaces OpenStreetMap links with permalinks.
* Also can edd an edit link.
* Replaces standard attribution control, because of https://github.com/Leaflet/Leaflet/issues/2177
*/
L.Control.StandardAttribution = L.Control.Attribution;
L.Control.PermalinkAttribution = L.Control.Attribution.extend({
onAdd: function( map ) {
var container = L.Control.StandardAttribution.prototype.onAdd.call(this, map);
map.on('moveend', this._update, this);
return container;
},
onRemove: function( map ) {
map.off('moveend', this._update);
L.Control.StandardAttribution.prototype.onRemove.call(this, map);
},
// copied from original class and slightly modified
_update: function () {
if (!this._map) { return; }
var attribs = [];
for (var i in this._attributions) {
if (this._attributions[i]) {
// make permalink for openstreetmap
if( i.indexOf('/openstreetmap.org') > 0 || i.indexOf('/www.openstreetmap.org') > 0 ) {
var permalink = 'http://www.openstreetmap.org/#map={zoom}/{lat}/{lon}';
i = i.replace(/(['"])http[^'"]+openstreetmap.org[^'"]*(['"])/, '$1' + permalink + '$2');
if( this._map.options.attributionEditLink ) {
var editlink = permalink.replace('#', 'edit#');
i = i.replace(/(openstreetmap.org[^'"]*(['"])[^>]*>[^<]+<\/a>)/, '$1 (<a href=$2' + editlink + '$2 target=$2osmedit$2>Edit</a>)');
}
}
var latlng = this._map.getCenter();
i = i.replace(/\{zoom\}/g, this._map.getZoom()).replace(/\{lat\}/g, L.Util.formatNum(latlng.lat, 4)).replace(/\{lon\}/g, L.Util.formatNum(latlng.lng, 4));
attribs.push(i);
}
}
var prefixAndAttribs = [];
if (this.options.prefix) {
prefixAndAttribs.push(this.options.prefix);
}
if (attribs.length) {
prefixAndAttribs.push(attribs.join(', '));
}
this._container.innerHTML = prefixAndAttribs.join(' | ');
}
});
L.control.permalinkAttribution = function( options ) {
return new L.Control.PermalinkAttribution(options);
};
L.Map.mergeOptions({
attributionEditLink: false
});
L.Control.Attribution = L.Control.PermalinkAttribution;
L.control.standardAttribution = L.control.attribution;
L.control.attribution = L.control.permalinkAttribution;