]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/application.js
Wrap marker coordinates in URLs
[rails.git] / app / assets / javascripts / application.js
1 //= require jquery
2 //= require jquery_ujs
3 //= require jquery.timers
4 //= require jquery.cookie
5 //= require jquery.throttle-debounce
6 //= require augment
7 //= require osm
8 //= require leaflet
9 //= require leaflet.osm
10 //= require leaflet.hash
11 //= require leaflet.zoom
12 //= require leaflet.extend
13 //= require leaflet.locationfilter
14 //= require i18n/translations
15 //= require oauth
16 //= require piwik
17 //= require map
18 //= require menu
19 //= require sidebar
20 //= require richtext
21 //= require geocoder
22 //= require querystring
23
24 var querystring = require('querystring-component');
25
26 function zoomPrecision(zoom) {
27     return Math.max(0, Math.ceil(Math.log(zoom) / Math.LN2));
28 }
29
30 function normalBounds(bounds) {
31     if (bounds instanceof L.LatLngBounds) return bounds;
32     return new L.LatLngBounds(
33         new L.LatLng(bounds[0][0], bounds[0][1]),
34         new L.LatLng(bounds[1][0], bounds[1][1]));
35 }
36
37 function remoteEditHandler(bbox, select) {
38   var loaded = false,
39       query = {
40           left: bbox.getWest() - 0.0001,
41           top: bbox.getNorth() + 0.0001,
42           right: bbox.getEast() + 0.0001,
43           bottom: bbox.getSouth() - 0.0001
44       };
45
46   if (select) query.select = select;
47
48   var iframe = $('<iframe>')
49     .hide()
50     .appendTo('body')
51     .attr("src", "http://127.0.0.1:8111/load_and_zoom?" + querystring.stringify(query))
52     .on('load', function() {
53       $(this).remove();
54       loaded = true;
55     });
56
57   setTimeout(function () {
58     if (!loaded) {
59       alert(I18n.t('site.index.remote_failed'));
60       iframe.remove();
61     }
62   }, 1000);
63
64   return false;
65 }
66
67 /*
68  * Called as the user scrolls/zooms around to maniplate hrefs of the
69  * view tab and various other links
70  */
71 function updatelinks(loc, zoom, layers, bounds, object) {
72   $(".geolink").each(function(index, link) {
73     var href = link.href.split(/[?#]/)[0],
74         args = querystring.parse(link.search.substring(1));
75
76     if (bounds && $(link).hasClass("bbox")) args.bbox = normalBounds(bounds).toBBoxString();
77     if (object && $(link).hasClass("object")) args[object.type] = object.id;
78
79     var query = querystring.stringify(args);
80     if (query) href += '?' + query;
81
82     if ($(link).hasClass("llz")) {
83       args = {
84         lat: loc.lat,
85         lon: loc.lon || loc.lng,
86         zoom: zoom
87       };
88
89       if (layers && $(link).hasClass("layers")) {
90         args.layers = layers;
91       }
92
93       href += OSM.formatHash(args);
94     }
95
96     link.href = href;
97
98     var minzoom = $(link).data("minzoom");
99     if (minzoom) {
100       var name = link.id.replace(/anchor$/, "");
101       $(link).off("click.minzoom");
102       if (zoom >= minzoom) {
103         $(link)
104           .attr("title", I18n.t("javascripts.site." + name + "_tooltip"))
105           .removeClass("disabled");
106       } else {
107         $(link)
108           .attr("title", I18n.t("javascripts.site." + name + "_disabled_tooltip"))
109           .addClass("disabled")
110           .on("click.minzoom", function () {
111             alert(I18n.t("javascripts.site." + name + "_zoom_alert"));
112             return false;
113           });
114       }
115     }
116   });
117 }
118
119 // generate a cookie-safe string of map state
120 function cookieContent(map) {
121   var center = map.getCenter().wrap();
122   return [center.lng, center.lat, map.getZoom(), map.getLayersCode()].join('|');
123 }
124
125 function escapeHTML(string) {
126   var htmlEscapes = {
127     '&': '&amp;',
128     '<': '&lt;',
129     '>': '&gt;',
130     '"': '&quot;',
131     "'": '&#x27;'
132   };
133   return string == null ? '' : (string + '').replace(/[&<>"']/g, function(match) {
134       return htmlEscapes[match];
135   });
136 }
137
138 /*
139  * Forms which have been cached by rails may have the wrong
140  * authenticity token, so patch up any forms with the correct
141  * token taken from the page header.
142  */
143 $(document).ready(function () {
144   var auth_token = $("meta[name=csrf-token]").attr("content");
145   $("form input[name=authenticity_token]").val(auth_token);
146 });