UNPKG

@arcgis/core

Version:

ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API

3 lines (2 loc) • 10.9 kB
/* COPYRIGHT Esri - https://js.arcgis.com/5.0.8/LICENSE.txt */ import{__decorate as t}from"tslib";import e from"../../Graphic.js";import{UndoRedo as o}from"../../UndoRedo.js";import{createTask as i}from"../../core/asyncUtils.js";import{makeHandle as r}from"../../core/handleUtils.js";import{destroyMaybe as s}from"../../core/maybe.js";import{watch as n,syncAndInitial as a,sync as l}from"../../core/reactiveUtils.js";import{property as p,subclass as c}from"../../core/accessorSupport/decorators.js";import{UpdatingHandles as d}from"../../core/support/UpdatingHandles.js";import{load as h}from"../../geometry/coordinateFormatter.js";import u from"../../layers/GraphicsLayer.js";import{pointEquals as m}from"../../layers/graphics/dehydratedFeatureComparison.js";import{CreateOperationGeometry as y}from"./support/CreateOperationGeometry.js";import{createCircle as g,createEllipse as v,createSquare as f,createRectangle as O,createPolygon as _,createPolyline as w,createMultipoint as C}from"./support/createUtils.js";import{createViewAlignedCoordinateSystem as G}from"./support/surfaceCoordinateSystems.js";import{createTooltipInfos as x,initializeConstraints as V,updateTooltipInfo as U,getConstraintZ as b,unlockConstraintsOnVertexAddOrRemove as k,lockElevationOnVertexAdd as T,getActiveTooltipInfo as H}from"./support/tooltipUtils.js";import{InteractiveToolBase as S}from"../interactive/InteractiveToolBase.js";import j from"../interactive/sketch/SketchOptions.js";import{makeTooltip as L,pasteLocation as R,enterInputModeIfAvailable as z}from"../interactive/tooltip/tooltipCommonUtils.js";import{autorun as A}from"../../core/accessorSupport/trackingUtils.js";let E=class extends S{constructor(t){super(t),this._graphic=null,this._coordinateFormatterLoadTask=null,this._createOperationGeometry=null,this.defaultZ=0,this.directionOptions=null,this.elevationLockOnVertexAddDisabled=!1,this.geometryType=null,this.hasZ=!0,this.geometryToPlace=null,this.snappingManager=null,this.snapToScene=!1,this.sketchOptions=new j,this._updatingHandles=new d}initialize(){const{view:t}=this;this.internalGraphicsLayer=new u({listMode:"hide",internal:!0,title:"DrawGraphicTool layer"}),this.view.map.layers.add(this.internalGraphicsLayer);const e=this.drawOperation=this.makeDrawOperation();this.tooltipInfos=x(t.type,this.sketchOptions);const o=L(()=>({view:t,options:this.sketchOptions.tooltips}));this.tooltip=o,V(this._tooltipsContext),this._coordinateFormatterLoadTask=i(()=>h()),this.addHandles([e.on("vertex-add",t=>this.onVertexAdd(t)),e.on("vertex-remove",t=>this.onVertexRemove(t)),e.on("vertex-update",t=>this.onVertexUpdate(t)),e.on("cursor-update",t=>this.onCursorUpdate(t)),e.on("cursor-remove",()=>this._updateGraphic()),e.on("complete",t=>this.onComplete(t)),this._coordinateFormatterLoadTask,o.on("paste",t=>R(t,this.activeTooltipInfo)),n(()=>this.cursor,t=>{e.cursor=t},a),A(()=>{const{activeTooltipInfo:t,sketchOptions:e}=this;U(t,this._tooltipsContext,this._updatingHandles),o.info=e.tooltips.effectiveEnabled?t:null}),A(()=>{e.constraintZ=b(this._tooltipsContext)},l)]),this.finishToolCreation(),e.initializePointer()}destroy(){this.drawOperation=s(this.drawOperation),this.tooltip=s(this.tooltip),this._destroyAllVisualizations(),this.view.map.remove(this.internalGraphicsLayer),this.internalGraphicsLayer=s(this.internalGraphicsLayer),this._updatingHandles.destroy(),this._set("view",null)}get _drawSpatialReference(){return this.drawOperation.coordinateHelper.spatialReference}get _tooltipsContext(){const{defaultZ:t,directionOptions:e,drawOperation:o,forceUniformSize:i,geometryType:r,graphic:s,sketchOptions:n,tooltipInfos:a,view:l,automaticAreaMeasurementUtils:p,automaticLengthMeasurementUtils:c}=this;return{createOperationGeometry:this._createOperationGeometry,defaultZ:t,directionOptions:e,drawOperation:o,forceUniformSize:i,geometryType:r,graphic:s,sketchOptions:n,tooltipInfos:a,view:l,automaticAreaMeasurementUtils:p,automaticLengthMeasurementUtils:c}}get canRedo(){return this.drawOperation.canRedo}get canUndo(){return this.drawOperation.canUndo}set centered(t){this._set("centered",t),this._updateGraphic()}get cursor(){return this._get("cursor")}set cursor(t){this._set("cursor",t)}set enabled(t){this.drawOperation.interactive=t,this._set("enabled",t)}set forceUniformSize(t){this._set("forceUniformSize",t),this._updateGraphic()}get graphic(){return this._graphic}set graphicSymbol(t){this._set("graphicSymbol",t),null!=this._graphic&&(this._graphic.symbol=t)}set mode(t){const e=this.drawOperation;e&&(e.drawingMode=t),this._set("mode",t)}get updating(){return(this._updatingHandles.updating||this.drawOperation?.updating)??!1}get undoRedo(){const{view:{type:t,map:e}}=this;return"2d"===t&&e&&"undoRedo"in e&&e.undoRedo instanceof o?e.undoRedo:null}set undoRedo(t){this._override("undoRedo",t)}completeCreateOperation(){this.drawOperation.complete()}onInputEvent(t){this.destroyed||z(t,this.tooltip)||this.drawOperation.onInputEvent(t)}redo(){this.drawOperation.redo()}reset(){}undo(){this.drawOperation.undo(),0===this.drawOperation.numCommittedVertices&&V(this._tooltipsContext)}_destroyAllVisualizations(){this.removeHandles(M.outline),this.removeHandles(M.regularVertices),this.removeHandles(M.activeVertex),this.removeHandles(M.activeEdge),this.removeHandles(I)}_createOrUpdateGraphic(t){if(null!=this._graphic)return this.updateGraphicGeometry(t),this._graphic;const o=new e({...this.graphicProperties,symbol:this.graphicSymbol});return this._graphic=o,this.updateGraphicGeometry(t),this.internalGraphicsLayer.add(o),this.addHandles(this.initializeGraphic(o)),this.notifyChange("graphic"),this.addHandles(r(()=>{this.internalGraphicsLayer.remove(o),this._graphic===o&&(this._graphic=null)}),I),o}updateGraphicGeometry(t){this._graphic.geometry=t}_getCreateOperationGeometry(t={operationComplete:!1}){if(null==this.drawOperation)return;const{coordinateHelper:e,view:o,visualizationCursorVertex:i,lastVertex:r,committedVertices:s,geometryIncludingUncommittedVertices:n,numCommittedVertices:a}=this.drawOperation;if(!(a>0||null!=i))return;const l=t.operationComplete?s:n,p=l.length,c=null!=i?e.pointToArray(i):null,d=this._drawSpatialReference,h="3d"===o.type&&"global"===o.viewingMode,u=new y;u.committedVertices=s,u.cursorVertex=c;const{geometryType:x}=this;switch(x){case"point":case"mesh":u.full=e.arrayToPoint(l[0]);break;case"multipoint":u.full=p>0?C(l,d):null;break;case"polyline":case"polygon":p>0&&(u.full="polygon"===x?_([l],d,h,!0):w([l],d,h),u.cursorEdge=null!=c&&r&&!m(i,r)?w([[c,e.pointToArray(r)]],d,h):null,u.outline=p>1?u.full:null);break;case"circle":case"rectangle":{if(u.committedVertices=u.cursorVertex=null,!p)break;const e=G(o,l[0]),i=l[0],r=e.makeMapPoint(i[0]+F*o.resolution,i[1]);"circle"===x?1===p&&t.operationComplete?u.circle=g([i,r],e,!0):2===p&&(this.forceUniformSize?u.circle=g(l,e,this.centered):u.rectangle=v(l,e,this.centered)):1===p&&t.operationComplete?u.rectangle=f([i,r],e,!0):2===p&&(u.rectangle=this.forceUniformSize?f(l,e,this.centered):O(l,e,this.centered)),u.full=null!=u.circle?u.circle.geometry:u.rectangle?.geometry,u.outline="polygon"===u.full?.type?u.full:null;break}default:return null}return u}initializeGraphic(t){return r()}onComplete(t){if(!this.drawOperation)return;this._updateGraphic();let e=null;if(this.drawOperation.isCompleted){const t=this._getCreateOperationGeometry({operationComplete:!0});null!=t&&(e=this._createOrUpdateGraphic(t.full))}this._createOperationGeometry=null,this.emit("complete",{graphic:e,...t})}onCursorUpdate(t){this._updateGraphic(),this.emit("cursor-update",t)}onDeactivate(){const{drawOperation:t}=this;t&&(t.isCompleted||t.cancel())}onOutlineChanged(t){return r()}onCursorEdgeChanged(t){return r()}onVertexAdd(t){k(this._tooltipsContext),this._updateGraphic(),this.elevationLockOnVertexAddDisabled||T(t.vertices.at(0)?.coordinates,this._tooltipsContext),this.emit("vertex-add",t)}onVertexRemove(t){k(this._tooltipsContext),this._updateGraphic(),this.emit("vertex-remove",t)}onVertexUpdate(t){this._updateGraphic(),this.emit("vertex-update",t)}_updateGraphic(){const t=this._getCreateOperationGeometry();this._createOperationGeometry=t,null!=t?(null!=t.cursorEdge?this.addHandles(this.onCursorEdgeChanged(t.cursorEdge),M.activeEdge):this.removeHandles(M.activeEdge),null!=t.outline?this.addHandles(this.onOutlineChanged(t.outline),M.outline):this.removeHandles(M.outline),null!=t.committedVertices?this.addHandles(this.onRegularVerticesChanged(t.committedVertices),M.regularVertices):this.removeHandles(M.regularVertices),null!=t.cursorVertex?this.addHandles(this.onActiveVertexChanged(t.cursorVertex),M.activeVertex):this.removeHandles(M.activeVertex),null!=t.full?this._createOrUpdateGraphic(t.full):this.removeHandles(I)):this._destroyAllVisualizations()}get activeTooltipInfo(){return this._coordinateFormatterLoadTask?.finished?H(this._tooltipsContext):null}};t([p()],E.prototype,"_coordinateFormatterLoadTask",void 0),t([p()],E.prototype,"_createOperationGeometry",void 0),t([p()],E.prototype,"_tooltipsContext",null),t([p({value:!0})],E.prototype,"centered",null),t([p()],E.prototype,"cursor",null),t([p({nonNullable:!0})],E.prototype,"defaultZ",void 0),t([p({constructOnly:!0})],E.prototype,"directionOptions",void 0),t([p()],E.prototype,"drawOperation",void 0),t([p()],E.prototype,"elevationLockOnVertexAddDisabled",void 0),t([p({value:!0})],E.prototype,"enabled",null),t([p({value:!0})],E.prototype,"forceUniformSize",null),t([p({constructOnly:!0})],E.prototype,"geometryType",void 0),t([p()],E.prototype,"graphic",null),t([p({constructOnly:!0})],E.prototype,"graphicProperties",void 0),t([p()],E.prototype,"graphicSymbol",null),t([p({constructOnly:!0})],E.prototype,"hasZ",void 0),t([p({constructOnly:!0})],E.prototype,"geometryToPlace",void 0),t([p()],E.prototype,"mode",null),t([p()],E.prototype,"snappingManager",void 0),t([p()],E.prototype,"snapToScene",void 0),t([p()],E.prototype,"tooltip",void 0),t([p()],E.prototype,"tooltipInfos",void 0),t([p({constructOnly:!0,type:j})],E.prototype,"sketchOptions",void 0),t([p()],E.prototype,"updating",null),t([p({constructOnly:!0,nonNullable:!0})],E.prototype,"view",void 0),t([p({constructOnly:!0})],E.prototype,"automaticAreaMeasurementUtils",void 0),t([p({constructOnly:!0})],E.prototype,"automaticLengthMeasurementUtils",void 0),t([p({constructOnly:!0})],E.prototype,"undoRedo",null),t([p()],E.prototype,"activeTooltipInfo",null),E=t([c("esri.views.draw.DrawGraphicTool")],E);const I=Symbol("create-operation-graphic"),M={outline:Symbol("outline-visual"),regularVertices:Symbol("regular-vertices-visual"),activeVertex:Symbol("active-vertex-visual"),activeEdge:Symbol("active-edge-visual")};function D(t){switch(t){case"point":case"polyline":case"polygon":case"multipoint":return t;case"circle":case"rectangle":return"segment";case"mesh":return"point"}}const F=48;export{E as DrawGraphicTool,D as geometryTypeToDrawOperationGeometryType};