// Generated by CoffeeScript 1.10.0 (function() { var Graph, extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }, hasProp = {}.hasOwnProperty; global(Graph = (function(superClass) { extend(Graph, superClass); function Graph(conf) { Graph.__super__.constructor.call(this, { events: ['change'] }); this.nodes = {}; this.links = {}; this.next_node_id = 0; this.next_link_id = 0; this.selection = null; } Graph.prototype.get_nodes = function() { return Object.keys(this.nodes).map((function(_this) { return function(k) { return _this.nodes[k]; }; })(this)); }; Graph.prototype.get_links = function() { return Object.keys(this.links).map((function(_this) { return function(k) { return _this.links[k]; }; })(this)); }; Graph.prototype.new_node = function(x, y) { var n; n = { type: 'node', id: this.next_node_id, x: x, y: y, out_links: {}, in_links: {} }; this.nodes[n.id] = n; this.next_node_id += 1; this.trigger('change'); return n; }; Graph.prototype.new_link = function(source, target) { var l; l = { type: 'link', id: this.next_link_id, source: source, target: target, d: Math.sqrt(Math.pow(target.x - source.x, 2) + Math.pow(target.y - source.y, 2)) }; this.links[l.id] = l; this.next_link_id += 1; l.source.out_links[l.id] = l; l.target.in_links[l.id] = l; this.trigger('change'); return l; }; Graph.prototype.select = function(d) { if (this.selection != null) { delete this.selection.selected; this.selection = null; } d.selected = true; this.selection = d; this.trigger('change'); return this; }; Graph.prototype.delete_node_id = function(id, silent) { var d; d = this.nodes[id]; this.delete_node(d); if (!silent) { return this.trigger('change'); } }; Graph.prototype.delete_node = function(d, silent) { Object.keys(d.out_links).forEach((function(_this) { return function(k) { return _this.delete_link_id(k, true); }; })(this)); Object.keys(d.in_links).forEach((function(_this) { return function(k) { return _this.delete_link_id(k, true); }; })(this)); delete this.nodes[d.id]; if (!silent) { return this.trigger('change'); } }; Graph.prototype.delete_link_id = function(id, silent) { var l; l = this.links[id]; this.delete_link(l); if (!silent) { return this.trigger('change'); } }; Graph.prototype.delete_link = function(l, silent) { delete this.links[l.id]; delete l.source.out_links[l.id]; delete l.target.in_links[l.id]; if (!silent) { return this.trigger('change'); } }; Graph.prototype.delete_selected = function() { if (this.selection == null) { return false; } if (this.selection.type === 'node' && this.selection.id in this.nodes) { this.delete_node_id(this.selection.id, true); } else if (this.selection.type === 'link' && this.selection.id in this.links) { this.delete_link_id(this.selection.id, true); } this.trigger('change'); return true; }; return Graph; })(EventSource)); }).call(this);