1 $(document).ready(function () {
2 $("#exportanchor").click(function (e) {
3 $.ajax({ url: $(this).data('url'), success: function (sidebarHtml) {
4 startExport(sidebarHtml);
9 if (window.location.pathname == "/export") {
10 $("#exportanchor").click();
13 function startExport(sidebarHtml) {
20 vectors = new OpenLayers.Layer.Vector("Vector Layer", {
21 displayInLayerSwitcher: false
23 map.addLayer(vectors);
25 box = new OpenLayers.Control.DrawFeature(vectors, OpenLayers.Handler.RegularPolygon, {
33 box.handler.callbacks.done = endDrag;
36 transform = new OpenLayers.Control.TransformFeature(vectors, {
40 transform.events.register("transformcomplete", transform, transformComplete);
41 map.addControl(transform);
43 map.events.register("moveend", map, mapMoved);
44 map.events.register("changebaselayer", map, htmlUrlChanged);
46 $("#sidebar_title").html(I18n.t('export.start_rjs.export'));
47 $("#sidebar_content").html(sidebarHtml);
49 $("#maxlat,#minlon,#maxlon,#minlat").change(boundsChanged);
51 $("#drag_box").click(startDrag);
53 $("#add_marker").click(startMarker);
55 $("#format_osm,#format_mapnik,#format_html").click(formatChanged);
57 $("#mapnik_scale").change(mapnikSizeChanged);
61 if (map.baseLayer.name == "Mapnik") {
62 $("#format_mapnik").prop("checked", true);
66 setBounds(map.getExtent());
68 $("body").removeClass("site-index").addClass("site-export");
70 $("#sidebar").one("closed", function () {
71 $("body").removeClass("site-export").addClass("site-index");
75 map.events.unregister("moveend", map, mapMoved);
76 map.events.unregister("changebaselayer", map, htmlUrlChanged);
77 map.removeLayer(vectors);
80 function getMercatorBounds() {
81 var bounds = new OpenLayers.Bounds($("#minlon").val(), $("#minlat").val(),
82 $("#maxlon").val(), $("#maxlat").val());
87 function boundsChanged() {
88 var bounds = getMercatorBounds();
90 map.events.unregister("moveend", map, mapMoved);
91 map.zoomToExtent(bounds);
100 function startDrag() {
101 $("#drag_box").html(I18n.t('export.start_rjs.drag_a_box'));
107 function endDrag(bbox) {
108 var bounds = bbox.getBounds();
110 map.events.unregister("moveend", map, mapMoved);
116 $("#drag_box").html(I18n.t('export.start_rjs.manually_select'));
119 function transformComplete(event) {
120 setBounds(event.feature.geometry.bounds);
124 function startMarker() {
125 $("#add_marker").html(I18n.t('export.start_rjs.click_add_marker'));
128 markerLayer = new OpenLayers.Layer.Vector("",{
129 displayInLayerSwitcher: false,
131 externalGraphic: OpenLayers.Util.getImageLocation("marker.png"),
132 graphicXOffset: -10.5,
138 map.addLayer(markerLayer);
140 markerControl = new OpenLayers.Control.DrawFeature(markerLayer, OpenLayers.Handler.Point);
141 map.addControl(markerControl);
143 markerLayer.events.on({ "featureadded": endMarker });
146 markerLayer.destroyFeatures();
147 markerControl.activate();
152 function endMarker(event) {
153 markerControl.deactivate();
155 $("#add_marker").html(I18n.t('export.start_rjs.change_marker'));
156 $("#marker_inputs").show();
158 var geom = unproj(event.feature.geometry);
160 $("#marker_lon").val(geom.x.toFixed(5));
161 $("#marker_lat").val(geom.y.toFixed(5));
166 function clearMarker() {
167 $("#marker_lon,#marker_lat").val("");
168 $("#marker_inputs").hide();
169 $("#add_marker").html(I18n.t('export.start_rjs.add_marker'));
172 markerControl.destroy();
173 markerLayer.destroy();
175 markerControl = null;
179 function mapMoved() {
180 setBounds(map.getExtent());
184 function setBounds(bounds) {
185 var toPrecision = zoomPrecision(map.getZoom());
187 bounds = unproj(bounds);
189 $("#minlon").val(toPrecision(bounds.left));
190 $("#minlat").val(toPrecision(bounds.bottom));
191 $("#maxlon").val(toPrecision(bounds.right));
192 $("#maxlat").val(toPrecision(bounds.top));
198 function clearBox() {
199 transform.deactivate();
200 vectors.destroyFeatures();
203 function drawBox(bounds) {
204 var feature = new OpenLayers.Feature.Vector(bounds.toGeometry());
206 vectors.addFeatures(feature);
207 transform.setFeature(feature);
210 function validateControls() {
211 var bounds = new OpenLayers.Bounds($("#minlon").val(), $("#minlat").val(), $("#maxlon").val(), $("#maxlat").val());
213 if (bounds.getWidth() * bounds.getHeight() > OSM.MAX_REQUEST_AREA) {
214 $("#export_osm_too_large").show();
216 $("#export_osm_too_large").hide();
219 var max_scale = maxMapnikScale();
222 if ($("#format_osm").prop("checked")) {
223 disabled = bounds.getWidth() * bounds.getHeight() > OSM.MAX_REQUEST_AREA;
224 } else if ($("#format_mapnik").prop("checked")) {
225 disabled = $("#mapnik_scale").val() < max_scale;
228 $("#export_commit").prop("disabled", disabled);
229 $("#mapnik_max_scale").html(roundScale(max_scale));
232 function htmlUrlChanged() {
233 var bounds = new OpenLayers.Bounds($("#minlon").val(), $("#minlat").val(), $("#maxlon").val(), $("#maxlat").val());
234 var layerName = map.baseLayer.keyid;
235 var url = "http://" + OSM.SERVER_URL + "/export/embed.html?bbox=" + bounds.toBBOX() + "&layer=" + layerName;
238 if ($("#marker_lat").val() && $("#marker_lon").val()) {
239 markerUrl = "&mlat=" + $("#marker_lat").val() + "&mlon=" + $("#marker_lon").val();
240 url += "&marker=" + $("#marker_lat").val() + "," + $("#marker_lon").val();
243 var html = '<iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="'+url+'" style="border: 1px solid black"></iframe>';
245 // Create "larger map" link
246 var center = bounds.getCenterLonLat();
248 var zoom = map.getZoomForExtent(proj(bounds));
250 var layers = getMapLayers();
252 var text = I18n.t('export.start_rjs.view_larger_map');
255 for (var i = 0; i < text.length; ++i) {
256 var c = text.charCodeAt(i);
257 escaped.push(c < 127 ? text.charAt(i) : "&#" + c + ";");
260 html += '<br /><small><a href="http://' + OSM.SERVER_URL + '/?lat='+center.lat+'&lon='+center.lon+'&zoom='+zoom+'&layers='+layers+markerUrl+'">'+escaped.join("")+'</a></small>';
262 $("#export_html_text").val(html);
264 if ($("#format_html").prop("checked")) {
265 $("#export_html_text").prop("selected", true);
269 function formatChanged() {
270 $("#export_commit").show();
272 if ($("#format_osm").prop("checked")) {
273 $("#export_osm").show();
275 $("#export_osm").hide();
278 if ($("#format_mapnik").prop("checked")) {
279 $("#mapnik_scale").val(roundScale(map.getScale()));
280 $("#export_mapnik").show();
284 $("#export_mapnik").hide();
287 if ($("#format_html").prop("checked")) {
288 $("#export_html").show();
289 $("#export_commit").hide();
290 $("#export_html_text").prop("selected", true);
292 $("#export_html").hide();
300 function maxMapnikScale() {
301 var bounds = getMercatorBounds();
303 return Math.floor(Math.sqrt(bounds.getWidth() * bounds.getHeight() / 0.3136));
306 function mapnikImageSize(scale) {
307 var bounds = getMercatorBounds();
309 return new OpenLayers.Size(Math.round(bounds.getWidth() / scale / 0.00028),
310 Math.round(bounds.getHeight() / scale / 0.00028));
313 function roundScale(scale) {
314 var precision = 5 * Math.pow(10, Math.floor(Math.LOG10E * Math.log(scale)) - 2);
316 return precision * Math.ceil(scale / precision);
319 function mapnikSizeChanged() {
320 var size = mapnikImageSize($("#mapnik_scale").val());
322 $("#mapnik_image_width").html(size.w);
323 $("#mapnik_image_height").html(size.h);