-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
381 lines (359 loc) · 12.8 KB
/
index.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
(function(context) {
var mapInfo;
$(function() {
mapInfo = initMap();
_.each(mapInfo.layers, function(info, id) {
setLayerVisibility(info, info.visible);
})
initLayerSwitchers();
initPrinting();
});
function getUrlVars(str) {
str = str || window.location.href;
var vars = [], hash;
var hashes = str.slice(str.indexOf('?') + 1).split('&');
for (var i = 0; i < hashes.length; i++) {
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return vars;
}
var exportMask = '<%=protocol%>//<%=hostname%><%=port%>'
+ '/export/layers/background/'
+ '<%=zoom%>/<%=west%>,<%=south%>,<%=east%>,<%=north%>/map.<%=format%>';
function initPrinting() {
var A4 = {
width : 210,
height : 297
};
function setExportMode(exp) {
$('[data-visible-in-export-mode]').each(function() {
var el = $(this);
if (exp == el.data('visible-in-export-mode')) {
el.show();
} else {
el.hide();
}
})
}
var exporting;
mapInfo.map.on('export:start', function(ev) {
setExportMode(true);
showSelector();
})
mapInfo.map.on('export:stop', function(ev) {
setExportMode(false);
hideSelector();
if (ev.cancel)
return;
})
mapInfo.map.on('export:generate', function(ev) {
var format = ev.format;
var bounds = ev.bbox;
var areaBounds = {
east : bounds._northEast.lng,
north : bounds._northEast.lat,
west : bounds._southWest.lng,
south : bounds._southWest.lat
}
var zoom = ev.zoom;
var currentLocation = window.location;
var port = currentLocation.port;
if (port && port != 80 && port != '80') {
port = ':' + port;
} else {
port = '';
}
var options = _.extend({}, areaBounds, {
zoom : mapInfo.map.getZoom(),
format : format,
protocol : currentLocation.protocol,
hostname : currentLocation.hostname,
port : port
});
var url = _.template(exportMask, options);
var win = window.open(url, '_blank');
win.focus();
})
var areaSelect;
function hideSelector() {
if (areaSelect) {
mapInfo.map.removeLayer(areaSelect);
delete areaSelect;
}
}
function showSelector() {
hideSelector();
var map = mapInfo.map;
var viewBounds = map.getPixelBounds();
var center = viewBounds.getCenter();
var delta = viewBounds.getSize().multiplyBy(3 / 8);
var sw = map.unproject(center.subtract(delta));
var ne = map.unproject(center.add(delta));
var bounds = new L.LatLngBounds(sw, ne);
areaSelect = new L.LocationFilter({
bounds : bounds,
enable : true,
enableButton : false,
adjustButton : false
});
areaSelect.addTo(map);
areaSelect.on("change", function() {
var bounds = areaSelect.getBounds();
console.log("Bounds:", this.getBounds());
});
}
$('[data-map-export-btn]').each(function() {
var elm = $(this);
elm.click(function() {
var ok = !!elm.data('map-export-btn');
if (!ok) {
mapInfo.map.fire('export:stop');
} else {
var bbox = areaSelect.getBounds();
var format = $('[data-map-export-format]').val();
mapInfo.map.fire('export:generate', {
zoom : mapInfo.map.getZoom(),
bbox : bbox,
format : format
});
}
})
})
$('[data-map-export]').each(function() {
var elm = $(this);
elm.click(function() {
mapInfo.map.fire('export:start', {});
})
})
setExportMode(false);
}
function initLayerSwitchers() {
$('[data-map-layer-selector]').each(function() {
var el = $(this);
var id = el.data('map-layer-selector');
if (!id)
return;
el.click(function(e) {
$(this).toggleClass('selected');
toggleLayer(id);
e.preventDefault();
e.stopPropagation();
})
})
}
function setLayerVisibility(info, visible) {
if (!info)
return;
info.visible = !!visible;
_.each([ info.tiles, info.grid ], function(layer) {
if (!layer)
return;
if (info.visible) {
mapInfo.map.addLayer(layer);
} else {
mapInfo.map.removeLayer(layer);
}
})
}
function toggleLayer(id) {
var info = mapInfo.layers[id];
if (!info)
return;
setLayerVisibility(info, !info.visible);
}
var marker;
var markerLatLng;
function showMarker(data) {
var coords = data.geometry.coordinates;
markerLatLng = [ coords[1], coords[0] ];
refreshMarker();
}
function hideMarker() {
if (marker) {
mapInfo.map.removeLayer(marker);
marker = null;
}
}
function refreshMarker() {
hideMarker();
if (!markerLatLng)
return;
var zoom = mapInfo.map.getZoom();
var baseRadius = 10;
var baseZoom = 16;
var baseWidth = 1;
var radius = Math.max(5, baseRadius * Math.pow(2, zoom - baseZoom));
var width = Math.max(1, baseWidth * Math.pow(2, zoom - baseZoom));
marker = L.circleMarker(markerLatLng, {
color : 'red',
opacity : 0.8,
weight : width,
radius : radius,
fillColor : 'white',
fillOpacity : 0.3
});
mapInfo.map.addLayer(marker);
}
function initMap() {
function appendRandomParam(url) {
var ch = (url.indexOf('?') < 0) ? '?' : '&';
url += ch;
url += 'x=' + Math.random() * 1000;
url += '-' + new Date().getTime();
return url;
}
var templates = {};
function getTemplate(elm, type) {
var template = templates[type];
if (template)
return template;
var templateElm = elm.find('[data-template="' + type + '"]');
if (!templateElm[0]) {
templateElm = elm.find('[data-template="default"]');
}
var content;
if (templateElm.prop('tagName') == 'SCRIPT') {
var t = _.template(templateElm.text());
template = function(data) {
var str = t({
data : data,
icon : function(key, css, text, title) {
console.log(key, data.properties)
if (!data.properties[key])
return '';
text = text || '';
if (title && title != '') {
title = ' title="' + title + '"'
}
var result = '<i class="' + css + '"' + title
+ '/>';
result += ' ' + text;
return '<div>' + result + '</div>';
}
});
return $('<div></div>').append(str);
}
} else {
var text = templateElm.html();
template = function(data) {
var content = $('<div></div>').append(text);
content.find('[data-property]').each(
function() {
var el = $(this);
var f = eval('(function(data){return '
+ el.data('property') + ';})');
var value = '';
if (_.isFunction(f)) {
value = f.call(data, data);
}
el.text(value);
})
return content;
}
}
templates[type] = template;
return template;
}
function formatContent(elm, data) {
var type = data.properties.type;
var template = getTemplate(elm, type);
return template ? template(data) : null;
}
var mapElement = $('#map');
var center = mapElement.data('center') || [ 0, 0 ];
center = [ center[1], center[0] ];
var zoom = mapElement.data('zoom');
var minZoom = mapElement.data('min-zoom') || 2;
var maxZoom = mapElement.data('max-zoom') || 18;
var forceReload = mapElement.data('force-reload') || false;
var layers = {};
var result = {
layers : layers
};
var zIndex = 0;
mapElement.find('[data-map-layer]').each(function() {
var elm = $(this);
var id = elm.data('map-layer');
var visible = elm.data('visible');
visible = !!visible;
zIndex++;
var layerInfo = layers[id] = {
visible : visible
};
var tilesUrl = elm.data('tiles-url');
if (tilesUrl) {
if (forceReload) {
tilesUrl = appendRandomParam(tilesUrl);
}
var attributionElm = elm.find('.attribution');
var attribution = attributionElm.html();
attributionElm.remove();
var tilesLayer = L.tileLayer(tilesUrl, {
attribution : attribution,
minZoom : minZoom,
maxZoom : maxZoom,
zIndex : zIndex
});
layerInfo.tiles = tilesLayer;
}
var utfgridUrl = elm.data('utfgrid-url');
if (utfgridUrl) {
if (forceReload) {
utfgridUrl = appendRandomParam(utfgridUrl);
}
var gridLayer = new L.UtfGrid(utfgridUrl, {
zIndex : zIndex + 1000
});
var html = elm.html();
function handler(ev) {
var panel = $(panelSelector);
var data = ev.data;
if (!data)
return;
if (_.isString(data.properties)) {
data.properties = JSON.parse(data.properties);
if (!data.properties.type) {
data.properties.type = data.type;
}
}
if (_.isString(data.geometry)) {
data.geometry = JSON.parse(data.geometry);
}
console.log(JSON.stringify(data, null, 2));
var content = formatContent(elm, data);
panel.html(content);
panel.removeClass('hidden');
showMarker(data);
}
var panelSelector = elm.data('panel') || '#info';
var actions = elm.data('action') || 'mouseover';
_.each(actions.split(/[,;]/gim), function(action) {
gridLayer.on(action, handler);
})
layerInfo.grid = gridLayer;
}
})
mapElement.html('');
var params = getUrlVars();
var scroll = params['wheel-scroll'];
scroll = !(scroll == 'no' || scroll == 'false' || scroll == '0');
var map = result.map = L.map(mapElement[0], {
zoomControl : false,
scrollWheelZoom : scroll
});
map.addControl(L.control.zoom({
position : 'topright'
}));
map.setView(center, zoom);
map.on('click', function(e) {
console.log(map.getZoom() + ' [' + e.latlng.lng + ','
+ e.latlng.lat + ']');
})
map.on('zoomend', function() {
refreshMarker();
});
return result;
}
})(this);