Jsplum динамически рисует диаграмму конечного автомата

Я использую jsplumb для рисования диаграммы динамического конечного автомата. По нажатию кнопки мне нужно добавить новое поле в область рисования и позволить пользователю расположить его в соответствии со своими потребностями.

Я не получаю никакой надлежащей простой для понимания документации для этого. Я пробовал несколько вещей:

var i=8; 
function AddDiv() {
    var obj = new Date();
    var Div = $('<div/>', {
        'class':'box ui-draggable ui-draggable-handle ui-droppable',
        'id':'box_'+i,
        'html':'BOXESNEW'
    }).appendTo('.statemachine_cont');
    jsPlumb.addEndpoint($(Div), targetEndpoint);
    $(Div).draggable(
    {
        drag: function(){
            jsPlumb.repaint($(this)); // (or) jsPlumb.repaintEverything(); to repaint the connections and endpoints
        //     jsPlumb.addEndpoint($(this));     
        }
    });
    $(Div).addClass('box ui-draggable ui-draggable-handle ui-droppable');
}
var a = $("#a");

//Setting up drop options
var targetDropOptions = {
    activeClass: 'dragActive'
};
//Setting up a Target endPoint
var targetColor = "#BEBEBE";
var targetEndpoint = {
    anchor: "BottomCenter", //Placement of Dot
    endpoint: ["Dot", { radius: 8}], //Other types are rectangle, Image, Blank, Triangle
    paintStyle: { fillStyle: targetColor }, //Line color
    isSource: true, //Starting point of the connector
    // scope: "green dot",
    connectorStyle: { strokeStyle: "#5C96BC", lineWidth: 2 }, // Means Bridge width and bridge color
    connector: ["Bezier"], //Other properties Bezier
    maxConnections: -1, //No upper limit
    isTarget: true, //Means same color is allowed to accept the connection
    dropOptions: targetDropOptions //Means when the drag is started, other terminals will start to highlight
};
jsPlumb.bind("ready", function () {
    //Set up endpoints on the divs
    jsPlumb.addEndpoint($(".box ui-draggable ui-draggable-handle ui-droppable"), targetEndpoint);
    jsPlumb.addEndpoint($(".box ui-draggable ui-draggable-handle ui-droppable"), sourceEndpoint);

    jsPlumb.draggable($(".box ui-draggable ui-draggable-handle ui-droppable"));
    jsPlumb.animate($("#a"), { "left": 50, "top": 100 }, { duration: "slow" });
});

Не уверен, что я сделал правильно, я сослался на какой-то доступный онлайн-код и изменил его.

Моя проблема заключается в следующем: при нажатии кнопки я могу добавить новое поле, а также перетащить соединение из этого поля. Но когда я пытаюсь перетащить этот ящик (т.е. изменить его положение), соединение не перемещается. Ящик перемещен, но я не могу переместить соединение с ящиком.

Когда я пытаюсь переместить только что добавленный ящик или ящик, подключенный к новому, оба ящика можно переместить, но соединение остается статическим и не перемещается. где, как если бы другие ящики перемещались, он перемещался вместе с соединениями. Я добавил изображение этого для справки.

1-е изображение показывает, как появляется новое добавленное поле и новое соединение. 2-е изображение показывает, как перемещение коробки создает проблему. Изображение 1

Изображение 2


person Arti    schedule 12.02.2015    source источник
comment
Вы можете создать скрипку для игры.   -  person coding_idiot    schedule 13.02.2015
comment
jsplumb все еще существует? сайт кажется не работает   -  person swinefeaster    schedule 14.07.2016
comment
Похоже, URL изменен на jsplumb.   -  person Arti    schedule 14.07.2016


Ответы (2)


Вот как мне удалось заставить его работать. Я изменил весь свой код

<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title> - jsFiddle demo</title>

  <script type='text/javascript' src='js/jquery-1.10.1.js'></script>
  <link rel="stylesheet" type="text/css" href="css/demo-all.css">
  <link rel="stylesheet" type="text/css" href="css/demo.css">
  <script type='text/javascript' src="js/jquery.ui.touch-punch-0.2.2.min.js"></script>
  <script type='text/javascript' src="js/jquery-ui-1.9.2.min.js"></script>
  <script type='text/javascript' src="js/jquery.jsPlumb-1.7.2-min.js"></script>
  <style type='text/css'>
    .hidden { display: none; }
  </style>
<script type='text/javascript'>
$(window).load(function(){
function cloneWindow(instance) {
    var $jspContainer = $("#statemachine-demo"),
        divid = "fromTemplate_" + new Date().getTime().toString()        
        $cloneElement = $("<div class='w'>New Window&nbsp;<div class='ep'></div></div>").attr("id", divid);

    $jspContainer.append($cloneElement);

    instance.draggable(divid);
    instance.makeSource($cloneElement, {
                filter: ".ep", // only supported by jquery
                anchor: "Continuous",
                connector: ["StateMachine", {
                    curviness: 1
                }],
                connectorStyle: {
                    strokeStyle: "#5c96bc",
                    lineWidth: 2,
                    outlineColor: "transparent",
                    outlineWidth: 4
                },
                maxConnections: 10,
                onMaxConnections: function (info, e) {
                    alert("Maximum connections (" + info.maxConnections + ") reached");
                }
            });   

    instance.bind("connection", function (info) {
        info.connection.getOverlay("label").setLabel(info.connection.id);
    });


    instance.makeTarget($cloneElement, { 
        anchor:"Continuous", 
        dropOptions:{ hoverClass:"dragHover" }
        });

}

jsPlumb.ready(function () {

        $("#addwindow").click(function() {
          cloneWindow(instance);
        });
        // setup some defaults for jsPlumb. 
        var instance = jsPlumb.getInstance({
            Endpoint: ["Dot", {
                radius: 2
            }],
            HoverPaintStyle: {
                strokeStyle: "#1e8151",
                lineWidth: 2
            },
            ConnectionOverlays: [
                ["Arrow", {
                    location: 1,
                    id: "arrow",
                    length: 14,
                    foldback: 0.8
                }],
                ["Label", {
                    label: "Drag this and drop it on another element to make a connection.",
                    id: "label",
                    cssClass: "aLabel"
                }]
            ],
            Container: "statemachine-demo"
        });

    jsPlumb.importDefaults({
                filter: ".ep",
                anchor: "Continuous",
                connector: ["StateMachine", {
                    curviness: 1
                }],
                connectorStyle: {
                    strokeStyle: "#5c96bc",
                    lineWidth: 2,
                    outlineColor: "transparent",
                    outlineWidth: 4
                },
                maxConnections: 10,
                dropOptions: {
                    hoverClass: "dragHover"
                }

    });
        var windows = jsPlumb.getSelector(".statemachine-demo .w");

        // initialise draggable elements.  
        instance.draggable(windows);

        // bind a click listener to each connection; the connection is deleted. you could of course
        // just do this: jsPlumb.bind("click", jsPlumb.detach), but I wanted to make it clear what was
        // happening.
        instance.bind("click", function (c) {
            instance.detach(c);
        });

        // bind a connection listener. note that the parameter passed to this function contains more than
        // just the new connection - see the documentation for a full list of what is included in 'info'.
        // this listener sets the connection's internal
        // id as the label overlay's text.
        instance.bind("connection", function (info) {
            info.connection.getOverlay("label").setLabel(info.connection.id);
        });

        // suspend drawing and initialise.
        instance.doWhileSuspended(function () {


            // make each ".ep" div a source and give it some parameters to work with.  here we tell it
            // to use a Continuous anchor and the StateMachine connectors, and also we give it the
            // connector's paint style.  note that in this demo the strokeStyle is dynamically generated,
            // which prevents us from just setting a jsPlumb.Defaults.PaintStyle.  but that is what i
            // would recommend you do. Note also here that we use the 'filter' option to tell jsPlumb
            // which parts of the element should actually respond to a drag start.
            instance.makeSource(windows, {
                filter: ".ep", // only supported by jquery
                anchor: "Continuous",
                connector: ["StateMachine", {
                    curviness: 1
                }],
                connectorStyle: {
                    strokeStyle: "#5c96bc",
                    lineWidth: 2,
                    outlineColor: "transparent",
                    outlineWidth: 4
                },
                maxConnections: 10,
                onMaxConnections: function (info, e) {
                    alert("Maximum connections (" + info.maxConnections + ") reached");
                }
            });


            // initialise all '.w' elements as connection targets.
            instance.makeTarget(windows, {
                dropOptions: {
                    hoverClass: "dragHover"
                },
                anchor: "Continuous"
            });

            // and finally, make a couple of connections
            instance.connect({
                source: "opened",
                target: "phone1"
            });
            instance.connect({
                source: "phone1",
                target: "inperson"
            });
            instance.connect({
                source: "phone1",
                target: "phone1"
            });


        });                          

    });
}); 

</script>


</head>
<body>
  <div class="demo statemachine-demo" id="statemachine-demo" style="border:2px solid;border-radius:25px;">
    <button type="button" id="addwindow">Add Window</button>
    <div class="w" id="opened">BEGIN&nbsp;
        <div class="ep"></div>
    </div>
    <div class="w" id="phone1">PHONE INTERVIEW 1&nbsp;
        <div class="ep"></div>
    </div>
    <div class="w" id="phone2">PHONE INTERVIEW 2&nbsp;
        <div class="ep"></div>
    </div>
    <div class="w" id="inperson">IN PERSON&nbsp;
        <div class="ep"></div>
    </div>
    <div class="w" id="rejected">REJECTED&nbsp;
        <div class="ep"></div>
    </div>
    <div class="w hidden" id="template_newwindow">
        <div class="ep"></div>
    </div>
</div>
</body>
</html>
person Arti    schedule 16.02.2015

Div уже является объектом jquery, его не нужно снова оборачивать.

Опция 1

Div.draggable(
    {
        drag: function(){
           jsPlumb.repaintEverything();
        }
    });

Вариант 2

jsPlumb.draggable(Div.attr('id'));
person coding_idiot    schedule 13.02.2015