我一直在使用arbor.js,需要解决几个问题。
我能够基于数据库(基本图)创建图。现在,我需要做的是,单击节点并获取节点数据,并将其显示在侧面。也要有针对性的优势。所以,问题是


我的mousedown功能不起作用。它要么完全不起作用,要么它完全起作用,当我单击一个节点时,它会自动拖动,即它被附着在鼠标上而无法释放。当我单击节点时,我需要在侧面显示节点详细信息。节点详细信息在另一页上,可以将其检索为json。我的鼠标处理代码如下

initMouseHandling:function(){
// no-nonsense drag and drop (thanks springy.js)
var dragged = null;

// set up a handler object that will initially listen for mousedowns then
// for moves and mouseups while dragging
var handler = {
  clicked:function(e){
    var pos = $(canvas).offset();
    _mouseP = arbor.Point(e.pageX-pos.left, e.pageY-pos.top)
    dragged = particleSystem.nearest(_mouseP);

    var d = document.getElementById("infoDiv");

    if (dragged && dragged.node !== null){
      // while we're dragging, don't let physics move the node
      dragged.node.fixed = true
    }
    ///document.getElementById('detailBox').innerHTML=selected.node.name;

    $(canvas).bind('mousemove', handler.dragged)
    $(window).bind('mouseup', handler.dropped)
    $(canvas).bind('mousedown', handler.clicked)

    return false
  },
  dragged:function(e){
    var pos = $(canvas).offset();
    var s = arbor.Point(e.pageX-pos.left, e.pageY-pos.top)

    if (dragged && dragged.node !== null){
      var p = particleSystem.fromScreen(s)
      dragged.node.p = p
    }

    return false
  },

  dropped:function(e){
    if (dragged===null || dragged.node===undefined) return
    if (dragged.node !== null) dragged.node.fixed = false
    dragged.node.tempMass = 1000
    dragged = null
    $(canvas).unbind('mousemove', handler.dragged)
    $(window).unbind('mouseup', handler.dropped)
    _mouseP = null
    return false
  }

  clicked:function(e){
    var pos = $(this).offset();
    var p = {x:e.pageX-pos.left, y:e.pageY-pos.top}
    selected = nearest = dragged = particleSystem.nearest(p);
    var nodeAddress = "http://localhost:7474/db/data/node/" + selected.node.name;

    if (selected.node !== null){
    // dragged.node.tempMass = 10000
        dragged.node.fixed = false;
        //$(canvas).unbind('mousemove', handler.dragged)
    }
    //document.getElementById('detailBox').innerHTML=selected.node.name;
    getData(nodeAddress);
    //alert(nodeData.self);
    return false;

  }
}

//My click function
//$(canvas).mousedown(function(e){

//});


},

}


上面的代码根本不起作用,我也是jQuery的新手,因此我无法弄清楚错误,最后单击的函数试图执行的操作是在单击节点时从中获取其详细信息另一页并显示它。这里的功能完全失败。
在我这样尝试之前

   //My click function
    $(canvas).mousedown(function(e){
        var pos = $(this).offset();
        var p = {x:e.pageX-pos.left, y:e.pageY-pos.top}
        selected = nearest = dragged = particleSystem.nearest(p);
        var nodeAddress = "http://localhost:7474/db/data/node/" + selected.node.name;

        if (selected.node !== null){
        // dragged.node.tempMass = 10000
            dragged.node.fixed = false;
            //$(canvas).unbind('mousemove', handler.dragged)
        }
        //document.getElementById('detailBox').innerHTML=selected.node.name;
        getData(nodeAddress);
        //alert(nodeData.self);
        return false;
    });



    // start listening
    $(canvas).mousedown(handler.clicked);


这在处理程序变量之外,JavaScript可以正常工作,并且在侧面显示了节点号。但是我无法查询和获取json。该图也卡住了。

这是尝试的方法吗?我如何才能完成它。抱歉,我遇到的第一个问题是格式错误,这是我的第一篇文章。

谢谢。

最佳答案

以下适用于我的处理程序的代码对我有用。 YMMV。

我没有clicked函数,而是有一个down映射到droppeddragged
我还包括一个move变量,该变量在dropped函数中用于指示该节点是实际单击还是被拖动。

希望这会有所帮助!

initMouseHandling:function(){
// no-nonsense drag and drop (thanks springy.js)
selected = null;
nearest = null;
var dragged = null;
var move = false;

// set up a handler object that will initially listen for mousedowns then
// for moves and mouseups while dragging
var handler = {
  moved:function(e){
    var pos = $(canvas).offset();
    _mouseP = arbor.Point(e.pageX-pos.left, e.pageY-pos.top);
    nearest = particleSystem.nearest(_mouseP);

    if(!nearest.node){
        return false;
    }

    selected = (nearest.distance < nearest.node.data.radius) ? nearest : null

    // code for node that mouse is hovered on ('selected')

  },
  down:function(e){
    var pos = $(canvas).offset();
    _mouseP = arbor.Point(e.pageX-pos.left, e.pageY-pos.top)
    nearest = dragged = particleSystem.nearest(_mouseP);
    move = false;

    if (dragged && dragged.node !== null){
        dragged.node.fixed = true
    }

    $(canvas).bind('mousemove', handler.dragged)
    $(window).bind('mouseup', handler.dropped)

    return false
  },
  dragged:function(e){
    var old_nearest = nearest && nearest.node._id
    var pos = $(canvas).offset();
    var s = arbor.Point(e.pageX-pos.left, e.pageY-pos.top)
    move = true;

    if (!nearest) return
    if (dragged !== null && dragged.node !== null){
      var p = particleSystem.fromScreen(s)
      dragged.node.p = p
    }

    return false
  },

  dropped:function(e){
    var edit = $("#edit").prop('checked')
    if (dragged===null || dragged.node===undefined) return
    if (dragged.node !== null) {
        if(move===false) {

            // code for clicked node (dragged.node)

        }
        dragged.node.fixed = false
    }
    dragged.node.tempMass = 1000
    dragged = null
    selected = null
    $(canvas).unbind('mousemove', handler.dragged)
    $(window).unbind('mouseup', handler.dropped)
    _mouseP = null
    return false
  }
}

$(canvas).mousedown(handler.down);
$(canvas).mousemove(handler.moved);
}

关于javascript - Arbor.js查询,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14286257/

10-09 22:51