]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/index/search.js
Merge remote-tracking branch 'upstream/pull/5936'
[rails.git] / app / assets / javascripts / index / search.js
1 OSM.Search = function (map) {
2   $(".search_form input[name=query]").on("input", function (e) {
3     if ($(e.target).val() === "") {
4       $(".describe_location").fadeIn(100);
5     } else {
6       $(".describe_location").fadeOut(100);
7     }
8   });
9
10   $(".search_form a.btn.switch_link").on("click", function (e) {
11     e.preventDefault();
12     const query = $(this).closest("form").find("input[name=query]").val();
13     let search = "";
14     if (query) search = "?" + new URLSearchParams({ to: query });
15     OSM.router.route("/directions" + search + OSM.formatHash(map));
16   });
17
18   $(".search_form").on("submit", function (e) {
19     e.preventDefault();
20     $("header").addClass("closed");
21     const params = new URLSearchParams({
22       query: this.elements.query.value,
23       zoom: map.getZoom(),
24       minlon: map.getBounds().getWest(),
25       minlat: map.getBounds().getSouth(),
26       maxlon: map.getBounds().getEast(),
27       maxlat: map.getBounds().getNorth()
28     });
29     const search = params.get("query") ? `/search?${params}` : "/";
30     OSM.router.route(search + OSM.formatHash(map));
31   });
32
33   $(".describe_location").on("click", function (e) {
34     e.preventDefault();
35     $("header").addClass("closed");
36     const zoom = map.getZoom();
37     const [lat, lon] = OSM.cropLocation(map.getCenter(), zoom);
38
39     OSM.router.route("/search?" + new URLSearchParams({ lat, lon, zoom }));
40   });
41
42   $("#sidebar_content")
43     .on("click", ".search_more a", clickSearchMore)
44     .on("click", ".search_results_entry a.set_position", clickSearchResult)
45     .on("mouseover", "li.search_results_entry:has(a.set_position)", showSearchResult)
46     .on("mouseout", "li.search_results_entry:has(a.set_position)", hideSearchResult);
47
48   const markers = L.layerGroup().addTo(map);
49
50   function clickSearchMore(e) {
51     e.preventDefault();
52     e.stopPropagation();
53
54     const div = $(this).parents(".search_more");
55
56     $(this).hide();
57     div.find(".loader").prop("hidden", false);
58
59     fetch($(this).attr("href"), {
60       method: "POST",
61       body: new URLSearchParams(OSM.csrf)
62     })
63       .then(response => response.text())
64       .then(data => div.replaceWith(data));
65   }
66
67   function showSearchResult() {
68     let marker = $(this).data("marker");
69
70     if (!marker) {
71       const data = $(this).find("a.set_position").data();
72
73       marker = L.marker([data.lat, data.lon], { icon: OSM.getMarker({}) });
74
75       $(this).data("marker", marker);
76     }
77
78     markers.addLayer(marker);
79   }
80
81   function hideSearchResult() {
82     const marker = $(this).data("marker");
83
84     if (marker) {
85       markers.removeLayer(marker);
86     }
87   }
88
89   function panToSearchResult(data) {
90     if (data.minLon && data.minLat && data.maxLon && data.maxLat) {
91       map.fitBounds([[data.minLat, data.minLon], [data.maxLat, data.maxLon]]);
92     } else {
93       map.setView([data.lat, data.lon], data.zoom);
94     }
95   }
96
97   function clickSearchResult(e) {
98     const data = $(this).data();
99
100     panToSearchResult(data);
101
102     // Let clicks to object browser links propagate.
103     if (data.type && data.id) return;
104
105     e.preventDefault();
106     e.stopPropagation();
107   }
108
109   const page = {};
110
111   page.pushstate = page.popstate = function (path) {
112     const params = new URLSearchParams(path.substring(path.indexOf("?")));
113     if (params.has("query")) {
114       $(".search_form input[name=query]").val(params.get("query"));
115       $(".describe_location").hide();
116     } else if (params.has("lat") && params.has("lon")) {
117       $(".search_form input[name=query]").val(params.get("lat") + ", " + params.get("lon"));
118       $(".describe_location").hide();
119     }
120     OSM.loadSidebarContent(path, page.load);
121   };
122
123   page.load = function () {
124     $(".search_results_entry").each(function (index) {
125       const entry = $(this);
126       fetch(entry.data("href"), {
127         method: "POST",
128         body: new URLSearchParams(OSM.csrf)
129       })
130         .then(response => response.text())
131         .then(function (html) {
132           entry.html(html);
133           // go to first result of first geocoder
134           if (index === 0) {
135             const firstResult = entry.find("*[data-lat][data-lon]:first").first();
136             if (firstResult.length) {
137               panToSearchResult(firstResult.data());
138             }
139           }
140         });
141     });
142
143     return map.getState();
144   };
145
146   page.unload = function () {
147     markers.clearLayers();
148     $(".search_form input[name=query]").val("");
149     $(".describe_location").fadeIn(100);
150   };
151
152   return page;
153 };