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