Skip to content
Snippets Groups Projects
editor.js 2.38 KiB
Newer Older
  • Learn to ignore specific revisions
  • import { State } from "./state";
    import * as Graph from "./graph";
    
    import { loadGraphJson } from "../../datasets/datasets";
    
    import ForceGraph from "force-graph";
    
    import * as Interactions from "./interactions";
    
    
    
    export var state;
    export var graph;
    
    Maximilian Giller's avatar
    Maximilian Giller committed
    var graphObj;
    
    window.onload = function () {
    
        // Only execute, if corresponding dom is present
        if (document.getElementById("ks-editor") === null) {
            return;
        }
    
        document.onkeydown = (e) => state.onKeyDown(e);
        document.onkeyup = (e) => state.onKeyUp(e);
    
    
        Interactions.initInteractions();
    
    
        loadGraphJson(space_id) // space_id defined globaly through knowledge-space.php
    
    Maximilian Giller's avatar
    Maximilian Giller committed
            .then((graphConfig) => {
                state = new State();
    
                graph = new Graph.Graph(graphConfig);
    
    Maximilian Giller's avatar
    Maximilian Giller committed
                load();
    
                // Deactivate physics after a short delay
                setTimeout(() => {
                    graph.stopPhysics();
    
                    graph.storeCurrentData("Physics stopped");
    
                }, Graph.STOP_PHYSICS_DELAY);
    
    function extractPositions(event) {
        return {
    
            graph: graphObj.screen2GraphCoords(event.layerX, event.layerY),
    
            window: { x: event.clientX, y: event.clientY },
        };
    }
    
    
    Maximilian Giller's avatar
    Maximilian Giller committed
    function load() {
        const graphContainer = document.getElementById("2d-graph");
        const width = graphContainer.offsetWidth;
    
        graphObj = ForceGraph()(graphContainer)
            .height(600)
            .width(width)
            .graphData(graph.data)
    
            .nodeLabel(Graph.NODE_LABEL)
    
            .linkColor((link) => state.linkColor(link))
            .nodeColor((node) => state.nodeColor(node))
    
    Maximilian Giller's avatar
    Maximilian Giller committed
            .onNodeClick((node) => state.onNodeClick(node))
            .autoPauseRedraw(false) // keep redrawing after engine has stopped
            .linkWidth((link) => state.linkWidth(link))
            .linkDirectionalParticles(state.linkDirectionalParticles())
            .linkDirectionalParticleWidth((link) =>
                state.linkDirectionalParticleWidth(link)
            )
    
            .onBackgroundClick((event) =>
                state.onBackgroundClick(event, extractPositions(event))
            )
    
    Maximilian Giller's avatar
    Maximilian Giller committed
            .nodeCanvasObjectMode((node) => state.nodeCanvasObjectMode(node))
    
            .nodeCanvasObject((node, ctx, globalScale) => state.nodeCanvasObject(node, ctx, globalScale))
    
    Maximilian Giller's avatar
    Maximilian Giller committed
            .onLinkClick((link) => state.onLinkClick(link));
    
    Maximilian Giller's avatar
    Maximilian Giller committed
        graph.onChangeCallbacks.push((data) => {
    
            graphObj.cooldownTicks(0);
    
    Maximilian Giller's avatar
    Maximilian Giller committed
            graphObj.graphData(data);
    
    Maximilian Giller's avatar
    Maximilian Giller committed
    }