将“双击”代码与旧的d3“重新绑定”替代项合并在一起时出错

时间:2019-01-29 22:05:09

标签: d3.js double-click dynamic-rebinding

我试图恢复其在“node.on”方法D3的早期版本中呈现双击事件。我需要与d3.forceSimulation使用它。

对于双击事件,我发现在旧版本的作品精细替代snippet d3。它还使用其从当前d3和其除去旧d3.rebind方法还具有substitute。 我试图合并这两个代码段,但失败并显示错误消息“无法读取未定义的属性'apply'。

这里的合并后的代码:

<div id='map'></div>
<script>

function clickcancel() {

// Copies a variable number of methods from source to target.
d3.rebind = function(target, source) {
  var i = 1, n = arguments.length, method;
  while (++i < n) target[method = arguments[i]] = d3_rebind(target, source, source[method]);
  return target;
};

// Method is assumed to be a standard D3 getter-setter:
// If passed with no arguments, gets the value.
// If passed with arguments, sets the value and returns the target.
function d3_rebind(target, source, method) {
  return function() {
    var value = method.apply(source, arguments);
    return value === source ? target : value;
  };
}

  // we want to a distinguish single/double click
  // details http://bl.ocks.org/couchand/6394506
  var event = d3.dispatch('click', 'dblclick');
  function cc(selection) {
      var down, tolerance = 5, last, wait = null, args;
      // euclidean distance
      function dist(a, b) {
          return Math.sqrt(Math.pow(a[0] - b[0], 2), Math.pow(a[1] - b[1], 2));
      }
      selection.on('mousedown', function() {
          down = d3.mouse(document.body);
          last = +new Date();
          args = arguments;
      });
      selection.on('mouseup', function() {
          if (dist(down, d3.mouse(document.body)) > tolerance) {
              return;
          } else {
          if (wait) {
              window.clearTimeout(wait);
              wait = null;
              event.dblclick.apply(this, args);
          } else {
              wait = window.setTimeout((function() {
                  return function() {
                      event.click.apply(this, args);
                      wait = null;
                  };
              })(), 300);
          }
      }
  });
};
  return d3.rebind(cc, event, 'on');
}

var cc = clickcancel();
d3.select('#map').call(cc);
cc.on('click', function(d, index) {
d3.select('#map').text(d3.select('#map').text() + 'click, ');
});
cc.on('dblclick', function(d, index) {
d3.select('#map').text(d3.select('#map').text() + 'dblclick, ');
});

1 个答案:

答案 0 :(得分:0)

获得双击功能的简单方法:

//Set variables
<script type="text/javascript">
var clickDate = new Date();
var difference_ms;

//Set dragstarted functionality fired on '.call(d3.drag().on("start", dragstarted)'
function dragstarted(d) {
    difference_ms = (new Date()).getTime() - clickDate.getTime();
    clickDate = new Date();
    if(difference_ms > 200) {
        d3.select(this).classed("fixed", d.fixed = true);
        if (!d3.event.active) simulation.alphaTarget(0.3).restart();
        d.fx = d.x;
        d.fy = d.y;
    } else {
        d3.select(this).classed("fixed", d.fixed = false);
        if (!d3.event.active) simulation.alphaTarget(0.3).restart();
        d.fx = d.x;
        d.fy = d.y;
        };
    }

//Set dragended functionality fired on '.call(d3.drag().on("end", dragended)'
function dragended(d) {
    if (!d3.event.active) simulation.alphaTarget(0);
    if(difference_ms < 200) {
        d.fx = null;
        d.fy = null;
    };
}

here在尊敬的nharrisanalyst的评论中描述了我偶然发现的棘手代码可以恢复的方式。尽管我仍然不明白为什么要遵循这样一条复杂的道路:

var cc = clickcancel();
d3.select('#map').call(cc); //#map is the element just for example

cc.on('click', function(d, index) {
    d3.select('#map').text(d3.select('#map').text() + 'click, ');
});

cc.on('dblclick', function(d, index) {
    d3.select('#map').text(d3.select('#map').text() + 'dblclick, ');
});

function clickcancel() {}