1 //= require jquery-simulate/jquery.simulate
4 OSM.Search = function (map) {
5 $(".search_form input[name=query]").on("input", function (e) {
6 if ($(e.target).val() === "") {
7 $(".describe_location").fadeIn(100);
9 $(".describe_location").fadeOut(100);
13 $(".search_form a.button.switch_link").on("click", function (e) {
15 var query = $(e.target).parent().parent().find("input[name=query]").val();
17 OSM.router.route("/directions?from=" + encodeURIComponent(query) + OSM.formatHash(map));
19 OSM.router.route("/directions" + OSM.formatHash(map));
23 $(".search_form").on("submit", function (e) {
25 $("header").addClass("closed");
26 var query = $(this).find("input[name=query]").val();
28 OSM.router.route("/search?query=" + encodeURIComponent(query) + OSM.formatHash(map));
30 OSM.router.route("/" + OSM.formatHash(map));
34 $(".describe_location").on("click", function (e) {
36 var center = map.getCenter().wrap(),
37 precision = OSM.zoomPrecision(map.getZoom());
38 OSM.router.route("/search?whereami=1&query=" + encodeURIComponent(
39 center.lat.toFixed(precision) + "," + center.lng.toFixed(precision)
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)
48 .on("mousedown", "li.search_results_entry:has(a.set_position)", function () {
50 $(this).one("click", function (e) {
51 if (!moved && !$(e.target).is("a")) {
52 $(this).find("a.set_position").simulate("click", e);
54 }).one("mousemove", function () {
59 var markers = L.layerGroup().addTo(map);
61 function clickSearchMore(e) {
65 var div = $(this).parents(".search_more");
68 div.find(".loader").show();
70 $.get($(this).attr("href"), function (data) {
71 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);
88 $(this).closest("li").addClass("selected");
91 function hideSearchResult() {
92 var marker = $(this).data("marker");
95 markers.removeLayer(marker);
98 $(this).closest("li").removeClass("selected");
101 function panToSearchResult(data) {
102 if (data.minLon && data.minLat && data.maxLon && data.maxLat) {
103 map.fitBounds([[data.minLat, data.minLon], [data.maxLat, data.maxLon]]);
105 map.setView([data.lat, data.lon], data.zoom);
109 function clickSearchResult(e) {
110 var data = $(this).data();
112 panToSearchResult(data);
114 // Let clicks to object browser links propagate.
115 if (data.type && data.id) return;
123 page.pushstate = page.popstate = function (path) {
124 var params = Qs.parse(path.substring(path.indexOf("?") + 1));
125 $(".search_form input[name=query]").val(params.query);
126 $(".describe_location").hide();
127 OSM.loadSidebarContent(path, page.load);
130 page.load = function () {
131 $(".search_results_entry").each(function (index) {
134 url: entry.data("href"),
138 minlon: map.getBounds().getWest(),
139 minlat: map.getBounds().getSouth(),
140 maxlon: map.getBounds().getEast(),
141 maxlat: map.getBounds().getNorth()
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);