]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/index/search.js
Rename map listeners on history page
[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 [lat, lon] = OSM.cropLocation(map.getCenter(), map.getZoom());
37
38     OSM.router.route("/search?" + new URLSearchParams({ lat, lon }));
39   });
40
41   $("#sidebar_content")
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);
46
47   const markers = L.layerGroup().addTo(map);
48
49   function clickSearchMore(e) {
50     e.preventDefault();
51     e.stopPropagation();
52
53     const div = $(this).parents(".search_more");
54
55     $(this).hide();
56     div.find(".loader").show();
57
58     fetch($(this).attr("href"), {
59       method: "POST",
60       body: new URLSearchParams(OSM.csrf)
61     })
62       .then(response => response.text())
63       .then(data => div.replaceWith(data));
64   }
65
66   function showSearchResult() {
67     let marker = $(this).data("marker");
68
69     if (!marker) {
70       const data = $(this).find("a.set_position").data();
71
72       marker = L.marker([data.lat, data.lon], { icon: OSM.getMarker({}) });
73
74       $(this).data("marker", marker);
75     }
76
77     markers.addLayer(marker);
78   }
79
80   function hideSearchResult() {
81     const marker = $(this).data("marker");
82
83     if (marker) {
84       markers.removeLayer(marker);
85     }
86   }
87
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]]);
91     } else {
92       map.setView([data.lat, data.lon], data.zoom);
93     }
94   }
95
96   function clickSearchResult(e) {
97     const data = $(this).data();
98
99     panToSearchResult(data);
100
101     // Let clicks to object browser links propagate.
102     if (data.type && data.id) return;
103
104     e.preventDefault();
105     e.stopPropagation();
106   }
107
108   const page = {};
109
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();
118     }
119     OSM.loadSidebarContent(path, page.load);
120   };
121
122   page.load = function () {
123     $(".search_results_entry").each(function (index) {
124       const entry = $(this);
125       fetch(entry.data("href"), {
126         method: "POST",
127         body: new URLSearchParams(OSM.csrf)
128       })
129         .then(response => response.text())
130         .then(function (html) {
131           entry.html(html);
132           // go to first result of first geocoder
133           if (index === 0) {
134             const firstResult = entry.find("*[data-lat][data-lon]:first").first();
135             if (firstResult.length) {
136               panToSearchResult(firstResult.data());
137             }
138           }
139         });
140     });
141
142     return map.getState();
143   };
144
145   page.unload = function () {
146     markers.clearLayers();
147     $(".search_form input[name=query]").val("");
148     $(".describe_location").fadeIn(100);
149   };
150
151   return page;
152 };