]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/index/search.js
Remove simulating clicks on <a> when <li> is clicked
[rails.git] / app / assets / javascripts / index / search.js
1 //= require qs/dist/qs
2
3 OSM.Search = function (map) {
4   $(".search_form input[name=query]").on("input", function (e) {
5     if ($(e.target).val() === "") {
6       $(".describe_location").fadeIn(100);
7     } else {
8       $(".describe_location").fadeOut(100);
9     }
10   });
11
12   $(".search_form a.button.switch_link").on("click", function (e) {
13     e.preventDefault();
14     var query = $(e.target).parent().parent().find("input[name=query]").val();
15     if (query) {
16       OSM.router.route("/directions?from=" + encodeURIComponent(query) + OSM.formatHash(map));
17     } else {
18       OSM.router.route("/directions" + OSM.formatHash(map));
19     }
20   });
21
22   $(".search_form").on("submit", function (e) {
23     e.preventDefault();
24     $("header").addClass("closed");
25     var query = $(this).find("input[name=query]").val();
26     if (query) {
27       OSM.router.route("/search?query=" + encodeURIComponent(query) + OSM.formatHash(map));
28     } else {
29       OSM.router.route("/" + OSM.formatHash(map));
30     }
31   });
32
33   $(".describe_location").on("click", function (e) {
34     e.preventDefault();
35     var center = map.getCenter().wrap(),
36         precision = OSM.zoomPrecision(map.getZoom());
37     OSM.router.route("/search?whereami=1&query=" + encodeURIComponent(
38       center.lat.toFixed(precision) + "," + center.lng.toFixed(precision)
39     ));
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   var markers = L.layerGroup().addTo(map);
49
50   function clickSearchMore(e) {
51     e.preventDefault();
52     e.stopPropagation();
53
54     var div = $(this).parents(".search_more"),
55         csrf_param = $("meta[name=csrf-param]").attr("content"),
56         csrf_token = $("meta[name=csrf-token]").attr("content"),
57         params = {};
58
59     $(this).hide();
60     div.find(".loader").show();
61
62     params[csrf_param] = csrf_token;
63
64     $.ajax({
65       url: $(this).attr("href"),
66       method: "POST",
67       data: params,
68       success: function (data) {
69         div.replaceWith(data);
70       }
71     });
72   }
73
74   function showSearchResult() {
75     var marker = $(this).data("marker");
76
77     if (!marker) {
78       var data = $(this).find("a.set_position").data();
79
80       marker = L.marker([data.lat, data.lon], { icon: OSM.getUserIcon() });
81
82       $(this).data("marker", marker);
83     }
84
85     markers.addLayer(marker);
86
87     $(this).closest("li").addClass("selected");
88   }
89
90   function hideSearchResult() {
91     var marker = $(this).data("marker");
92
93     if (marker) {
94       markers.removeLayer(marker);
95     }
96
97     $(this).closest("li").removeClass("selected");
98   }
99
100   function panToSearchResult(data) {
101     if (data.minLon && data.minLat && data.maxLon && data.maxLat) {
102       map.fitBounds([[data.minLat, data.minLon], [data.maxLat, data.maxLon]]);
103     } else {
104       map.setView([data.lat, data.lon], data.zoom);
105     }
106   }
107
108   function clickSearchResult(e) {
109     var data = $(this).data();
110
111     panToSearchResult(data);
112
113     // Let clicks to object browser links propagate.
114     if (data.type && data.id) return;
115
116     e.preventDefault();
117     e.stopPropagation();
118   }
119
120   var page = {};
121
122   page.pushstate = page.popstate = function (path) {
123     var params = Qs.parse(path.substring(path.indexOf("?") + 1));
124     $(".search_form input[name=query]").val(params.query);
125     $(".describe_location").hide();
126     OSM.loadSidebarContent(path, page.load);
127   };
128
129   page.load = function () {
130     $(".search_results_entry").each(function (index) {
131       var entry = $(this),
132           csrf_param = $("meta[name=csrf-param]").attr("content"),
133           csrf_token = $("meta[name=csrf-token]").attr("content"),
134           params = {
135             zoom: map.getZoom(),
136             minlon: map.getBounds().getWest(),
137             minlat: map.getBounds().getSouth(),
138             maxlon: map.getBounds().getEast(),
139             maxlat: map.getBounds().getNorth()
140           };
141       params[csrf_param] = csrf_token;
142       $.ajax({
143         url: entry.data("href"),
144         method: "POST",
145         data: params,
146         success: function (html) {
147           entry.html(html);
148           // go to first result of first geocoder
149           if (index === 0) {
150             var firstResult = entry.find("*[data-lat][data-lon]:first").first();
151             if (firstResult.length) {
152               panToSearchResult(firstResult.data());
153             }
154           }
155         }
156       });
157     });
158
159     return map.getState();
160   };
161
162   page.unload = function () {
163     markers.clearLayers();
164     $(".search_form input[name=query]").val("");
165     $(".describe_location").fadeIn(100);
166   };
167
168   return page;
169 };