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").on("submit", function(e) {
14 $("header").addClass("closed");
15 var query = $(this).find("input[name=query]").val();
17 OSM.router.route("/search?query=" + encodeURIComponent(query) + OSM.formatHash(map));
19 OSM.router.route("/" + OSM.formatHash(map));
23 $(".describe_location").on("click", function(e) {
25 var precision = OSM.zoomPrecision(map.getZoom());
26 OSM.router.route("/search?query=" + encodeURIComponent(
27 map.getCenter().lat.toFixed(precision) + "," +
28 map.getCenter().lng.toFixed(precision)));
32 .on("click", ".search_more a", clickSearchMore)
33 .on("click", ".search_results_entry a.set_position", clickSearchResult)
34 .on("mouseover", "p.search_results_entry:has(a.set_position)", showSearchResult)
35 .on("mouseout", "p.search_results_entry:has(a.set_position)", hideSearchResult)
36 .on("mousedown", "p.search_results_entry:has(a.set_position)", function () {
38 $(this).one("click", function (e) {
39 if (!moved && !$(e.target).is('a')) {
40 $(this).find("a.set_position").simulate("click", e);
42 }).one("mousemove", function () {
47 function clickSearchMore(e) {
51 var div = $(this).parents(".search_more");
54 div.find(".loader").show();
56 $.get($(this).attr("href"), function(data) {
57 div.replaceWith(data);
61 function showSearchResult(e) {
62 var marker = $(this).data("marker");
65 var data = $(this).find("a.set_position").data();
67 marker = L.marker([data.lat, data.lon], {icon: getUserIcon()});
69 $(this).data("marker", marker);
72 markers.addLayer(marker);
74 $(this).closest("li").addClass("selected");
77 function hideSearchResult(e) {
78 var marker = $(this).data("marker");
81 markers.removeLayer(marker);
84 $(this).closest("li").removeClass("selected");
87 function clickSearchResult(e) {
88 var data = $(this).data(),
89 center = L.latLng(data.lat, data.lon);
91 if (data.minLon && data.minLat && data.maxLon && data.maxLat) {
92 map.fitBounds([[data.minLat, data.minLon], [data.maxLat, data.maxLon]]);
94 map.setView(center, data.zoom);
97 // Let clicks to object browser links propagate.
98 if (data.type && data.id) return;
104 var markers = L.layerGroup().addTo(map);
108 page.pushstate = page.popstate = function(path) {
109 var params = querystring.parse(path.substring(path.indexOf('?') + 1));
110 $(".search_form input[name=query]").val(params.query);
111 OSM.loadSidebarContent(path, page.load);
114 page.load = function() {
115 $(".search_results_entry").each(function() {
118 url: entry.data("href"),
122 minlon: map.getBounds().getWest(),
123 minlat: map.getBounds().getSouth(),
124 maxlon: map.getBounds().getEast(),
125 maxlat: map.getBounds().getNorth()
127 success: function(html) {
133 return map.getState();
136 page.unload = function() {
137 markers.clearLayers();
138 $(".search_form input[name=query]").val("");
139 $(".describe_location").fadeIn(100);