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);
8 $(".describe_location").fadeOut(100);
12 $(".search_form a.btn.switch_link").on("click", function (e) {
14 var query = $(this).closest("form").find("input[name=query]").val();
16 OSM.router.route("/directions?from=" + encodeURIComponent(query) + OSM.formatHash(map));
18 OSM.router.route("/directions" + OSM.formatHash(map));
22 $(".search_form").on("submit", function (e) {
24 $("header").addClass("closed");
25 var query = $(this).find("input[name=query]").val();
27 OSM.router.route("/search?query=" + encodeURIComponent(query) + OSM.formatHash(map));
29 OSM.router.route("/" + OSM.formatHash(map));
33 $(".describe_location").on("click", function (e) {
35 var center = map.getCenter().wrap(),
36 precision = OSM.zoomPrecision(map.getZoom()),
37 lat = center.lat.toFixed(precision),
38 lng = center.lng.toFixed(precision);
40 OSM.router.route("/search?lat=" + encodeURIComponent(lat) + "&lon=" + encodeURIComponent(lng));
44 .on("click", ".search_more a", clickSearchMore)
45 .on("click", ".search_results_entry a.set_position", clickSearchResult)
46 .on("mouseover", "li.search_results_entry:has(a.set_position)", showSearchResult)
47 .on("mouseout", "li.search_results_entry:has(a.set_position)", hideSearchResult);
49 var markers = L.layerGroup().addTo(map);
51 function clickSearchMore(e) {
55 var div = $(this).parents(".search_more"),
56 csrf_param = $("meta[name=csrf-param]").attr("content"),
57 csrf_token = $("meta[name=csrf-token]").attr("content"),
61 div.find(".loader").show();
63 params[csrf_param] = csrf_token;
66 url: $(this).attr("href"),
69 success: function (data) {
70 div.replaceWith(data);
75 function showSearchResult() {
76 var marker = $(this).data("marker");
79 var data = $(this).find("a.set_position").data();
81 marker = L.marker([data.lat, data.lon], { icon: OSM.getUserIcon() });
83 $(this).data("marker", marker);
86 markers.addLayer(marker);
89 function hideSearchResult() {
90 var marker = $(this).data("marker");
93 markers.removeLayer(marker);
97 function panToSearchResult(data) {
98 if (data.minLon && data.minLat && data.maxLon && data.maxLat) {
99 map.fitBounds([[data.minLat, data.minLon], [data.maxLat, data.maxLon]]);
101 map.setView([data.lat, data.lon], data.zoom);
105 function clickSearchResult(e) {
106 var data = $(this).data();
108 panToSearchResult(data);
110 // Let clicks to object browser links propagate.
111 if (data.type && data.id) return;
119 page.pushstate = page.popstate = function (path) {
120 var params = Qs.parse(path.substring(path.indexOf("?") + 1));
121 $(".search_form input[name=query]").val(params.query);
122 $(".describe_location").hide();
123 OSM.loadSidebarContent(path, page.load);
126 page.load = function () {
127 $(".search_results_entry").each(function (index) {
129 csrf_param = $("meta[name=csrf-param]").attr("content"),
130 csrf_token = $("meta[name=csrf-token]").attr("content"),
133 minlon: map.getBounds().getWest(),
134 minlat: map.getBounds().getSouth(),
135 maxlon: map.getBounds().getEast(),
136 maxlat: map.getBounds().getNorth()
138 params[csrf_param] = csrf_token;
140 url: entry.data("href"),
143 success: function (html) {
145 // go to first result of first geocoder
147 var firstResult = entry.find("*[data-lat][data-lon]:first").first();
148 if (firstResult.length) {
149 panToSearchResult(firstResult.data());
156 return map.getState();
159 page.unload = function () {
160 markers.clearLayers();
161 $(".search_form input[name=query]").val("");
162 $(".describe_location").fadeIn(100);