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