]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/index/search.js
Render flash in 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_content").load(path, page.load);
42   };
43
44   page.load = function() {
45     $(".search_results_entry").each(function() {
46       var entry = $(this);
47       $.ajax({
48         url: entry.data("href"),
49         method: 'GET',
50         data: {
51           zoom: map.getZoom(),
52           minlon: map.getBounds().getWest(),
53           minlat: map.getBounds().getSouth(),
54           maxlon: map.getBounds().getEast(),
55           maxlat: map.getBounds().getNorth()
56         },
57         success: function(html) {
58           entry.html(html);
59         }
60       });
61     });
62   };
63
64   page.unload = function() {
65     map.removeLayer(marker);
66     map.removeObject();
67     $("#query").val("");
68   };
69
70   return page;
71 };