Skip to content
Snippets Groups Projects
graph.js 5.55 KiB
const NODE_LABEL = "name";
const NODE_ID = "id";
const NODE_GROUP = "group";
const NODE_DESCRIPTION = "description";
const NODE_IMAGE = "image";

const LINK_SOURCE = "source";
const LINK_TARGET = "target";
const LINK_TYPE = "type";
const LINK_PARTICLE_COUNT = 4;

const GRAPH_NODES = "nodes";
const GRAPH_LINKS = "links";

const IMAGE_SIZE = 12;
const IMAGE_SRC = PLUGIN_PATH + "datasets/images/";

const LINK_PARAMS = [LINK_TYPE];
const NODE_PARAMS = [NODE_ID, NODE_LABEL, NODE_IMAGE, NODE_DESCRIPTION];

const JSON_CONFIG = PLUGIN_PATH + "datasets/aud1.json";

const STOP_PHYSICS_DELAY = 5000; // ms

const graph = {
    data: undefined,
    externUpdate: [],

    update() {
        graph.externUpdate.forEach((fn) => fn());
    },

    deleteNode(nodeId) {
        // Delete node from nodes
        graph.data[GRAPH_NODES] = graph.data[GRAPH_NODES].filter(
            (n) => n[NODE_ID] !== nodeId
        );

        // Delete links with node
        graph.data[GRAPH_LINKS] = graph.data[GRAPH_LINKS].filter(
            (l) =>
                l[LINK_SOURCE][NODE_ID] !== nodeId &&
                l[LINK_TARGET][NODE_ID] !== nodeId
        );
    },

    stopPhysics() {
        graph.data[GRAPH_NODES].forEach((n) => {
            n.fx = n.x;
            n.fy = n.y;
        });
    },

    addIdentifiers() {
        graph.data[GRAPH_NODES].forEach((n) => {
            n.node = true;
            n.link = false;
        });
        graph.data[GRAPH_LINKS].forEach((l) => {
            l.node = false;
            l.link = true;
        });
    },

    deleteLink(sourceId, targetId) {
        // Only keep links, of one of the nodes is different
        graph.data[GRAPH_LINKS] = graph.data[GRAPH_LINKS].filter(
            (l) =>
                l[LINK_SOURCE][NODE_ID] !== sourceId ||
                l[LINK_TARGET][NODE_ID] !== targetId