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 $("header").addClass("closed");
36 var center = map.getCenter().wrap(),
37 precision = OSM.zoomPrecision(map.getZoom()),
38 lat = center.lat.toFixed(precision),
39 lng = center.lng.toFixed(precision);
41 OSM.router.route("/search?lat=" + encodeURIComponent(lat) + "&lon=" + encodeURIComponent(lng));
45 .on("click", ".search_more a", clickSearchMore)
46 .on("click", ".search_results_entry a.set_position", clickSearchResult)
47 .on("mouseover", "li.search_results_entry:has(a.set_position)", showSearchResult)
48 .on("mouseout", "li.search_results_entry:has(a.set_position)", hideSearchResult);
50 var markers = L.layerGroup().addTo(map);
52 function clickSearchMore(e) {
56 var div = $(this).parents(".search_more"),
57 csrf_param = $("meta[name=csrf-param]").attr("content"),
58 csrf_token = $("meta[name=csrf-token]").attr("content"),
62 div.find(".loader").show();
64 params[csrf_param] = csrf_token;
67 url: $(this).attr("href"),
70 success: function (data) {
71 div.replaceWith(data);
76 function showSearchResult() {
77 var marker = $(this).data("marker");
80 var data = $(this).find("a.set_position").data();
82 marker = L.marker([data.lat, data.lon], { icon: OSM.getUserIcon() });
84 $(this).data("marker", marker);
87 markers.addLayer(marker);
90 function hideSearchResult() {
91 var marker = $(this).data("marker");
94 markers.removeLayer(marker);
98 function panToSearchResult(data) {
99 if (data.minLon && data.minLat && data.maxLon && data.maxLat) {
100 map.fitBounds([[data.minLat, data.minLon], [data.maxLat, data.maxLon]]);
102 map.setView([data.lat, data.lon], data.zoom);
106 function clickSearchResult(e) {
107 var data = $(this).data();
109 panToSearchResult(data);
111 // Let clicks to object browser links propagate.
112 if (data.type && data.id) return;
120 page.pushstate = page.popstate = function (path) {
121 var params = Qs.parse(path.substring(path.indexOf("?") + 1));
123 $(".search_form input[name=query]").val(params.query);
124 $(".describe_location").hide();
125 } else if (params.lat && params.lon) {
126 $(".search_form input[name=query]").val(params.lat + ", " + params.lon);
127 $(".describe_location").hide();
129 OSM.loadSidebarContent(path, page.load);
132 page.load = function () {
133 $(".search_results_entry").each(function (index) {
135 csrf_param = $("meta[name=csrf-param]").attr("content"),
136 csrf_token = $("meta[name=csrf-token]").attr("content"),
139 minlon: map.getBounds().getWest(),
140 minlat: map.getBounds().getSouth(),
141 maxlon: map.getBounds().getEast(),
142 maxlat: map.getBounds().getNorth()
144 params[csrf_param] = csrf_token;
146 url: entry.data("href"),
149 success: function (html) {
151 // go to first result of first geocoder
153 var firstResult = entry.find("*[data-lat][data-lon]:first").first();
154 if (firstResult.length) {
155 panToSearchResult(firstResult.data());
162 return map.getState();
165 page.unload = function () {
166 markers.clearLayers();
167 $(".search_form input[name=query]").val("");
168 $(".describe_location").fadeIn(100);