/*
* leaflet-geocoder-mapzen
* Leaflet plugin to search (geocode) using Mapzen Search or your
* own hosted version of the Pelias Geocoder API.
*
* License: MIT
* (c) Mapzen
*/(function(e){var t;if(typeof define=="function"&&define.amd)define(["leaflet"],e);else if(typeof module!="undefined")t=require("leaflet"),module.exports=e(t);else{if(typeof window.L=="undefined")throw new Error("Leaflet must be loaded first");e(window.L)}})(function(e){"use strict";function u(e,t,n){var r,i,s,o=null,u=0;n||(n={});var a=function(){u=n.leading===!1?0:(new Date).getTime(),o=null,s=e.apply(r,i),o||(r=i=null)};return function(){var f=(new Date).getTime();!u&&n.leading===!1&&(u=f);var l=t-(f-u);return r=this,i=arguments,l<=0||l>t?(o&&(clearTimeout(o),o=null),u=f,s=e.apply(r,i),o||(r=i=null)):!o&&n.trailing!==!1&&(o=setTimeout(a,l)),s}}function a(e){return e.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g,"\\$&")}var t=1,n=20,r=4,i=20,s=250;e.Control.Geocoder=e.Control.extend({includes:e.Mixin.Events,options:{position:"topleft",attribution:'Geocoding by Mapzen',url:"https://search.mapzen.com/v1",placeholder:"Search",title:"Search",bounds:!1,latlng:null,layers:null,panToPoint:!0,pointIcon:!0,polygonIcon:!0,fullWidth:650,markers:!0,expanded:!1,autocomplete:!0},initialize:function(t,n){window.XDomainRequest&&(this.options.url="//search.mapzen.com/v1"),typeof t!="object"||!t?this.apiKey=t:n=t,e.Util.setOptions(this,n),this.marker,this.markers=[]},getLayers:function(e){var t=this.options.layers;return t?(e.layers=t,e):e},getBoundingBoxParam:function(t){function i(e,t){return e["boundary.rect.min_lon"]=t.getWest(),e["boundary.rect.min_lat"]=t.getSouth(),e["boundary.rect.max_lon"]=t.getEast(),e["boundary.rect.max_lat"]=t.getNorth(),e}var n=this.options.bounds;if(!n)return t;if(n===!0)n=this._map.getBounds(),t=i(t,n);else if(typeof n=="object"&&n.isValid&&n.isValid())t=i(t,n);else if(e.Util.isArray(n)){var r=e.latLngBounds(n);r.isValid&&r.isValid()&&(t=i(t,r))}return t},getLatlngParam:function(e){var t=this.options.latlng;return t?(t.constructor===Array?(e["focus.point.lat"]=t[0],e["focus.point.lon"]=t[1]):typeof t!="object"?(t=this._map.getCenter(),e["focus.point.lat"]=t.lat,e["focus.point.lon"]=t.lng):(e["focus.point.lat"]=t.lat,e["focus.point.lon"]=t.lng?t.lng:t.lon),e):e},search:function(e){if(!e)return;var t=this.options.url+"/search",n={text:e};this.callPelias(t,n,"search")},autocomplete:u(function(e){if(!e)return;var t=this.options.url+"/autocomplete",n={text:e};this.callPelias(t,n,"autocomplete")},s),maxReqTimestampRendered:(new Date).getTime(),callPelias:function(t,n,r){n=this.getBoundingBoxParam(n),n=this.getLatlngParam(n),n=this.getLayers(n),this.apiKey&&(n.api_key=this.apiKey),e.DomUtil.addClass(this._search,"leaflet-pelias-loading");var i=(new Date).getTime();o.request(t,n,function(s,o){e.DomUtil.removeClass(this._search,"leaflet-pelias-loading");if(s){var u;switch(s.code){case 403:u="A valid API key is needed for this search feature.";break;case 404:u="The search service cannot be found. :-(";break;case 408:u="The search service took too long to respond. Try again in a second.";break;case 429:u="There were too many requests. Try again in a second.";break;case 500:u="The search service is not working right now. Please try again later.";break;case 502:u="Connection lost. Please try again later.";break;default:u="The search service is having problems :-("}this.showMessage(u),this.fire("error",{results:o,endpoint:t,requestType:r,params:n,errorCode:s.code,errorMessage:u})}o&&o.features&&this.maxReqTimestampRendered$1")},getIconType:function(e){var t=this.options.pointIcon,n=this.options.polygonIcon,r="leaflet-pelias-layer-icon-";return e.match("venue")||e.match("address")?t===!0?{type:"class",value:r+"point"}:t===!1?!1:{type:"image",value:t}:n===!0?{type:"class",value:r+"polygon"}:n===!1?!1:{type:"image",value:n}},showResults:function(t){if(t.length===0){this.showMessage("No results were found.");return}var n=this._results;n.innerHTML="",n.style.display="block",n.style.maxHeight=this._map.getSize().y-n.offsetTop-this._container.offsetTop-i+"px";var r=e.DomUtil.create("ul","leaflet-pelias-list",n);for(var s=0,o=t.length;s0&&(a.innerHTML+=this.highlight(u.properties.label,this._input.value))}},showMessage:function(t){var n=this._results;n.innerHTML="",n.style.display="block";var r=e.DomUtil.create("div","leaflet-pelias-message",n);r.textContent=t},removeMarkers:function(){if(this.options.markers){for(var e=0;e=window.parseInt(this.options.fullWidth,10)){this.clearFullWidth();return}this._container.style.width=s.toString()+"px"}},clearFullWidth:function(){this.options.fullWidth&&(this._container.style.width="")},onAdd:function(n){var r=e.DomUtil.create("div","leaflet-pelias-control leaflet-bar leaflet-control");return this._body=document.body||document.getElementsByTagName("body")[0],this._container=r,this._input=e.DomUtil.create("input","leaflet-pelias-input",this._container),this._input.spellcheck=!1,this.options.title&&(this._input.title=this.options.title),this.options.placeholder&&(this._input.placeholder=this.options.placeholder),this._search=e.DomUtil.create("a","leaflet-pelias-search-icon",this._container),this._close=e.DomUtil.create("div","leaflet-pelias-close leaflet-pelias-hidden",this._container),this._close.innerHTML="×",this._close.title="Close",this._results=e.DomUtil.create("div","leaflet-pelias-results leaflet-bar",this._container),this.options.expanded&&this.expand(),e.DomEvent.on(this._container,"click",function(e){this._input.focus()},this).on(this._input,"focus",function(e){this._input.value&&(this._results.style.display="block")},this).on(this._map,"click",function(e){this.blur()},this).on(this._search,"click",function(t){e.DomEvent.stopPropagation(t);if(e.DomUtil.hasClass(this._container,"leaflet-pelias-expanded")){if(this.options.expanded===!0){this._input.focus();return}e.DomUtil.addClass(this._close,"leaflet-pelias-hidden"),this.collapse()}else this._input.value.length>0&&e.DomUtil.removeClass(this._close,"leaflet-pelias-hidden"),this.expand(),this._input.focus()},this).on(this._close,"click",function(t){this.resetInput(),this.clearResults(),e.DomEvent.stopPropagation(t)},this).on(this._input,"keydown",function(t){var n=this._results.querySelectorAll(".leaflet-pelias-result"),r=this._results.querySelectorAll(".leaflet-pelias-selected")[0],i,s=this,o=function(t){var n=s._results.querySelectorAll(".leaflet-pelias-selected")[0];n&&t&&s.showMarker(n.innerHTML,e.GeoJSON.coordsToLatLng(n.feature.geometry.coordinates))},u=function(){var e=s._results.querySelectorAll(".leaflet-pelias-selected")[0],t=e.getBoundingClientRect(),n=s._results.getBoundingClientRect();t.bottom>n.bottom?s._results.scrollTop=e.offsetTop+e.offsetHeight-s._results.offsetHeight:t.top0?e.DomUtil.removeClass(this._close,"leaflet-pelias-hidden"):e.DomUtil.addClass(this._close,"leaflet-pelias-hidden");if(r===13||r===38||r===40)return;if(r===27){if(i.length===0||this._results.style.display==="none")this._input.blur(),e.DomUtil.hasClass(this._container,"leaflet-pelias-expanded")&&(this.options.expanded||this.collapse(),this.clearResults());this._results.innerHTML="",this._results.style.display="none",e.DomUtil.removeClass(this._search,"leaflet-pelias-loading");return}this._input.value!==this._lastValue&&(this._lastValue=this._input.value,i.length>=t&&this.options.autocomplete===!0?this.autocomplete(i):this.clearResults())},this).on(this._results,"click",function(t){e.DomEvent.preventDefault(t),e.DomEvent.stopPropagation(t);var n=this._results.querySelectorAll(".leaflet-pelias-selected")[0];n&&e.DomUtil.removeClass(n,"leaflet-pelias-selected");var r=t.target||t.srcElement,i=function(){return e.DomUtil.hasClass(r,"leaflet-pelias-result")||(r=r.parentElement,r&&i()),r};i(),r&&(e.DomUtil.addClass(r,"leaflet-pelias-selected"),this.setSelectedResult(r,t))},this).on(this._results,"mouseover",function(e){this._scrollWheelZoomEnabled=n.scrollWheelZoom.enabled(),this._scrollWheelZoomEnabled&&n.scrollWheelZoom.disable()},this).on(this._results,"mouseout",function(e){this._scrollWheelZoomEnabled&&n.scrollWheelZoom.enable()},this),this.options.fullWidth&&e.DomEvent.on(window,"resize",function(t){e.DomUtil.hasClass(this._container,"leaflet-pelias-expanded")&&this.setFullWidth()},this),this.options.expanded||(e.DomEvent.on(this._map,"mousedown",this._onMapInteraction,this),e.DomEvent.on(this._map,"touchstart",this._onMapInteraction,this)),e.DomEvent.disableClickPropagation(this._container),n.attributionControl&&n.attributionControl.addAttribution(this.options.attribution),r},_onMapInteraction:function(t){!this._input.value&&e.DomUtil.hasClass(this._container,"leaflet-pelias-expanded")&&this.collapse()},onRemove:function(e){e.attributionControl.removeAttribution(this.options.attribution)}}),e.control.geocoder=function(t,n){return new e.Control.Geocoder(t,n)};var o={serialize:function(e){var t="";for(var n in e)if(e.hasOwnProperty(n)){var r=e[n],i=r.toString(),s;t.length&&(t+="&");switch(i){case"[object Array]":s=r[0].toString()==="[object Object]"?JSON.stringify(r):r.join(",");break;case"[object Object]":s=JSON.stringify(r);break;case"[object Date]":s=r.valueOf();break;default:s=r}t+=encodeURIComponent(n)+"="+encodeURIComponent(s)}return t},http_request:function(e,t){return window.XDomainRequest?this.xdr(e,t):this.xhr(e,t)},xhr:function(t,n){var r=new XMLHttpRequest;return r.onerror=function(i){r.onreadystatechange=e.Util.falseFn;var s={code:r.status,message:r.statusText};t.call(n,s,null)},r.onreadystatechange=function(){var i,s;if(r.readyState===4)if(r.status!==200)s={code:r.status,message:r.statusText},t.call(n,s,null);else{try{i=JSON.parse(r.responseText)}catch(o){i=null,s={code:500,message:"Parse Error"}}!s&&i.error&&(s=i.error,i=null),r.onerror=e.Util.falseFn,t.call(n,s,i)}},r},xdr:function(t,n){var r=new window.XDomainRequest;return r.onerror=function(i){r.onload=e.Util.falseFn;var s={code:500,message:"XMLHttpRequest Error"};t.call(n,s,null)},r.onload=function(){var i,s;try{i=JSON.parse(r.responseText)}catch(o){i=null,s={code:500,message:"Parse Error"}}!s&&i.error&&(s=i.error,i=null),r.onerror=e.Util.falseFn,t.call(n,s,i)},r},request:function(e,t,n,r){var i=this.serialize(t),s=this.http_request(n,r);s.open("GET",e+"?"+i),setTimeout(function(){s.send(null)},0)}}});