RoamController.ts 10.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License.  You may obtain a copy of the License at
*
*   http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied.  See the License for the
* specific language governing permissions and limitations
* under the License.
*/

20
import Eventful from 'zrender/src/core/Eventful';
S
sushuang 已提交
21
import * as eventTool from 'zrender/src/core/event';
S
sushuang 已提交
22
import * as interactionMutex from './interactionMutex';
P
pissang 已提交
23
import { ZRenderType } from 'zrender/src/zrender';
P
pissang 已提交
24
import { ZRElementEvent, RoamOptionMixin } from '../../util/types';
P
pissang 已提交
25 26 27 28
import { Bind3, isString, bind, defaults, clone } from 'zrender/src/core/util';

// Can be null/undefined or true/false
// or 'pan/move' or 'zoom'/'scale'
P
pissang 已提交
29
export type RoamType = RoamOptionMixin['roam']
P
pissang 已提交
30 31 32 33 34

interface RoamOption {
    zoomOnMouseWheel?: boolean | 'ctrl' | 'shift' | 'alt'
    moveOnMouseMove?: boolean | 'ctrl' | 'shift' | 'alt'
    moveOnMouseWheel?: boolean | 'ctrl' | 'shift' | 'alt'
L
lang 已提交
35
    /**
P
pissang 已提交
36
     * If fixed the page when pan
L
lang 已提交
37
     */
P
pissang 已提交
38 39
    preventDefaultMouseMove?: boolean
}
L
lang 已提交
40

P
pissang 已提交
41
type RoamEventType = 'zoom' | 'scrollMove' | 'pan'
L
lang 已提交
42

P
pissang 已提交
43
type RoamBehavior = 'zoomOnMouseWheel' | 'moveOnMouseMove' | 'moveOnMouseWheel'
P
pah100 已提交
44

P
pissang 已提交
45 46 47 48 49
export type RoamEventParams = {
    'zoom': {
        scale: number
        originX: number
        originY: number
P
pah100 已提交
50

P
pissang 已提交
51 52 53 54 55 56
        isAvailableBehavior: Bind3<typeof isAvailableBehavior, null, RoamBehavior, ZRElementEvent>
    }
    'scrollMove': {
        scrollDelta: number
        originX: number
        originY: number
P
pah100 已提交
57

P
pissang 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70
        isAvailableBehavior: Bind3<typeof isAvailableBehavior, null, RoamBehavior, ZRElementEvent>
    }
    'pan': {
        dx: number
        dy: number
        oldX: number
        oldY: number
        newX: number
        newY: number

        isAvailableBehavior: Bind3<typeof isAvailableBehavior, null, RoamBehavior, ZRElementEvent>
    }
}
S
sushuang 已提交
71

P
pissang 已提交
72
class RoamController extends Eventful {
S
sushuang 已提交
73

P
pissang 已提交
74
    pointerChecker: (e: ZRElementEvent, x: number, y: number) => boolean
S
sushuang 已提交
75

P
pissang 已提交
76
    private _zr: ZRenderType
S
sushuang 已提交
77

P
pissang 已提交
78
    private _opt: Required<RoamOption>
P
pah100 已提交
79

P
pissang 已提交
80
    private _dragging: boolean
P
pah100 已提交
81

P
pissang 已提交
82
    private _pinching: boolean
P
pah100 已提交
83

P
pissang 已提交
84
    private _x: number
P
pah100 已提交
85

P
pissang 已提交
86
    private _y: number
P
pah100 已提交
87

P
pissang 已提交
88
    readonly enable: (this: this, controlType?: RoamType, opt?: RoamOption) => void
P
pah100 已提交
89

P
pissang 已提交
90
    readonly disable: () => void
P
pah100 已提交
91

S
sushuang 已提交
92

P
pissang 已提交
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
    constructor(zr: ZRenderType) {
        super();

        this._zr = zr;

        // Avoid two roamController bind the same handler
        const mousedownHandler = bind(this._mousedownHandler, this);
        const mousemoveHandler = bind(this._mousemoveHandler, this);
        const mouseupHandler = bind(this._mouseupHandler, this);
        const mousewheelHandler = bind(this._mousewheelHandler, this);
        const pinchHandler = bind(this._pinchHandler, this);

        /**
         * Notice: only enable needed types. For example, if 'zoom'
         * is not needed, 'zoom' should not be enabled, otherwise
         * default mousewheel behaviour (scroll page) will be disabled.
         */
        this.enable = function (controlType, opt) {

            // Disable previous first
            this.disable();

            this._opt = defaults(clone(opt) || {}, {
                zoomOnMouseWheel: true,
                moveOnMouseMove: true,
                // By default, wheel do not trigger move.
                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);
        };
P
pah100 已提交
145 146
    }

P
pissang 已提交
147 148 149
    isDragging() {
        return this._dragging;
    }
S
sushuang 已提交
150

P
pissang 已提交
151 152
    isPinching() {
        return this._pinching;
S
sushuang 已提交
153 154
    }

P
pissang 已提交
155 156
    setPointerChecker(pointerChecker: RoamController['pointerChecker']) {
        this.pointerChecker = pointerChecker;
P
pah100 已提交
157 158
    }

P
pissang 已提交
159 160 161 162 163 164 165 166 167 168
    dispose() {
        this.disable();
    }

    private _mousedownHandler(e: ZRElementEvent) {
        if (eventTool.isMiddleOrRightButtonOnMouseUpDown(e)
            || (e.target && e.target.draggable)
        ) {
            return;
        }
P
pah100 已提交
169

P
pissang 已提交
170 171
        var x = e.offsetX;
        var y = e.offsetY;
S
sushuang 已提交
172

P
pissang 已提交
173 174 175 176 177 178 179 180
        // Only check on mosedown, but not mousemove.
        // Mouse can be out of target when mouse moving.
        if (this.pointerChecker && this.pointerChecker(e, x, y)) {
            this._x = x;
            this._y = y;
            this._dragging = true;
        }
    }
S
sushuang 已提交
181

P
pissang 已提交
182 183 184 185 186 187 188 189
    private _mousemoveHandler(e: ZRElementEvent) {
        if (!this._dragging
            || !isAvailableBehavior('moveOnMouseMove', e, this._opt)
            || e.gestureEvent === 'pinch'
            || interactionMutex.isTaken(this._zr, 'globalPan')
        ) {
            return;
        }
S
sushuang 已提交
190

P
pissang 已提交
191 192
        var x = e.offsetX;
        var y = e.offsetY;
S
sushuang 已提交
193

P
pissang 已提交
194 195
        var oldX = this._x;
        var oldY = this._y;
S
sushuang 已提交
196

P
pissang 已提交
197 198 199 200 201 202 203 204 205 206 207
        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, isAvailableBehavior: null
        });
P
pah100 已提交
208 209
    }

P
pissang 已提交
210 211 212 213
    private _mouseupHandler(e: ZRElementEvent) {
        if (!eventTool.isMiddleOrRightButtonOnMouseUpDown(e)) {
            this._dragging = false;
        }
P
pah100 已提交
214
    }
S
sushuang 已提交
215

P
pissang 已提交
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
    private _mousewheelHandler(e: ZRElementEvent) {
        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;

        // wheelDelta maybe -0 in chrome mac.
        if (wheelDelta === 0 || (!shouldZoom && !shouldMove)) {
            return;
        }

        // If both `shouldZoom` and `shouldMove` is true, trigger
        // their event both, and the final behavior is determined
        // by event listener themselves.

        if (shouldZoom) {
            // Convenience:
            // Mac and VM Windows on Mac: scroll up: zoom out.
            // Windows: scroll up: zoom in.

            // FIXME: Should do more test in different environment.
            // wheelDelta is too complicated in difference nvironment
            // (https://developer.mozilla.org/en-US/docs/Web/Events/mousewheel),
            // although it has been normallized by zrender.
            // wheelDelta of mouse wheel is bigger than touch pad.
            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, isAvailableBehavior: null
            });
        }

        if (shouldMove) {
            // FIXME: Should do more test in different environment.
            var absDelta = Math.abs(wheelDelta);
            // wheelDelta of mouse wheel is bigger than touch pad.
            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, isAvailableBehavior: null
            });
        }
259
    }
S
sushuang 已提交
260

P
pissang 已提交
261 262 263 264 265 266 267
    private _pinchHandler(e: ZRElementEvent) {
        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, isAvailableBehavior: null
S
sushuang 已提交
268
        });
269
    }
S
sushuang 已提交
270
}
P
pah100 已提交
271

S
sushuang 已提交
272

P
pissang 已提交
273 274 275 276 277 278 279
function checkPointerAndTrigger<T extends 'scrollMove' | 'zoom'>(
    controller: RoamController,
    eventName: T,
    behaviorToCheck: RoamBehavior,
    e: ZRElementEvent,
    contollerEvent: RoamEventParams[T]
) {
S
sushuang 已提交
280 281 282
    if (controller.pointerChecker
        && controller.pointerChecker(e, contollerEvent.originX, contollerEvent.originY)
    ) {
S
sushuang 已提交
283 284 285 286 287
        // When mouse is out of roamController rect,
        // default befavoius should not be be disabled, otherwise
        // page sliding is disabled, contrary to expectation.
        eventTool.stop(e.event);

S
sushuang 已提交
288
        trigger(controller, eventName, behaviorToCheck, e, contollerEvent);
P
pah100 已提交
289
    }
S
sushuang 已提交
290 291
}

P
pissang 已提交
292 293 294 295 296 297 298
function trigger<T extends RoamEventType>(
    controller: RoamController,
    eventName: T,
    behaviorToCheck: RoamBehavior,
    e: ZRElementEvent,
    contollerEvent: RoamEventParams[T]
) {
S
sushuang 已提交
299 300
    // Also provide behavior checker for event listener, for some case that
    // multiple components share one listener.
P
pissang 已提交
301
    contollerEvent.isAvailableBehavior = bind(isAvailableBehavior, null, behaviorToCheck, e);
S
sushuang 已提交
302 303 304 305 306 307 308 309 310
    controller.trigger(eventName, contollerEvent);
}

// settings: {
//     zoomOnMouseWheel
//     moveOnMouseMove
//     moveOnMouseWheel
// }
// The value can be: true / false / 'shift' / 'ctrl' / 'alt'.
P
pissang 已提交
311 312 313 314 315
function isAvailableBehavior(
    behaviorToCheck: RoamBehavior,
    e: ZRElementEvent,
    settings: Pick<RoamOption, RoamBehavior>
) {
S
sushuang 已提交
316 317
    var setting = settings[behaviorToCheck];
    return !behaviorToCheck || (
P
pissang 已提交
318
        setting && (!isString(setting) || e.event[setting + 'Key' as 'shiftKey' | 'ctrlKey' | 'altKey'])
S
sushuang 已提交
319
    );
S
sushuang 已提交
320
}
P
pah100 已提交
321

S
sushuang 已提交
322
export default RoamController;