删除所有连接以及 jsPlumb 中的元素

Deleting all Connections along with the Element in jsPlumb

当我尝试在 jsPlumb 中删除删除的元素时,连接没有被删除。相反,它们只是存在于相同的位置。当双击删除该连接的源或目标时,我想删除所有连接。代码如下

<!doctype html>
<html>
<head>

    <script src="../lib/jquery.min.js"></script>
    <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
    <script src="../lib/jquery-ui.min.js"></script>
    <script src="../lib/jquery.jsPlumb-1.4.1-all-min.js"></script>
    <script src="../lib/jquery.jsPlumb-1.6.4-min.js"></script>

    <style>

        @import url(http://fonts.googleapis.com/css?family=Montserrat);

        * {
            font-family: 'Montserrat', sans-serif;
        }
        #container {
            position: relative;
            resize: both;
            margin-left: 180px;
            border: 1px solid #aaaaaa;
            width: 800px;
            height: 650px;
            overflow-x: scroll;
            overflow-y: scroll;
        }
        .title {
            padding: 10px;
            cursor: move;
        }
        .connect {
            font-size:10px;
            text-align: center;
            width: 100%;
            height: 20px;
            background-color: #ffffff;
            cursor: pointer;
        }
        .project {
            border: 1px solid gray;
            text-align: center;
            width: 170px;
            height: 75px;
            background-color: lightpink;
            position: absolute;
        }
        .pro {
            border: 1px solid gray;
            text-align: center;
            width: 170px;
            height: 75px;
            background-color:  lightpink;
            position: absolute;
        }
        .task {
            font-size: 12px;
            text-align: center;
            font-weight: 200;
            border: 1px solid black;
            background-color: #ddddff;
        }

    </style>

</head>

<body>

<div class="project" id="cId">
</div>
<div id="container">
</div>

<script>

    jsPlumb.ready(function() {

        jsPlumb.Defaults.Container=$("#container");
        jsPlumb.Defaults.PaintStyle = { strokeStyle:"palevioletred", lineWidth:2, dashstyle: '3 3', };
        jsPlumb.Defaults.EndpointStyle = { radius:7, fillStyle:"palevioletred" };
        jsPlumb.importDefaults({Connector : [ "Bezier", { curviness:50 } ]});
        jsPlumb.setContainer($('#container'));
        var i = 1;
        $(".project").draggable
        ({
            helper : 'clone',
            cursor : 'pointer',
            tolerance : 'fit',
            revert : true

        });

        var saveState = function(state) {
            $.post('http://www.example.com/saveState', {
                id: $(state).attr('id'),
                top: $(state).position().top,
                left: $(state).position().left
            });
        }

        $("#container").droppable
        ({
            accept: '.project',
            containment: 'container',

            drop: function (e, ui) {
                droppedElement = ui.helper.clone();
                ui.helper.remove();
                $(droppedElement).removeAttr("class");
                jsPlumb.repaint(ui.helper);

                var newAgent = $('<div>').attr('id', 'pro' + i).addClass('pro');
                newAgent.text('Element ' + i);
                $(droppedElement).draggable({containment: "container"});
                $('#container').append(newAgent);

                jsPlumb.draggable(newAgent, {
                    containment: 'parent'
                });
                newAgent.dblclick(function(e) {

                    jsPlumb.detachAllConnections($(this));
                    jsPlumb.removeAllEndpoints($(this));
                    jsPlumb.detach($(this)); 
                    $(this).remove()
                });
                i++;
            }
        });

        $("#container").on('click', '.pro', function (e) {
            i++;
            var newState = $('<div>').attr('id', 'state' + i).addClass('task').text('Section ' + (i-1));
            var title = $('<div>').addClass('title');
            var stateName = $('<input>').attr('type', 'text');
            title.append(stateName);
            var connect = $('<div>').addClass('connect').text('Click here to drag');

            newState.css({
                'top': e.pageY,
                'left': e.pageX
            });

            newState.append(title);
            newState.append(connect);

            $(this).append(newState);

            jsPlumb.makeTarget(newState, {
                anchor: 'Continuous'
            });

            jsPlumb.makeSource(connect, {
                anchor: 'Continuous'

            });

            newState.dblclick(function(e) {
                jsPlumb.detachAllConnections($(this));
                $(this).remove();
                e.stopPropagation();
            });

            stateName.keyup(function(e) {
                if (e.keyCode === 13) {
                    $(this).parent().text(this.value);
                }
            });
            stateName.focus();
        });


    });


</script>

</body>

</html>

得到这个工作,我想分享它以供将来参考。我已将我的 solution 上传到 github,其中 work.html 文件与此问题相关。