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);
6 $(".describe_location").fadeOut(100);
10 $(".search_form a.btn.switch_link").on("click", function (e) {
12 const query = $(this).closest("form").find("input[name=query]").val();
14 if (query) search = "?" + new URLSearchParams({ to: query });
15 OSM.router.route("/directions" + search + OSM.formatHash(map));
18 $(".search_form").on("submit", function (e) {
20 $("header").addClass("closed");
21 const query = $(this).find("input[name=query]").val();
23 if (query) search = "/search?" + new URLSearchParams({ query });
24 OSM.router.route(search + OSM.formatHash(map));
27 $(".describe_location").on("click", function (e) {
29 $("header").addClass("closed");
30 const [lat, lon] = OSM.cropLocation(map.getCenter(), map.getZoom());
32 OSM.router.route("/search?" + new URLSearchParams({ lat, lon }));
36 .on("click", ".search_more a", clickSearchMore)
37 .on("click", ".search_results_entry a.set_position", clickSearchResult)
38 .on("mouseover", "li.search_results_entry:has(a.set_position)", showSearchResult)
39 .on("mouseout", "li.search_results_entry:has(a.set_position)", hideSearchResult);
41 const markers = L.layerGroup().addTo(map);
43 function clickSearchMore(e) {
47 const div = $(this).parents(".search_more");
50 div.find(".loader").show();
52 fetch($(this).attr("href"), {
54 body: new URLSearchParams(OSM.csrf)
56 .then(response => response.text())
57 .then(data => div.replaceWith(data));
60 function showSearchResult() {
61 let marker = $(this).data("marker");
64 const data = $(this).find("a.set_position").data();
66 marker = L.marker([data.lat, data.lon], { icon: OSM.getUserIcon() });
68 $(this).data("marker", marker);
71 markers.addLayer(marker);
74 function hideSearchResult() {
75 const marker = $(this).data("marker");
78 markers.removeLayer(marker);
82 function panToSearchResult(data) {
83 if (data.minLon && data.minLat && data.maxLon && data.maxLat) {
84 map.fitBounds([[data.minLat, data.minLon], [data.maxLat, data.maxLon]]);
86 map.setView([data.lat, data.lon], data.zoom);
90 function clickSearchResult(e) {
91 const data = $(this).data();
93 panToSearchResult(data);
95 // Let clicks to object browser links propagate.
96 if (data.type && data.id) return;
104 page.pushstate = page.popstate = function (path) {
105 const params = new URLSearchParams(path.substring(path.indexOf("?")));
106 if (params.has("query")) {
107 $(".search_form input[name=query]").val(params.get("query"));
108 $(".describe_location").hide();
109 } else if (params.has("lat") && params.has("lon")) {
110 $(".search_form input[name=query]").val(params.get("lat") + ", " + params.get("lon"));
111 $(".describe_location").hide();
113 OSM.loadSidebarContent(path, page.load);
116 page.load = function () {
117 $(".search_results_entry").each(function (index) {
118 const entry = $(this);
119 fetch(entry.data("href"), {
121 body: new URLSearchParams({
123 minlon: map.getBounds().getWest(),
124 minlat: map.getBounds().getSouth(),
125 maxlon: map.getBounds().getEast(),
126 maxlat: map.getBounds().getNorth(),
130 .then(response => response.text())
131 .then(function (html) {
133 // go to first result of first geocoder
135 const firstResult = entry.find("*[data-lat][data-lon]:first").first();
136 if (firstResult.length) {
137 panToSearchResult(firstResult.data());
143 return map.getState();
146 page.unload = function () {
147 markers.clearLayers();
148 $(".search_form input[name=query]").val("");
149 $(".describe_location").fadeIn(100);