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);
98 $(this).closest("li").addClass("selected");
101 function hideSearchResult() {
102 var marker = $(this).data("marker");
105 markers.removeLayer(marker);
108 $(this).closest("li").removeClass("selected");
111 function panToSearchResult(data) {
112 if (data.minLon && data.minLat && data.maxLon && data.maxLat) {
113 map.fitBounds([[data.minLat, data.minLon], [data.maxLat, data.maxLon]]);
115 map.setView([data.lat, data.lon], data.zoom);
119 function clickSearchResult(e) {
120 var data = $(this).data();
122 panToSearchResult(data);
124 // Let clicks to object browser links propagate.
125 if (data.type && data.id) return;
133 page.pushstate = page.popstate = function (path) {
134 var params = Qs.parse(path.substring(path.indexOf("?") + 1));
135 $(".search_form input[name=query]").val(params.query);
136 $(".describe_location").hide();
137 OSM.loadSidebarContent(path, page.load);
140 page.load = function () {
141 $(".search_results_entry").each(function (index) {
143 csrf_param = $("meta[name=csrf-param]").attr("content"),
144 csrf_token = $("meta[name=csrf-token]").attr("content"),
147 minlon: map.getBounds().getWest(),
148 minlat: map.getBounds().getSouth(),
149 maxlon: map.getBounds().getEast(),
150 maxlat: map.getBounds().getNorth()
152 params[csrf_param] = csrf_token;
154 url: entry.data("href"),
157 success: function (html) {
159 // go to first result of first geocoder
161 var firstResult = entry.find("*[data-lat][data-lon]:first").first();
162 if (firstResult.length) {
163 panToSearchResult(firstResult.data());
170 return map.getState();
173 page.unload = function () {
174 markers.clearLayers();
175 $(".search_form input[name=query]").val("");
176 $(".describe_location").fadeIn(100);