]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/index/search.js
Update to iD v1.3.4
[rails.git] / app / assets / javascripts / index / search.js
1 function initializeSearch(map) {
2   $("#search_form").submit(submitSearch);
3   $("#describe_location").click(describeLocation);
4
5   if ($("#query").val()) {
6     $("#search_form").submit();
7   }
8
9   // Focus the search field for browsers that don't support
10   // the HTML5 'autofocus' attribute
11   if (!("autofocus" in document.createElement("input"))) {
12     $("#query").focus();
13   }
14
15   $("#sidebar_content").on("click", ".search_results_entry a.set_position", clickSearchResult);
16
17   var marker = L.marker([0, 0], {icon: getUserIcon()});
18
19   function submitSearch(e) {
20     e.preventDefault();
21
22     var bounds = map.getBounds();
23
24     $("#sidebar_title").html(I18n.t('site.sidebar.search_results'));
25     $("#sidebar_content").load($(this).attr("action"), {
26       query: $("#query").val(),
27       zoom: map.getZoom(),
28       minlon: bounds.getWest(),
29       minlat: bounds.getSouth(),
30       maxlon: bounds.getEast(),
31       maxlat: bounds.getNorth()
32     });
33
34     openSidebar();
35
36     $("#sidebar").one("closed", function () {
37       map.removeLayer(marker);
38       map.removeObject();
39     });
40   }
41
42   function clickSearchResult(e) {
43     e.preventDefault();
44
45     var data = $(this).data(),
46       center = L.latLng(data.lat, data.lon);
47
48     if (data.minLon && data.minLat && data.maxLon && data.maxLat) {
49       map.fitBounds([[data.minLat, data.minLon],
50                      [data.maxLat, data.maxLon]]);
51     } else {
52       map.setView(center, data.zoom);
53     }
54
55     marker
56       .setLatLng(center)
57       .addTo(map);
58
59     if (data.type && data.id) {
60       map.addObject(data, { zoom: false, style: { opacity: 0.2, fill: false } });
61     }
62   }
63
64   function describeLocation(e) {
65     e.preventDefault();
66
67     var center = map.getCenter(),
68       zoom = map.getZoom();
69
70     $("#sidebar_title").html(I18n.t('site.sidebar.search_results'));
71     $("#sidebar_content").load($(this).attr("href"), {
72       lat: center.lat,
73       lon: center.lng,
74       zoom: zoom
75     }, openSidebar);    
76   }
77 }