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 precision = OSM.zoomPrecision(map.getZoom());
36 OSM.router.route("/search?query=" + encodeURIComponent(
37 map.getCenter().lat.toFixed(precision) + "," +
38 map.getCenter().lng.toFixed(precision)));
42 .on("click", ".search_more a", clickSearchMore)
43 .on("click", ".search_results_entry a.set_position", clickSearchResult)
44 .on("mouseover", "p.search_results_entry:has(a.set_position)", showSearchResult)
45 .on("mouseout", "p.search_results_entry:has(a.set_position)", hideSearchResult)
46 .on("mousedown", "p.search_results_entry:has(a.set_position)", function () {
48 $(this).one("click", function (e) {
49 if (!moved && !$(e.target).is('a')) {
50 $(this).find("a.set_position").simulate("click", e);
52 }).one("mousemove", function () {
57 function clickSearchMore(e) {
61 var div = $(this).parents(".search_more");
64 div.find(".loader").show();
66 $.get($(this).attr("href"), function(data) {
67 div.replaceWith(data);
71 function showSearchResult(e) {
72 var marker = $(this).data("marker");
75 var data = $(this).find("a.set_position").data();
77 marker = L.marker([data.lat, data.lon], {icon: getUserIcon()});
79 $(this).data("marker", marker);
82 markers.addLayer(marker);
84 $(this).closest("li").addClass("selected");
87 function hideSearchResult(e) {
88 var marker = $(this).data("marker");
91 markers.removeLayer(marker);
94 $(this).closest("li").removeClass("selected");
97 function clickSearchResult(e) {
98 var data = $(this).data(),
99 center = L.latLng(data.lat, data.lon);
101 if (data.minLon && data.minLat && data.maxLon && data.maxLat) {
102 map.fitBounds([[data.minLat, data.minLon], [data.maxLat, data.maxLon]]);
104 map.setView(center, data.zoom);
107 // Let clicks to object browser links propagate.
108 if (data.type && data.id) return;
114 var markers = L.layerGroup().addTo(map);
118 page.pushstate = page.popstate = function(path) {
119 var params = querystring.parse(path.substring(path.indexOf('?') + 1));
120 $(".search_form input[name=query]").val(params.query);
121 OSM.loadSidebarContent(path, page.load);
124 page.load = function() {
125 $(".search_results_entry").each(function() {
128 url: entry.data("href"),
132 minlon: map.getBounds().getWest(),
133 minlat: map.getBounds().getSouth(),
134 maxlon: map.getBounds().getEast(),
135 maxlat: map.getBounds().getNorth()
137 success: function(html) {
143 return map.getState();
146 page.unload = function() {
147 markers.clearLayers();
148 $(".search_form input[name=query]").val("");
149 $(".describe_location").fadeIn(100);