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