mirror of
https://github.com/sakitam-fdd/maptalks.plot.git
synced 2026-01-25 16:08:13 +00:00
6 lines
7.5 KiB
JavaScript
6 lines
7.5 KiB
JavaScript
/*!
|
|
* maptalks.plot v0.0.0
|
|
* LICENSE : MIT
|
|
* (c) 2017-2017 https://sakitam-fdd.github.io/maptalks.plot
|
|
*/
|
|
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e(require("maptalks")):"function"==typeof define&&define.amd?define(["maptalks"],e):t.MaptalksPlot=e(t.maptalks)}(this,function(t){"use strict";var e=Object.freeze({TextArea:"TextArea",ARC:"Arc",CURVE:"Curve",GATHERING_PLACE:"GatheringPlace",POLYLINE:"Polyline",FREE_LINE:"FreeLine",POINT:"Point",PENNANT:"Pennant",RECTANGLE:"RectAngle",CIRCLE:"Circle",ELLIPSE:"Ellipse",LUNE:"Lune",SECTOR:"Sector",CLOSED_CURVE:"ClosedCurve",POLYGON:"Polygon",FREE_POLYGON:"FreePolygon",ATTACK_ARROW:"AttackArrow",DOUBLE_ARROW:"DoubleArrow",STRAIGHT_ARROW:"StraightArrow",FINE_ARROW:"FineArrow",ASSAULT_DIRECTION:"AssaultDirection",TAILED_SQUAD_COMBAT:"TailedSquadCombat",TAILED_ATTACK_ARROW:"TailedAttackArrow",SQUAD_COMBAT:"SquadCombat",RECTFLAG:"RectFlag",TRIANGLEFLAG:"TriangleFlag",CURVEFLAG:"CurveFlag"}),i="ol-plot-vector-layer",o=function(t,e){return Math.sqrt(Math.pow(t[0]-e[0],2)+Math.pow(t[1]-e[1],2))},n="mousemove",r=(function(){function t(t){this.value=t}function e(e){function i(n,r){try{var a=e[n](r),s=a.value;s instanceof t?Promise.resolve(s.value).then(function(t){i("next",t)},function(t){i("throw",t)}):o(a.done?"return":"normal",a.value)}catch(t){o("throw",t)}}function o(t,e){switch(t){case"return":n.resolve({value:e,done:!0});break;case"throw":n.reject(e);break;default:n.resolve({value:e,done:!1})}(n=n.next)?i(n.key,n.arg):r=null}var n,r;this._invoke=function(t,e){return new Promise(function(o,a){var s={key:t,arg:e,resolve:o,reject:a,next:null};r?r=r.next=s:(n=r=s,i(t,e))})},"function"!=typeof e.return&&(this.return=void 0)}"function"==typeof Symbol&&Symbol.asyncIterator&&(e.prototype[Symbol.asyncIterator]=function(){return this}),e.prototype.next=function(t){return this._invoke("next",t)},e.prototype.throw=function(t){return this._invoke("throw",t)},e.prototype.return=function(t){return this._invoke("return",t)}}(),function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}),a=function(t,e){if(!t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!e||"object"!=typeof e&&"function"!=typeof e?t:e},s=t.Canvas,l=function(t){function e(){return r(this,e),a(this,t.apply(this,arguments))}return function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function, not "+typeof e);t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),e&&(Object.setPrototypeOf?Object.setPrototypeOf(t,e):t.__proto__=e)}(e,t),e.prototype._arc=function(t,e,i){for(var o=this.options.arcDegree*Math.PI/180,n=1,r=e.length;n<r;n++)s._arcBetween(t,e[n-1],e[n],o),s._stroke(t,i)},e.prototype._quadraticCurve=function(t,e){e.length<=2?s._path(t,e):s.quadraticCurve(t,e)},e.prototype._bezierCurve=function(t,e){if(e.length<=3)s._path(t,e);else{var i=void 0,o=void 0;for(i=1,o=e.length;i+2<o;i+=3)t.bezierCurveTo(e[i].x,e[i].y,e[i+1].x,e[i+1].y,e[i+2].x,e[i+2].y);if(i<o)for(;i<o;i++)t.lineTo(e[i].x,e[i].y)}},e}(t.LineString),h=function(){function e(o){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(r(this,e),!(o&&o instanceof t.Map))throw new Error("\u4f20\u5165\u7684\u4e0d\u662f\u5730\u56fe\u5bf9\u8c61\uff01");this.map=o,this.options=n||{},this.points=null,this.plot=null,this.feature=null,this.plotType=null,this.plotParams=null,this.mapViewport=this.map.getViewport(),this.dblClickZoomInteraction=null,this.drawOverlay=null,this.textInter=null,this.layerName=this.options&&this.options.layerName?this.options.layerName:i,this.drawLayer=this.createVectorLayer(this.layerName,{create:!0}),this.textAreas=[],this.addWindowEventListener()}return e.prototype.active=function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};this.disActive(),this.deactiveMapTools(),this.plotType=t,this.plotParams=e,this.map.on("click",this.mapFirstClickHandler,this)},e.prototype.addWindowEventListener=function(){var t=this;document.querySelector(".ol-overlaycontainer").addEventListener("click",function(e){var i=e||window.event,o=i.target||i.srcElement;"div"===o.nodeName.toLowerCase()&&t.textAreas&&t.textAreas.length>0&&t.textAreas.every(function(e){e.uuid===o.id&&t.dispatch("active_textArea",e)})})},e.prototype.disActive=function(){this.removeEventHandlers(),this.map.removeOverlay(this.drawOverlay),this.textInter&&this.textInter.disActiveInteraction(),this.points=[],this.plot=null,this.feature=null,this.plotType=null,this.plotParams=null,this.activateMapTools()},e.prototype.isDrawing=function(){return!!this.plotType},e.prototype.mapFirstClickHandler=function(e){this.map.un("click",this.mapFirstClickHandler,this),this.points.push(e.coordinate),this.plot=function(t,e,i){switch(t){case"Curve":return new l(e,i||{},"")}return null}(this.plotType,this.points,this.plotParams),this.plot.setMap(this.map),this.feature=new t.Feature(this.plot),this.feature.set("isPlot",!0),this.drawLayer.getSource().addFeature(this.feature),"Point"===this.plotType||"Pennant"===this.plotType?(this.plot.finishDrawing(),this.drawEnd(e)):(this.map.on("click",this.mapNextClickHandler,this),this.plot.freehand||this.map.on("dblclick",this.mapDoubleClickHandler,this),t.DomUtil.listensDomEvent(this.mapViewport,n,this.mapMouseMoveHandler,this,!1)),this.plotType&&this.feature&&(this.plotParams.plotType=this.plotType,this.feature.setProperties(this.plotParams))},e.prototype.mapNextClickHandler=function(t){if(!this.plot.freehand&&o(t.coordinate,this.points[this.points.length-1])<1e-4)return!1;this.points.push(t.coordinate),this.plot.setPoints(this.points),this.plot.fixPointCount===this.plot.getPointCount()&&this.mapDoubleClickHandler(t),this.plot&&this.plot.freehand&&this.mapDoubleClickHandler(t)},e.prototype.mapDoubleClickHandler=function(t){t.preventDefault(),this.plot.finishDrawing(),this.drawEnd(t)},e.prototype.mapMouseMoveHandler=function(t){var e=this.map.getCoordinateFromPixel([t.offsetX,t.offsetY]);if(o(e,this.points[this.points.length-1])<1e-4)return!1;if(this.plot.freehand)this.points.push(e),this.plot.setPoints(this.points);else{var i=this.points.concat([e]);this.plot.setPoints(i)}},e.prototype.removeEventHandlers=function(){this.map.un("click",this.mapFirstClickHandler,this),this.map.un("click",this.mapNextClickHandler,this),t.DomUtil.removeDomEvent(this.mapViewport,n,this.mapMouseMoveHandler,this),this.map.un("dblclick",this.mapDoubleClickHandler,this)},e.prototype.drawEnd=function(t){this.dispatchSync("drawEnd",{type:"drawEnd",originalEvent:t,feature:this.feature}),this.feature&&this.options.isClear&&this.drawLayer.getSource().removeFeature(this.feature),this.activateMapTools(),this.removeEventHandlers(),this.map.removeOverlay(this.drawOverlay),this.points=[],this.plot=null,this.plotType=null,this.plotParams=null,this.feature=null},e.prototype.addFeature=function(){this.feature=new t.Feature(this.plot),this.feature&&this.drawLayer&&this.drawLayer.getSource().addFeature(this.feature)},e.prototype.deactiveMapTools=function(){var e=this;this.map.getInteractions().getArray().every(function(i){return!(i instanceof t.interaction.DoubleClickZoom)||(e.dblClickZoomInteraction=i,e.map.removeInteraction(i),!1)})},e.prototype.activateMapTools=function(){this.dblClickZoomInteraction&&this.dblClickZoomInteraction instanceof t.interaction.DoubleClickZoom&&(this.map.addInteraction(this.dblClickZoomInteraction),this.dblClickZoomInteraction=null)},e}(),p=function t(e){r(this,t),this.plotDraw=new h(e)};return p.PlotTypes=e,p}); |