1 //= require jquery.simulate
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.button.switch_link").on("click", function(e) {
14 var query = $(e.target).parent().parent().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", "p.search_results_entry:has(a.set_position)", showSearchResult)
46 .on("mouseout", "p.search_results_entry:has(a.set_position)", hideSearchResult)
47 .on("mousedown", "p.search_results_entry:has(a.set_position)", function () {
49 $(this).one("click", function (e) {
50 if (!moved && !$(e.target).is('a')) {
51 $(this).find("a.set_position").simulate("click", e);
53 }).one("mousemove", function () {
58 var markers = L.layerGroup().addTo(map);
60 function clickSearchMore(e) {
64 var div = $(this).parents(".search_more");
67 div.find(".loader").show();
69 $.get($(this).attr("href"), function(data) {
70 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);
87 $(this).closest("li").addClass("selected");
90 function hideSearchResult() {
91 var marker = $(this).data("marker");
94 markers.removeLayer(marker);
97 $(this).closest("li").removeClass("selected");
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]]);
104 map.setView([data.lat, data.lon], data.zoom);
108 function clickSearchResult(e) {
109 var data = $(this).data();
111 panToSearchResult(data);
113 // Let clicks to object browser links propagate.
114 if (data.type && data.id) return;
122 page.pushstate = page.popstate = function(path) {
123 var params = querystring.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);
129 page.load = function() {
130 $(".search_results_entry").each(function(index) {
133 url: entry.data("href"),
137 minlon: map.getBounds().getWest(),
138 minlat: map.getBounds().getSouth(),
139 maxlon: map.getBounds().getEast(),
140 maxlat: map.getBounds().getNorth()
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);