X-Git-Url: https://git.openstreetmap.org./rails.git/blobdiff_plain/567a9e5b23b07babe32381fd45647dc88ff1915e..415f497d89b75466679841101bc14b0bb9662dcc:/app/assets/javascripts/index/directions.js?ds=sidebyside diff --git a/app/assets/javascripts/index/directions.js b/app/assets/javascripts/index/directions.js index f7ce2adc1..ccd4a68c2 100644 --- a/app/assets/javascripts/index/directions.js +++ b/app/assets/javascripts/index/directions.js @@ -1,3 +1,4 @@ +//= require ./directions-endpoint //= require_self //= require_tree ./directions //= require qs/dist/qs @@ -32,8 +33,8 @@ OSM.Directions = function (map) { }; var endpoints = [ - Endpoint($("input[name='route_from']"), OSM.MARKER_GREEN, endpointDragCallback, endpointGeocodeCallback), - Endpoint($("input[name='route_to']"), OSM.MARKER_RED, endpointDragCallback, endpointGeocodeCallback) + OSM.DirectionsEndpoint(map, $("input[name='route_from']"), OSM.MARKER_GREEN, endpointDragCallback, endpointGeocodeCallback), + OSM.DirectionsEndpoint(map, $("input[name='route_to']"), OSM.MARKER_RED, endpointDragCallback, endpointGeocodeCallback) ]; var expiry = new Date(); @@ -53,91 +54,6 @@ OSM.Directions = function (map) { select.append(""); }); - function Endpoint(input, iconUrl, dragCallback, geocodeCallback) { - var endpoint = {}; - - endpoint.marker = L.marker([0, 0], { - icon: L.icon({ - iconUrl: iconUrl, - iconSize: [25, 41], - iconAnchor: [12, 41], - popupAnchor: [1, -34], - shadowUrl: OSM.MARKER_SHADOW, - shadowSize: [41, 41] - }), - draggable: true, - autoPan: true - }); - - endpoint.marker.on("drag dragend", function (e) { - endpoint.setLatLng(e.target.getLatLng()); - dragCallback(e.type === "drag"); - }); - - input.on("keydown", function () { - input.removeClass("is-invalid"); - }); - - input.on("change", function (e) { - // make text the same in both text boxes - var value = e.target.value; - endpoint.setValue(value); - }); - - endpoint.setValue = function (value, latlng) { - endpoint.value = value; - delete endpoint.latlng; - input.removeClass("is-invalid"); - input.val(value); - - if (latlng) { - endpoint.setLatLng(latlng); - } else { - endpoint.getGeocode(); - } - }; - - endpoint.getGeocode = function () { - // if no one has entered a value yet, then we can't geocode, so don't - // even try. - if (!endpoint.value) { - return; - } - - endpoint.awaitingGeocode = true; - - var viewbox = map.getBounds().toBBoxString(); // ,,, - - $.getJSON(OSM.NOMINATIM_URL + "search?q=" + encodeURIComponent(endpoint.value) + "&format=json&viewbox=" + viewbox, function (json) { - endpoint.awaitingGeocode = false; - endpoint.hasGeocode = true; - if (json.length === 0) { - input.addClass("is-invalid"); - alert(I18n.t("javascripts.directions.errors.no_place", { place: endpoint.value })); - return; - } - - endpoint.setLatLng(L.latLng(json[0])); - - input.val(json[0].display_name); - - geocodeCallback(); - }); - }; - - endpoint.setLatLng = function (ll) { - var precision = OSM.zoomPrecision(map.getZoom()); - input.val(ll.lat.toFixed(precision) + ", " + ll.lng.toFixed(precision)); - endpoint.hasGeocode = true; - endpoint.latlng = ll; - endpoint.marker - .setLatLng(ll) - .addTo(map); - }; - - return endpoint; - } - $(".directions_form .reverse_directions").on("click", function () { var coordFrom = endpoints[0].latlng, coordTo = endpoints[1].latlng,