]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/index.js
Improve layout and styling of forms
[rails.git] / app / assets / javascripts / index.js
1 //= require_self
2 //= require index/browse
3 //= require index/export
4 //= require index/key
5 //= require index/notes
6
7 $(document).ready(function () {
8   var permalinks = $("#permalink").detach().html();
9   var marker;
10   var params = OSM.mapParams();
11   var map = createMap("map");
12
13   L.control.scale().addTo(map);
14
15   map.attributionControl.setPrefix(permalinks);
16
17   map.on("moveend layeradd layerremove", updateLocation);
18
19   if (!params.object_zoom) {
20     if (params.bbox) {
21       var bbox = L.latLngBounds([params.minlat, params.minlon],
22                                 [params.maxlat, params.maxlon]);
23
24       map.fitBounds(bbox);
25
26       if (params.box) {
27         addBoxToMap(bbox);
28       }
29     } else {
30       map.setView([params.lat, params.lon], params.zoom);
31     }
32   }
33
34   if (params.layers) {
35     setMapLayers(params.layers);
36   }
37
38   if (params.marker) {
39     marker = L.marker([params.mlat, params.mlon], {icon: getUserIcon()}).addTo(map);
40   }
41
42   if (params.object) {
43     addObjectToMap(params.object, params.object_zoom);
44   }
45
46   handleResize();
47
48   $("body").on("click", "a.set_position", function (e) {
49     e.preventDefault();
50
51     var data = $(this).data();
52     var centre = L.latLng(data.lat, data.lon);
53
54     if (data.minLon && data.minLat && data.maxLon && data.maxLat) {
55       map.fitBounds([[data.minLat, data.minLon],
56                      [data.maxLat, data.maxLon]]);
57     } else {
58       map.setView(centre, data.zoom);
59     }
60
61     if (data.type && data.id) {
62       addObjectToMap(data, true);
63     }
64
65     if (marker) {
66       map.removeLayer(marker);
67     }
68
69     marker = L.marker(centre, {icon: getUserIcon()}).addTo(map);
70   });
71
72   function updateLocation() {
73     var center = map.getCenter().wrap();
74     var zoom = map.getZoom();
75     var layers = getMapLayers();
76     var extents = map.getBounds().wrap();
77
78     updatelinks(center.lng,
79                 center.lat,
80                 zoom,
81                 layers,
82                 extents.getWest(),
83                 extents.getSouth(),
84                 extents.getEast(),
85                 extents.getNorth(),
86                 params.object);
87
88     var expiry = new Date();
89     expiry.setYear(expiry.getFullYear() + 10);
90     $.cookie("_osm_location", [center.lng, center.lat, zoom, layers].join("|"), {expires: expiry});
91   }
92
93   function remoteEditHandler() {
94     var extent = map.getBounds();
95     var loaded = false;
96
97     $("#linkloader").load(function () { loaded = true; });
98     $("#linkloader").attr("src", "http://127.0.0.1:8111/load_and_zoom?left=" + extent.getWest()
99                                                                    + "&bottom=" + extent.getSouth()
100                                                                    + "&right=" + extent.getEast()
101                                                                    + "&top=" + extent.getNorth());
102
103     setTimeout(function () {
104       if (!loaded) alert(I18n.t('site.index.remote_failed'));
105     }, 1000);
106
107     return false;
108   }
109
110   $("a[data-editor=remote]").click(remoteEditHandler);
111
112   if (OSM.preferred_editor == "remote" && $('body').hasClass("site-edit")) {
113     remoteEditHandler();
114   }
115
116   $(window).resize(handleResize);
117
118   $("#search_form").submit(function () {
119     var bounds = map.getBounds();
120
121     $("#sidebar_title").html(I18n.t('site.sidebar.search_results'));
122     $("#sidebar_content").load($(this).attr("action"), {
123       query: $("#query").val(),
124       minlon: bounds.getWest(),
125       minlat: bounds.getSouth(),
126       maxlon: bounds.getEast(),
127       maxlat: bounds.getNorth()
128     }, openSidebar);
129
130     return false;
131   });
132
133   if ($("#query").val()) {
134     $("#search_form").submit();
135   }
136
137   // Focus the search field for browsers that don't support
138   // the HTML5 'autofocus' attribute
139   if (!("autofocus" in document.createElement("input"))) {
140     $("#query").focus();
141   }
142 });