// Generated by CoffeeScript 1.10.0 (function() { var MultilineTool, get_closest_point, bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; global(MultilineTool = (function() { function MultilineTool(conf) { this.hover_on_point = bind(this.hover_on_point, this); this.touch_link = bind(this.touch_link, this); this.touch_node = bind(this.touch_node, this); this.new_point = bind(this.new_point, this); this.graph = conf.graph; this.view = conf.view; this.last_point = null; this.listeners = []; this.listeners.push(this.view.on('hover_on_point', this.hover_on_point)); this.listeners.push(this.view.on('action_on_point', this.new_point)); this.listeners.push(this.view.on('action_on_node', this.touch_node)); this.listeners.push(this.view.on('action_on_link', this.touch_link)); } MultilineTool.prototype.destroy = function() { this.listeners.forEach((function(_this) { return function(l) { return _this.view.on(l, null); }; })(this)); if (this.preview_line != null) { return this.preview_line.remove(); } }; MultilineTool.prototype.new_point = function(x, y) { var nn; nn = this.graph.new_node(x, y); if (this.last_point != null) { this.graph.new_link(this.last_point, nn); } this.last_point = nn; return nn; }; MultilineTool.prototype.touch_node = function(n, x, y) { if (this.last_point != null) { if (this.last_point !== n) { this.graph.new_link(this.last_point, n); } return this.last_point = null; } else { return this.last_point = n; } }; MultilineTool.prototype.touch_link = function(l, x, y) { var A, B, CP, P, must_close, nn; A = { x: l.source.x, y: l.source.y }; B = { x: l.target.x, y: l.target.y }; P = { x: x, y: y }; CP = get_closest_point(A, B, P); must_close = this.last_point != null; nn = this.new_point(CP.x, CP.y); if (must_close) { this.last_point = null; } this.graph.delete_link(l); this.graph.new_link(l.source, nn); return this.graph.new_link(nn, l.target); }; MultilineTool.prototype.hover_on_point = function(x, y) { if (this.last_point != null) { if (this.preview_line == null) { this.preview_line = this.view.tools_overlay.append('line'); } return this.preview_line.attrs({ "class": 'preview_line', x1: (function(_this) { return function(d) { return _this.last_point.x; }; })(this), y1: (function(_this) { return function(d) { return _this.last_point.y; }; })(this), x2: x, y2: y }); } else { if (this.preview_line != null) { this.preview_line.remove(); return this.preview_line = null; } } }; return MultilineTool; })()); get_closest_point = function(A, B, P) { var C, a_to_b, a_to_p, atb2, atp_dot_atb, t; a_to_p = [P.x - A.x, P.y - A.y]; a_to_b = [B.x - A.x, B.y - A.y]; atb2 = a_to_b[0] * a_to_b[0] + a_to_b[1] * a_to_b[1]; atp_dot_atb = a_to_p[0] * a_to_b[0] + a_to_p[1] * a_to_b[1]; t = atp_dot_atb / atb2; C = { x: A.x + a_to_b[0] * t, y: A.y + a_to_b[1] * t }; return C; }; }).call(this);