mirror of
				https://github.com/node-red/node-red.git
				synced 2025-03-01 10:36:34 +00:00 
			
		
		
		
	Add option to RED.nodes.createCompleteNodeSet to include node dimensions
This allows the linter to use a flow json with more than just position information.
This commit is contained in:
		@@ -694,11 +694,23 @@ RED.nodes = (function() {
 | 
			
		||||
    /**
 | 
			
		||||
     * Converts a node to an exportable JSON Object
 | 
			
		||||
     **/
 | 
			
		||||
    function convertNode(n, exportCreds) {
 | 
			
		||||
    function convertNode(n, opts) {
 | 
			
		||||
        var exportCreds = true;
 | 
			
		||||
        var exportDimensions = false;
 | 
			
		||||
        if (opts === false) {
 | 
			
		||||
            exportCreds = false;
 | 
			
		||||
        } else if (typeof opts === "object") {
 | 
			
		||||
            if (opts.hasOwnProperty("credentials")) {
 | 
			
		||||
                exportCreds = opts.credentials;
 | 
			
		||||
            }
 | 
			
		||||
            if (opts.hasOwnProperty("dimensions")) {
 | 
			
		||||
                exportDimensions = opts.dimensions;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (n.type === 'tab') {
 | 
			
		||||
            return convertWorkspace(n);
 | 
			
		||||
        }
 | 
			
		||||
        exportCreds = exportCreds || false;
 | 
			
		||||
        var node = {};
 | 
			
		||||
        node.id = n.id;
 | 
			
		||||
        node.type = n.type;
 | 
			
		||||
@@ -771,6 +783,19 @@ RED.nodes = (function() {
 | 
			
		||||
        if (n._def.category != "config") {
 | 
			
		||||
            node.x = n.x;
 | 
			
		||||
            node.y = n.y;
 | 
			
		||||
            if (exportDimensions) {
 | 
			
		||||
                if (!n.hasOwnProperty('w')) {
 | 
			
		||||
                    // This node has not yet been drawn in the view. So we need
 | 
			
		||||
                    // to explicitly calculate its dimensions. Store the result
 | 
			
		||||
                    // on the node as if it had been drawn will save us doing
 | 
			
		||||
                    // it again
 | 
			
		||||
                    var dimensions = RED.view.calculateNodeDimensions(n);
 | 
			
		||||
                    n.w = dimensions[0];
 | 
			
		||||
                    n.h = dimensions[1];
 | 
			
		||||
                }
 | 
			
		||||
                node.w = n.w;
 | 
			
		||||
                node.h = n.h;
 | 
			
		||||
            }
 | 
			
		||||
            node.wires = [];
 | 
			
		||||
            for(var i=0;i<n.outputs;i++) {
 | 
			
		||||
                node.wires.push([]);
 | 
			
		||||
@@ -810,7 +835,21 @@ RED.nodes = (function() {
 | 
			
		||||
        return node;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    function convertSubflow(n, exportCreds) {
 | 
			
		||||
    function convertSubflow(n, opts) {
 | 
			
		||||
        var exportCreds = true;
 | 
			
		||||
        var exportDimensions = false;
 | 
			
		||||
        if (opts === false) {
 | 
			
		||||
            exportCreds = false;
 | 
			
		||||
        } else if (typeof opts === "object") {
 | 
			
		||||
            if (opts.hasOwnProperty("credentials")) {
 | 
			
		||||
                exportCreds = opts.credentials;
 | 
			
		||||
            }
 | 
			
		||||
            if (opts.hasOwnProperty("dimensions")) {
 | 
			
		||||
                exportDimensions = opts.dimensions;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        var node = {};
 | 
			
		||||
        node.id = n.id;
 | 
			
		||||
        node.type = n.type;
 | 
			
		||||
@@ -982,11 +1021,10 @@ RED.nodes = (function() {
 | 
			
		||||
        return nns;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    //TODO: rename this (createCompleteNodeSet)
 | 
			
		||||
    function createCompleteNodeSet(exportCredentials) {
 | 
			
		||||
        if (exportCredentials === undefined) {
 | 
			
		||||
            exportCredentials = true;
 | 
			
		||||
        }
 | 
			
		||||
    // Create the Flow JSON for the current configuration
 | 
			
		||||
    // opts.credentials (whether to include (known) credentials) - default: true
 | 
			
		||||
    // opts.dimensions (whether to include node dimensions) - default: false
 | 
			
		||||
    function createCompleteNodeSet(opts) {
 | 
			
		||||
        var nns = [];
 | 
			
		||||
        var i;
 | 
			
		||||
        for (i=0;i<workspacesOrder.length;i++) {
 | 
			
		||||
@@ -996,22 +1034,22 @@ RED.nodes = (function() {
 | 
			
		||||
        }
 | 
			
		||||
        for (i in subflows) {
 | 
			
		||||
            if (subflows.hasOwnProperty(i)) {
 | 
			
		||||
                nns.push(convertSubflow(subflows[i], exportCredentials));
 | 
			
		||||
                nns.push(convertSubflow(subflows[i], opts));
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        for (i in groups) {
 | 
			
		||||
            if (groups.hasOwnProperty(i)) {
 | 
			
		||||
                nns.push(convertNode(groups[i]));
 | 
			
		||||
                nns.push(convertNode(groups[i], opts));
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        for (i in configNodes) {
 | 
			
		||||
            if (configNodes.hasOwnProperty(i)) {
 | 
			
		||||
                nns.push(convertNode(configNodes[i], exportCredentials));
 | 
			
		||||
                nns.push(convertNode(configNodes[i], opts));
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        for (i in nodes) {
 | 
			
		||||
            if (nodes.hasOwnProperty(i)) {
 | 
			
		||||
                nns.push(convertNode(nodes[i], exportCredentials));
 | 
			
		||||
                nns.push(convertNode(nodes[i], opts));
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        return nns;
 | 
			
		||||
 
 | 
			
		||||
@@ -5044,6 +5044,30 @@ RED.view = (function() {
 | 
			
		||||
        }
 | 
			
		||||
        return selection;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    function calculateNodeDimensions(node) {
 | 
			
		||||
        var result = [node_width,node_height];
 | 
			
		||||
        try {
 | 
			
		||||
        var isLink = (node.type === "link in" || node.type === "link out")
 | 
			
		||||
        var hideLabel = node.hasOwnProperty('l')?!node.l : isLink;
 | 
			
		||||
        var label = RED.utils.getNodeLabel(node, node.type);
 | 
			
		||||
        var labelParts = getLabelParts(label, "red-ui-flow-node-label");
 | 
			
		||||
        if (hideLabel) {
 | 
			
		||||
            result[1] = Math.max(node_height,(node.outputs || 0) * 15);
 | 
			
		||||
        } else {
 | 
			
		||||
            result[1] = Math.max(6+24*labelParts.lines.length,(node.outputs || 0) * 15, 30);
 | 
			
		||||
        }
 | 
			
		||||
        if (hideLabel) {
 | 
			
		||||
            result[0] = node_height;
 | 
			
		||||
        } else {
 | 
			
		||||
            result[0] = Math.max(node_width,20*(Math.ceil((labelParts.width+50+(node._def.inputs>0?7:0))/20)) );
 | 
			
		||||
        }
 | 
			
		||||
    }catch(err) {
 | 
			
		||||
        console.log("Error",node);
 | 
			
		||||
    }
 | 
			
		||||
        return result;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return {
 | 
			
		||||
        init: init,
 | 
			
		||||
        state:function(state) {
 | 
			
		||||
@@ -5275,6 +5299,7 @@ RED.view = (function() {
 | 
			
		||||
            return clipboard
 | 
			
		||||
        },
 | 
			
		||||
        redrawStatus: redrawStatus,
 | 
			
		||||
        showQuickAddDialog:showQuickAddDialog
 | 
			
		||||
        showQuickAddDialog:showQuickAddDialog,
 | 
			
		||||
        calculateNodeDimensions: calculateNodeDimensions
 | 
			
		||||
    };
 | 
			
		||||
})();
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user