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

1.79% Statements 1/56
0% Branches 0/26
9.09% Functions 1/11
1.96% Lines 1/51

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                4x                                                                                                                                                                                                                                                                    
export default function configure(sigma) {
  /**
   * 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...
   *
   * It has to be called in the scope of the related renderer.
   */
  sigma.register("sigma.misc.bindDOMEvents", function bindDOMEvents(container) {
    const self = this;
 
    const { graph } = this;
 
    // DOMElement abstraction
    function Element(domElement) {
      // Helpers
      this.attr = attrName => domElement.getAttributeNS(null, attrName);
 
      // Properties
      this.tag = domElement.tagName;
      this.class = this.attr("class");
      this.id = this.attr("id");
 
      // Methods
      this.isNode = function isNode() {
        return !!~this.class.indexOf(`${self.settings("classPrefix")}-node`);
      };
 
      this.isEdge = function isEdge() {
        return !!~this.class.indexOf(`${self.settings("classPrefix")}-edge`);
      };
 
      this.isHover = function isHover() {
        return !!~this.class.indexOf(`${self.settings("classPrefix")}-hover`);
      };
    }
 
    // Click
    function click(e) {
      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) {
      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) {
      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) {
      const target = e.fromElement || e.originalTarget;
 
      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);
  });
}