All files / core/domain/classes MouseCaptor.ts

39.53% Statements 51/129
5.88% Branches 2/34
33.33% Functions 4/12
40.98% Lines 50/122

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 3534x 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                                                                                               4x                                                                                         4x                   4x                                         4x                                                                                   4x                                                                                     4x 3x 3x 3x 3x 3x 3x 3x 3x 3x   4x    
import Dispatcher from "./Dispatcher";
import unbindDoubleClick from "../utils/events/unbindDoubleClick";
import mouseCoords from "../utils/events/mouseCoords";
import getX from "../utils/events/getX";
import getY from "../utils/events/getY";
import getCenter from "../utils/events/getCenter";
import getDelta from "../utils/events/getDelta";
import Camera from "./Camera";
import { SigmaLibrary } from "../../interfaces";
 
export default (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 MouseCaptor extends Dispatcher {
    // CAMERA MANAGEMENT:
    // ******************
    // The camera position when the user starts dragging:
    private startCameraX = 0;
    private startCameraY = 0;
 
    // The latest stage position:
    private lastCameraX = 0;
    private lastCameraY = 0;
 
    // MOUSE MANAGEMENT:
    // *****************
    // The mouse position when the user starts dragging:
    private startMouseX = 0;
    private startMouseY = 0;
    private isMouseDown = false;
    private isMoving = false;
    private hasDragged = false;
    private downStartTime = 0;
    private movingTimeoutId = null;
 
    constructor(
      private target: HTMLElement,
      private camera: Camera,
      private settings: any
    ) {
      super();
      sigma.utils.events.doubleClick(target, "click", this.doubleClickHandler);
      target.addEventListener(
        "DOMMouseScroll",
        this.wheelHandler.bind(this),
        false
      );
      target.addEventListener(
        "mousewheel",
        this.wheelHandler.bind(this),
        false
      );
      target.addEventListener("mousemove", this.moveHandler, false);
      target.addEventListener("mousedown", this.downHandler, false);
      target.addEventListener("click", this.clickHandler, false);
      target.addEventListener("mouseout", this.outHandler, false);
      document.addEventListener("mouseup", this.upHandler, false);
    }
 
    /**
     * The handler listening to the 'move' mouse event. It will effectively
     * drag the graph.
     *
     * @param {event} e A mouse event.
     */
    private moveHandler = e => {
      let x;
      let y;
      let pos;
 
      // Dispatch event:
      if (this.settings("mouseEnabled")) {
        this.dispatchEvent("mousemove", mouseCoords(e));
 
        if (this.isMouseDown) {
          this.isMoving = true;
          this.hasDragged = true;
 
          if (this.movingTimeoutId) clearTimeout(this.movingTimeoutId);
 
          this.movingTimeoutId = setTimeout(function stopMoving() {
            this.isMoving = false;
          }, this.settings("dragTimeout"));
 
          sigma.misc.animation.killAll(this.camera);
 
          this.camera.isMoving = true;
          pos = this.camera.cameraPosition(
            getX(e) - this.startMouseX,
            getY(e) - this.startMouseY,
            true
          );
 
          x = this.startCameraX - pos.x;
          y = this.startCameraY - pos.y;
 
          if (x !== this.camera.x || y !== this.camera.y) {
            this.lastCameraX = this.camera.x;
            this.lastCameraY = this.camera.y;
 
            this.camera.goTo({
              x,
              y
            });
          }
 
          if (e.preventDefault) e.preventDefault();
          else e.returnValue = false;
 
          e.stopPropagation();
          return false;
        }
      }
      return undefined;
    };
 
    /**
     * The handler listening to the 'up' mouse event. It will stop dragging the
     * graph.
     *
     * @param {event} e A mouse event.
     */
    private upHandler = e => {
      if (this.settings("mouseEnabled") && this.isMouseDown) {
        this.isMouseDown = false;
        if (this.movingTimeoutId) clearTimeout(this.movingTimeoutId);
 
        this.camera.isMoving = false;
 
        const x = getX(e);
        const y = getY(e);
 
        if (this.isMoving) {
          sigma.misc.animation.killAll(this.camera);
          sigma.misc.animation.camera(
            this.camera,
            {
              x:
                this.camera.x +
                this.settings("mouseInertiaRatio") *
                  (this.camera.x - this.lastCameraX),
              y:
                this.camera.y +
                this.settings("mouseInertiaRatio") *
                  (this.camera.y - this.lastCameraY)
            },
            {
              easing: "quadraticOut",
              duration: this.settings("mouseInertiaDuration")
            }
          );
        } else if (this.startMouseX !== x || this.startMouseY !== y)
          this.camera.goTo({
            x: this.camera.x,
            y: this.camera.y
          });
 
        this.dispatchEvent("mouseup", mouseCoords(e));
 
        // Update this.isMoving flag:
        this.isMoving = false;
      }
    };
 
    /**
     * The handler listening to the 'down' mouse event. It will start observing
     * the mouse position for dragging the graph.
     *
     * @param {event} e A mouse event.
     */
    private downHandler = e => {
      if (this.settings("mouseEnabled")) {
        this.startCameraX = this.camera.x;
        this.startCameraY = this.camera.y;
 
        this.lastCameraX = this.camera.x;
        this.lastCameraY = this.camera.y;
 
        this.startMouseX = getX(e);
        this.startMouseY = getY(e);
 
        this.hasDragged = false;
        this.downStartTime = new Date().getTime();
 
        switch (e.which) {
          case 2:
            // Middle mouse button pressed
            // Do nothing.
            break;
          case 3:
            // Right mouse button pressed
            this.dispatchEvent(
              "rightclick",
              mouseCoords(e, this.startMouseX, this.startMouseY)
            );
            break;
          // case 1:
          default:
            // Left mouse button pressed
            this.isMouseDown = true;
 
            this.dispatchEvent(
              "mousedown",
              mouseCoords(e, this.startMouseX, this.startMouseY)
            );
        }
      }
    };
 
    /**
     * The handler listening to the 'out' mouse event. It will just redispatch
     * the event.
     *
     * @param {event} e A mouse event.
     */
    private outHandler = () => {
      if (this.settings("mouseEnabled")) this.dispatchEvent("mouseout");
    };
 
    /**
     * The handler listening to the 'click' mouse event. It will redispatch the
     * click event, but with normalized X and Y coordinates.
     *
     * @param {event} e A mouse event.
     */
    private clickHandler = (e: MouseEvent) => {
      if (this.settings("mouseEnabled")) {
        const event = mouseCoords(e);
        (event as any).isDragging =
          new Date().getTime() - this.downStartTime > 100 && this.hasDragged;
        this.dispatchEvent("click", event);
      }
 
      if (e.preventDefault) e.preventDefault();
      else e.returnValue = false;
 
      e.stopPropagation();
      return false;
    };
 
    /**
     * The handler listening to the double click custom event. It will
     * basically zoom into the graph.
     *
     * @param {event} e A mouse event.
     */
    private doubleClickHandler = (e: MouseEvent) => {
      let pos;
      let ratio;
      let animation;
 
      if (this.settings("mouseEnabled")) {
        ratio = 1 / this.settings("doubleClickZoomingRatio");
 
        this.dispatchEvent(
          "doubleclick",
          mouseCoords(e, this.startMouseX, this.startMouseY)
        );
 
        if (this.settings("doubleClickEnabled")) {
          pos = this.camera.cameraPosition(
            getX(e) - getCenter(e).x,
            getY(e) - getCenter(e).y,
            true
          );
 
          animation = {
            duration: this.settings("doubleClickZoomDuration")
          };
 
          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;
    };
 
    /**
     * The handler listening to the 'wheel' mouse event. It will basically zoom
     * in or not into the graph.
     *
     * @param {event} e A mouse event.
     */
    private wheelHandler = (e: MouseEvent) => {
      let pos;
      let ratio;
      let animation;
      const wheelDelta = getDelta(e);
 
      if (
        this.settings("mouseEnabled") &&
        this.settings("mouseWheelEnabled") &&
        wheelDelta !== 0
      ) {
        ratio =
          wheelDelta > 0
            ? 1 / this.settings("zoomingRatio")
            : this.settings("zoomingRatio");
 
        pos = this.camera.cameraPosition(
          getX(e) - getCenter(e).x,
          getY(e) - getCenter(e).y,
          true
        );
 
        animation = {
          duration: this.settings("mouseZoomDuration")
        };
 
        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;
    };
 
    /**
     * This method unbinds every handlers that makes the captor work.
     */
    public kill = () => {
      const { target } = this;
      unbindDoubleClick(target, "click");
      target.removeEventListener("DOMMouseScroll", this.wheelHandler);
      target.removeEventListener("mousewheel", this.wheelHandler);
      target.removeEventListener("mousemove", this.moveHandler);
      target.removeEventListener("mousedown", this.downHandler);
      target.removeEventListener("click", this.clickHandler);
      target.removeEventListener("mouseout", this.outHandler);
      document.removeEventListener("mouseup", this.upHandler);
    };
  };
};