-
Notifications
You must be signed in to change notification settings - Fork 685
/
Copy pathpreview.js
223 lines (193 loc) · 5.87 KB
/
preview.js
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
(function () {
'use strict';
var map = L.map('map', {
zoomControl: false,
}).setView([48, -3], 5);
function escapeHtml (string) {
return string
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
}
function renderValue (value) {
if (typeof value === 'string') {
return "'" + escapeHtml(value) + "'";
} else {
return JSON.stringify(value).replace(/,/g, ', ');
}
}
L.TileLayer.include({
getExampleJS: function () {
var layerName = this._providerName.replace('.', '_');
var url = this._exampleUrl || this._url;
var options = L.extend({}, this._options, this._exampleAPIcodes || {});
// replace {variant} in urls with the selected variant, since
// keeping it in the options map doesn't make sense for one layer
if (options.variant) {
url = url.replace('{variant}', options.variant);
delete options.variant;
}
var code = 'var ' + layerName + ' = L.tileLayer(\'' + url + '\', {\n';
var first = true;
for (var option in options) {
if (first) {
first = false;
} else {
code += ',\n';
}
code += '\t' + option + ': ' + renderValue(options[option]);
}
code += '\n});\n';
return code;
}
});
var isOverlay = function (providerName, layer) {
if (layer.options.opacity && layer.options.opacity < 1) {
return true;
}
var overlayPatterns = [
'^(OpenWeatherMap|OpenSeaMap)',
'OpenMapSurfer.(Hybrid|AdminBounds|ContourLines|Hillshade|ElementsAtRisk)',
'Stamen.Toner(Hybrid|Lines|Labels)',
'Hydda.RoadsAndLabels',
'^JusticeMap',
'OpenPtMap',
'OpenRailwayMap',
'OpenFireMap',
'SafeCast'
];
return providerName.match('(' + overlayPatterns.join('|') + ')') !== null;
};
// Ignore some providers in the preview
var isIgnored = function (providerName) {
if (providerName === 'ignored') {
return true;
}
// reduce the number of layers previewed for some providers
if (providerName.startsWith('HERE') || providerName.startsWith('OpenWeatherMap') || providerName.startsWith('MapBox')) {
var whitelist = [
// API threshold almost reached, disabling for now.
// 'HERE.normalDay',
'OpenWeatherMap.Clouds',
'OpenWeatherMap.Pressure',
'OpenWeatherMap.Wind'
];
return whitelist.indexOf(providerName) === -1;
}
return false;
};
// collect all layers available in the provider definition
var baseLayers = {};
var overlays = {};
var addLayer = function (name) {
if (isIgnored(name)) {
return;
}
var layer = L.tileLayer.provider(name);
if (isOverlay(name, layer)) {
overlays[name] = layer;
} else {
baseLayers[name] = layer;
}
};
L.tileLayer.provider.eachLayer(addLayer);
// add minimap control to the map
var layersControl = L.control.layers.minimap(baseLayers, overlays, {
collapsed: false
}).addTo(map);
// Pass a filter in the hash tag to show only layers containing that string
// for example: #filter=Open
var filterLayersControl = function () {
var hash = window.location.hash;
var filterIndex = hash.indexOf('filter=');
if (filterIndex !== -1) {
var filterString = hash.substr(filterIndex + 7).trim();
var visible = layersControl.filter(filterString);
// enable first layer as actual layer.
var first = Object.keys(visible)[0];
if (first in baseLayers) {
map.addLayer(baseLayers[first]);
map.eachLayer(function (layer) {
if (layer._providerName !== first) {
map.removeLayer(layer);
}
});
layersControl.filter(filterString);
}
}
};
L.DomEvent.on(window, 'hashchange', filterLayersControl);
// Does not work if called immediately, so ugly hack to apply filter
// at first page load
setTimeout(filterLayersControl, 100);
// add OpenStreetMap.Mapnik, or the first if it does not exist
if (baseLayers['OpenStreetMap.Mapnik']) {
baseLayers['OpenStreetMap.Mapnik'].addTo(map);
} else {
baseLayers[Object.keys(baseLayers)[0]].addTo(map);
}
// if a layer is selected and if it has bounds an the bounds are not in the
// current view, move the map view to contain the bounds
map.on('baselayerchange', function (e) {
var layer = e.layer;
if (!map.hasLayer(layer)) {
return;
}
if (layer.options.minZoom > 1 && map.getZoom() > layer.options.minZoom) {
map.setZoom(layer.options.minZoom);
}
if (!layer.options.bounds) {
return;
}
var bounds = L.latLngBounds(layer.options.bounds);
map.fitBounds(bounds, {
paddingTopLeft: [0, 200],
paddingBottomRight: [200, 0]
});
});
// Add the TileLayer source code control to the map
map.addControl(new (L.Control.extend({
options: {
position: 'topleft'
},
onAdd: function (map) {
var container = L.DomUtil.get('info');
L.DomEvent.disableClickPropagation(container);
L.DomUtil.create('h4', null, container).innerHTML = 'Provider names for <code>leaflet-providers.js</code>';
var providerNames = L.DomUtil.create('code', 'provider-names', container);
L.DomUtil.create('h4', '', container).innerHTML = 'Plain JavaScript:';
var pre = L.DomUtil.create('pre', null, container);
var code = L.DomUtil.create('code', 'javascript', pre);
var update = function (event) {
code.innerHTML = '';
var names = [];
// loop over the layers in the map and add the JS
for (var key in map._layers) {
var layer = map._layers[key];
if (!layer.getExampleJS) {
continue;
}
// do not add the layer currently being removed
if (event && event.type === 'layerremove' && layer === event.layer) {
continue;
}
names.push(L.Util.template('<a href="#filter={name}">{name}</a>', {
name: layer._providerName
}));
code.innerHTML += layer.getExampleJS();
}
providerNames.innerHTML = names.join(', ');
/* global hljs:true */
hljs.highlightBlock(code);
};
map.on({
'layeradd': update,
'layerremove': update
});
update();
return container;
}
}))());
})();