]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/index/search.js
Implement closing sidebar
[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").on("click", ".search_results_entry a.set_position", clickSearchResult);
11
12   function clickSearchResult(e) {
13     e.preventDefault();
14
15     var data = $(this).data(),
16       center = L.latLng(data.lat, data.lon);
17
18     if (data.minLon && data.minLat && data.maxLon && data.maxLat) {
19       map.fitBounds([[data.minLat, data.minLon],
20         [data.maxLat, data.maxLon]]);
21     } else {
22       map.setView(center, data.zoom);
23     }
24
25     marker
26       .setLatLng(center)
27       .addTo(map);
28
29     if (data.type && data.id) {
30       map.addObject(data, { zoom: false, style: { opacity: 0.2, fill: false } });
31     }
32   }
33
34   var marker = L.marker([0, 0], {icon: getUserIcon()});
35
36   var page = {};
37
38   page.pushstate = page.popstate = function(path) {
39     var params = querystring.parse(path.substring(path.indexOf('?') + 1));
40     $("#query").val(params.query);
41     $("#sidebar").removeClass("minimized");
42     map.invalidateSize();
43     $("#sidebar_content").load(path, page.load);
44   };
45
46   page.load = function() {
47     $(".search_results_entry").each(function() {
48       var entry = $(this);
49       $.ajax({
50         url: entry.data("href"),
51         method: 'GET',
52         data: {
53           zoom: map.getZoom(),
54           minlon: map.getBounds().getWest(),
55           minlat: map.getBounds().getSouth(),
56           maxlon: map.getBounds().getEast(),
57           maxlat: map.getBounds().getNorth()
58         },
59         success: function(html) {
60           entry.html(html);
61         }
62       });
63     });
64   };
65
66   page.unload = function() {
67     map.removeLayer(marker);
68     map.removeObject();
69     $("#query").val("");
70   };
71
72   return page;
73 };