forked from Esri/arcgis-js-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MapNavigationManager.js
25 lines (24 loc) · 15.8 KB
/
MapNavigationManager.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// COPYRIGHT © 2016 Esri
//
// All rights reserved under the copyright laws of the United States
// and applicable international laws, treaties, and conventions.
//
// This material is licensed for use under the Esri Master License
// Agreement (MLA), and is bound by the terms of that agreement.
// You may redistribute and use this code without modification,
// provided you adhere to the terms of the MLA and include this
// copyright notice.
//
// See use restrictions at http://www.esri.com/legal/pdfs/mla_e204_e300/english
//
// For additional information, contact:
// Environmental Systems Research Institute, Inc.
// Attn: Contracts and Legal Services Department
// 380 New York Street
// Redlands, California, USA 92373
// USA
//
// email: [email protected]
//
// See http://js.arcgis.com/3.16/esri/copyright.txt for details.
define(["dojo/_base/declare","dojo/_base/lang","dojo/_base/array","dojo/_base/connect","dojo/_base/event","dojo/mouse","dojo/keys","./kernel","./MouseEvents","./TouchEvents","./PointerEvents","./config","./sniff","./lang","./fx","./graphic","./tileUtils","./geometry/ScreenPoint","./geometry/Extent","./geometry/Rect","./geometry/mathUtils","./symbols/SimpleFillSymbol"],function(t,e,i,n,s,o,a,h,l,_,r,d,c,m,p,u,g,E,v,H,b,y){var f=n.connect,P=n.disconnect,x=c("chrome"),w=c("safari"),k=1,D=-1,z=100,M=10,S=[a.NUMPAD_PLUS,61,187,a.NUMPAD_MINUS,173,189,a.UP_ARROW,a.NUMPAD_8,a.RIGHT_ARROW,a.NUMPAD_6,a.DOWN_ARROW,a.NUMPAD_2,a.LEFT_ARROW,a.NUMPAD_4,a.PAGE_UP,a.NUMPAD_9,a.PAGE_DOWN,a.NUMPAD_3,a.END,a.NUMPAD_1,a.HOME,a.NUMPAD_7],A=t(null,{declaredClass:"esri.MapNavigationManager",eventModel:"",constructor:function(t,i){this.map=t,e.mixin(this,i);var n=t.__container;c("esri-pointer")?(this.pointerEvents=new r(n,{map:t}),this.eventModel="pointer"):c("esri-touch")?(c("ios")||(this.mouseEvents=new l(n,{map:t})),this.touchEvents=new _(n,{map:t,mouseEvents:this.mouseEvents}),this.eventModel="touch"):(this.mouseEvents=new l(n,{map:t}),this.eventModel="mouse"),this._zoomRect=new u(null,new y(d.defaults.map.zoomSymbol)),this._keyDx=this._keyDy=0,this._adjustPinch=e.hitch(this,this._adjustPinch),this._adjustPinchEnd=e.hitch(this,this._adjustPinchEnd)},_panInit:function(t){var i=this.mouseEvents;o.isLeft(t)&&this.map.isPan&&!t.shiftKey&&(this._dragOrigin=new E(0,0),e.mixin(this._dragOrigin,t.screenPoint),P(this._panStartHandle),P(this._panHandle),P(this._panEndHandle),this._panStartHandle=f(i,"onMouseDragStart",this,this._panStart),this._panHandle=f(i,"onMouseDrag",this,this._pan),this._panEndHandle=f(i,"onMouseUp",this,this._panEnd),(x||w)&&t.preventDefault())},_panStart:function(t){this.map.setCursor("move"),this.map.__panStart(t.screenPoint.x,t.screenPoint.y)},_pan:function(t){this.map.__pan(t.screenPoint.x-this._dragOrigin.x,t.screenPoint.y-this._dragOrigin.y)},_panEnd:function(t){P(this._panStartHandle),P(this._panHandle),P(this._panEndHandle),this._panStartHandle=this._panHandle=this._panEndHandle=null;var e=this.map;e.__panning&&(e.__panEnd(t.screenPoint.x-this._dragOrigin.x,t.screenPoint.y-this._dragOrigin.y),e.resetMapCursor())},_zoomInit:function(t){var i=this.map,n=this.pointerEvents||this.mouseEvents;(o.isLeft(t)||t.pointerType)&&i.isRubberBandZoom&&t.shiftKey&&(i.setCursor("crosshair"),this._dragOrigin=e.mixin({},t.screenPoint),this._zoomDir=t.ctrlKey||t.metaKey?D:k,this.pointerEvents?(this._zoomHandle=f(n,"onSwipeMove",this,this._zoom),this._zoomEndHandle=f(n,"onSwipeEnd",this,this._zoomEnd)):(this._zoomHandle=f(n,"onMouseDrag",this,this._zoom),this._zoomEndHandle=f(n,"onMouseUp",this,this._zoomEnd)),x&&t.preventDefault())},_zoom:function(t){var e=this.map,i=this._normalizeRect(t).offset(e.__visibleRect.x,e.__visibleRect.y),n=e.graphics,s=this._zoomRect;s.geometry||e.setCursor("crosshair"),s.geometry&&n.remove(s,!0);var o=e.toMap(new E(i.x,i.y)),a=e.toMap(new E(i.x+i.width,i.y+i.height));i=new H(o.x,o.y,a.x-o.x,o.y-a.y,e.spatialReference),i._originOnly=!0,s.setGeometry(i),n.add(s,!0)},_zoomEnd:function(t){var e=this._zoomRect,i=this.map,n=i.extent,s=i.spatialReference;if(P(this._zoomHandle),P(this._zoomEndHandle),this._zoomHandle=this._zoomEndHandle=null,i._canZoom(this._zoomDir)&&e.getDojoShape()){i.graphics.remove(e),e.geometry=null;var o=this._normalizeRect(t);o.x+=i.__visibleRect.x,o.y+=i.__visibleRect.y;var a;if(this._zoomDir===D){var h=n.getWidth(),l=h*i.width/o.width,_=(l-h)/2;a=new v(n.xmin-_,n.ymin-_,n.xmax+_,n.ymax+_,s)}else{var r=i.toMap({x:o.x,y:o.y+o.height}),d=i.toMap({x:o.x+o.width,y:o.y});a=new v(r.x,r.y,d.x,d.y,s)}i._extentUtil(null,null,a)}e.getDojoShape()&&i.graphics.remove(e,!0),this._zoomDir=0,i.resetMapCursor()},_wheelZoom:function(t,e){var i=this.map;if(!e){if(i.smartNavigation&&!t.shiftKey&&!i._isPanningOrZooming())return i.disableScrollWheelZoom(),this._setScrollWheelPan(!0),void this._wheelPan(t);var n=t.timeStamp;(!m.isDefined(n)||0>=n)&&(n=(new Date).getTime());var s=this._mwts?n-this._mwts:n;if(z>s)return;this._mwts=n}i._canZoom(t.value)&&i._extentUtil({numLevels:t.value,mapAnchor:t.mapPoint,screenAnchor:t.screenPoint})},_wheelPan:function(t){var e=this.map;if(t.shiftKey&&!e._isPanningOrZooming())return this._setScrollWheelPan(!1),e.enableScrollWheelZoom(),void this._wheelZoom(t);var i=0,n=0;c("ff")?t.axis===t.HORIZONTAL_AXIS?i=-t.detail:n=-t.detail:(i=t.wheelDeltaX,n=t.wheelDeltaY),e.translate(i,n)},_setScrollWheelPan:function(t){var e=this.map;e.isScrollWheelPan=t,this.mouseEvents.enableMouseWheel(t),P(this._mwMacHandle),this._mwMacHandle=null,t&&(this._mwMacHandle=f(this.mouseEvents,"onMouseWheel",this,this._wheelPan))},_recenter:function(t){t.shiftKey&&!this.map._isPanningOrZooming()&&this.map.centerAt(t.mapPoint)},_recenterZoom:function(t){t.shiftKey&&!this.map._isPanningOrZooming()&&(t.value=t.ctrlKey||t.metaKey?D:k,this._wheelZoom(t,!0))},_dblClickZoom:function(t){this.map._isPanningOrZooming()||(t.value=1,this._wheelZoom(t,!0))},_twoFingerTap:function(t){this.map._isPanningOrZooming()||(t.value=-1,this._wheelZoom(t,!0))},_keyDown:function(t){var e=t.keyCode,n=this.map;if(-1!==i.indexOf(S,e)){if(e===a.NUMPAD_PLUS||61===e||187===e)n._extentUtil({numLevels:1});else if(e===a.NUMPAD_MINUS||173===e||189===e)n._extentUtil({numLevels:-1});else{switch(n.__panning||n.__panStart(0,0),e){case a.UP_ARROW:case a.NUMPAD_8:this._keyDy+=M;break;case a.RIGHT_ARROW:case a.NUMPAD_6:this._keyDx-=M;break;case a.DOWN_ARROW:case a.NUMPAD_2:this._keyDy-=M;break;case a.LEFT_ARROW:case a.NUMPAD_4:this._keyDx+=M;break;case a.PAGE_UP:case a.NUMPAD_9:this._keyDx-=M,this._keyDy+=M;break;case a.PAGE_DOWN:case a.NUMPAD_3:this._keyDx-=M,this._keyDy-=M;break;case a.END:case a.NUMPAD_1:this._keyDx+=M,this._keyDy-=M;break;case a.HOME:case a.NUMPAD_7:this._keyDx+=M,this._keyDy+=M;break;default:return}n.__pan(this._keyDx,this._keyDy)}s.stop(t)}},_keyEnd:function(t){var e=this.map;e.__panning&&t.keyCode!==a.SHIFT&&(e.__panEnd(this._keyDx,this._keyDy),this._keyDx=this._keyDy=0)},_swipeInit:function(t){var i=this.map,n=i._zoomAnim||i._panAnim;return t.shiftKey?!1:(n&&n._active&&(n.stop(),n._fire("onEnd",[n.node])),this._dragOrigin=new E(0,0),e.mixin(this._dragOrigin,t.screenPoint),P(this._swipeHandle),P(this._swipeEndHandle),this._swipeHandle=f(this.touchEvents||this.pointerEvents,"onSwipeMove",this,this._swipe),void(this._swipeEndHandle=f(this.touchEvents||this.pointerEvents,"onSwipeEnd",this,this._swipeEnd)))},_swipe:function(t){var e=this.map;e.__panning?(this._panX=t.screenPoint.x,this._panY=t.screenPoint.y,e.__pan(t.screenPoint.x-this._dragOrigin.x,t.screenPoint.y-this._dragOrigin.y)):(e.setCursor("move"),e.__panStart(t.screenPoint.x,t.screenPoint.y))},_swipeEnd:function(t){P(this._swipeHandle),P(this._swipeEndHandle),this._swipeHandle=this._swipeEndHandle=null;var e=this.map;e.__panning&&(e.resetMapCursor(),e.__panEnd(t.screenPoint.x-this._dragOrigin.x,t.screenPoint.y-this._dragOrigin.y))},_pinchInit:function(t){var e=this.map,i=e._zoomAnim||e._panAnim;i&&i._active?(i.stop(),i._fire("onEnd",[i.node])):e.__panning&&(t.screenPoint=new E(this._panX,this._panY),t.mapPoint=e.toMap(t.screenPoint),this._swipeEnd(t)),P(this._pinchHandle),P(this._pinchEndHandle),this._pinchHandle=f(this.touchEvents||this.pointerEvents,"onPinchMove",this,this._pinch),this._pinchEndHandle=f(this.touchEvents||this.pointerEvents,"onPinchEnd",this,this._pinchEnd)},_pinch:function(t){var e=this.map;if(t.screenPoints){if(this.currLength=b.getLength(t.screenPoints[0],t.screenPoints[1]),e.__zooming){var i=this.currLength/this._length;this._zoomStartExtent=this.__scaleExtent(e.extent,i,this._dragOrigin),e.__zoom(this._zoomStartExtent,i,this._dragOrigin)}else this._dragOrigin=new E((t.screenPoints[0].x+t.screenPoints[1].x)/2,(t.screenPoints[0].y+t.screenPoints[1].y)/2),this._length=this.currLength,e.__zoomStart(e.extent,this._dragOrigin);e._fireOnScale(this.currLength/this._length,this._dragOrigin,!0)}},_pinchEnd:function(){var t=this.map;if(P(this._pinchHandle),P(this._pinchEndHandle),this._pinchHandle=this._pinchEndHandle=null,t.__zooming&&null===t._zoomAnim){var e=this.currLength/this._length,i=t.extent.getWidth();if(this._zoomAnimAnchor=t.toMap(this._dragOrigin),this._zoomStartExtent=this.__scaleExtent(t.extent,1/e,this._zoomAnimAnchor),t.__tileInfo){var n=g.getCandidateTileInfo(t,t.__tileInfo,this._zoomStartExtent),s=t.__getExtentForLevel(n.lod.level,this._zoomAnimAnchor),o=t.getMinZoom(),a=t.getMaxZoom(),h=s.extent,l=s.lod,_=i/h.getWidth(),r=n.lod.level;1>e?_>e&&r--:e>_&&r++,o>r?r=o:r>a&&(r=a),r!==n.lod.level&&(s=t.__getExtentForLevel(r,this._zoomAnimAnchor),h=s.extent,l=s.lod),this._zoomEndExtent=h,this._zoomEndLod=l,t._zoomAnim=p.animateRange({range:{start:i/this._zoomStartExtent.getWidth(),end:_},duration:d.defaults.map.zoomDuration,rate:d.defaults.map.zoomRate,onAnimate:this._adjustPinch,onEnd:this._adjustPinchEnd}),t._zoomAnim.play(),t._fireOnScale(t.extent.getWidth()/this._zoomEndExtent.getWidth(),this._dragOrigin)}else this._zoomEndExtent=this._zoomStartExtent,t._fireOnScale(t.extent.getWidth()/this._zoomEndExtent.getWidth(),this._dragOrigin),this._adjustPinchEnd()}},_adjustPinch:function(t){var e=this.__scaleExtent(this.map.extent,t,this._zoomAnimAnchor);this.map.__zoom(e,t,this._dragOrigin)},_adjustPinchEnd:function(){var t=this.map,e=t.extent.getWidth()/this._zoomEndExtent.getWidth(),i=this.__scaleExtent(t.extent,1/e,this._zoomAnimAnchor),n=this._dragOrigin,s=this._zoomEndLod;this._zoomStartExtent=this._zoomEndExtent=this._zoomEndLod=this._dragOrigin=t._zoomAnim=this._zoomAnimAnchor=null,t.__zoomEnd(i,e,n,s,!0)},__scaleExtent:function(t,e,i){var n=i||t.getCenter(),s=t.expand(e),o=t.xmin-(s.getWidth()-t.getWidth())*(n.x-t.xmin)/t.getWidth(),a=t.ymax-(s.getHeight()-t.getHeight())*(n.y-t.ymax)/t.getHeight();return new v(o,a-s.getHeight(),o+s.getWidth(),a,t.spatialReference)},_normalizeRect:function(t){var e=t.screenPoint,i=this._dragOrigin.x,n=this._dragOrigin.y,s=new H((e.x<i?e.x:i)-this.map.__visibleRect.x,(e.y<n?e.y:n)-this.map.__visibleRect.y,Math.abs(e.x-i),Math.abs(e.y-n));return delete s.spatialReference,0===s.width&&(s.width=1),0===s.height&&(s.height=1),s},setImmediateClick:function(t){switch(this.eventModel){case"mouse":this.mouseEvents.setImmediateClick(t);break;case"touch":this.touchEvents.setImmediateTap(t),this.mouseEvents&&this.mouseEvents.setImmediateClick(t);break;case"pointer":this.pointerEvents.setImmediateTap(t)}},enablePan:function(){switch(this.disablePan(),this.eventModel){case"mouse":this._panInitHandle=f(this.mouseEvents,"onMouseDown",this,this._panInit);break;case"touch":this._panInitHandle=f(this.mouseEvents,"onMouseDown",this,this._panInit),this._swipeInitHandle=f(this.touchEvents,"onSwipeStart",this,this._swipeInit);break;case"pointer":this._swipeInitHandle=f(this.pointerEvents,"onSwipeStart",this,this._swipeInit)}},disablePan:function(){P(this._panInitHandle),this._panInitHandle=null,P(this._swipeInitHandle),this._swipeInitHandle=null},enableRubberBandZoom:function(){this.disableRubberBandZoom(),this._zoomInitHandle=this.pointerEvents?f(this.pointerEvents,"onSwipeStart",this,this._zoomInit):f(this.mouseEvents,"onMouseDown",this,this._zoomInit)},disableRubberBandZoom:function(){P(this._zoomInitHandle),this._zoomInitHandle=null},enablePinchZoom:function(){this.disablePinchZoom(),("touch"===this.eventModel||"pointer"===this.eventModel)&&(this._pinchInitHandle=f(this.touchEvents||this.pointerEvents,"onPinchStart",this,this._pinchInit))},disablePinchZoom:function(){P(this._pinchInitHandle),this._pinchInitHandle=null},enableScrollWheelZoom:function(){this.disableScrollWheelZoom(),this._wheelHandle=f(this.mouseEvents||this.pointerEvents,"onMouseWheel",this,this._wheelZoom)},disableScrollWheelZoom:function(){P(this._wheelHandle),this._wheelHandle=null},enableDoubleClickZoom:function(){switch(this.disableDoubleClickZoom(),this.eventModel){case"mouse":this._dblClickHandle=f(this.mouseEvents,"onDblClick",this,this._dblClickZoom);break;case"touch":this._dblClickHandle=f(this.mouseEvents,"onDblClick",this,this._dblClickZoom),this._dblTapHandle=f(this.touchEvents,"onDoubleTap",this,this._dblClickZoom),this._zoomOutHandle=f(this.touchEvents,"onTwoFingerTap",this,this._twoFingerTap);break;case"pointer":this._dblTapHandle=f(this.pointerEvents,"onDoubleTap",this,this._dblClickZoom),this._zoomOutHandle=f(this.pointerEvents,"onTwoFingerTap",this,this._twoFingerTap)}},disableDoubleClickZoom:function(){P(this._dblClickHandle),P(this._zoomOutHandle),this._dblTapHandle&&P(this._dblTapHandle),this._dblClickHandle=this._zoomOutHandle=this._dblTapHandle=null},enableShiftDoubleClickZoom:function(){this.disableShiftDoubleClickZoom(),this._sDblClickHandle=f(this.pointerEvents||this.mouseEvents,"onDblClick",this,this._recenterZoom)},disableShiftDoubleClickZoom:function(){P(this._sDblClickHandle),this._sDblClickHandle=null},enableClickRecenter:function(){this.disableClickRecenter(),this._recenterHandle=f(this.pointerEvents||this.mouseEvents,"onClick",this,this._recenter)},disableClickRecenter:function(){P(this._recenterHandle),this._recenterHandle=null},enableKeyboardNavigation:function(){this.disableKeyboardNavigation(),this._keyHandle=f(this.pointerEvents||this.mouseEvents,"onKeyDown",this,this._keyDown),this._keyEndHandle=f(this.pointerEvents||this.mouseEvents,"onKeyUp",this,this._keyEnd)},disableKeyboardNavigation:function(){P(this._keyHandle),P(this._keyEndHandle),this._keyHandle=this._keyEndHandle=null},enableNavigation:function(){var t=this.map;t&&t.loaded&&(t.enableDoubleClickZoom(),t.enableClickRecenter(),t.enablePan(),t.enableRubberBandZoom(),this.enablePinchZoom(),t.enableKeyboardNavigation(),t.smartNavigation?this._setScrollWheelPan(!0):t.enableScrollWheelZoom())},disableNavigation:function(){var t=this.map;t&&t.loaded&&(t.disableDoubleClickZoom(),t.disableClickRecenter(),t.disablePan(),t.disableRubberBandZoom(),this.disablePinchZoom(),t.disableKeyboardNavigation(),t.disableScrollWheelZoom(),t.smartNavigation&&this._setScrollWheelPan(!1))},destroy:function(){this.touchEvents&&this.touchEvents.destroy(),this.mouseEvents&&this.mouseEvents.destroy(),this.pointerEvents&&this.pointerEvents.destroy();var t,e=[this._panInitHandle,this._panStartHandle,this._panHandle,this._panEndHandle,this._zoomInitHandle,this._zoomHandle,this._zoomEndHandle,this._wheelHandle,this._mwMacHandle,this._dblClickHandle,this._zoomOutHandle,this._recenterHandle,this._sDblClickHandle,this._dblTapHandle,this._keyHandle,this._keyEndHandle,this._swipeInitHandle,this._swipeHandle,this._swipeEndHandle,this._pinchInitHandle,this._pinchHandle,this._pinchEndHandle];for(t=0;t<e.length;t++)P(e[t]);this.map=this.touchEvents=this.mouseEvents=this.eventModel=this.pointerEvents=this._zoomRect=this._dragOrigin=this._panInitHandle=this._panStartHandle=this._panHandle=this._panEndHandle=this._zoomInitHandle=this._zoomHandle=this._zoomEndHandle=this._wheelHandle=this._mwMacHandle=this._dblClickHandle=this._zoomOutHandle=this._recenterHandle=this._sDblClickHandle=this._dblTapHandle=this._keyHandle=this._keyEndHandle=this._swipeInitHandle=this._swipeHandle=this._swipeEndHandle=this._pinchInitHandle=this._pinchHandle=this._pinchEndHandle=null}});return c("extend-esri")&&(h.MapNavigationManager=A),A});