]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/index/search.js
Load pluralizers
[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 query = $(this).find("input[name=query]").val();
22     let search = "/";
23     if (query) search = "/search?" + new URLSearchParams({ query });
24     OSM.router.route(search + OSM.formatHash(map));
25   });
26
27   $(".describe_location").on("click", function (e) {
28     e.preventDefault();
29     $("header").addClass("closed");
30     const [lat, lon] = OSM.cropLocation(map.getCenter(), map.getZoom());
31
32     OSM.router.route("/search?" + new URLSearchParams({ lat, lon }));
33   });
34
35   $("#sidebar_content")
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);
40
41   const markers = L.layerGroup().addTo(map);
42
43   function clickSearchMore(e) {
44     e.preventDefault();
45     e.stopPropagation();
46
47     const div = $(this).parents(".search_more");
48
49     $(this).hide();
50     div.find(".loader").show();
51
52     fetch($(this).attr("href"), {
53       method: "POST",
54       body: new URLSearchParams(OSM.csrf)
55     })
56       .then(response => response.text())
57       .then(data => div.replaceWith(data));
58   }
59
60   function showSearchResult() {
61     let marker = $(this).data("marker");
62
63     if (!marker) {
64       const data = $(this).find("a.set_position").data();
65
66       marker = L.marker([data.lat, data.lon], { icon: OSM.getUserIcon() });
67
68       $(this).data("marker", marker);
69     }
70
71     markers.addLayer(marker);
72   }
73
74   function hideSearchResult() {
75     const marker = $(this).data("marker");
76
77     if (marker) {
78       markers.removeLayer(marker);
79     }
80   }
81
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]]);
85     } else {
86       map.setView([data.lat, data.lon], data.zoom);
87     }
88   }
89
90   function clickSearchResult(e) {
91     const data = $(this).data();
92
93     panToSearchResult(data);
94
95     // Let clicks to object browser links propagate.
96     if (data.type && data.id) return;
97
98     e.preventDefault();
99     e.stopPropagation();
100   }
101
102   const page = {};
103
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();
112     }
113     OSM.loadSidebarContent(path, page.load);
114   };
115
116   page.load = function () {
117     $(".search_results_entry").each(function (index) {
118       const entry = $(this);
119       fetch(entry.data("href"), {
120         method: "POST",
121         body: new URLSearchParams({
122           zoom: map.getZoom(),
123           minlon: map.getBounds().getWest(),
124           minlat: map.getBounds().getSouth(),
125           maxlon: map.getBounds().getEast(),
126           maxlat: map.getBounds().getNorth(),
127           ...OSM.csrf
128         })
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 };