RoamController.ts 10.5 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
import { Bind3, isString, bind, defaults, clone } from 'zrender/src/core/util';
26
import Group from 'zrender/src/container/Group';
P
pissang 已提交
27 28 29

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

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

1
100pah 已提交
42
type RoamEventType = keyof RoamEventParams;
L
lang 已提交
43

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

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

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

P
pissang 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71
        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 已提交
72

73 74 75 76 77 78 79 80 81
export interface RoamControllerHost {
    target: Group
    zoom: number
    zoomLimit: {
        min?: number
        max?: number
    }
}

1
100pah 已提交
82
class RoamController extends Eventful<RoamEventParams> {
S
sushuang 已提交
83

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

P
pissang 已提交
86
    private _zr: ZRenderType
S
sushuang 已提交
87

P
pissang 已提交
88
    private _opt: Required<RoamOption>
P
pah100 已提交
89

P
pissang 已提交
90
    private _dragging: boolean
P
pah100 已提交
91

P
pissang 已提交
92
    private _pinching: boolean
P
pah100 已提交
93

P
pissang 已提交
94
    private _x: number
P
pah100 已提交
95

P
pissang 已提交
96
    private _y: number
P
pah100 已提交
97

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

P
pissang 已提交
100
    readonly disable: () => void
P
pah100 已提交
101

S
sushuang 已提交
102

P
pissang 已提交
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 145 146 147 148 149 150 151 152 153 154
    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 已提交
155 156
    }

P
pissang 已提交
157 158 159
    isDragging() {
        return this._dragging;
    }
S
sushuang 已提交
160

P
pissang 已提交
161 162
    isPinching() {
        return this._pinching;
S
sushuang 已提交
163 164
    }

P
pissang 已提交
165 166
    setPointerChecker(pointerChecker: RoamController['pointerChecker']) {
        this.pointerChecker = pointerChecker;
P
pah100 已提交
167 168
    }

P
pissang 已提交
169 170 171 172 173 174 175 176 177 178
    dispose() {
        this.disable();
    }

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

P
pissang 已提交
180 181
        var x = e.offsetX;
        var y = e.offsetY;
S
sushuang 已提交
182

P
pissang 已提交
183 184 185 186 187 188 189 190
        // 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 已提交
191

P
pissang 已提交
192 193 194 195 196 197 198 199
    private _mousemoveHandler(e: ZRElementEvent) {
        if (!this._dragging
            || !isAvailableBehavior('moveOnMouseMove', e, this._opt)
            || e.gestureEvent === 'pinch'
            || interactionMutex.isTaken(this._zr, 'globalPan')
        ) {
            return;
        }
S
sushuang 已提交
200

P
pissang 已提交
201 202
        var x = e.offsetX;
        var y = e.offsetY;
S
sushuang 已提交
203

P
pissang 已提交
204 205
        var oldX = this._x;
        var oldY = this._y;
S
sushuang 已提交
206

P
pissang 已提交
207 208 209 210 211 212 213 214 215 216 217
        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 已提交
218 219
    }

P
pissang 已提交
220 221 222 223
    private _mouseupHandler(e: ZRElementEvent) {
        if (!eventTool.isMiddleOrRightButtonOnMouseUpDown(e)) {
            this._dragging = false;
        }
P
pah100 已提交
224
    }
S
sushuang 已提交
225

P
pissang 已提交
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 259 260 261 262 263 264 265 266 267 268
    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
            });
        }
269
    }
S
sushuang 已提交
270

P
pissang 已提交
271 272 273 274 275 276 277
    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 已提交
278
        });
279
    }
S
sushuang 已提交
280
}
P
pah100 已提交
281

S
sushuang 已提交
282

P
pissang 已提交
283 284 285 286 287 288 289
function checkPointerAndTrigger<T extends 'scrollMove' | 'zoom'>(
    controller: RoamController,
    eventName: T,
    behaviorToCheck: RoamBehavior,
    e: ZRElementEvent,
    contollerEvent: RoamEventParams[T]
) {
S
sushuang 已提交
290 291 292
    if (controller.pointerChecker
        && controller.pointerChecker(e, contollerEvent.originX, contollerEvent.originY)
    ) {
S
sushuang 已提交
293 294 295 296 297
        // 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 已提交
298
        trigger(controller, eventName, behaviorToCheck, e, contollerEvent);
P
pah100 已提交
299
    }
S
sushuang 已提交
300 301
}

P
pissang 已提交
302 303 304 305 306 307 308
function trigger<T extends RoamEventType>(
    controller: RoamController,
    eventName: T,
    behaviorToCheck: RoamBehavior,
    e: ZRElementEvent,
    contollerEvent: RoamEventParams[T]
) {
S
sushuang 已提交
309 310
    // Also provide behavior checker for event listener, for some case that
    // multiple components share one listener.
P
pissang 已提交
311
    contollerEvent.isAvailableBehavior = bind(isAvailableBehavior, null, behaviorToCheck, e);
S
sushuang 已提交
312 313 314 315 316 317 318 319 320
    controller.trigger(eventName, contollerEvent);
}

// settings: {
//     zoomOnMouseWheel
//     moveOnMouseMove
//     moveOnMouseWheel
// }
// The value can be: true / false / 'shift' / 'ctrl' / 'alt'.
P
pissang 已提交
321 322 323 324 325
function isAvailableBehavior(
    behaviorToCheck: RoamBehavior,
    e: ZRElementEvent,
    settings: Pick<RoamOption, RoamBehavior>
) {
S
sushuang 已提交
326 327
    var setting = settings[behaviorToCheck];
    return !behaviorToCheck || (
P
pissang 已提交
328
        setting && (!isString(setting) || e.event[setting + 'Key' as 'shiftKey' | 'ctrlKey' | 'altKey'])
S
sushuang 已提交
329
    );
S
sushuang 已提交
330
}
P
pah100 已提交
331

S
sushuang 已提交
332
export default RoamController;