1 //= require jquery-simulate/jquery.simulate
4 OSM.Search = function (map) {
5 $(".search_form input[name=query]").on("input", function (e) {
6 if ($(e.target).val() === "") {
7 $(".describe_location").fadeIn(100);
9 $(".describe_location").fadeOut(100);
13 $(".search_form a.button.switch_link").on("click", function (e) {
15 var query = $(e.target).parent().parent().find("input[name=query]").val();
17 OSM.router.route("/directions?from=" + encodeURIComponent(query) + OSM.formatHash(map));
19 OSM.router.route("/directions" + OSM.formatHash(map));
23 $(".search_form").on("submit", function (e) {
25 $("header").addClass("closed");
26 var query = $(this).find("input[name=query]").val();
28 OSM.router.route("/search?query=" + encodeURIComponent(query) + OSM.formatHash(map));
30 OSM.router.route("/" + OSM.formatHash(map));
34 $(".describe_location").on("click", function (e) {
36 var center = map.getCenter().wrap(),
37 precision = OSM.zoomPrecision(map.getZoom());
38 OSM.router.route("/search?whereami=1&query=" + encodeURIComponent(
39 center.lat.toFixed(precision) + "," + center.lng.toFixed(precision)
44 .on("click", ".search_more a", clickSearchMore)
45 .on("click", ".search_results_entry a.set_position", clickSearchResult)
46 .on("mouseover", "li.search_results_entry:has(a.set_position)", showSearchResult)
47 .on("mouseout", "li.search_results_entry:has(a.set_position)", hideSearchResult)
48 .on("mousedown", "li.search_results_entry:has(a.set_position)", function () {
50 $(this).one("click", function (e) {
51 if (!moved && !$(e.target).is("a")) {
52 $(this).find("a.set_position").simulate("click", e);
54 }).one("mousemove", function () {
59 var markers = L.layerGroup().addTo(map);
61 function clickSearchMore(e) {
65 var div = $(this).parents(".search_more"),
66 csrf_param = $("meta[name=csrf-param]").attr("content"),
67 csrf_token = $("meta[name=csrf-token]").attr("content"),
71 div.find(".loader").show();
73 params[csrf_param] = csrf_token;
76 url: $(this).attr("href"),
79 success: function (data) {
80 div.replaceWith(data);
85 function showSearchResult() {
86 var marker = $(this).data("marker");
89 var data = $(this).find("a.set_position").data();
91 marker = L.marker([data.lat, data.lon], { icon: OSM.getUserIcon() });
93 $(this).data("marker", marker);
96 markers.addLayer(marker);
99 function hideSearchResult() {
100 var marker = $(this).data("marker");
103 markers.removeLayer(marker);
107 function panToSearchResult(data) {
108 if (data.minLon && data.minLat && data.maxLon && data.maxLat) {
109 map.fitBounds([[data.minLat, data.minLon], [data.maxLat, data.maxLon]]);
111 map.setView([data.lat, data.lon], data.zoom);
115 function clickSearchResult(e) {
116 var data = $(this).data();
118 panToSearchResult(data);
120 // Let clicks to object browser links propagate.
121 if (data.type && data.id) return;
129 page.pushstate = page.popstate = function (path) {
130 var params = Qs.parse(path.substring(path.indexOf("?") + 1));
131 $(".search_form input[name=query]").val(params.query);
132 $(".describe_location").hide();
133 OSM.loadSidebarContent(path, page.load);
136 page.load = function () {
137 $(".search_results_entry").each(function (index) {
139 csrf_param = $("meta[name=csrf-param]").attr("content"),
140 csrf_token = $("meta[name=csrf-token]").attr("content"),
143 minlon: map.getBounds().getWest(),
144 minlat: map.getBounds().getSouth(),
145 maxlon: map.getBounds().getEast(),
146 maxlat: map.getBounds().getNorth()
148 params[csrf_param] = csrf_token;
150 url: entry.data("href"),
153 success: function (html) {
155 // go to first result of first geocoder
157 var firstResult = entry.find("*[data-lat][data-lon]:first").first();
158 if (firstResult.length) {
159 panToSearchResult(firstResult.data());
166 return map.getState();
169 page.unload = function () {
170 markers.clearLayers();
171 $(".search_form input[name=query]").val("");
172 $(".describe_location").fadeIn(100);