]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/index/search.js
46f756f3bc017275fb0c1a51b112d54c65d0aea7
[rails.git] / app / assets / javascripts / index / search.js
1 //= require jquery.simulate
2
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);
7     } else {
8       $(".describe_location").fadeOut(100);
9     }
10   });
11
12   $(".search_form").on("submit", function(e) {
13     e.preventDefault();
14     $("header").addClass("closed");
15     var query = $(this).find("input[name=query]").val();
16     if (query) {
17       OSM.router.route("/search?query=" + encodeURIComponent(query) + OSM.formatHash(map));
18     } else {
19       OSM.router.route("/" + OSM.formatHash(map));
20     }
21   });
22
23   $(".describe_location").on("click", function(e) {
24     e.preventDefault();
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)));
29   });
30
31   $("#sidebar_content")
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 () {
37       var moved = false;
38       $(this).one("click", function (e) {
39         if (!moved && !$(e.target).is('a')) {
40           $(this).find("a.set_position").simulate("click", e);
41         }
42       }).one("mousemove", function () {
43         moved = true;
44       });
45     });
46
47   function clickSearchMore(e) {
48     e.preventDefault();
49     e.stopPropagation();
50
51     var div = $(this).parents(".search_more");
52
53     $(this).hide();
54     div.find(".loader").show();
55
56     $.get($(this).attr("href"), function(data) {
57       div.replaceWith(data);
58     });
59   }
60
61   function showSearchResult(e) {
62     var marker = $(this).data("marker");
63
64     if (!marker) {
65       var data = $(this).find("a.set_position").data();
66
67       marker = L.marker([data.lat, data.lon], {icon: getUserIcon()});
68
69       $(this).data("marker", marker);
70     }
71
72     markers.addLayer(marker);
73
74     $(this).closest("li").addClass("selected");
75   }
76
77   function hideSearchResult(e) {
78     var marker = $(this).data("marker");
79
80     if (marker) {
81       markers.removeLayer(marker);
82     }
83
84     $(this).closest("li").removeClass("selected");
85   }
86
87   function clickSearchResult(e) {
88     var data = $(this).data(),
89       center = L.latLng(data.lat, data.lon);
90
91     if (data.minLon && data.minLat && data.maxLon && data.maxLat) {
92       map.fitBounds([[data.minLat, data.minLon], [data.maxLat, data.maxLon]]);
93     } else {
94       map.setView(center, data.zoom);
95     }
96
97     // Let clicks to object browser links propagate.
98     if (data.type && data.id) return;
99
100     e.preventDefault();
101     e.stopPropagation();
102   }
103
104   var markers = L.layerGroup().addTo(map);
105
106   var page = {};
107
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);
112   };
113
114   page.load = function() {
115     $(".search_results_entry").each(function() {
116       var entry = $(this);
117       $.ajax({
118         url: entry.data("href"),
119         method: 'GET',
120         data: {
121           zoom: map.getZoom(),
122           minlon: map.getBounds().getWest(),
123           minlat: map.getBounds().getSouth(),
124           maxlon: map.getBounds().getEast(),
125           maxlat: map.getBounds().getNorth()
126         },
127         success: function(html) {
128           entry.html(html);
129         }
130       });
131     });
132
133     return map.getState();
134   };
135
136   page.unload = function() {
137     markers.clearLayers();
138     $(".search_form input[name=query]").val("");
139     $(".describe_location").fadeIn(100);
140   };
141
142   return page;
143 };