123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285 |
- var zrUtil = require("zrender/lib/core/util");
- var Eventful = require("zrender/lib/mixin/Eventful");
- var eventTool = require("zrender/lib/core/event");
- var interactionMutex = require("./interactionMutex");
- function RoamController(zr) {
-
- this.pointerChecker;
-
- this._zr = zr;
-
- this._opt = {};
- var bind = zrUtil.bind;
- var mousedownHandler = bind(mousedown, this);
- var mousemoveHandler = bind(mousemove, this);
- var mouseupHandler = bind(mouseup, this);
- var mousewheelHandler = bind(mousewheel, this);
- var pinchHandler = bind(pinch, this);
- Eventful.call(this);
-
- this.setPointerChecker = function (pointerChecker) {
- this.pointerChecker = pointerChecker;
- };
-
- this.enable = function (controlType, opt) {
-
- this.disable();
- this._opt = zrUtil.defaults(zrUtil.clone(opt) || {}, {
- zoomOnMouseWheel: true,
- moveOnMouseMove: true,
-
- moveOnMouseWheel: false,
- preventDefaultMouseMove: true
- });
- if (controlType == null) {
- controlType = true;
- }
- if (controlType === true || controlType === 'move' || controlType === 'pan') {
- zr.on('mousedown', mousedownHandler);
- zr.on('mousemove', mousemoveHandler);
- zr.on('mouseup', mouseupHandler);
- }
- if (controlType === true || controlType === 'scale' || controlType === 'zoom') {
- zr.on('mousewheel', mousewheelHandler);
- zr.on('pinch', pinchHandler);
- }
- };
- this.disable = function () {
- zr.off('mousedown', mousedownHandler);
- zr.off('mousemove', mousemoveHandler);
- zr.off('mouseup', mouseupHandler);
- zr.off('mousewheel', mousewheelHandler);
- zr.off('pinch', pinchHandler);
- };
- this.dispose = this.disable;
- this.isDragging = function () {
- return this._dragging;
- };
- this.isPinching = function () {
- return this._pinching;
- };
- }
- zrUtil.mixin(RoamController, Eventful);
- function mousedown(e) {
- if (eventTool.isMiddleOrRightButtonOnMouseUpDown(e) || e.target && e.target.draggable) {
- return;
- }
- var x = e.offsetX;
- var y = e.offsetY;
-
- if (this.pointerChecker && this.pointerChecker(e, x, y)) {
- this._x = x;
- this._y = y;
- this._dragging = true;
- }
- }
- function mousemove(e) {
- if (!this._dragging || !isAvailableBehavior('moveOnMouseMove', e, this._opt) || e.gestureEvent === 'pinch' || interactionMutex.isTaken(this._zr, 'globalPan')) {
- return;
- }
- var x = e.offsetX;
- var y = e.offsetY;
- var oldX = this._x;
- var oldY = this._y;
- var dx = x - oldX;
- var dy = y - oldY;
- this._x = x;
- this._y = y;
- this._opt.preventDefaultMouseMove && eventTool.stop(e.event);
- trigger(this, 'pan', 'moveOnMouseMove', e, {
- dx: dx,
- dy: dy,
- oldX: oldX,
- oldY: oldY,
- newX: x,
- newY: y
- });
- }
- function mouseup(e) {
- if (!eventTool.isMiddleOrRightButtonOnMouseUpDown(e)) {
- this._dragging = false;
- }
- }
- function mousewheel(e) {
- var shouldZoom = isAvailableBehavior('zoomOnMouseWheel', e, this._opt);
- var shouldMove = isAvailableBehavior('moveOnMouseWheel', e, this._opt);
- var wheelDelta = e.wheelDelta;
- var absWheelDeltaDelta = Math.abs(wheelDelta);
- var originX = e.offsetX;
- var originY = e.offsetY;
- if (wheelDelta === 0 || !shouldZoom && !shouldMove) {
- return;
- }
-
-
- if (shouldZoom) {
-
-
-
-
-
-
-
-
- var factor = absWheelDeltaDelta > 3 ? 1.4 : absWheelDeltaDelta > 1 ? 1.2 : 1.1;
- var scale = wheelDelta > 0 ? factor : 1 / factor;
- checkPointerAndTrigger(this, 'zoom', 'zoomOnMouseWheel', e, {
- scale: scale,
- originX: originX,
- originY: originY
- });
- }
- if (shouldMove) {
-
- var absDelta = Math.abs(wheelDelta);
- var scrollDelta = (wheelDelta > 0 ? 1 : -1) * (absDelta > 3 ? 0.4 : absDelta > 1 ? 0.15 : 0.05);
- checkPointerAndTrigger(this, 'scrollMove', 'moveOnMouseWheel', e, {
- scrollDelta: scrollDelta,
- originX: originX,
- originY: originY
- });
- }
- }
- function pinch(e) {
- if (interactionMutex.isTaken(this._zr, 'globalPan')) {
- return;
- }
- var scale = e.pinchScale > 1 ? 1.1 : 1 / 1.1;
- checkPointerAndTrigger(this, 'zoom', null, e, {
- scale: scale,
- originX: e.pinchX,
- originY: e.pinchY
- });
- }
- function checkPointerAndTrigger(controller, eventName, behaviorToCheck, e, contollerEvent) {
- if (controller.pointerChecker && controller.pointerChecker(e, contollerEvent.originX, contollerEvent.originY)) {
-
-
-
- eventTool.stop(e.event);
- trigger(controller, eventName, behaviorToCheck, e, contollerEvent);
- }
- }
- function trigger(controller, eventName, behaviorToCheck, e, contollerEvent) {
-
-
- contollerEvent.isAvailableBehavior = zrUtil.bind(isAvailableBehavior, null, behaviorToCheck, e);
- controller.trigger(eventName, contollerEvent);
- }
- function isAvailableBehavior(behaviorToCheck, e, settings) {
- var setting = settings[behaviorToCheck];
- return !behaviorToCheck || setting && (!zrUtil.isString(setting) || e.event[setting + 'Key']);
- }
- var _default = RoamController;
- module.exports = _default;
|