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