]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/application.js
Add share button to map
[rails.git] / app / assets / javascripts / application.js
1 //= require jquery
2 //= require jquery_ujs
3 //= require jquery.timers
4 //= require jquery.cookie
5 //= require augment
6 //= require leaflet
7 //= require leaflet.osm
8 //= require leaflet.locationfilter
9 //= require leaflet.locate
10 //= require i18n/translations
11 //= require oauth
12 //= require osm
13 //= require piwik
14 //= require map
15 //= require menu
16 //= require sidebar
17 //= require leaflet.share
18 //= require richtext
19 //= require resize
20 //= require geocoder
21
22 function zoomPrecision(zoom) {
23     var decimals = Math.pow(10, Math.floor(zoom/3));
24     return function(x) {
25          return Math.round(x * decimals) / decimals;
26     };
27 }
28
29 /*
30  * Called as the user scrolls/zooms around to aniplate hrefs of the
31  * view tab and various other links
32  */
33 function updatelinks(lon,lat,zoom,layers,minlon,minlat,maxlon,maxlat,object) {
34   var toPrecision = zoomPrecision(zoom);
35   var node;
36
37   lat = toPrecision(lat);
38   lon = toPrecision(lon);
39
40   if (minlon) {
41     minlon = toPrecision(minlon);
42     minlat = toPrecision(minlat);
43     maxlon = toPrecision(maxlon);
44     maxlat = toPrecision(maxlat);
45   }
46
47   $(".geolink").each(function (index, link) {
48     var args = getArgs(link.href);
49
50     if ($(link).hasClass("llz")) {
51       args.lat = lat;
52       args.lon = lon;
53       args.zoom = zoom;
54     } else if (minlon && $(link).hasClass("bbox")) {
55       args.bbox = minlon + "," + minlat + "," + maxlon + "," + maxlat;
56     }
57
58     if (layers && $(link).hasClass("layers")) {
59       args.layers = layers;
60     }
61
62     if (object && $(link).hasClass("object")) {
63       args[object.type] = object.id;
64     }
65
66     var minzoom = $(link).data("minzoom");
67     if (minzoom) {
68         var name = link.id.replace(/anchor$/, "");
69
70         $(link).off("click.minzoom");
71
72         if (zoom >= minzoom) {
73           $(link).attr("title", I18n.t("javascripts.site." + name + "_tooltip"));
74           $(link).removeClass("disabled");
75         } else {
76           $(link).on("click.minzoom", function () { alert(I18n.t("javascripts.site." + name + "_zoom_alert")); return false; });
77           $(link).attr("title", I18n.t("javascripts.site." + name + "_disabled_tooltip"));
78           $(link).addClass("disabled");
79         }
80     }
81
82     link.href = setArgs(link.href, args);
83   });
84
85   $("#shortlinkanchor").each(function () {
86     var args = getArgs(this.href);
87     var code = makeShortCode(lat, lon, zoom);
88     var prefix = shortlinkPrefix();
89
90     // Add ?{node,way,relation}=id to the arguments
91     if (object) {
92       args[object.type] = object.id;
93     }
94
95     // This is a hack to omit the default mapnik layer from the shortlink.
96     if (layers && layers != "M") {
97       args.layers = layers;
98     }
99     else {
100       delete args.layers;
101     }
102
103     // Here we're assuming that all parameters but ?layers= and
104     // ?{node,way,relation}= can be safely omitted from the shortlink
105     // which encodes lat/lon/zoom. If new URL parameters are added to
106     // the main slippy map this needs to be changed.
107     if (args.layers || object) {
108       this.href = setArgs(prefix + "/go/" + code, args);
109     } else {
110       this.href = prefix + "/go/" + code;
111     }
112   });
113 }
114
115 /*
116  * Get the URL prefix to use for a short link
117  */
118 function shortlinkPrefix() {
119   if (window.location.hostname.match(/^www\.openstreetmap\.org/i)) {
120     return "http://osm.org";
121   } else {
122     return "";
123   }
124 }
125
126 /*
127  * Called to get the arguments from a URL as a hash.
128  */
129 function getArgs(url) {
130   var args = {};
131   var querystart = url.indexOf("?");
132
133   if (querystart >= 0) {
134      var querystring = url.substring(querystart + 1);
135      var queryitems = querystring.split("&");
136
137      for (var i = 0; i < queryitems.length; i++) {
138         if (match = queryitems[i].match(/^(.*)=(.*)$/)) {
139            args[unescape(match[1])] = unescape(match[2]);
140         } else {
141            args[unescape(queryitems[i])] = null;
142         }
143      }
144   }
145
146   return args;
147 }
148
149 /*
150  * Called to set the arguments on a URL from the given hash.
151  */
152 function setArgs(url, args) {
153    var queryitems = [];
154
155    for (arg in args) {
156       if (args[arg] == null) {
157          queryitems.push(escape(arg));
158       } else {
159          queryitems.push(escape(arg) + "=" + escape(args[arg]));
160       }
161    }
162
163    return url.replace(/\?.*$/, "") + "?" + queryitems.join("&");
164 }
165
166 /*
167  * Called to interlace the bits in x and y, making a Morton code.
168  */
169 function interlace(x, y) {
170     x = (x | (x << 8)) & 0x00ff00ff;
171     x = (x | (x << 4)) & 0x0f0f0f0f;
172     x = (x | (x << 2)) & 0x33333333;
173     x = (x | (x << 1)) & 0x55555555;
174
175     y = (y | (y << 8)) & 0x00ff00ff;
176     y = (y | (y << 4)) & 0x0f0f0f0f;
177     y = (y | (y << 2)) & 0x33333333;
178     y = (y | (y << 1)) & 0x55555555;
179
180     return (x << 1) | y;
181 }
182
183 /*
184  * Called to create a short code for the short link.
185  */
186 function makeShortCode(lat, lon, zoom) {
187     char_array = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_~";
188     var x = Math.round((lon + 180.0) * ((1 << 30) / 90.0));
189     var y = Math.round((lat +  90.0) * ((1 << 30) / 45.0));
190     // JavaScript only has to keep 32 bits of bitwise operators, so this has to be
191     // done in two parts. each of the parts c1/c2 has 30 bits of the total in it
192     // and drops the last 4 bits of the full 64 bit Morton code.
193     var str = "";
194     var c1 = interlace(x >>> 17, y >>> 17), c2 = interlace((x >>> 2) & 0x7fff, (y >>> 2) & 0x7fff);
195     for (var i = 0; i < Math.ceil((zoom + 8) / 3.0) && i < 5; ++i) {
196         digit = (c1 >> (24 - 6 * i)) & 0x3f;
197         str += char_array.charAt(digit);
198     }
199     for (var i = 5; i < Math.ceil((zoom + 8) / 3.0); ++i) {
200         digit = (c2 >> (24 - 6 * (i - 5))) & 0x3f;
201         str += char_array.charAt(digit);
202     }
203     for (var i = 0; i < ((zoom + 8) % 3); ++i) {
204         str += "-";
205     }
206     return str;
207 }
208
209 /*
210  * Forms which have been cached by rails may have he wrong
211  * authenticity token, so patch up any forms with the correct
212  * token taken from the page header.
213  */
214 $(document).ready(function () {
215   var auth_token = $("meta[name=csrf-token]").attr("content");
216   $("form input[name=authenticity_token]").val(auth_token);
217 });