|
| 1 | +import Graph from "graphology"; |
| 2 | +import Sigma from "sigma"; |
| 3 | +import { MouseCoords } from "sigma/types"; |
| 4 | + |
| 5 | +import data from "../_data/data.json"; |
| 6 | + |
| 7 | +/** |
| 8 | + * This is a minimal example of sigma. You can use it as a base to write new |
| 9 | + * examples, or reproducible test cases for new issues, for instance. |
| 10 | + */ |
| 11 | +export default () => { |
| 12 | + const logsDOM = document.getElementById("sigma-logs") as HTMLElement; |
| 13 | + const graph = new Graph(); |
| 14 | + graph.import(data); |
| 15 | + |
| 16 | + function logEvent(event: string, itemType: "node" | "edge" | "positions", item: string | MouseCoords): void { |
| 17 | + const div = document.createElement("div"); |
| 18 | + let message = `Event "${event}"`; |
| 19 | + if (item && itemType) { |
| 20 | + if (itemType === "positions") { |
| 21 | + item = item as MouseCoords; |
| 22 | + message += `, x ${item.x}, y ${item.y}`; |
| 23 | + } else { |
| 24 | + const label = |
| 25 | + itemType === "node" ? graph.getNodeAttribute(item, "label") : graph.getEdgeAttribute(item, "label"); |
| 26 | + message += `, ${itemType} ${label || "with no label"} (id "${item}")`; |
| 27 | + |
| 28 | + if (itemType === "edge") { |
| 29 | + message += `, source ${graph.getSourceAttribute(item, "label")}, target: ${graph.getTargetAttribute( |
| 30 | + item, |
| 31 | + "label", |
| 32 | + )}`; |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | + div.innerHTML = `<span>${message}</span>`; |
| 37 | + logsDOM.appendChild(div); |
| 38 | + logsDOM.scrollTo({ top: logsDOM.scrollHeight }); |
| 39 | + |
| 40 | + if (logsDOM.children.length > 50) logsDOM.children[0].remove(); |
| 41 | + } |
| 42 | + |
| 43 | + window.customElements.define( |
| 44 | + "sigma-shadow", |
| 45 | + class extends HTMLElement { |
| 46 | + _container: HTMLDivElement | null = null; |
| 47 | + |
| 48 | + constructor() { |
| 49 | + super(); |
| 50 | + } |
| 51 | + |
| 52 | + connectedCallback() { |
| 53 | + const shadowRoot = this.attachShadow({ mode: "open" }); |
| 54 | + const container = document.createElement("div"); |
| 55 | + container.style.cssText = ` |
| 56 | + width: 100%; |
| 57 | + height: 100%; |
| 58 | + margin: 0; |
| 59 | + padding: 0; |
| 60 | + overflow: hidden; |
| 61 | + `; |
| 62 | + this._container = container; |
| 63 | + shadowRoot.appendChild(container); |
| 64 | + |
| 65 | + let hoveredEdge: null | string = null; |
| 66 | + const renderer = new Sigma(graph, this._container, { |
| 67 | + enableEdgeEvents: true, |
| 68 | + edgeReducer(edge, data) { |
| 69 | + const res = { ...data }; |
| 70 | + if (edge === hoveredEdge) res.color = "#cc0000"; |
| 71 | + return res; |
| 72 | + }, |
| 73 | + }); |
| 74 | + |
| 75 | + const nodeEvents = [ |
| 76 | + "enterNode", |
| 77 | + "leaveNode", |
| 78 | + "downNode", |
| 79 | + "clickNode", |
| 80 | + "rightClickNode", |
| 81 | + "doubleClickNode", |
| 82 | + "wheelNode", |
| 83 | + ] as const; |
| 84 | + const edgeEvents = ["downEdge", "clickEdge", "rightClickEdge", "doubleClickEdge", "wheelEdge"] as const; |
| 85 | + const stageEvents = ["downStage", "clickStage", "doubleClickStage", "wheelStage"] as const; |
| 86 | + |
| 87 | + nodeEvents.forEach((eventType) => renderer.on(eventType, ({ node }) => logEvent(eventType, "node", node))); |
| 88 | + edgeEvents.forEach((eventType) => renderer.on(eventType, ({ edge }) => logEvent(eventType, "edge", edge))); |
| 89 | + |
| 90 | + renderer.on("enterEdge", ({ edge }) => { |
| 91 | + logEvent("enterEdge", "edge", edge); |
| 92 | + hoveredEdge = edge; |
| 93 | + renderer.refresh(); |
| 94 | + }); |
| 95 | + renderer.on("leaveEdge", ({ edge }) => { |
| 96 | + logEvent("leaveEdge", "edge", edge); |
| 97 | + hoveredEdge = null; |
| 98 | + renderer.refresh(); |
| 99 | + }); |
| 100 | + |
| 101 | + stageEvents.forEach((eventType) => { |
| 102 | + renderer.on(eventType, ({ event }) => { |
| 103 | + logEvent(eventType, "positions", event); |
| 104 | + }); |
| 105 | + }); |
| 106 | + } |
| 107 | + }, |
| 108 | + ); |
| 109 | +}; |
0 commit comments