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 params = new URLSearchParams({
22 query: this.elements.query.value,
24 minlon: map.getBounds().getWest(),
25 minlat: map.getBounds().getSouth(),
26 maxlon: map.getBounds().getEast(),
27 maxlat: map.getBounds().getNorth()
29 const search = params.get("query") ? `/search?${params}` : "/";
30 OSM.router.route(search + OSM.formatHash(map));
33 $(".describe_location").on("click", function (e) {
35 $("header").addClass("closed");
36 const [lat, lon] = OSM.cropLocation(map.getCenter(), map.getZoom());
38 OSM.router.route("/search?" + new URLSearchParams({ lat, lon }));
42 .on("click", ".search_more a", clickSearchMore)
43 .on("click", ".search_results_entry a.set_position", clickSearchResult)
44 .on("mouseover", "li.search_results_entry:has(a.set_position)", showSearchResult)
45 .on("mouseout", "li.search_results_entry:has(a.set_position)", hideSearchResult);
47 const markers = L.layerGroup().addTo(map);
49 function clickSearchMore(e) {
53 const div = $(this).parents(".search_more");
56 div.find(".loader").show();
58 fetch($(this).attr("href"), {
60 body: new URLSearchParams(OSM.csrf)
62 .then(response => response.text())
63 .then(data => div.replaceWith(data));
66 function showSearchResult() {
67 let marker = $(this).data("marker");
70 const data = $(this).find("a.set_position").data();
72 marker = L.marker([data.lat, data.lon], { icon: OSM.getMarker({}) });
74 $(this).data("marker", marker);
77 markers.addLayer(marker);
80 function hideSearchResult() {
81 const marker = $(this).data("marker");
84 markers.removeLayer(marker);
88 function panToSearchResult(data) {
89 if (data.minLon && data.minLat && data.maxLon && data.maxLat) {
90 map.fitBounds([[data.minLat, data.minLon], [data.maxLat, data.maxLon]]);
92 map.setView([data.lat, data.lon], data.zoom);
96 function clickSearchResult(e) {
97 const data = $(this).data();
99 panToSearchResult(data);
101 // Let clicks to object browser links propagate.
102 if (data.type && data.id) return;
110 page.pushstate = page.popstate = function (path) {
111 const params = new URLSearchParams(path.substring(path.indexOf("?")));
112 if (params.has("query")) {
113 $(".search_form input[name=query]").val(params.get("query"));
114 $(".describe_location").hide();
115 } else if (params.has("lat") && params.has("lon")) {
116 $(".search_form input[name=query]").val(params.get("lat") + ", " + params.get("lon"));
117 $(".describe_location").hide();
119 OSM.loadSidebarContent(path, page.load);
122 page.load = function () {
123 $(".search_results_entry").each(function (index) {
124 const entry = $(this);
125 fetch(entry.data("href"), {
127 body: new URLSearchParams(OSM.csrf)
129 .then(response => response.text())
130 .then(function (html) {
132 // go to first result of first geocoder
134 const firstResult = entry.find("*[data-lat][data-lon]:first").first();
135 if (firstResult.length) {
136 panToSearchResult(firstResult.data());
142 return map.getState();
145 page.unload = function () {
146 markers.clearLayers();
147 $(".search_form input[name=query]").val("");
148 $(".describe_location").fadeIn(100);