1 //= require jquery.simulate
3 OSM.Search = function(map) {
4 $(".search_form input[name=query]")
5 .on("input", function(e) {
6 if ($(e.target).val() == "") {
7 $(".describe_location").fadeIn(100);
9 $(".describe_location").fadeOut(100);
14 .on("click", ".search_more a", clickSearchMore)
15 .on("mouseover", "p.search_results_entry:has(a.set_position)", showSearchResult)
16 .on("mouseout", "p.search_results_entry:has(a.set_position)", hideSearchResult)
17 .on("mousedown", "p.search_results_entry:has(a.set_position)", function () {
19 $(this).one("click", function (e) {
20 if (!moved && !$(e.target).is('a')) {
21 clickSearchResult(this, e);
23 }).one("mousemove", function () {
28 function clickSearchMore(e) {
32 var div = $(this).parents(".search_more");
35 div.find(".loader").show();
37 $.get($(this).attr("href"), function(data) {
38 div.replaceWith(data);
42 function showSearchResult(e) {
43 var marker = $(this).data("marker");
46 var data = $(this).find("a.set_position").data();
48 marker = L.marker([data.lat, data.lon]);
50 $(this).data("marker", marker);
55 $(this).closest("li").addClass("selected");
58 function hideSearchResult(e) {
59 var marker = $(this).data("marker");
62 map.removeLayer(marker);
65 $(this).closest("li").removeClass("selected");
68 function clickSearchResult(result, e) {
69 var link = $(result).find("a.set_position"),
71 center = L.latLng(data.lat, data.lon);
73 if (data.minLon && data.minLat && data.maxLon && data.maxLat) {
74 map.fitBounds([[data.minLat, data.minLon], [data.maxLat, data.maxLon]]);
76 map.setView(center, data.zoom);
82 // Let clicks to object browser links propagate.
83 if (data.type && data.id) {
84 link.simulate("click", e);
86 marker.setLatLng(center).addTo(map);
90 var marker = L.marker([0, 0], {icon: getUserIcon()});
94 page.pushstate = page.popstate = function(path) {
95 var params = querystring.parse(path.substring(path.indexOf('?') + 1));
96 $(".search_form input[name=query]").val(params.query);
97 OSM.loadSidebarContent(path, page.load);
100 page.load = function() {
101 $(".search_results_entry").each(function() {
104 url: entry.data("href"),
108 minlon: map.getBounds().getWest(),
109 minlat: map.getBounds().getSouth(),
110 maxlon: map.getBounds().getEast(),
111 maxlat: map.getBounds().getNorth()
113 success: function(html) {
119 return map.getState();
122 page.unload = function() {
123 map.removeLayer(marker);
125 $(".search_form input[name=query]").val("");
126 $(".describe_location").fadeIn(100);