All files / sigma.js/src/core/misc sigma.misc.bindDOMEvents.ts

3.57% Statements 2/56
0% Branches 0/20
6.67% Functions 1/15
3.85% Lines 2/52

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    4x                                                                                                                                                                                                                                                                                               4x    
import { SigmaLibrary, Renderer, Edge } from "../interfaces";
 
export default function configure(sigma: SigmaLibrary) {
  /**
   * This helper will bind any DOM renderer (for instance svg)
   * to its captors, to properly dispatch the good events to the sigma instance
   * to manage clicking, hovering etc...
   */
  function bindDOMEvents(this: Renderer, container: HTMLElement) {
    const self = this;
    const { graph } = this;
 
    // DOMElement abstraction
    class Element {
      constructor(private domElement: HTMLElement) {}
 
      public get class() {
        return this.attr("class");
      }
 
      public get id() {
        return this.attr("id");
      }
 
      public get tag() {
        return this.domElement.tagName;
      }
 
      public isNode(): boolean {
        return !!~this.class.indexOf(`${self.settings("classPrefix")}-node`);
      }
 
      public isEdge(): boolean {
        return !!~this.class.indexOf(`${self.settings("classPrefix")}-edge`);
      }
 
      public isHover(): boolean {
        return !!~this.class.indexOf(`${self.settings("classPrefix")}-hover`);
      }
 
      public attr(name: string): string {
        return this.domElement.getAttributeNS(null, name) || "";
      }
    }
 
    // Click
    function click(e: any) {
      if (!self.settings("eventsEnabled")) return;
 
      // Generic event
      self.dispatchEvent("click", e);
 
      // Are we on a node?
      const element = new Element(e.target);
 
      if (element.isNode())
        self.dispatchEvent("clickNode", {
          node: graph.nodes(element.attr("data-node-id"))
        });
      else self.dispatchEvent("clickStage");
 
      e.preventDefault();
      e.stopPropagation();
    }
 
    // Double click
    function doubleClick(e: any) {
      if (!self.settings("eventsEnabled")) return;
 
      // Generic event
      self.dispatchEvent("doubleClick", e);
 
      // Are we on a node?
      const element = new Element(e.target);
 
      if (element.isNode())
        self.dispatchEvent("doubleClickNode", {
          node: graph.nodes(element.attr("data-node-id"))
        });
      else self.dispatchEvent("doubleClickStage");
 
      e.preventDefault();
      e.stopPropagation();
    }
 
    // On over
    function onOver(e: any) {
      const target = e.toElement || e.target;
 
      if (!self.settings("eventsEnabled") || !target) return;
 
      const el = new Element(target);
 
      if (el.isNode()) {
        self.dispatchEvent("overNode", {
          node: graph.nodes(el.attr("data-node-id"))
        });
      } else if (el.isEdge()) {
        const [edge] = graph.edges(el.attr("data-edge-id"));
        self.dispatchEvent("overEdge", {
          edge,
          source: graph.nodes(edge.source),
          target: graph.nodes(edge.target)
        });
      }
    }
 
    // On out
    function onOut(e: any) {
      const target = (e.fromElement || e.originalTarget) as HTMLElement;
 
      if (!self.settings("eventsEnabled")) return;
 
      const el = new Element(target);
 
      if (el.isNode()) {
        self.dispatchEvent("outNode", {
          node: graph.nodes(el.attr("data-node-id"))
        });
      } else if (el.isEdge()) {
        const [edge] = graph.edges(el.attr("data-edge-id"));
        self.dispatchEvent("outEdge", {
          edge,
          source: graph.nodes(edge.source),
          target: graph.nodes(edge.target)
        });
      }
    }
 
    // Registering Events:
 
    // Click
    container.addEventListener("click", click, false);
    sigma.utils.events.doubleClick(container, "click", doubleClick);
 
    // Touch counterparts
    container.addEventListener("touchstart", click, false);
    sigma.utils.events.doubleClick(container, "touchstart", doubleClick);
 
    // Mouseover
    container.addEventListener("mouseover", onOver, true);
 
    // Mouseout
    container.addEventListener("mouseout", onOut, true);
  }
 
  sigma.register("sigma.misc.bindDOMEvents", bindDOMEvents);
}