1 //= require jquery.simulate
2 //= require querystring
4 OSM.Search = function (map) {
5 var querystring = require("querystring-component");
7 $(".search_form input[name=query]").on("input", function (e) {
8 if ($(e.target).val() === "") {
9 $(".describe_location").fadeIn(100);
11 $(".describe_location").fadeOut(100);
15 $(".search_form a.button.switch_link").on("click", function (e) {
17 var query = $(e.target).parent().parent().find("input[name=query]").val();
19 OSM.router.route("/directions?from=" + encodeURIComponent(query) + OSM.formatHash(map));
21 OSM.router.route("/directions" + OSM.formatHash(map));
25 $(".search_form").on("submit", function (e) {
27 $("header").addClass("closed");
28 var query = $(this).find("input[name=query]").val();
30 OSM.router.route("/search?query=" + encodeURIComponent(query) + OSM.formatHash(map));
32 OSM.router.route("/" + OSM.formatHash(map));
36 $(".describe_location").on("click", function (e) {
38 var center = map.getCenter().wrap(),
39 precision = OSM.zoomPrecision(map.getZoom());
40 OSM.router.route("/search?whereami=1&query=" + encodeURIComponent(
41 center.lat.toFixed(precision) + "," + center.lng.toFixed(precision)
46 .on("click", ".search_more a", clickSearchMore)
47 .on("click", ".search_results_entry a.set_position", clickSearchResult)
48 .on("mouseover", "p.search_results_entry:has(a.set_position)", showSearchResult)
49 .on("mouseout", "p.search_results_entry:has(a.set_position)", hideSearchResult)
50 .on("mousedown", "p.search_results_entry:has(a.set_position)", function () {
52 $(this).one("click", function (e) {
53 if (!moved && !$(e.target).is("a")) {
54 $(this).find("a.set_position").simulate("click", e);
56 }).one("mousemove", function () {
61 var markers = L.layerGroup().addTo(map);
63 function clickSearchMore(e) {
67 var div = $(this).parents(".search_more");
70 div.find(".loader").show();
72 $.get($(this).attr("href"), function (data) {
73 div.replaceWith(data);
77 function showSearchResult() {
78 var marker = $(this).data("marker");
81 var data = $(this).find("a.set_position").data();
83 marker = L.marker([data.lat, data.lon], { icon: OSM.getUserIcon() });
85 $(this).data("marker", marker);
88 markers.addLayer(marker);
90 $(this).closest("li").addClass("selected");
93 function hideSearchResult() {
94 var marker = $(this).data("marker");
97 markers.removeLayer(marker);
100 $(this).closest("li").removeClass("selected");
103 function panToSearchResult(data) {
104 if (data.minLon && data.minLat && data.maxLon && data.maxLat) {
105 map.fitBounds([[data.minLat, data.minLon], [data.maxLat, data.maxLon]]);
107 map.setView([data.lat, data.lon], data.zoom);
111 function clickSearchResult(e) {
112 var data = $(this).data();
114 panToSearchResult(data);
116 // Let clicks to object browser links propagate.
117 if (data.type && data.id) return;
125 page.pushstate = page.popstate = function (path) {
126 var params = querystring.parse(path.substring(path.indexOf("?") + 1));
127 $(".search_form input[name=query]").val(params.query);
128 $(".describe_location").hide();
129 OSM.loadSidebarContent(path, page.load);
132 page.load = function () {
133 $(".search_results_entry").each(function (index) {
136 url: entry.data("href"),
140 minlon: map.getBounds().getWest(),
141 minlat: map.getBounds().getSouth(),
142 maxlon: map.getBounds().getEast(),
143 maxlat: map.getBounds().getNorth()
145 success: function (html) {
147 // go to first result of first geocoder
149 var firstResult = entry.find("*[data-lat][data-lon]:first").first();
150 if (firstResult.length) {
151 panToSearchResult(firstResult.data());
158 return map.getState();
161 page.unload = function () {
162 markers.clearLayers();
163 $(".search_form input[name=query]").val("");
164 $(".describe_location").fadeIn(100);