]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/index/search.js
89418e006c6c0772497d229e8f3299e192377dd4
[rails.git] / app / assets / javascripts / index / search.js
1 OSM.Search = function(map) {
2   $("#query")
3     .on("focus", function() {
4       $("#describe_location").fadeOut(100);
5     })
6     .on("blur", function() {
7       $("#describe_location").fadeIn(100);
8     });
9
10   $("#sidebar_content")
11     .on("click", ".search_more a", clickSearchMore)
12     .on("click", ".search_results_entry a.set_position", clickSearchResult);
13
14   function clickSearchMore(e) {
15     e.preventDefault();
16     e.stopPropagation();
17
18     var div = $(this).parents(".search_more");
19
20     div.find(".search_results_entry").hide();
21     div.find(".search_searching").show();
22
23     $.get($(this).attr("href"), function(data) {
24       div.replaceWith(data);
25     });
26   }
27
28   function clickSearchResult(e) {
29     e.preventDefault();
30
31     var data = $(this).data(),
32       center = L.latLng(data.lat, data.lon);
33
34     if (data.minLon && data.minLat && data.maxLon && data.maxLat) {
35       map.fitBounds([[data.minLat, data.minLon],
36         [data.maxLat, data.maxLon]]);
37     } else {
38       map.setView(center, data.zoom);
39     }
40
41     marker
42       .setLatLng(center)
43       .addTo(map);
44
45     if (data.type && data.id) {
46       map.addObject(data, { zoom: false, style: { opacity: 0.2, fill: false } });
47     }
48   }
49
50   var marker = L.marker([0, 0], {icon: getUserIcon()});
51
52   var page = {};
53
54   page.pushstate = page.popstate = function(path) {
55     var params = querystring.parse(path.substring(path.indexOf('?') + 1));
56     $("#query").val(params.query);
57     $("#sidebar").removeClass("minimized");
58     map.invalidateSize();
59     $("#sidebar_content").load(path, page.load);
60   };
61
62   page.load = function() {
63     $(".search_results_entry").each(function() {
64       var entry = $(this);
65       $.ajax({
66         url: entry.data("href"),
67         method: 'GET',
68         data: {
69           zoom: map.getZoom(),
70           minlon: map.getBounds().getWest(),
71           minlat: map.getBounds().getSouth(),
72           maxlon: map.getBounds().getEast(),
73           maxlat: map.getBounds().getNorth()
74         },
75         success: function(html) {
76           entry.html(html);
77         }
78       });
79     });
80   };
81
82   page.unload = function() {
83     map.removeLayer(marker);
84     map.removeObject();
85     $("#query").val("");
86   };
87
88   return page;
89 };