All files / core/domain/classes TouchCaptor.ts

25.13% Statements 47/187
8% Branches 2/25
36.36% Functions 4/11
25% Lines 46/184

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 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 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 4144x 4x 4x 4x 4x       4x                     4x       4x 4x 4x 4x     4x 4x           4x 4x 4x 4x 4x 4x 4x 4x 4x 4x     4x 4x 4x   4x   4x         4x 4x 4x 4x 4x           4x 3x 3x 3x 3x 3x 3x     4x                               4x                                                                                                                                                             4x                                                                                                                 4x                                                                                                                                                                                                                                                                                     4x                                                                                   4x    
import Dispatcher from "./Dispatcher";
import unbindDoubleClick from "../utils/events/unbindDoubleClick";
import mouseCoords from "../utils/events/mouseCoords";
import getCenter from "../utils/events/getCenter";
import getOffset from "../utils/events/getOffset";
import Camera from "./Camera";
import { SigmaLibrary } from "../../interfaces";
 
export default function touchCaptor(sigma: SigmaLibrary) {
  /**
   * The user inputs default captor. It deals with mouse events, keyboards
   * events and touch events.
   *
   * @param  {DOMElement}   target   The DOM element where the listeners will be
   *                                 bound.
   * @param  {camera}       camera   The camera related to the target.
   * @param  {configurable} settings The settings function.
   * @return {captor}          The fresh new captor instance.
   */
  return class TouchCaptor extends Dispatcher {
    // CAMERA MANAGEMENT:
    // ******************
    // The camera position when the user starts dragging:
    public startCameraX = 0;
    public startCameraY = 0;
    public startCameraAngle = 0;
    public startCameraRatio = 0;
 
    // The latest stage position:
    public lastCameraX = 0;
    public lastCameraY = 0;
 
    // TOUCH MANAGEMENT:
    // *****************
    // Touches that are down:
    private downTouches: TouchList | undefined;
    private startTouchX0 = 0;
    private startTouchY0 = 0;
    private startTouchX1 = 0;
    private startTouchY1 = 0;
    private startTouchAngle = 0;
    private startTouchDistance = 0;
    private touchMode = 0;
    private isMoving = false;
    private doubleTap = false;
    private movingTimeout = 0;
 
    constructor(
      public target: HTMLElement,
      public camera: Camera,
      public settings: any
    ) {
      super();
 
      sigma.utils.events.doubleClick(
        target,
        "touchstart",
        this.doubleTapHandler
      );
      target.addEventListener("touchstart", this.handleStart, false);
      target.addEventListener("touchend", this.handleLeave, false);
      target.addEventListener("touchcancel", this.handleLeave, false);
      target.addEventListener("touchleave", this.handleLeave, false);
      target.addEventListener("touchmove", this.handleMove, false);
    }
 
    /**
     * This method unbinds every handlers that makes the captor work.
     */
    public kill = () => {
      unbindDoubleClick(this.target, "touchstart");
      this.target.addEventListener("touchstart", this.handleStart);
      this.target.addEventListener("touchend", this.handleLeave);
      this.target.addEventListener("touchcancel", this.handleLeave);
      this.target.addEventListener("touchleave", this.handleLeave);
      this.target.addEventListener("touchmove", this.handleMove);
    };
 
    private position(e: Touch) {
      const offset = getOffset(this.target);
      return {
        x: e.pageX - offset.left,
        y: e.pageY - offset.top
      };
    }
 
    // TOUCH EVENTS:
    // *************
    /**
     * The handler listening to the 'touchstart' event. It will set the touch
     * mode ("this.touchMode") and start observing the user touch moves.
     *
     * @param {event} e A touch event.
     */
    private handleStart = (e: TouchEvent) => {
      if (this.settings("touchEnabled")) {
        let x0;
        let x1;
        let y0;
        let y1;
        let pos0;
        let pos1;
 
        this.downTouches = e.touches;
 
        switch (this.downTouches.length) {
          case 1:
            this.camera.isMoving = true;
            this.touchMode = 1;
 
            this.startCameraX = this.camera.x;
            this.startCameraY = this.camera.y;
 
            this.lastCameraX = this.camera.x;
            this.lastCameraY = this.camera.y;
 
            pos0 = this.position(this.downTouches[0]);
            this.startTouchX0 = pos0.x;
            this.startTouchY0 = pos0.y;
 
            break;
          case 2:
            this.camera.isMoving = true;
            this.touchMode = 2;
 
            pos0 = this.position(this.downTouches[0]);
            pos1 = this.position(this.downTouches[1]);
            x0 = pos0.x;
            y0 = pos0.y;
            x1 = pos1.x;
            y1 = pos1.y;
 
            this.lastCameraX = this.camera.x;
            this.lastCameraY = this.camera.y;
 
            this.startCameraAngle = this.camera.angle;
            this.startCameraRatio = this.camera.ratio;
 
            this.startCameraX = this.camera.x;
            this.startCameraY = this.camera.y;
 
            this.startTouchX0 = x0;
            this.startTouchY0 = y0;
            this.startTouchX1 = x1;
            this.startTouchY1 = y1;
 
            this.startTouchAngle = Math.atan2(
              this.startTouchY1 - this.startTouchY0,
              this.startTouchX1 - this.startTouchX0
            );
            this.startTouchDistance = Math.sqrt(
              (this.startTouchY1 - this.startTouchY0) *
                (this.startTouchY1 - this.startTouchY0) +
                (this.startTouchX1 - this.startTouchX0) *
                  (this.startTouchX1 - this.startTouchX0)
            );
 
            e.preventDefault();
            return false;
          default:
            console.log(`Unhandled downtouches ${this.downTouches.length}`);
        }
      }
      return undefined;
    };
 
    /**
     * The handler listening to the 'touchend', 'touchcancel' and 'touchleave'
     * event. It will update the touch mode if there are still at least one
     * finger, and stop dragging else.
     *
     * @param {event} e A touch event.
     */
    private handleLeave = (e: TouchEvent) => {
      if (this.settings("touchEnabled")) {
        this.downTouches = e.touches;
        const inertiaRatio = this.settings("touchInertiaRatio");
 
        if (this.movingTimeout) {
          this.isMoving = false;
          clearTimeout(this.movingTimeout);
        }
 
        switch (this.touchMode) {
          case 2:
            if (e.touches.length === 1) {
              this.handleStart(e);
              e.preventDefault();
              break;
            }
          /* falls through */
          case 1:
            this.camera.isMoving = false;
            this.dispatchEvent("stopDrag");
 
            if (this.isMoving) {
              this.doubleTap = false;
              sigma.misc.animation.camera(
                this.camera,
                {
                  x:
                    this.camera.x +
                    inertiaRatio * (this.camera.x - this.lastCameraX),
                  y:
                    this.camera.y +
                    inertiaRatio * (this.camera.y - this.lastCameraY)
                },
                {
                  easing: "quadraticOut",
                  duration: this.settings("touchInertiaDuration")
                }
              );
            }
 
            this.isMoving = false;
            this.touchMode = 0;
            break;
          default:
            console.log(`Unhandled touchMode ${this.touchMode}`);
        }
      }
    };
 
    /**
     * The handler listening to the 'touchmove' event. It will effectively drag
     * the graph, and eventually zooms and turn it if the user is using two
     * fingers.
     *
     * @param {event} e A touch event.
     */
    private handleMove = e => {
      if (!this.doubleTap && this.settings("touchEnabled")) {
        let x0;
        let x1;
        let y0;
        let y1;
        let cos;
        let sin;
        let end;
        let pos0;
        let pos1;
        let diff;
        let start;
        let dAngle;
        let dRatio;
        let newStageX;
        let newStageY;
        let newStageRatio;
        let newStageAngle;
        this.downTouches = e.touches;
        this.isMoving = true;
 
        if (this.movingTimeout) clearTimeout(this.movingTimeout);
 
        this.movingTimeout = setTimeout(() => {
          this.isMoving = false;
        }, this.settings("dragTimeout")) as any;
 
        switch (this.touchMode) {
          case 1:
            pos0 = this.position(this.downTouches[0]);
            x0 = pos0.x;
            y0 = pos0.y;
 
            diff = this.camera.cameraPosition(
              x0 - this.startTouchX0,
              y0 - this.startTouchY0,
              true
            );
 
            newStageX = this.startCameraX - diff.x;
            newStageY = this.startCameraY - diff.y;
 
            if (newStageX !== this.camera.x || newStageY !== this.camera.y) {
              this.lastCameraX = this.camera.x;
              this.lastCameraY = this.camera.y;
 
              this.camera.goTo({
                x: newStageX,
                y: newStageY
              });
 
              this.dispatchEvent("mousemove", mouseCoords(e, pos0.x, pos0.y));
 
              this.dispatchEvent("drag");
            }
            break;
          case 2:
            pos0 = this.position(this.downTouches[0]);
            pos1 = this.position(this.downTouches[1]);
            x0 = pos0.x;
            y0 = pos0.y;
            x1 = pos1.x;
            y1 = pos1.y;
 
            start = this.camera.cameraPosition(
              (this.startTouchX0 + this.startTouchX1) / 2 - getCenter(e).x,
              (this.startTouchY0 + this.startTouchY1) / 2 - getCenter(e).y,
              true
            );
            end = this.camera.cameraPosition(
              (x0 + x1) / 2 - getCenter(e).x,
              (y0 + y1) / 2 - getCenter(e).y,
              true
            );
 
            dAngle = Math.atan2(y1 - y0, x1 - x0) - this.startTouchAngle;
            dRatio =
              Math.sqrt((y1 - y0) * (y1 - y0) + (x1 - x0) * (x1 - x0)) /
              this.startTouchDistance;
 
            // Translation:
            x0 = start.x;
            y0 = start.y;
 
            // Homothetic transformation:
            newStageRatio = this.startCameraRatio / dRatio;
            x0 *= dRatio;
            y0 *= dRatio;
 
            // Rotation:
            newStageAngle = this.startCameraAngle - dAngle;
            cos = Math.cos(-dAngle);
            sin = Math.sin(-dAngle);
            x1 = x0 * cos + y0 * sin;
            y1 = y0 * cos - x0 * sin;
            x0 = x1;
            y0 = y1;
 
            // Finalize:
            newStageX = x0 - end.x + this.startCameraX;
            newStageY = y0 - end.y + this.startCameraY;
 
            if (
              newStageRatio !== this.camera.ratio ||
              newStageAngle !== this.camera.angle ||
              newStageX !== this.camera.x ||
              newStageY !== this.camera.y
            ) {
              this.lastCameraX = this.camera.x;
              this.lastCameraY = this.camera.y;
 
              this.camera.goTo({
                x: newStageX,
                y: newStageY,
                angle: newStageAngle,
                ratio: newStageRatio
              });
 
              this.dispatchEvent("drag");
            }
 
            break;
          default:
            console.log(`Unhandled touchMode ${this.touchMode}`);
        }
 
        e.preventDefault();
        return false;
      }
      return undefined;
    };
 
    /**
     * The handler listening to the double tap custom event. It will
     * basically zoom into the graph.
     *
     * @param {event} e A touch event.
     */
    private doubleTapHandler = e => {
      let pos;
      let ratio;
      let animation;
 
      if (
        e.touches &&
        e.touches.length === 1 &&
        this.settings("touchEnabled")
      ) {
        this.doubleTap = true;
 
        ratio = 1 / this.settings("doubleClickZoomingRatio");
 
        pos = this.position(e.touches[0]);
        this.dispatchEvent("doubleclick", mouseCoords(e, pos.x, pos.y));
 
        if (this.settings("doubleClickEnabled")) {
          pos = this.camera.cameraPosition(
            pos.x - getCenter(e).x,
            pos.y - getCenter(e).y,
            true
          );
 
          animation = {
            duration: this.settings("doubleClickZoomDuration"),
            onComplete() {
              this.doubleTap = false;
            }
          };
 
          sigma.utils.zoomTo(this.camera, pos.x, pos.y, ratio, animation);
        }
 
        if (e.preventDefault) e.preventDefault();
        else e.returnValue = false;
 
        e.stopPropagation();
        return false;
      }
      return undefined;
    };
  } as any;
}