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('class', 'standard-form')
150 .attr('action', '/export/finish')
151 .attr('method', 'post')
152 .appendTo($imageSection);
155 .attr('class', 'form-row')
159 .attr('for', 'image_filter')
162 .attr('id', 'image_filter')
163 .attr('type', 'checkbox')
164 .bind('change', toggleFilter))
165 .append(I18n.t('javascripts.share.custom_dimensions')));
168 .attr('class', 'form-row')
172 .attr('for', 'mapnik_format')
173 .text(I18n.t('javascripts.share.format')))
174 .append($('<select>')
175 .attr('name', 'mapnik_format')
176 .attr('id', 'mapnik_format')
177 .append($('<option>').val('png').text('PNG').prop('selected', true))
178 .append($('<option>').val('jpeg').text('JPEG'))
179 .append($('<option>').val('svg').text('SVG'))
180 .append($('<option>').val('pdf').text('PDF')));
183 .attr('class', 'form-row')
186 .attr('for', 'mapnik_scale')
187 .text(I18n.t('javascripts.share.scale')))
190 .attr('name', 'mapnik_scale')
191 .attr('id', 'mapnik_scale')
192 .attr('type', 'text')
193 .on('change', update));
195 ['minlon', 'minlat', 'maxlon', 'maxlat'].forEach(function(name) {
197 .attr('id', 'mapnik_' + name)
199 .attr('type', 'hidden')
204 .attr('name', 'format')
205 .attr('value', 'mapnik')
206 .attr('type', 'hidden')
210 .attr('class', 'deemphasize')
211 .html(I18n.t('javascripts.share.image_size') + ' <span id="mapnik_image_width"></span> x <span id="mapnik_image_height"></span>')
215 .attr('type', 'submit')
216 .attr('value', I18n.t('javascripts.share.download'))
220 .on('change', update)
223 marker.on('dragend', movedMarker);
224 map.on('move', movedMap);
225 map.on('moveend layeradd layerremove', update);
227 options.sidebar.addPane($ui);
233 map.removeLayer(marker);
234 map.options.scrollWheelZoom = map.options.doubleClickZoom = true;
235 locationFilter.disable();
243 $('#mapnik_scale').val(getScale());
244 marker.setLatLng(map.getCenter());
247 options.sidebar.togglePane($ui, button);
248 $('.leaflet-control .control-button').tooltip('hide');
251 function toggleMarker() {
252 if ($(this).is(':checked')) {
253 marker.setLatLng(map.getCenter());
254 map.addLayer(marker);
255 map.options.scrollWheelZoom = map.options.doubleClickZoom = 'center';
257 map.removeLayer(marker);
258 map.options.scrollWheelZoom = map.options.doubleClickZoom = true;
263 function toggleFilter() {
264 if ($(this).is(':checked')) {
265 locationFilter.setBounds(map.getBounds().pad(-0.2));
266 locationFilter.enable();
268 locationFilter.disable();
273 function movedMap() {
274 marker.setLatLng(map.getCenter());
278 function movedMarker() {
279 if (map.hasLayer(marker)) {
280 map.off('move', movedMap);
281 map.on('moveend', updateOnce);
282 map.panTo(marker.getLatLng());
286 function updateOnce() {
287 map.off('moveend', updateOnce);
288 map.on('move', movedMap);
292 function escapeHTML(string) {
300 return string === null ? '' : (string + '').replace(/[&<>"']/g, function(match) {
301 return htmlEscapes[match];
306 var bounds = map.getBounds();
309 .prop('checked', map.hasLayer(marker));
312 .prop('checked', locationFilter.isEnabled());
316 $('#short_input').val(map.getShortUrl(marker));
317 $('#long_input').val(map.getUrl(marker));
318 $('#short_link').attr('href', map.getShortUrl(marker));
319 $('#long_link').attr('href', map.getUrl(marker));
322 bbox: bounds.toBBoxString(),
323 layer: map.getMapBaseLayerId()
326 if (map.hasLayer(marker)) {
327 var latLng = marker.getLatLng().wrap();
328 params.marker = latLng.lat + ',' + latLng.lng;
331 $('#embed_html').val(
332 '<iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="' +
333 escapeHTML('http://' + OSM.SERVER_URL + '/export/embed.html?' + $.param(params)) +
334 '" style="border: 1px solid black"></iframe><br/>' +
335 '<small><a href="' + escapeHTML(map.getUrl(marker)) + '">' +
336 escapeHTML(I18n.t('javascripts.share.view_larger_map')) + '</a></small>');
341 .attr('href', map.getGeoUri(marker))
342 .html(map.getGeoUri(marker));
346 if (locationFilter.isEnabled()) {
347 bounds = locationFilter.getBounds();
350 var scale = $("#mapnik_scale").val(),
351 size = L.bounds(L.CRS.EPSG3857.project(bounds.getSouthWest()),
352 L.CRS.EPSG3857.project(bounds.getNorthEast())).getSize(),
353 maxScale = Math.floor(Math.sqrt(size.x * size.y / 0.3136));
355 $('#mapnik_minlon').val(bounds.getWest());
356 $('#mapnik_minlat').val(bounds.getSouth());
357 $('#mapnik_maxlon').val(bounds.getEast());
358 $('#mapnik_maxlat').val(bounds.getNorth());
360 if (scale < maxScale) {
361 scale = roundScale(maxScale);
362 $("#mapnik_scale").val(scale);
365 $("#mapnik_image_width").text(Math.round(size.x / scale / 0.00028));
366 $("#mapnik_image_height").text(Math.round(size.y / scale / 0.00028));
373 function getScale() {
374 var bounds = map.getBounds(),
375 centerLat = bounds.getCenter().lat,
376 halfWorldMeters = 6378137 * Math.PI * Math.cos(centerLat * Math.PI / 180),
377 meters = halfWorldMeters * (bounds.getEast() - bounds.getWest()) / 180,
378 pixelsPerMeter = map.getSize().x / meters,
379 metersPerPixel = 1 / (92 * 39.3701);
380 return Math.round(1 / (pixelsPerMeter * metersPerPixel));
383 function roundScale(scale) {
384 var precision = 5 * Math.pow(10, Math.floor(Math.LOG10E * Math.log(scale)) - 2);
385 return precision * Math.ceil(scale / precision);
388 return $container[0];