]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/index/search.js
Fix root link
[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   $("#query")
10     .on("focus", function() {
11       $("#describe_location").fadeOut(100);
12     })
13     .on("blur", function() {
14       $("#describe_location").fadeIn(100);
15     });
16
17   $("#sidebar_content").on("click", ".search_results_entry a.set_position", clickSearchResult);
18
19   var marker = L.marker([0, 0], {icon: getUserIcon()});
20
21   function submitSearch(e) {
22     e.preventDefault();
23
24     var bounds = map.getBounds();
25
26     $("#sidebar_content").load($(this).attr("action"), {
27       query: $("#query").val(),
28       zoom: map.getZoom(),
29       minlon: bounds.getWest(),
30       minlat: bounds.getSouth(),
31       maxlon: bounds.getEast(),
32       maxlat: bounds.getNorth()
33     });
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_content").load($(this).attr("href"), {
70       lat: center.lat,
71       lon: center.lng,
72       zoom: zoom
73     });
74   }
75 }