]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/index/search.js
486c9b36bbc521f3551635c34a755141365bd235
[rails.git] / app / assets / javascripts / index / search.js
1 function initializeSearch(map) {
2   $("#search_form").submit(submitSearch);
3
4   if ($("#query").val()) {
5     $("#search_form").submit();
6   }
7
8   // Focus the search field for browsers that don't support
9   // the HTML5 'autofocus' attribute
10   if (!("autofocus" in document.createElement("input"))) {
11     $("#query").focus();
12   }
13
14   $("#sidebar_content").on("click", ".search_results_entry a.set_position", clickSearchResult);
15
16   var marker = L.marker([0, 0], {icon: getUserIcon()});
17
18   function submitSearch(e) {
19     e.preventDefault();
20
21     var bounds = map.getBounds();
22
23     $("#sidebar_title").html(I18n.t('site.sidebar.search_results'));
24     $("#sidebar_content").load($(this).attr("action"), {
25       query: $("#query").val(),
26       minlon: bounds.getWest(),
27       minlat: bounds.getSouth(),
28       maxlon: bounds.getEast(),
29       maxlat: bounds.getNorth()
30     });
31
32     openSidebar();
33
34     $("#sidebar").one("closed", function () {
35       map.removeLayer(marker);
36       map.removeObject();
37     });
38   }
39
40   function clickSearchResult(e) {
41     e.preventDefault();
42
43     var data = $(this).data(),
44       center = L.latLng(data.lat, data.lon);
45
46     if (data.minLon && data.minLat && data.maxLon && data.maxLat) {
47       map.fitBounds([[data.minLat, data.minLon],
48                      [data.maxLat, data.maxLon]]);
49     } else {
50       map.setView(center, data.zoom);
51     }
52
53     marker
54       .setLatLng(center)
55       .addTo(map);
56
57     if (data.type && data.id) {
58       map.addObject(data, { zoom: false, style: { opacity: 0.2, fill: false } });
59     }
60   }
61 }