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("click", ".search_results_entry a.set_position", clickSearchResult)
16 .on("mouseover", "p.search_results_entry:has(a.set_position)", showSearchResult)
17 .on("mouseout", "p.search_results_entry:has(a.set_position)", hideSearchResult)
18 .on("mousedown", "p.search_results_entry:has(a.set_position)", function () {
20 $(this).one("click", function (e) {
21 if (!moved && !$(e.target).is('a')) {
22 $(this).find("a.set_position").simulate("click", e);
24 }).one("mousemove", function () {
29 function clickSearchMore(e) {
33 var div = $(this).parents(".search_more");
36 div.find(".loader").show();
38 $.get($(this).attr("href"), function(data) {
39 div.replaceWith(data);
43 function showSearchResult(e) {
44 var marker = $(this).data("marker");
47 var data = $(this).find("a.set_position").data();
49 marker = L.marker([data.lat, data.lon], {icon: getUserIcon()});
51 $(this).data("marker", marker);
54 markers.addLayer(marker);
56 $(this).closest("li").addClass("selected");
59 function hideSearchResult(e) {
60 var marker = $(this).data("marker");
63 markers.removeLayer(marker);
66 $(this).closest("li").removeClass("selected");
69 function clickSearchResult(e) {
70 var data = $(this).data(),
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);
79 // Let clicks to object browser links propagate.
80 if (data.type && data.id) return;
86 var markers = L.layerGroup().addTo(map);
90 page.pushstate = page.popstate = function(path) {
91 var params = querystring.parse(path.substring(path.indexOf('?') + 1));
92 $(".search_form input[name=query]").val(params.query);
93 OSM.loadSidebarContent(path, page.load);
96 page.load = function() {
97 $(".search_results_entry").each(function() {
100 url: entry.data("href"),
104 minlon: map.getBounds().getWest(),
105 minlat: map.getBounds().getSouth(),
106 maxlon: map.getBounds().getEast(),
107 maxlat: map.getBounds().getNorth()
109 success: function(html) {
115 return map.getState();
118 page.unload = function() {
119 markers.clearLayers();
120 $(".search_form input[name=query]").val("");
121 $(".describe_location").fadeIn(100);