summaryrefslogtreecommitdiff
path: root/assets/static/vendor/leaflet-elevation/libs/leaflet-gpxgroup.min.js
blob: 5663a42f6ed5532cf2e44049f1e70d5e0e2180ef (plain) (blame)
1
const selectable={setSelected(e){var t=!!e;this._selected!==t&&(this._selected=t,this.fire("selected"))},isSelected(){return!!this._selected}},selection={getSelection(){return this._selected},setSelection(e){this._selected===e?e!==null&&(e.setSelected(!e.isSelected()),e.isSelected()||(this._selected=null)):(this._selected&&this._selected.setSelected(!1),this._selected=e,this._selected&&this._selected.setSelected(!0)),this.fire("selection_changed")}};L.Control.LayersLegend=L.Control.Layers.extend({_onInputClick(){this._handlingClick=!0,this._layerControlInputs.reduceRight((e,t)=>{if(t.checked)return this._map.fire("legend_selected",{layer:this._getLayer(t.layerId).layer,input:t},!0),t},0),this._handlingClick=!1,this._refocusOnMap()}}),L.control=L.control||{},L.control.layersLegend=(e,t,i)=>new L.Control.LayersLegend(e,t,i),L.GeoJSON.include(selectable),L.GpxGroup=L.Evented.extend({...selection,options:{highlight:{color:"#ff0",opacity:1},points:[],points_options:{icon:{iconUrl:"../images/elevation-poi.png",iconSize:[12,12]}},flyToBounds:!0,legend:!1,legend_options:{position:"topright",collapsed:!1},elevation:!0,elevation_options:{theme:"yellow-theme",detached:!0,elevationDiv:"#elevation-div"},distanceMarkers:!0,distanceMarkers_options:{lazy:!0}},initialize(e,t){L.Util.setOptions(this,t),this._count=0,this._loadedCount=0,this._tracks=e,this._layers=new L.FeatureGroup,this._markers=new L.FeatureGroup,this._elevation=L.control.elevation(this.options.elevation_options),this._legend=L.control.layersLegend(null,null,this.options.legend_options),this.options.points.forEach(i=>new L.Marker(i.latlng,{icon:new L.Icon(this.options.points_options.icon)}).bindTooltip(i.name,{direction:"auto"}).addTo(this._markers))},getBounds(){return this._layers.getBounds()},addTo(e){this._layers.addTo(e),this._markers.addTo(e),this._map=e,this.on("selection_changed",this._onSelectionChanged,this),this._map.on("legend_selected",this._onLegendSelected,this),this._tracks.forEach(this.addTrack,this)},addTrack(e){e instanceof Object?this._loadGeoJSON(e):fetch(e).then(t=>t.ok&&t.text()).then(t=>this._elevation._parseFromString(t)).then(t=>this._loadGeoJSON(t,e.split("/").pop().split("#")[0].split("?")[0]))},_loadGeoJSON(e,t){e&&(e.name=e.name||e[0]&&e[0].properties.name||t,this._loadRoute(e))},_loadRoute(e){if(e){var t={color:this._uniqueColors(this._tracks.length)[this._count++],opacity:.75,weight:5,distanceMarkers:this.options.distanceMarkers_options},i=new L.GeoJSON(e,{name:e.name||"",style:o=>t,distanceMarkers:t.distanceMarkers,originalStyle:t,filter:o=>o.geometry.type!="Point"});i.addTo(this._layers),i.eachLayer(o=>this._onEachRouteLayer(i,o)),this._onEachRouteLoaded(i)}},_onEachRouteLayer(e,t){var i=t,o=L.version.startsWith("2.")?["pointerover","pointerout"]:["mouseover","mouseout"];e.on("selected",this._onRouteSelected.bind(this,e,i)),i.on(o[0],this._onRouteMouseOver.bind(this,e,i)),i.on(o[1],this._onRouteMouseOut.bind(this,e,i)),i.on("click",this._onRouteClick.bind(this,e,i)),i.bindTooltip(e.options.name,{direction:"auto",sticky:!0})},_onEachRouteLoaded(e){this.options.legend&&this._legend.addBaseLayer(e,'<svg id="legend_'+e._leaflet_id+'" width="25" height="10" version="1.1" xmlns="http://www.w3.org/2000/svg"><line x1="0" x2="50" y1="5" y2="5" stroke="'+e.options.originalStyle.color+'" fill="transparent" stroke-width="5" /></svg> '+e.options.name),this.fire("route_loaded",{route:e}),++this._loadedCount===this._tracks.length&&(this.fire("loaded"),this.options.flyToBounds&&this._map.flyToBounds(this.getBounds(),{duration:.25,easeLinearity:.25,noMoveStart:!0}),this.options.legend&&this._legend.addTo(this._map))},highlight(e,t){t.setStyle(this.options.highlight),this.options.distanceMarkers&&t.addDistanceMarkers()},unhighlight(e,t){t.setStyle(e.options.originalStyle),this.options.distanceMarkers&&t.removeDistanceMarkers()},_onRouteMouseOver(e,t){e.isSelected()||(this.highlight(e,t),this.options.legend&&(this.setSelection(e),L.DomUtil.get("legend_"+e._leaflet_id).parentNode.previousSibling.click())),this.fire("route_mouseover",{route:e,polyline:t})},_onRouteMouseOut(e,t){e.isSelected()||this.unhighlight(e,t),this.fire("route_mouseout",{route:e,polyline:t})},_onRouteClick(e,t){this.setSelection(e)},_onRouteSelected(e,t){e.isSelected()||this.unhighlight(e,t)},_onSelectionChanged(e){var t=this._elevation,i=t.getContainer(),o=this.getSelection();t.clear(),o&&o.isSelected()?(i||t.addTo(this._map),o.getLayers().forEach(function(s){s instanceof L.Polyline&&(t.addData(s,!1),s.bringToFront())})):i&&t.remove()},_onLegendSelected(e){var t=e.input.closest(".leaflet-control-layers-list"),i=e.layer;if(!i.isSelected()){this.setSelection(i);for(var o in i._layers)this.highlight(i,i._layers[o]);this._map.flyToBounds(e.layer.getBounds())}t.scroll({top:e.input.offsetTop-t.offsetTop||0,behavior:"smooth"}),this._layers.eachLayer(s=>{var n=L.DomUtil.get("legend_"+s._leaflet_id);n.querySelector("line").style.stroke=s.isSelected()?this.options.highlight.color:"",n.parentNode.style.fontWeight=s.isSelected()?"700":""})},_uniqueColors(e){return e===1?["#0000ff"]:new Array(e).fill(null).map((t,i)=>this._hsvToHex(i*(1/e),1,.7))},_hsvToHex(e,t,i){var o=Math.floor(e*6),s=e*6-o,n=i*(1-t),l=i*(1-s*t),r=i*(1-(1-s)*t),d={0:[i,r,n],1:[l,i,n],2:[n,i,r],3:[n,l,i],4:[r,n,i],5:[i,n,l]}[o%6];return d.map(a=>a*255).reduce((a,h)=>a+(h>>4&15).toString(16)+(h&15).toString(16),"#")},removeFrom(e){this._layers.removeFrom(e)}}),L.gpxGroup=(e,t)=>new L.GpxGroup(e,t);