App-Netdisco

 view release on metacpan or  search on metacpan

share/public/javascripts/d3-force-network-chart.js  view on Meta::CPAN

 * @returns {Object} The public graph API function to allow method chaining
 */
function netGobrechtsD3Force(domContainerId, options, apexPluginId, apexPageItemsToSubmit) { // jshint ignore:line
    /* exported netGobrechtsD3Force */
    /* globals apex, $v, navigator, d3, document, console, window, clearInterval, ActiveXObject, DOMParser, setTimeout */
    /* jshint -W101 */

    "use strict";

    // setup graph variable
    var v = {
        "conf": {},
        "confDefaults": {},
        "data": {},
        "dom": {},
        "events": {},
        "lib": {},
        "main": {},
        "status": {},
        "tools": {},
        "version": "x.x.x"
    };

    /**
     * A module representing the public graph API.
     * @exports API
     */
    var graph = {};

    /**
     * A helper function to initialize the graph
     */
    v.main.init = function() {

        // save parameter for later use
        v.dom.containerId = domContainerId || "D3Force" + Math.floor(Math.random() * 1000000);
        v.confUser = options || {};
        v.status.apexPluginId = apexPluginId;
        v.status.apexPageItemsToSubmit = (!apexPageItemsToSubmit || apexPageItemsToSubmit === "" ? false :
            apexPageItemsToSubmit.replace(/\s/g, "").split(","));

        // initialize the graph function
        v.main.setupConfiguration();
        v.main.setupDom();
        v.main.setupFunctionReferences();
    };


    /*******************************************************************************************************************
     * MAIN: SETUP CONFIGURATION
     */
    v.main.setupConfiguration = function() {
        /* jshint -W074, -W071 */
        // configure debug mode for APEX, can be overwritten by users configuration object
        // or later on with the API debug method
        v.conf.debug = (v.status.apexPluginId && apex.jQuery("#pdebug").length === 1);
        v.status.debugPrefix = "D3 Force in DOM container #" + v.dom.containerId + ": ";

        // status variables
        v.status.customize = false;
        v.status.customizeCurrentMenu = "nodes";
        v.status.customizeCurrentTabPosition = null;
        v.status.forceTickCounter = 0;
        v.status.forceStartTime = 0;
        v.status.forceRunning = false;
        v.status.graphStarted = false;
        v.status.graphRendering = false;
        v.status.graphReady = false;
        v.status.graphOldPositions = null;
        v.status.sampleData = false;
        v.status.wrapLabelsOnNextTick = false;
        v.status.labelFontSize = null;

        // default configuration
        v.confDefaults.minNodeRadius = {
            "display": true,
            "relation": "node",
            "type": "number",
            "val": 6,
            "options": [12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
        };
        v.confDefaults.maxNodeRadius = {
            "display": true,
            "relation": "node",
            "type": "number",
            "val": 18,
            "options": [36, 34, 32, 30, 28, 26, 24, 22, 20, 18, 16, 14, 12]
        };
        v.confDefaults.colorScheme = {
            "display": true,
            "relation": "node",
            "type": "text",
            "val": "color20",
            "options": ["color20", "color20b", "color20c", "color10", "direct"]
        };
        v.confDefaults.dragMode = {
            "display": true,
            "relation": "node",
            "type": "bool",
            "val": true,
            "options": [true, false]
        };
        v.confDefaults.pinMode = {
            "display": true,
            "relation": "node",
            "type": "bool",
            "val": false,
            "options": [true, false]
        };
        v.confDefaults.nodeEventToStopPinMode = {
            "display": true,
            "relation": "node",
            "type": "text",
            "val": "contextmenu",
            "options": ["none", "dblclick", "contextmenu"]
        };
        v.confDefaults.onNodeContextmenuPreventDefault = {
            "display": true,
            "relation": "node",
            "type": "bool",
            "val": false,

share/public/javascripts/d3-force-network-chart.js  view on Meta::CPAN

                                return "selected";
                            } else {
                                return null;
                            }
                        } else if (v.confDefaults[key].type === "number") {
                            if (parseFloat(currentOption) === v.conf[key]) {
                                valueInOptions = true;
                                return "selected";
                            } else {
                                return null;
                            }
                        }
                    })
                    .text(option);
            });
        };
        // render customization wizard only if we have the right status, otherwise remove the wizard
        if (!v.status.customize) {
            v.tools.removeCustomizeWizard();
            v.tools.createCustomizeLink();
        } else {
            v.tools.removeCustomizeLink();
            // set initial position
            if (!v.dom.customizePosition) {
                v.dom.customizePosition = v.tools.getOffsetRect(v.dom.svg.node());
                v.dom.customizePosition.left = v.dom.customizePosition.left + v.conf.width + 8;
            }
            if (document.querySelector("#" + v.dom.containerId + "_customizing") !== null) {
                v.dom.customize.remove();
            }
            v.dom.customize = v.dom.body.insert("div")
                .attr("id", v.dom.containerId + "_customizing")
                .attr("class", "net_gobrechts_d3_force_customize")
                .style("left", v.dom.customizePosition.left + "px")
                .style("top", v.dom.customizePosition.top + "px");
            v.dom.customize.append("span")
                .attr("class", "drag")
                .call(v.tools.customizeDrag)
                .append("span")
                .attr("class", "title")
                .text("Customize \"" + v.dom.containerId + "\"");
            v.dom.customize.append("a")
                .attr("class", "close focus")
                .attr("tabindex", 1)
                .text("Close")
                .on("click", function() {
                    v.status.customize = false;
                    v.tools.removeCustomizeWizard();
                    v.tools.createCustomizeLink();
                })
                .on("keydown", function() {
                    if (d3.event.keyCode === 13) {
                        v.status.customize = false;
                        v.tools.removeCustomizeWizard();
                        v.tools.createCustomizeLink();
                    }
                });
            grid = v.dom.customize.append("table");
            gridRow = grid.append("tr");
            gridCell = gridRow.append("td").style("vertical-align", "top");
            v.dom.customizeMenu = gridCell.append("span");
            v.dom.customizeOptionsTable = gridCell.append("table");
            for (key in v.confDefaults) {
                if (v.confDefaults.hasOwnProperty(key) && v.confDefaults[key].display) {
                    i += 1;
                    row = v.dom.customizeOptionsTable.append("tr")
                        .attr("class", v.confDefaults[key].relation + "-related");
                    row.append("td")
                        .attr("class", "label")
                        .html("<a href=\"https://ogobrecht.github.io/d3-force-apex-plugin/module-API.html#." +
                            key + "\" target=\"github_d3_force\" tabindex=\"" + i + 100 + "\">" +
                            key + "</a>");
                    td = row.append("td");
                    form = td.append("select")
                        .attr("id", v.dom.containerId + "_" + key)
                        .attr("name", key)
                        .attr("value", v.conf[key])
                        .attr("tabindex", i + 1)
                        .classed("warning", v.confDefaults[key].internal)
                        .on("change", onSelectChange);
                    valueInOptions = false;
                    appendOptionsToSelect(key);
                    // append current value if not existing in default options
                    if (!valueInOptions) {
                        form.append("option")
                            .attr("value", v.conf[key])
                            .attr("selected", "selected")
                            .text(v.conf[key]);
                        v.confDefaults[key].options.push(v.conf[key]);
                    }
                    // add short link to release all fixed (pinned) nodes
                    if (key === "pinMode") {
                        td.append("a")
                            .text(" release all")
                            .attr("href", null)
                            .on("click", releaseFixedNodesAndResume);
                    }
                }
            }
            v.dom.customizeOptionsTable.style("width", d3.select(v.dom.customizeOptionsTable).node()[0][0].clientWidth +
                "px");
            gridCell.append("span").html("<br>");
            gridCell = gridRow.append("td")
                .style("vertical-align", "top")
                .style("padding-left", "5px");
            gridCell.append("span")
                .html("Your Configuration Object<p style=\"font-size:10px;margin:0;\">" +
                    (v.status.apexPluginId ?
                        "To save your options please copy<br>this to your plugin region attributes.<br>" +
                        "Only non-default options are shown.</p>" :
                        "Use this to initialize your graph.<br>Only non-default options are shown.</p>")
                );
            v.dom.customizeConfObject = gridCell.append("textarea")
                .attr("tabindex", i + 5)
                .attr("readonly", "readonly");
            gridCell.append("span").html("<br><br>Current Positions<br>");
            v.dom.customizePositions = gridCell.append("textarea")
                .attr("tabindex", i + 6)
                .attr("readonly", "readonly")
                .text((v.status.forceRunning ? "Force started - wait for end event to show positions..." :
                    JSON.stringify(graph.positions())));
            gridCell.append("span").html("<br><br>Debug Log (descending)<br>");
            v.dom.customizeLog = gridCell.append("textarea")
                .attr("tabindex", i + 7)
                .attr("readonly", "readonly");
            gridRow = grid.append("tr");
            gridCell = gridRow.append("td")
                .attr("colspan", 2)
                .html("Copyrights:");
            gridRow = grid.append("tr");
            gridCell = gridRow.append("td")
                .attr("colspan", 2)
                .html("<table><tr><td style=\"padding-right:20px;\">" +
                    "<a href=\"https://github.com/ogobrecht/d3-force-apex-plugin\" target=\"_blank\" " +
                    "tabindex=\"" + (i + 8) + "\">D3 Force APEX Plugin</a> (" + v.version +
                    ")<br>Ottmar Gobrecht</td><td style=\"padding-right:20px;\">" +
                    "<a href=\"https://github.com/mbostock/d3\" target=\"d3js_org\" tabindex=\"" + (i + 9) +
                    "\">D3.js</a> (" + d3.version + ") and " +
                    "<a href=\"https://github.com/d3/d3-plugins/tree/master/lasso\" target=\"_blank\" tabindex=\"" +
                    (i + 10) + "\">D3 Lasso Plugin</a> (modified)<br>Mike Bostock" +
                    "</td></tr><tr><td colspan=\"3\">" +
                    "<a href=\"https://github.com/tinker10/D3-Labeler\" target=\"github_d3_labeler\" " +
                    "tabindex=\"" + (i + 11) +
                    "\">D3 Labeler Plugin</a> (automatic label placement using simulated annealing)" +
                    "<br>Evan Wang</td></tr></table>"); // https://github.com/tinker10/D3-Labeler
            v.tools.createCustomizeMenu(v.status.customizeCurrentMenu);
            v.tools.writeConfObjectIntoWizard();
            if (v.status.customizeCurrentTabPosition) {
                document.getElementById(v.status.customizeCurrentTabPosition).focus();
            }
        }
    };

    v.tools.removeCustomizeWizard = function() {
        d3.select("#" + v.dom.containerId + "_customizing").remove();
    };

    v.tools.createCustomizeMenu = function(relation) {
        v.status.customizeCurrentMenu = relation;
        v.dom.customizeMenu.selectAll("*").remove();
        v.dom.customizeMenu.append("span").text("Show options for:");
        if (v.status.customizeCurrentMenu === "nodes") {
            v.dom.customizeMenu.append("span").style("font-weight", "bold").style("margin-left", "10px").text("NODES");
            v.dom.customizeOptionsTable.selectAll("tr.node-related").classed("hidden", false);
            v.dom.customizeOptionsTable.selectAll("tr.label-related,tr.link-related,tr.graph-related")
                .classed("hidden", true);
        } else {
            v.dom.customizeMenu.append("a")
                .style("font-weight", "bold")
                .style("margin-left", "10px")
                .text("NODES")
                .attr("tabindex", 2)
                .on("click", function() {
                    v.tools.createCustomizeMenu("nodes");
                    v.dom.customizeOptionsTable.selectAll("tr.node-related").classed("hidden", false);
                    v.dom.customizeOptionsTable.selectAll("tr.label-related,tr.link-related,tr.graph-related")
                        .classed("hidden", true);
                })
                .on("keydown", function() {
                    if (d3.event.keyCode === 13) {
                        v.tools.createCustomizeMenu("nodes");
                        v.dom.customizeOptionsTable.selectAll("tr.node-related").classed("hidden", false);
                        v.dom.customizeOptionsTable.selectAll("tr.label-related,tr.link-related,tr.graph-related")
                            .classed("hidden", true);
                    }
                });
        }
        if (v.status.customizeCurrentMenu === "labels") {
            v.dom.customizeMenu.append("span").style("font-weight", "bold").style("margin-left", "10px").text("LABELS");
            v.dom.customizeOptionsTable.selectAll("tr.label-related").classed("hidden", false);
            v.dom.customizeOptionsTable.selectAll("tr.node-related,tr.link-related,tr.graph-related")
                .classed("hidden", true);
        } else {
            v.dom.customizeMenu.append("a")
                .style("font-weight", "bold")
                .style("margin-left", "10px")
                .text("LABELS")
                .attr("tabindex", 2)
                .on("click", function() {
                    v.tools.createCustomizeMenu("labels");
                    v.dom.customizeOptionsTable.selectAll("tr.label-related").classed("hidden", false);
                    v.dom.customizeOptionsTable.selectAll("tr.node-related,tr.link-related,tr.graph-related")
                        .classed("hidden", true);
                })
                .on("keydown", function() {
                    if (d3.event.keyCode === 13) {
                        v.tools.createCustomizeMenu("labels");
                        v.dom.customizeOptionsTable.selectAll("tr.label-related").classed("hidden", false);
                        v.dom.customizeOptionsTable.selectAll("tr.node-related,tr.link-related,tr.graph-related")
                            .classed("hidden", true);
                    }
                });
        }
        if (v.status.customizeCurrentMenu === "links") {
            v.dom.customizeMenu.append("span").style("font-weight", "bold").style("margin-left", "10px").text("LINKS");
            v.dom.customizeOptionsTable.selectAll("tr.link-related").classed("hidden", false);
            v.dom.customizeOptionsTable.selectAll("tr.node-related,tr.label-related,tr.graph-related")
                .classed("hidden", true);
        } else {
            v.dom.customizeMenu.append("a")
                .style("font-weight", "bold")
                .style("margin-left", "10px")
                .text("LINKS")
                .attr("tabindex", 3)
                .on("click", function() {
                    v.tools.createCustomizeMenu("links");
                    v.dom.customizeOptionsTable.selectAll("tr.link-related").classed("hidden", false);
                    v.dom.customizeOptionsTable.selectAll("tr.node-related,tr.label-related,tr.graph-related")
                        .classed("hidden", true);
                })
                .on("keydown", function() {
                    if (d3.event.keyCode === 13) {
                        v.tools.createCustomizeMenu("links");
                        v.dom.customizeOptionsTable.selectAll("tr.link-related").classed("hidden", false);
                        v.dom.customizeOptionsTable.selectAll("tr.node-related,tr.label-related,tr.graph-related")
                            .classed("hidden", true);
                    }
                });
        }
        if (v.status.customizeCurrentMenu === "graph") {
            v.dom.customizeMenu.append("span").style("font-weight", "bold").style("margin-left", "10px").text("GRAPH");
            v.dom.customizeOptionsTable.selectAll("tr.graph-related").classed("hidden", false);
            v.dom.customizeOptionsTable.selectAll("tr.node-related,tr.label-related,tr.link-related")
                .classed("hidden", true);
        } else {
            v.dom.customizeMenu.append("a")
                .style("font-weight", "bold")
                .style("margin-left", "10px")
                .text("GRAPH")
                .attr("tabindex", 4)
                .on("click", function() {
                    v.tools.createCustomizeMenu("graph");
                    v.dom.customizeOptionsTable.selectAll("tr.graph-related").classed("hidden", false);
                    v.dom.customizeOptionsTable.selectAll("tr.node-related,tr.label-related,tr.link-related")
                        .classed("hidden", true);
                })
                .on("keydown", function() {
                    if (d3.event.keyCode === 13) {
                        v.tools.createCustomizeMenu("graph");
                        v.dom.customizeOptionsTable.selectAll("tr.graph-related").classed("hidden", false);
                        v.dom.customizeOptionsTable.selectAll("tr.node-related,tr.label-related,tr.link-related")
                            .classed("hidden", true);
                    }
                });
        }
        v.dom.customizeMenu.append("span").html("<br><br>");
    };

    // helper function to wrap text - https://bl.ocks.org/mbostock/7555321
    v.tools.wrapLabels = function(labels, width) {
        labels.each(function(label, i) {
            var text = d3.select(this);
            if (i === 0) {
                v.status.labelFontSize = parseInt(text.style("font-size"));
            }
            if (!this.hasAttribute("lines")) {
                var words = text.text().split(/\s+/).reverse(),
                    word,
                    line = [],
                    lineNumber = 0,
                    lineHeight = v.status.labelFontSize * v.conf.wrappedLabelLineHeight,
                    x = text.attr("x"),
                    y = text.attr("y"),
                    dy = 0,
                    tspan = text.text(null).append("tspan").attr("x", x).attr("y", y).attr("dy", dy + "px");

                while (word = words.pop()) { // jshint ignore:line
                    line.push(word);
                    tspan.text(line.join(" "));
                    if (tspan.node().getComputedTextLength() > width) {
                        line.pop();
                        tspan.text(line.join(" "));
                        line = [word];
                        tspan = text.append("tspan").attr("x", x).attr("y", y).attr("dy", ++lineNumber * lineHeight +
                            dy + "px").text(word);
                    }
                }
                //save number of lines
                text.attr("lines", lineNumber + 1);
            }
        });
    };

    /*******************************************************************************************************************
     * LIBRARIES
     */

    // D3 labeler plugin
    /* Source Code: https://github.com/tinker10/D3-Labeler
    The MIT License (MIT)

    Copyright (c) 2013 Evan Wang

    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in all
    copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE



( run in 1.970 second using v1.01-cache-2.11-cpan-140bd7fdf52 )