1 L.OSM.share = function (options) {
2 var control = L.control(options),
3 marker = L.marker([0, 0], {draggable: true}),
4 locationFilter = new L.LocationFilter({
9 control.onAdd = function (map) {
10 var $container = $('<div>')
11 .attr('class', 'control-share');
14 .attr('class', 'control-button')
16 .attr('title', I18n.t('javascripts.share.title'))
17 .html('<span class="icon share"></span>')
19 .appendTo($container);
22 .attr('class', 'share-ui');
25 .attr('class', 'sidebar_heading')
29 .text(I18n.t('javascripts.close'))
30 .attr('class', 'icon close')
31 .bind('click', toggle))
34 .text(I18n.t('javascripts.share.title')));
38 var $linkSection = $('<div>')
39 .attr('class', 'section share-link')
43 .text(I18n.t('javascripts.share.link'))
44 .appendTo($linkSection);
46 var $form = $('<form>')
47 .attr('class', 'standard-form')
48 .appendTo($linkSection);
51 .attr('class', 'form-row')
55 .attr('for', 'link_marker')
58 .attr('id', 'link_marker')
59 .attr('type', 'checkbox')
60 .bind('change', toggleMarker))
61 .append(I18n.t('javascripts.share.include_marker')));
64 .attr('class', 'share-tabs')
67 .attr('class', 'active')
68 .attr('for', 'long_input')
69 .attr('id', 'long_link')
70 .text(I18n.t('javascripts.share.long_link')))
72 .attr('for', 'short_input')
73 .attr('id', 'short_link')
74 .text(I18n.t('javascripts.share.short_link')))
76 .attr('for', 'embed_html')
78 .text(I18n.t('javascripts.share.embed')))
79 .on('click', 'a', function(e) {
81 var id = '#' + $(this).attr('for');
82 $linkSection.find('.share-tabs a')
83 .removeClass('active');
84 $(this).addClass('active');
85 $linkSection.find('.share-tab')
87 $linkSection.find('.share-tab:has(' + id + ')')
89 .find('input, textarea')
94 .attr('class', 'form-row share-tab')
95 .css('display', 'block')
98 .attr('id', 'long_input')
100 .on('click', select));
103 .attr('class', 'form-row share-tab')
106 .attr('id', 'short_input')
107 .attr('type', 'text')
108 .on('click', select));
111 .attr('class', 'form-row share-tab')
115 .attr('id', 'embed_html')
116 .on('click', select))
119 .attr('class', 'deemphasize')
120 .text(I18n.t('javascripts.share.paste_html'))
121 .appendTo($linkSection));
125 var $geoUriSection = $('<div>')
126 .attr('class', 'section share-geo-uri')
130 .text(I18n.t('javascripts.share.geo_uri'))
131 .appendTo($geoUriSection);
134 .appendTo($geoUriSection)
136 .attr('id', 'geo_uri'));
140 var $imageSection = $('<div>')
141 .attr('class', 'section share-image')
145 .text(I18n.t('javascripts.share.image'))
146 .appendTo($imageSection);
149 .attr('id', 'export-warning')
150 .attr('class', 'deemphasize')
151 .text(I18n.t('javascripts.share.only_standard_layer'))
152 .appendTo($imageSection);
155 .attr('id', 'export-image')
156 .attr('class', 'standard-form')
157 .attr('action', '/export/finish')
158 .attr('method', 'post')
159 .appendTo($imageSection);
162 .attr('class', 'form-row')
166 .attr('for', 'image_filter')
169 .attr('id', 'image_filter')
170 .attr('type', 'checkbox')
171 .bind('change', toggleFilter))
172 .append(I18n.t('javascripts.share.custom_dimensions')));
175 .attr('class', 'form-row')
179 .attr('for', 'mapnik_format')
180 .text(I18n.t('javascripts.share.format')))
181 .append($('<select>')
182 .attr('name', 'mapnik_format')
183 .attr('id', 'mapnik_format')
184 .append($('<option>').val('png').text('PNG').prop('selected', true))
185 .append($('<option>').val('jpeg').text('JPEG'))
186 .append($('<option>').val('svg').text('SVG'))
187 .append($('<option>').val('pdf').text('PDF')));
190 .attr('class', 'form-row')
193 .attr('for', 'mapnik_scale')
194 .text(I18n.t('javascripts.share.scale')))
197 .attr('name', 'mapnik_scale')
198 .attr('id', 'mapnik_scale')
199 .attr('type', 'text')
200 .on('change', update));
202 ['minlon', 'minlat', 'maxlon', 'maxlat'].forEach(function(name) {
204 .attr('id', 'mapnik_' + name)
206 .attr('type', 'hidden')
211 .attr('name', 'format')
212 .attr('value', 'mapnik')
213 .attr('type', 'hidden')
217 .attr('class', 'deemphasize')
218 .html(I18n.t('javascripts.share.image_size') + ' <span id="mapnik_image_width"></span> x <span id="mapnik_image_height"></span>')
222 .attr('type', 'submit')
223 .attr('value', I18n.t('javascripts.share.download'))
227 .on('change', update)
230 marker.on('dragend', movedMarker);
231 map.on('move', movedMap);
232 map.on('moveend layeradd layerremove', update);
234 options.sidebar.addPane($ui);
240 map.removeLayer(marker);
241 map.options.scrollWheelZoom = map.options.doubleClickZoom = true;
242 locationFilter.disable();
250 $('#mapnik_scale').val(getScale());
251 marker.setLatLng(map.getCenter());
254 options.sidebar.togglePane($ui, button);
255 $('.leaflet-control .control-button').tooltip('hide');
258 function toggleMarker() {
259 if ($(this).is(':checked')) {
260 marker.setLatLng(map.getCenter());
261 map.addLayer(marker);
262 map.options.scrollWheelZoom = map.options.doubleClickZoom = 'center';
264 map.removeLayer(marker);
265 map.options.scrollWheelZoom = map.options.doubleClickZoom = true;
270 function toggleFilter() {
271 if ($(this).is(':checked')) {
272 locationFilter.setBounds(map.getBounds().pad(-0.2));
273 locationFilter.enable();
275 locationFilter.disable();
280 function movedMap() {
281 marker.setLatLng(map.getCenter());
285 function movedMarker() {
286 if (map.hasLayer(marker)) {
287 map.off('move', movedMap);
288 map.on('moveend', updateOnce);
289 map.panTo(marker.getLatLng());
293 function updateOnce() {
294 map.off('moveend', updateOnce);
295 map.on('move', movedMap);
299 function escapeHTML(string) {
307 return string === null ? '' : (string + '').replace(/[&<>"']/g, function(match) {
308 return htmlEscapes[match];
313 var bounds = map.getBounds();
316 .prop('checked', map.hasLayer(marker));
319 .prop('checked', locationFilter.isEnabled());
323 $('#short_input').val(map.getShortUrl(marker));
324 $('#long_input').val(map.getUrl(marker));
325 $('#short_link').attr('href', map.getShortUrl(marker));
326 $('#long_link').attr('href', map.getUrl(marker));
329 bbox: bounds.toBBoxString(),
330 layer: map.getMapBaseLayerId()
333 if (map.hasLayer(marker)) {
334 var latLng = marker.getLatLng().wrap();
335 params.marker = latLng.lat + ',' + latLng.lng;
338 $('#embed_html').val(
339 '<iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="' +
340 escapeHTML('http://' + OSM.SERVER_URL + '/export/embed.html?' + $.param(params)) +
341 '" style="border: 1px solid black"></iframe><br/>' +
342 '<small><a href="' + escapeHTML(map.getUrl(marker)) + '">' +
343 escapeHTML(I18n.t('javascripts.share.view_larger_map')) + '</a></small>');
348 .attr('href', map.getGeoUri(marker))
349 .html(map.getGeoUri(marker));
353 if (locationFilter.isEnabled()) {
354 bounds = locationFilter.getBounds();
357 var scale = $("#mapnik_scale").val(),
358 size = L.bounds(L.CRS.EPSG3857.project(bounds.getSouthWest()),
359 L.CRS.EPSG3857.project(bounds.getNorthEast())).getSize(),
360 maxScale = Math.floor(Math.sqrt(size.x * size.y / 0.3136));
362 $('#mapnik_minlon').val(bounds.getWest());
363 $('#mapnik_minlat').val(bounds.getSouth());
364 $('#mapnik_maxlon').val(bounds.getEast());
365 $('#mapnik_maxlat').val(bounds.getNorth());
367 if (scale < maxScale) {
368 scale = roundScale(maxScale);
369 $("#mapnik_scale").val(scale);
372 $("#mapnik_image_width").text(Math.round(size.x / scale / 0.00028));
373 $("#mapnik_image_height").text(Math.round(size.y / scale / 0.00028));
375 if (map.getMapBaseLayerId() === 'mapnik') {
376 $('#export-image').show();
377 $('#export-warning').hide();
379 $('#export-image').hide();
380 $('#export-warning').show();
388 function getScale() {
389 var bounds = map.getBounds(),
390 centerLat = bounds.getCenter().lat,
391 halfWorldMeters = 6378137 * Math.PI * Math.cos(centerLat * Math.PI / 180),
392 meters = halfWorldMeters * (bounds.getEast() - bounds.getWest()) / 180,
393 pixelsPerMeter = map.getSize().x / meters,
394 metersPerPixel = 1 / (92 * 39.3701);
395 return Math.round(1 / (pixelsPerMeter * metersPerPixel));
398 function roundScale(scale) {
399 var precision = 5 * Math.pow(10, Math.floor(Math.LOG10E * Math.log(scale)) - 2);
400 return precision * Math.ceil(scale / precision);
403 return $container[0];