]> git.openstreetmap.org Git - rails.git/blob - public/javascripts/site.js
typo
[rails.git] / public / javascripts / site.js
1 /*
2  * Called as the user scrolls/zooms around to aniplate hrefs of the
3  * view tab and various other links
4  */
5 function updatelinks(lon,lat,zoom,layers,minlon,minlat,maxlon,maxlat) {
6   var decimals = Math.pow(10, Math.floor(zoom/3));
7   var node;
8
9   lat = Math.round(lat * decimals) / decimals;
10   lon = Math.round(lon * decimals) / decimals;
11
12   node = document.getElementById("permalinkanchor");
13   if (node) {
14     var args = getArgs(node.href);
15     args["lat"] = lat;
16     args["lon"] = lon;
17     args["zoom"] = zoom;
18     if (layers) {
19       args["layers"] = layers;
20     }
21     node.href = setArgs(node.href, args);
22   }
23
24   node = document.getElementById("viewanchor");
25   if (node) {
26     var args = getArgs(node.href);
27     args["lat"] = lat;
28     args["lon"] = lon;
29     args["zoom"] = zoom;
30     if (layers) {
31       args["layers"] = layers;
32     }
33     node.href = setArgs(node.href, args);
34   }
35
36   node = document.getElementById("exportanchor");
37   if (node) {
38     var args = getArgs(node.href);
39     args["lat"] = lat;
40     args["lon"] = lon;
41     args["zoom"] = zoom;
42     if (layers) {
43       args["layers"] = layers;
44     }
45     node.href = setArgs(node.href, args);
46   }
47
48   node = document.getElementById("editanchor");
49   if (node) {
50     if (zoom >= 13) {
51       var args = new Object();
52       args.lat = lat;
53       args.lon = lon;
54       args.zoom = zoom;
55       node.href = setArgs("/edit", args);
56       node.style.fontStyle = 'normal';
57     } else {
58       node.href = 'javascript:alert("zoom in to edit map");';
59       node.style.fontStyle = 'italic';
60     }
61   }
62   
63   node = document.getElementById("historyanchor");
64   if (node) {
65     if (zoom >= 11) {
66       var args = new Object();
67       //set bbox param from 'extents' object
68       if (typeof minlon == "number" &&
69           typeof minlat == "number" &&
70           typeof maxlon == "number" &&
71           typeof maxlat == "number") {
72       
73         minlon = Math.round(minlon * decimals) / decimals;
74         minlat = Math.round(minlat * decimals) / decimals;
75         maxlon = Math.round(maxlon * decimals) / decimals;
76         maxlat = Math.round(maxlat * decimals) / decimals;
77         args.bbox = minlon + "," + minlat + "," + maxlon + "," + maxlat;
78       }
79       
80       node.href = setArgs("/history", args);
81       node.style.fontStyle = 'normal';
82     } else {
83       node.href = 'javascript:alert("zoom in to see editing history");';
84       node.style.fontStyle = 'italic';
85     }
86   }
87 }
88
89 function getArgs(url) {
90   var args = new Object();
91   var querystart = url.indexOf("?");
92
93   if (querystart >= 0) {
94      var querystring = url.substring(querystart + 1);
95      var queryitems = querystring.split("&");
96
97      for (var i = 0; i < queryitems.length; i++) {
98         if (match = queryitems[i].match(/^(.*)=(.*)$/)) {
99            args[unescape(match[1])] = unescape(match[2]);
100         } else {
101            args[unescape(queryitems[i])] = null
102         }
103      }
104   }
105
106   return args;
107 }
108
109 /*
110  * Called to set the arguments on a URL from the given hash.
111  */
112 function setArgs(url, args) {
113    var queryitems = new Array();
114
115    for (arg in args)
116    {
117       if (args[arg] == null) {
118          queryitems.push(escape(arg));
119       } else {
120          queryitems.push(escape(arg) + "=" + escape(args[arg]));
121       }
122    }
123
124    return url.replace(/\?.*$/, "") + "?" + queryitems.join("&");
125 }
126
127 /*
128  * Called to get the arguments from a URL as a hash.
129  */
130 function getStyle(el, property) {
131   var style;
132
133   if (el.currentStyle) {
134     style = el.currentStyle[property];
135   } else if( window.getComputedStyle ) {
136     style = document.defaultView.getComputedStyle(el,null).getPropertyValue(property);
137   } else {
138     style = el.style[property];
139   }
140
141   return style;
142 }
143
144 /*
145  * Called to interpolate JavaScript variables in strings using a
146  * similar syntax to rails I18n string interpolation - the only
147  * difference is that [[foo]] is the placeholder syntax instead
148  * of {{foo}} which allows the same string to be processed by both
149  * rails and then later by javascript.
150  */
151 function i18n(string, keys) {
152   for (var key in keys) {
153     var re_key = '\\[\\[' + key + '\\]\\]';
154     var re = new RegExp(re_key, "g");
155       
156     string = string.replace(re, keys[key]);
157   }
158    
159   return string;
160