terra-draw-mapbox-gl-adapter
Version:
Terra Draw Adapter for Mapbox GL JS
3 lines (2 loc) • 8.28 kB
JavaScript
var e=require("terra-draw");function t(){return t=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var i in r)({}).hasOwnProperty.call(r,i)&&(e[i]=r[i])}return e},t.apply(null,arguments)}function r(e,t){return r=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(e,t){return e.__proto__=t,e},r(e,t)}exports.TerraDrawMapboxGLAdapter=/*#__PURE__*/function(e){function i(t){var r;return(r=e.call(this,t)||this)._renderBeforeLayerId=void 0,r._prefixId=void 0,r._initialDragPan=void 0,r._initialDragRotate=void 0,r._nextRender=void 0,r._map=void 0,r._container=void 0,r.markerCounter=0,r.markerMap=new Map,r.changedIds={deletion:!1,points:!1,linestrings:!1,polygons:!1,styling:!1},r._map=t.map,r._container=r._map.getContainer(),r._initialDragRotate=r._map.dragRotate.isEnabled(),r._initialDragPan=r._map.dragPan.isEnabled(),r._renderBeforeLayerId=t.renderBelowLayerId,r._prefixId=t.prefixId||"td",r}var n,a;a=e,(n=i).prototype=Object.create(a.prototype),n.prototype.constructor=n,r(n,a);var o=i.prototype;return o.toGlDashArrayFromPixels=function(e,t){if(!e)return null;var r=e[0],i=e[1];if(!Number.isFinite(r)||!Number.isFinite(i)||r<0||i<0)return null;var n=Math.max(1e-4,t);return[r/n,i/n]},o.resizeImage=function(e,t,r,i){var n=new Image;n.crossOrigin="anonymous",n.onload=function(){var e=document.createElement("canvas");e.width=t,e.height=r;var a=e.getContext("2d");if(!a)throw new Error("Could not get canvas context");a.drawImage(n,0,0,t,r);var o=e.toDataURL();i(o)},n.src=e},o._addGeoJSONSource=function(e,t){this._map.addSource(e,{type:"geojson",data:{type:"FeatureCollection",features:t},tolerance:0})},o._addFillLayer=function(e){return this._map.addLayer({id:e,source:e,type:"fill",layout:{"fill-sort-key":["get","zIndex"]},paint:{"fill-color":["get","polygonFillColor"],"fill-opacity":["get","polygonFillOpacity"]}})},o._addFillOutlineLayer=function(e){return this._map.addLayer({id:e+"-outline",source:e,type:"line",layout:{"line-sort-key":["get","zIndex"]},paint:{"line-width":["get","polygonOutlineWidth"],"line-color":["get","polygonOutlineColor"],"line-opacity":["get","polygonOutlineOpacity"]}})},o._addLineLayer=function(e){return this._map.addLayer({id:e,source:e,type:"line",layout:{"line-sort-key":["get","zIndex"]},paint:t({},{"line-dasharray":["coalesce",["get","lineStringDash"],["literal",[]]]},{"line-width":["get","lineStringWidth"],"line-color":["get","lineStringColor"],"line-opacity":["get","lineStringOpacity"]})})},o._addPointLayer=function(e){return this._map.addLayer({id:e,source:e,type:"circle",layout:{"circle-sort-key":["get","zIndex"]},paint:{"circle-stroke-color":["get","pointOutlineColor"],"circle-stroke-width":["get","pointOutlineWidth"],"circle-stroke-opacity":["get","pointOutlineOpacity"],"circle-radius":["get","pointWidth"],"circle-color":["get","pointColor"],"circle-opacity":["get","pointOpacity"]}})},o._addMarkerLayer=function(e){return this._map.addLayer({id:e+"-marker",source:e,type:"symbol",filter:["has","markerId"],layout:{"icon-image":["get","markerId"],"icon-anchor":"bottom","icon-allow-overlap":!0}})},o._addLayer=function(e,t){"Point"===t&&(this._addPointLayer(e),this._addMarkerLayer(e)),"LineString"===t&&this._addLineLayer(e),"Polygon"===t&&(this._addFillLayer(e),this._addFillOutlineLayer(e))},o._addGeoJSONLayer=function(e,t){var r=this._prefixId+"-"+e.toLowerCase();return this._addGeoJSONSource(r,t),this._addLayer(r,e),r},o._setGeoJSONLayerData=function(e,t){var r=this._prefixId+"-"+e.toLowerCase();return this._map.getSource(r).setData({type:"FeatureCollection",features:t}),r},o.updateChangedIds=function(e){var t=this;[].concat(e.updated,e.created).forEach(function(e){"Point"===e.geometry.type?t.changedIds.points=!0:"LineString"===e.geometry.type?t.changedIds.linestrings=!0:"Polygon"===e.geometry.type&&(t.changedIds.polygons=!0)}),e.deletedIds.length>0&&(this.changedIds.deletion=!0),0===e.created.length&&0===e.updated.length&&0===e.deletedIds.length&&(this.changedIds.styling=!0)},o.getLngLatFromEvent=function(e){var t=this._container.getBoundingClientRect();return this.unproject(e.clientX-t.left,e.clientY-t.top)},o.getMapEventElement=function(){return this._map.getCanvas()},o.setDraggability=function(e){e?(this._initialDragRotate&&this._map.dragRotate.enable(),this._initialDragPan&&this._map.dragPan.enable()):(this._initialDragRotate&&this._map.dragRotate.disable(),this._initialDragPan&&this._map.dragPan.disable())},o.project=function(e,t){var r=this._map.project({lng:e,lat:t});return{x:r.x,y:r.y}},o.unproject=function(e,t){var r=this._map.unproject({x:e,y:t});return{lng:r.lng,lat:r.lat}},o.setCursor=function(e){var t=this._map.getCanvas();"unset"===e?t.style.removeProperty("cursor"):t.style.cursor=e},o.setDoubleClickToZoom=function(e){e?this._map.doubleClickZoom.enable():this._map.doubleClickZoom.disable()},o.render=function(e,t){var r=this;this.updateChangedIds(e),this._nextRender&&cancelAnimationFrame(this._nextRender),this._nextRender=requestAnimationFrame(function(){for(var i=[].concat(e.created,e.updated,e.unchanged),n=[],a=[],o=[],l=function(){var e=i[d],l=e.properties,s=t[l.mode](e);if(l.zIndex=s.zIndex,"Point"===e.geometry.type){var p=s.pointOpacity,c=s.pointOutlineOpacity;if(l.pointColor=s.pointColor,l.pointOutlineColor=s.pointOutlineColor,l.pointOutlineWidth=s.pointOutlineWidth,l.pointOutlineOpacity=void 0===c?1:c,l.pointWidth=s.pointWidth,l.pointOpacity=void 0===p?1:p,s.markerUrl&&s.markerWidth&&s.markerHeight&&!r.markerMap.has(s.markerUrl)){var g="marker-"+r.markerCounter++;r.resizeImage(s.markerUrl,s.markerWidth,s.markerHeight,function(e){r._map.loadImage(e,function(e,t){t&&!e?(r._map.addImage(g,t),r._map.setLayoutProperty(r._prefixId+"-point-marker","icon-image",g)):console.error("Error loading marker image: "+s.markerUrl,e)})}),r.markerMap.set(s.markerUrl,g),l.markerId=g,l.pointWidth=0}else s.markerUrl&&r.markerMap.has(s.markerUrl)&&(l.markerId=r.markerMap.get(s.markerUrl),l.pointWidth=0);n.push(e)}else if("LineString"===e.geometry.type){l.lineStringDash=r.toGlDashArrayFromPixels(s.lineStringDash,s.lineStringWidth),l.lineStringColor=s.lineStringColor,l.lineStringWidth=s.lineStringWidth;var y=s.lineStringOpacity;l.lineStringOpacity=void 0===y?1:y,a.push(e)}else if("Polygon"===e.geometry.type){var h=s.polygonOutlineOpacity;l.polygonFillColor=s.polygonFillColor,l.polygonFillOpacity=s.polygonFillOpacity,l.polygonOutlineOpacity=void 0===h?1:h,l.polygonOutlineColor=s.polygonOutlineColor,l.polygonOutlineWidth=s.polygonOutlineWidth,o.push(e)}},d=0;d<i.length;d++)l();var s=r.changedIds.deletion||r.changedIds.styling,p=s||r.changedIds.linestrings,c=s||r.changedIds.polygons;(s||r.changedIds.points)&&r._setGeoJSONLayerData("Point",n),p&&r._setGeoJSONLayerData("LineString",a),c&&r._setGeoJSONLayerData("Polygon",o),r.changedIds={points:!1,linestrings:!1,polygons:!1,deletion:!1,styling:!1}})},o.clear=function(){this._currentModeCallbacks&&(this._currentModeCallbacks.onClear(),this._nextRender&&(cancelAnimationFrame(this._nextRender),this._nextRender=void 0),this._setGeoJSONLayerData("Point",[]),this._setGeoJSONLayerData("LineString",[]),this._setGeoJSONLayerData("Polygon",[]))},o.getCoordinatePrecision=function(){return e.prototype.getCoordinatePrecision.call(this)},o.unregister=function(){e.prototype.unregister.call(this),this._map.removeLayer(this._prefixId+"-point"),this._map.removeLayer(this._prefixId+"-point-marker"),this._map.removeSource(this._prefixId+"-point"),this._map.removeLayer(this._prefixId+"-linestring"),this._map.removeSource(this._prefixId+"-linestring"),this._map.removeLayer(this._prefixId+"-polygon"),this._map.removeLayer(this._prefixId+"-polygon-outline"),this._map.removeSource(this._prefixId+"-polygon")},o.register=function(t){var r;e.prototype.register.call(this,t);var i=this._addGeoJSONLayer("Polygon",[]),n=this._addGeoJSONLayer("LineString",[]),a=this._addGeoJSONLayer("Point",[]);this._renderBeforeLayerId&&(this._map.moveLayer(a,this._renderBeforeLayerId),this._map.moveLayer(n,a),this._map.moveLayer(i+"-outline",n),this._map.moveLayer(i,n)),null!=(r=this._currentModeCallbacks)&&r.onReady&&this._currentModeCallbacks.onReady()},i}(e.TerraDrawExtend.TerraDrawBaseAdapter);
//# sourceMappingURL=terra-draw-mapbox-gl-adapter.cjs.map