]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/index/search.js
Icon for shop=car_parts added
[rails.git] / app / assets / javascripts / index / search.js
1 //= require jquery.simulate
2
3 OSM.Search = function(map) {
4   $(".search_form input[name=query]").on("input", function(e) {
5     if ($(e.target).val() === "") {
6       $(".describe_location").fadeIn(100);
7     } else {
8       $(".describe_location").fadeOut(100);
9     }
10   });
11
12   $(".search_form a.button.switch_link").on("click", function(e) {
13     e.preventDefault();
14     var query = $(e.target).parent().parent().find("input[name=query]").val();
15     if (query) {
16       OSM.router.route("/directions?from=" + encodeURIComponent(query) + OSM.formatHash(map));
17     } else {
18       OSM.router.route("/directions" + OSM.formatHash(map));
19     }
20   });
21
22   $(".search_form").on("submit", function(e) {
23     e.preventDefault();
24     $("header").addClass("closed");
25     var query = $(this).find("input[name=query]").val();
26     if (query) {
27       OSM.router.route("/search?query=" + encodeURIComponent(query) + OSM.formatHash(map));
28     } else {
29       OSM.router.route("/" + OSM.formatHash(map));
30     }
31   });
32
33   $(".describe_location").on("click", function(e) {
34     e.preventDefault();
35     var center = map.getCenter().wrap(),
36       precision = OSM.zoomPrecision(map.getZoom());
37     OSM.router.route("/search?query=" + encodeURIComponent(
38       center.lat.toFixed(precision) + "," + center.lng.toFixed(precision)
39     ));
40   });
41
42   $("#sidebar_content")
43     .on("click", ".search_more a", clickSearchMore)
44     .on("click", ".search_results_entry a.set_position", clickSearchResult)
45     .on("mouseover", "p.search_results_entry:has(a.set_position)", showSearchResult)
46     .on("mouseout", "p.search_results_entry:has(a.set_position)", hideSearchResult)
47     .on("mousedown", "p.search_results_entry:has(a.set_position)", function () {
48       var moved = false;
49       $(this).one("click", function (e) {
50         if (!moved && !$(e.target).is('a')) {
51           $(this).find("a.set_position").simulate("click", e);
52         }
53       }).one("mousemove", function () {
54         moved = true;
55       });
56     });
57
58   function clickSearchMore(e) {
59     e.preventDefault();
60     e.stopPropagation();
61
62     var div = $(this).parents(".search_more");
63
64     $(this).hide();
65     div.find(".loader").show();
66
67     $.get($(this).attr("href"), function(data) {
68       div.replaceWith(data);
69     });
70   }
71
72   function showSearchResult() {
73     var marker = $(this).data("marker");
74
75     if (!marker) {
76       var data = $(this).find("a.set_position").data();
77
78       marker = L.marker([data.lat, data.lon], {icon: OSM.getUserIcon()});
79
80       $(this).data("marker", marker);
81     }
82
83     markers.addLayer(marker);
84
85     $(this).closest("li").addClass("selected");
86   }
87
88   function hideSearchResult() {
89     var marker = $(this).data("marker");
90
91     if (marker) {
92       markers.removeLayer(marker);
93     }
94
95     $(this).closest("li").removeClass("selected");
96   }
97
98   function clickSearchResult(e) {
99     var data = $(this).data(),
100       center = L.latLng(data.lat, data.lon);
101
102     if (data.minLon && data.minLat && data.maxLon && data.maxLat) {
103       map.fitBounds([[data.minLat, data.minLon], [data.maxLat, data.maxLon]]);
104     } else {
105       map.setView(center, data.zoom);
106     }
107
108     // Let clicks to object browser links propagate.
109     if (data.type && data.id) return;
110
111     e.preventDefault();
112     e.stopPropagation();
113   }
114
115   var markers = L.layerGroup().addTo(map);
116
117   var page = {};
118
119   page.pushstate = page.popstate = function(path) {
120     var params = querystring.parse(path.substring(path.indexOf('?') + 1));
121     $(".search_form input[name=query]").val(params.query);
122     OSM.loadSidebarContent(path, page.load);
123   };
124
125   page.load = function() {
126     $(".search_results_entry").each(function() {
127       var entry = $(this);
128       $.ajax({
129         url: entry.data("href"),
130         method: 'GET',
131         data: {
132           zoom: map.getZoom(),
133           minlon: map.getBounds().getWest(),
134           minlat: map.getBounds().getSouth(),
135           maxlon: map.getBounds().getEast(),
136           maxlat: map.getBounds().getNorth()
137         },
138         success: function(html) {
139           entry.html(html);
140         }
141       });
142     });
143
144     return map.getState();
145   };
146
147   page.unload = function() {
148     markers.clearLayers();
149     $(".search_form input[name=query]").val("");
150     $(".describe_location").fadeIn(100);
151   };
152
153   return page;
154 };