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 OSM.router.route("/search?whereami=1&query=" + encodeURIComponent(
38 center.lat.toFixed(precision) + "," + center.lng.toFixed(precision)
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);
48 var markers = L.layerGroup().addTo(map);
50 function clickSearchMore(e) {
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"),
60 div.find(".loader").show();
62 params[csrf_param] = csrf_token;
65 url: $(this).attr("href"),
68 success: function (data) {
69 div.replaceWith(data);
74 function showSearchResult() {
75 var marker = $(this).data("marker");
78 var data = $(this).find("a.set_position").data();
80 marker = L.marker([data.lat, data.lon], { icon: OSM.getUserIcon() });
82 $(this).data("marker", marker);
85 markers.addLayer(marker);
88 function hideSearchResult() {
89 var marker = $(this).data("marker");
92 markers.removeLayer(marker);
96 function panToSearchResult(data) {
97 if (data.minLon && data.minLat && data.maxLon && data.maxLat) {
98 map.fitBounds([[data.minLat, data.minLon], [data.maxLat, data.maxLon]]);
100 map.setView([data.lat, data.lon], data.zoom);
104 function clickSearchResult(e) {
105 var data = $(this).data();
107 panToSearchResult(data);
109 // Let clicks to object browser links propagate.
110 if (data.type && data.id) return;
118 page.pushstate = page.popstate = function (path) {
119 var params = Qs.parse(path.substring(path.indexOf("?") + 1));
120 $(".search_form input[name=query]").val(params.query);
121 $(".describe_location").hide();
122 OSM.loadSidebarContent(path, page.load);
125 page.load = function () {
126 $(".search_results_entry").each(function (index) {
128 csrf_param = $("meta[name=csrf-param]").attr("content"),
129 csrf_token = $("meta[name=csrf-token]").attr("content"),
132 minlon: map.getBounds().getWest(),
133 minlat: map.getBounds().getSouth(),
134 maxlon: map.getBounds().getEast(),
135 maxlat: map.getBounds().getNorth()
137 params[csrf_param] = csrf_token;
139 url: entry.data("href"),
142 success: function (html) {
144 // go to first result of first geocoder
146 var firstResult = entry.find("*[data-lat][data-lon]:first").first();
147 if (firstResult.length) {
148 panToSearchResult(firstResult.data());
155 return map.getState();
158 page.unload = function () {
159 markers.clearLayers();
160 $(".search_form input[name=query]").val("");
161 $(".describe_location").fadeIn(100);