vis.js is a dynamic, browser-based visualization library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1494 lines
42 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. var Emitter = require('emitter-component');
  2. var Hammer = require('../module/hammer');
  3. var keycharm = require('keycharm');
  4. var util = require('../util');
  5. var hammerUtil = require('../hammerUtil');
  6. var DataSet = require('../DataSet');
  7. var DataView = require('../DataView');
  8. var dotparser = require('./dotparser');
  9. var gephiParser = require('./gephiParser');
  10. var Groups = require('./Groups');
  11. var Images = require('./Images');
  12. var Node = require('./Node');
  13. var Edge = require('./Edge');
  14. var Popup = require('./Popup');
  15. var MixinLoader = require('./mixins/MixinLoader');
  16. var Activator = require('../shared/Activator');
  17. var locales = require('./locales');
  18. // Load custom shapes into CanvasRenderingContext2D
  19. require('./shapes');
  20. import { PhysicsEngine } from './modules/PhysicsEngine'
  21. import { ClusterEngine } from './modules/Clustering'
  22. import { CanvasRenderer } from './modules/CanvasRenderer'
  23. import { Canvas } from './modules/Canvas'
  24. import { View } from './modules/View'
  25. import { InteractionHandler } from './modules/InteractionHandler'
  26. import { SelectionHandler } from "./modules/SelectionHandler"
  27. /**
  28. * @constructor Network
  29. * Create a network visualization, displaying nodes and edges.
  30. *
  31. * @param {Element} container The DOM element in which the Network will
  32. * be created. Normally a div element.
  33. * @param {Object} data An object containing parameters
  34. * {Array} nodes
  35. * {Array} edges
  36. * @param {Object} options Options
  37. */
  38. function Network (container, data, options) {
  39. if (!(this instanceof Network)) {
  40. throw new SyntaxError('Constructor must be called with the new operator');
  41. }
  42. this._initializeMixinLoaders();
  43. // render and calculation settings
  44. this.initializing = true;
  45. this.triggerFunctions = {add:null,edit:null,editEdge:null,connect:null,del:null};
  46. var customScalingFunction = function (min,max,total,value) {
  47. if (max == min) {
  48. return 0.5;
  49. }
  50. else {
  51. var scale = 1 / (max - min);
  52. return Math.max(0,(value - min)*scale);
  53. }
  54. };
  55. // set constant values
  56. this.defaultOptions = {
  57. nodes: {
  58. customScalingFunction: customScalingFunction,
  59. mass: 1,
  60. radiusMin: 10,
  61. radiusMax: 30,
  62. radius: 10,
  63. shape: 'ellipse',
  64. image: undefined,
  65. widthMin: 16, // px
  66. widthMax: 64, // px
  67. fontColor: 'black',
  68. fontSize: 14, // px
  69. fontFace: 'verdana',
  70. fontFill: undefined,
  71. fontStrokeWidth: 0, // px
  72. fontStrokeColor: '#ffffff',
  73. fontDrawThreshold: 3,
  74. scaleFontWithValue: false,
  75. fontSizeMin: 14,
  76. fontSizeMax: 30,
  77. fontSizeMaxVisible: 30,
  78. value: 1,
  79. level: -1,
  80. color: {
  81. border: '#2B7CE9',
  82. background: '#97C2FC',
  83. highlight: {
  84. border: '#2B7CE9',
  85. background: '#D2E5FF'
  86. },
  87. hover: {
  88. border: '#2B7CE9',
  89. background: '#D2E5FF'
  90. }
  91. },
  92. group: undefined,
  93. borderWidth: 1,
  94. borderWidthSelected: undefined
  95. },
  96. edges: {
  97. customScalingFunction: customScalingFunction,
  98. widthMin: 1, //
  99. widthMax: 15,//
  100. width: 1,
  101. widthSelectionMultiplier: 2,
  102. hoverWidth: 1.5,
  103. value:1,
  104. style: 'line',
  105. color: {
  106. color:'#848484',
  107. highlight:'#848484',
  108. hover: '#848484'
  109. },
  110. opacity:1.0,
  111. fontColor: '#343434',
  112. fontSize: 14, // px
  113. fontFace: 'arial',
  114. fontFill: 'white',
  115. fontStrokeWidth: 0, // px
  116. fontStrokeColor: 'white',
  117. labelAlignment:'horizontal',
  118. arrowScaleFactor: 1,
  119. dash: {
  120. length: 10,
  121. gap: 5,
  122. altLength: undefined
  123. },
  124. inheritColor: "from", // to, from, false, true (== from)
  125. useGradients: false // release in 4.0
  126. },
  127. navigation: {
  128. enabled: false
  129. },
  130. dataManipulation: {
  131. enabled: false,
  132. initiallyVisible: false
  133. },
  134. hierarchicalLayout: {
  135. enabled:false,
  136. levelSeparation: 150,
  137. nodeSpacing: 100,
  138. direction: "UD", // UD, DU, LR, RL
  139. layout: "hubsize" // hubsize, directed
  140. },
  141. interaction: {
  142. dragNodes:true,
  143. dragView: true,
  144. zoomView: true,
  145. hoverEnabled: false,
  146. tooltip: {
  147. delay: 300,
  148. fontColor: 'black',
  149. fontSize: 14, // px
  150. fontFace: 'verdana',
  151. color: {
  152. border: '#666',
  153. background: '#FFFFC6'
  154. }
  155. },
  156. keyboard: {
  157. enabled: false,
  158. speed: {x: 10, y: 10, zoom: 0.02},
  159. bindToWindow: true
  160. }
  161. },
  162. selection: {
  163. enabled: true,
  164. selectConnectedEdges: true
  165. },
  166. smoothCurves: {
  167. enabled: true,
  168. dynamic: true,
  169. type: "continuous",
  170. roundness: 0.5
  171. },
  172. locale: 'en',
  173. locales: locales,
  174. useDefaultGroups: true
  175. };
  176. this.constants = util.extend({}, this.defaultOptions);
  177. // containers for nodes and edges
  178. this.body = {
  179. nodes: {},
  180. nodeIndices: [],
  181. supportNodes: {},
  182. supportNodeIndices: [],
  183. edges: {},
  184. data: {
  185. nodes: null, // A DataSet or DataView
  186. edges: null // A DataSet or DataView
  187. },
  188. functions:{
  189. createNode: this._createNode.bind(this),
  190. createEdge: this._createEdge.bind(this)
  191. },
  192. emitter: {
  193. on: this.on.bind(this),
  194. off: this.off.bind(this),
  195. emit: this.emit.bind(this),
  196. once: this.once.bind(this)
  197. },
  198. eventListeners: {
  199. onTap: function() {},
  200. onTouch: function() {},
  201. onDoubleTap: function() {},
  202. onHold: function() {},
  203. onDragStart: function() {},
  204. onDrag: function() {},
  205. onDragEnd: function() {},
  206. onMouseWheel: function() {},
  207. onPinch: function() {},
  208. onMouseMove: function() {},
  209. onRelease: function() {}
  210. },
  211. container: container,
  212. view: {
  213. scale:1,
  214. translation:{x:0,y:0}
  215. }
  216. };
  217. // modules
  218. this.canvas = new Canvas(this.body);
  219. this.selectionHandler = new SelectionHandler(this.body, this.canvas);
  220. this.interactionHandler = new InteractionHandler(this.body, this.canvas, this.selectionHandler);
  221. this.view = new View(this.body, this.canvas);
  222. this.renderer = new CanvasRenderer(this.body, this.canvas);
  223. this.clustering = new ClusterEngine(this.body);
  224. this.physics = new PhysicsEngine(this.body);
  225. // create the DOM elements
  226. this.canvas.create();
  227. this.hoverObj = {nodes:{},edges:{}};
  228. this.controlNodesActive = false;
  229. this.navigationHammers = [];
  230. this.manipulationHammers = [];
  231. // Node variables
  232. var me = this;
  233. this.groups = new Groups(); // object with groups
  234. this.images = new Images(); // object with images
  235. this.images.setOnloadCallback(function (status) {
  236. me._requestRedraw();
  237. });
  238. // keyboard navigation variables
  239. this.xIncrement = 0;
  240. this.yIncrement = 0;
  241. this.zoomIncrement = 0;
  242. // loading all the mixins:
  243. // load the force calculation functions, grouped under the physics system.
  244. //this._loadPhysicsSystem();
  245. // create a frame and canvas
  246. // load the cluster system. (mandatory, even when not using the cluster system, there are function calls to it)
  247. // load the selection system. (mandatory, required by Network)
  248. this._loadSelectionSystem();
  249. // load the selection system. (mandatory, required by Network)
  250. //this._loadHierarchySystem();
  251. // apply options
  252. this.setOptions(options);
  253. // position and scale variables and objects
  254. this.pointerPosition = {"x": 0,"y": 0}; // coordinates of the bottom right of the canvas. they will be set during _redraw
  255. this.scale = 1; // defining the global scale variable in the constructor
  256. // create event listeners used to subscribe on the DataSets of the nodes and edges
  257. this.nodesListeners = {
  258. 'add': function (event, params) {
  259. me._addNodes(params.items);
  260. me.start();
  261. },
  262. 'update': function (event, params) {
  263. me._updateNodes(params.items, params.data);
  264. me.start();
  265. },
  266. 'remove': function (event, params) {
  267. me._removeNodes(params.items);
  268. me.start();
  269. }
  270. };
  271. this.edgesListeners = {
  272. 'add': function (event, params) {
  273. me._addEdges(params.items);
  274. me.start();
  275. },
  276. 'update': function (event, params) {
  277. me._updateEdges(params.items);
  278. me.start();
  279. },
  280. 'remove': function (event, params) {
  281. me._removeEdges(params.items);
  282. me.start();
  283. }
  284. };
  285. // properties for the animation
  286. this.moving = true;
  287. this.renderTimer = undefined; // Scheduling function. Is definded in this.start();
  288. // load data (the disable start variable will be the same as the enabled clustering)
  289. this.setData(data, this.constants.hierarchicalLayout.enabled);
  290. // hierarchical layout
  291. if (this.constants.hierarchicalLayout.enabled == true) {
  292. this._setupHierarchicalLayout();
  293. }
  294. else {
  295. // zoom so all data will fit on the screen, if clustering is enabled, we do not want start to be called here.
  296. if (this.constants.stabilize == false) {
  297. this.zoomExtent({duration:0}, true, this.constants.clustering.enabled);
  298. }
  299. }
  300. if (this.constants.stabilize == false) {
  301. this.initializing = false;
  302. }
  303. var me = this;
  304. // this event will trigger a rebuilding of the cache of colors, nodes etc.
  305. this.on("_dataChanged", function () {
  306. me._updateNodeIndexList();
  307. me.physics._updateCalculationNodes();
  308. me._markAllEdgesAsDirty();
  309. if (me.initializing !== true) {
  310. me.moving = true;
  311. me.start();
  312. }
  313. })
  314. this.on("_newEdgesCreated", this._createBezierNodes.bind(this));
  315. //this.on("stabilizationIterationsDone", function () {me.initializing = false; me.start();}.bind(this));
  316. }
  317. // Extend Network with an Emitter mixin
  318. Emitter(Network.prototype);
  319. Network.prototype._createNode = function(properties) {
  320. return new Node(properties, this.images, this.groups, this.constants)
  321. }
  322. Network.prototype._createEdge = function(properties) {
  323. return new Edge(properties, this.body, this.constants)
  324. }
  325. /**
  326. * Update the this.body.nodeIndices with the most recent node index list
  327. * @private
  328. */
  329. Network.prototype._updateNodeIndexList = function() {
  330. this.body.supportNodeIndices = Object.keys(this.body.supportNodes)
  331. this.body.nodeIndices = Object.keys(this.body.nodes);
  332. };
  333. /**
  334. * Set nodes and edges, and optionally options as well.
  335. *
  336. * @param {Object} data Object containing parameters:
  337. * {Array | DataSet | DataView} [nodes] Array with nodes
  338. * {Array | DataSet | DataView} [edges] Array with edges
  339. * {String} [dot] String containing data in DOT format
  340. * {String} [gephi] String containing data in gephi JSON format
  341. * {Options} [options] Object with options
  342. * @param {Boolean} [disableStart] | optional: disable the calling of the start function.
  343. */
  344. Network.prototype.setData = function(data, disableStart) {
  345. if (disableStart === undefined) {
  346. disableStart = false;
  347. }
  348. // unselect all to ensure no selections from old data are carried over.
  349. this.selectionHandler.unselectAll();
  350. // we set initializing to true to ensure that the hierarchical layout is not performed until both nodes and edges are added.
  351. this.initializing = true;
  352. if (data && data.dot && (data.nodes || data.edges)) {
  353. throw new SyntaxError('Data must contain either parameter "dot" or ' +
  354. ' parameter pair "nodes" and "edges", but not both.');
  355. }
  356. // clean up in case there is anyone in an active mode of the manipulation. This is the same option as bound to the escape button.
  357. if (this.constants.dataManipulation.enabled == true) {
  358. this._createManipulatorBar();
  359. }
  360. // set options
  361. this.setOptions(data && data.options);
  362. // set all data
  363. if (data && data.dot) {
  364. // parse DOT file
  365. if(data && data.dot) {
  366. var dotData = dotparser.DOTToGraph(data.dot);
  367. this.setData(dotData);
  368. return;
  369. }
  370. }
  371. else if (data && data.gephi) {
  372. // parse DOT file
  373. if(data && data.gephi) {
  374. var gephiData = gephiParser.parseGephi(data.gephi);
  375. this.setData(gephiData);
  376. return;
  377. }
  378. }
  379. else {
  380. this._setNodes(data && data.nodes);
  381. this._setEdges(data && data.edges);
  382. }
  383. if (disableStart == false) {
  384. if (this.constants.hierarchicalLayout.enabled == true) {
  385. this._resetLevels();
  386. this._setupHierarchicalLayout();
  387. }
  388. else {
  389. // find a stable position or start animating to a stable position
  390. this.body.emitter.emit("stabilize");
  391. }
  392. }
  393. else {
  394. this.initializing = false;
  395. }
  396. };
  397. /**
  398. * Set options
  399. * @param {Object} options
  400. */
  401. Network.prototype.setOptions = function (options) {
  402. if (options) {
  403. var prop;
  404. var fields = ['nodes','edges','smoothCurves','hierarchicalLayout','navigation',
  405. 'keyboard','dataManipulation','onAdd','onEdit','onEditEdge','onConnect','onDelete','clickToUse'
  406. ];
  407. // extend all but the values in fields
  408. util.selectiveNotDeepExtend(fields,this.constants, options);
  409. util.selectiveNotDeepExtend(['color'],this.constants.nodes, options.nodes);
  410. util.selectiveNotDeepExtend(['color','length'],this.constants.edges, options.edges);
  411. this.groups.useDefaultGroups = this.constants.useDefaultGroups;
  412. this.physics.setOptions(options.physics);
  413. this.canvas.setOptions(options.canvas);
  414. this.renderer.setOptions(options.rendering);
  415. this.interactionHandler.setOptions(options.interaction);
  416. this.selectionHandler.setOptions(options.selection);
  417. if (options.onAdd) {this.triggerFunctions.add = options.onAdd;}
  418. if (options.onEdit) {this.triggerFunctions.edit = options.onEdit;}
  419. if (options.onEditEdge) {this.triggerFunctions.editEdge = options.onEditEdge;}
  420. if (options.onConnect) {this.triggerFunctions.connect = options.onConnect;}
  421. if (options.onDelete) {this.triggerFunctions.del = options.onDelete;}
  422. util.mergeOptions(this.constants, options,'smoothCurves');
  423. util.mergeOptions(this.constants, options,'hierarchicalLayout');
  424. util.mergeOptions(this.constants, options,'clustering');
  425. util.mergeOptions(this.constants, options,'navigation');
  426. util.mergeOptions(this.constants, options,'keyboard');
  427. util.mergeOptions(this.constants, options,'dataManipulation');
  428. if (options.dataManipulation) {
  429. this.editMode = this.constants.dataManipulation.initiallyVisible;
  430. }
  431. // TODO: work out these options and document them
  432. if (options.edges) {
  433. if (options.edges.color !== undefined) {
  434. if (util.isString(options.edges.color)) {
  435. this.constants.edges.color = {};
  436. this.constants.edges.color.color = options.edges.color;
  437. this.constants.edges.color.highlight = options.edges.color;
  438. this.constants.edges.color.hover = options.edges.color;
  439. }
  440. else {
  441. if (options.edges.color.color !== undefined) {this.constants.edges.color.color = options.edges.color.color;}
  442. if (options.edges.color.highlight !== undefined) {this.constants.edges.color.highlight = options.edges.color.highlight;}
  443. if (options.edges.color.hover !== undefined) {this.constants.edges.color.hover = options.edges.color.hover;}
  444. }
  445. this.constants.edges.inheritColor = false;
  446. }
  447. if (!options.edges.fontColor) {
  448. if (options.edges.color !== undefined) {
  449. if (util.isString(options.edges.color)) {this.constants.edges.fontColor = options.edges.color;}
  450. else if (options.edges.color.color !== undefined) {this.constants.edges.fontColor = options.edges.color.color;}
  451. }
  452. }
  453. }
  454. if (options.nodes) {
  455. if (options.nodes.color) {
  456. var newColorObj = util.parseColor(options.nodes.color);
  457. this.constants.nodes.color.background = newColorObj.background;
  458. this.constants.nodes.color.border = newColorObj.border;
  459. this.constants.nodes.color.highlight.background = newColorObj.highlight.background;
  460. this.constants.nodes.color.highlight.border = newColorObj.highlight.border;
  461. this.constants.nodes.color.hover.background = newColorObj.hover.background;
  462. this.constants.nodes.color.hover.border = newColorObj.hover.border;
  463. }
  464. }
  465. if (options.groups) {
  466. for (var groupname in options.groups) {
  467. if (options.groups.hasOwnProperty(groupname)) {
  468. var group = options.groups[groupname];
  469. this.groups.add(groupname, group);
  470. }
  471. }
  472. }
  473. if (options.tooltip) {
  474. for (prop in options.tooltip) {
  475. if (options.tooltip.hasOwnProperty(prop)) {
  476. this.constants.tooltip[prop] = options.tooltip[prop];
  477. }
  478. }
  479. if (options.tooltip.color) {
  480. this.constants.tooltip.color = util.parseColor(options.tooltip.color);
  481. }
  482. }
  483. if ('clickToUse' in options) {
  484. if (options.clickToUse) {
  485. if (!this.activator) {
  486. this.activator = new Activator(this.frame);
  487. this.activator.on('change', this._createKeyBinds.bind(this));
  488. }
  489. }
  490. else {
  491. if (this.activator) {
  492. this.activator.destroy();
  493. delete this.activator;
  494. }
  495. }
  496. }
  497. if (options.labels) {
  498. throw new Error('Option "labels" is deprecated. Use options "locale" and "locales" instead.');
  499. }
  500. // (Re)loading the mixins that can be enabled or disabled in the options.
  501. // load the force calculation functions, grouped under the physics system.
  502. // load the navigation system.
  503. //this._loadNavigationControls();
  504. //// load the data manipulation system
  505. //this._loadManipulationSystem();
  506. //// configure the smooth curves
  507. //this._configureSmoothCurves();
  508. // bind hammer
  509. //this.canvas._bindHammer();
  510. // bind keys. If disabled, this will not do anything;
  511. //this._createKeyBinds();
  512. this._markAllEdgesAsDirty();
  513. this.canvas.setSize();
  514. if (this.constants.hierarchicalLayout.enabled == true && this.initializing == false) {
  515. this._resetLevels();
  516. this._setupHierarchicalLayout();
  517. }
  518. if (this.initializing !== true) {
  519. this.moving = true;
  520. this.start();
  521. }
  522. }
  523. };
  524. /**
  525. * Binding the keys for keyboard navigation. These functions are defined in the NavigationMixin
  526. * @private
  527. */
  528. Network.prototype._createKeyBinds = function() {
  529. return;
  530. //var me = this;
  531. //if (this.keycharm !== undefined) {
  532. // this.keycharm.destroy();
  533. //}
  534. //
  535. //if (this.constants.keyboard.bindToWindow == true) {
  536. // this.keycharm = keycharm({container: window, preventDefault: false});
  537. //}
  538. //else {
  539. // this.keycharm = keycharm({container: this.frame, preventDefault: false});
  540. //}
  541. //
  542. //this.keycharm.reset();
  543. //
  544. //if (this.constants.keyboard.enabled && this.isActive()) {
  545. // this.keycharm.bind("up", this._moveUp.bind(me) , "keydown");
  546. // this.keycharm.bind("up", this._yStopMoving.bind(me), "keyup");
  547. // this.keycharm.bind("down", this._moveDown.bind(me) , "keydown");
  548. // this.keycharm.bind("down", this._yStopMoving.bind(me), "keyup");
  549. // this.keycharm.bind("left", this._moveLeft.bind(me) , "keydown");
  550. // this.keycharm.bind("left", this._xStopMoving.bind(me), "keyup");
  551. // this.keycharm.bind("right",this._moveRight.bind(me), "keydown");
  552. // this.keycharm.bind("right",this._xStopMoving.bind(me), "keyup");
  553. // this.keycharm.bind("=", this._zoomIn.bind(me), "keydown");
  554. // this.keycharm.bind("=", this._stopZoom.bind(me), "keyup");
  555. // this.keycharm.bind("num+", this._zoomIn.bind(me), "keydown");
  556. // this.keycharm.bind("num+", this._stopZoom.bind(me), "keyup");
  557. // this.keycharm.bind("num-", this._zoomOut.bind(me), "keydown");
  558. // this.keycharm.bind("num-", this._stopZoom.bind(me), "keyup");
  559. // this.keycharm.bind("-", this._zoomOut.bind(me), "keydown");
  560. // this.keycharm.bind("-", this._stopZoom.bind(me), "keyup");
  561. // this.keycharm.bind("[", this._zoomIn.bind(me), "keydown");
  562. // this.keycharm.bind("[", this._stopZoom.bind(me), "keyup");
  563. // this.keycharm.bind("]", this._zoomOut.bind(me), "keydown");
  564. // this.keycharm.bind("]", this._stopZoom.bind(me), "keyup");
  565. // this.keycharm.bind("pageup",this._zoomIn.bind(me), "keydown");
  566. // this.keycharm.bind("pageup",this._stopZoom.bind(me), "keyup");
  567. // this.keycharm.bind("pagedown",this._zoomOut.bind(me),"keydown");
  568. // this.keycharm.bind("pagedown",this._stopZoom.bind(me), "keyup");
  569. //}
  570. //
  571. //if (this.constants.dataManipulation.enabled == true) {
  572. // this.keycharm.bind("esc",this._createManipulatorBar.bind(me));
  573. // this.keycharm.bind("delete",this._deleteSelected.bind(me));
  574. //}
  575. };
  576. /**
  577. * Cleans up all bindings of the network, removing it fully from the memory IF the variable is set to null after calling this function.
  578. * var network = new vis.Network(..);
  579. * network.destroy();
  580. * network = null;
  581. */
  582. Network.prototype.destroy = function() {
  583. this.start = function () {};
  584. this.redraw = function () {};
  585. this.renderTimer = false;
  586. // cleanup physicsConfiguration if it exists
  587. this._cleanupPhysicsConfiguration();
  588. // remove keybindings
  589. this.keycharm.reset();
  590. // clear hammer bindings
  591. this.hammer.destroy();
  592. // clear events
  593. this.off();
  594. this._recursiveDOMDelete(this.containerElement);
  595. };
  596. Network.prototype._recursiveDOMDelete = function(DOMobject) {
  597. while (DOMobject.hasChildNodes() == true) {
  598. this._recursiveDOMDelete(DOMobject.firstChild);
  599. DOMobject.removeChild(DOMobject.firstChild);
  600. }
  601. };
  602. /**
  603. * Check if there is an element on the given position in the network
  604. * (a node or edge). If so, and if this element has a title,
  605. * show a popup window with its title.
  606. *
  607. * @param {{x:Number, y:Number}} pointer
  608. * @private
  609. */
  610. Network.prototype._checkShowPopup = function (pointer) {
  611. var obj = {
  612. left: this._XconvertDOMtoCanvas(pointer.x),
  613. top: this._YconvertDOMtoCanvas(pointer.y),
  614. right: this._XconvertDOMtoCanvas(pointer.x),
  615. bottom: this._YconvertDOMtoCanvas(pointer.y)
  616. };
  617. var id;
  618. var previousPopupObjId = this.popupObj === undefined ? "" : this.popupObj.id;
  619. var nodeUnderCursor = false;
  620. var popupType = "node";
  621. if (this.popupObj == undefined) {
  622. // search the nodes for overlap, select the top one in case of multiple nodes
  623. var nodes = this.body.nodes;
  624. var overlappingNodes = [];
  625. for (id in nodes) {
  626. if (nodes.hasOwnProperty(id)) {
  627. var node = nodes[id];
  628. if (node.isOverlappingWith(obj)) {
  629. if (node.getTitle() !== undefined) {
  630. overlappingNodes.push(id);
  631. }
  632. }
  633. }
  634. }
  635. if (overlappingNodes.length > 0) {
  636. // if there are overlapping nodes, select the last one, this is the
  637. // one which is drawn on top of the others
  638. this.popupObj = this.body.nodes[overlappingNodes[overlappingNodes.length - 1]];
  639. // if you hover over a node, the title of the edge is not supposed to be shown.
  640. nodeUnderCursor = true;
  641. }
  642. }
  643. if (this.popupObj === undefined && nodeUnderCursor == false) {
  644. // search the edges for overlap
  645. var edges = this.body.edges;
  646. var overlappingEdges = [];
  647. for (id in edges) {
  648. if (edges.hasOwnProperty(id)) {
  649. var edge = edges[id];
  650. if (edge.connected === true && (edge.getTitle() !== undefined) &&
  651. edge.isOverlappingWith(obj)) {
  652. overlappingEdges.push(id);
  653. }
  654. }
  655. }
  656. if (overlappingEdges.length > 0) {
  657. this.popupObj = this.body.edges[overlappingEdges[overlappingEdges.length - 1]];
  658. popupType = "edge";
  659. }
  660. }
  661. if (this.popupObj) {
  662. // show popup message window
  663. if (this.popupObj.id != previousPopupObjId) {
  664. if (this.popup === undefined) {
  665. this.popup = new Popup(this.frame, this.constants.tooltip);
  666. }
  667. this.popup.popupTargetType = popupType;
  668. this.popup.popupTargetId = this.popupObj.id;
  669. // adjust a small offset such that the mouse cursor is located in the
  670. // bottom left location of the popup, and you can easily move over the
  671. // popup area
  672. this.popup.setPosition(pointer.x + 3, pointer.y - 5);
  673. this.popup.setText(this.popupObj.getTitle());
  674. this.popup.show();
  675. }
  676. }
  677. else {
  678. if (this.popup) {
  679. this.popup.hide();
  680. }
  681. }
  682. };
  683. /**
  684. * Check if the popup must be hidden, which is the case when the mouse is no
  685. * longer hovering on the object
  686. * @param {{x:Number, y:Number}} pointer
  687. * @private
  688. */
  689. Network.prototype._checkHidePopup = function (pointer) {
  690. var pointerObj = {
  691. left: this._XconvertDOMtoCanvas(pointer.x),
  692. top: this._YconvertDOMtoCanvas(pointer.y),
  693. right: this._XconvertDOMtoCanvas(pointer.x),
  694. bottom: this._YconvertDOMtoCanvas(pointer.y)
  695. };
  696. var stillOnObj = false;
  697. if (this.popup.popupTargetType == 'node') {
  698. stillOnObj = this.body.nodes[this.popup.popupTargetId].isOverlappingWith(pointerObj);
  699. if (stillOnObj === true) {
  700. var overNode = this.getNodeAt(pointer);
  701. stillOnObj = overNode.id == this.popup.popupTargetId;
  702. }
  703. }
  704. else {
  705. if (this.getNodeAt(pointer) === null) {
  706. stillOnObj = this.body.edges[this.popup.popupTargetId].isOverlappingWith(pointerObj);
  707. }
  708. }
  709. if (stillOnObj === false) {
  710. this.popupObj = undefined;
  711. this.popup.hide();
  712. }
  713. };
  714. /**
  715. * Set a data set with nodes for the network
  716. * @param {Array | DataSet | DataView} nodes The data containing the nodes.
  717. * @private
  718. */
  719. Network.prototype._setNodes = function(nodes) {
  720. var oldNodesData = this.body.data.nodes;
  721. if (nodes instanceof DataSet || nodes instanceof DataView) {
  722. this.body.data.nodes = nodes;
  723. }
  724. else if (Array.isArray(nodes)) {
  725. this.body.data.nodes = new DataSet();
  726. this.body.data.nodes.add(nodes);
  727. }
  728. else if (!nodes) {
  729. this.body.data.nodes = new DataSet();
  730. }
  731. else {
  732. throw new TypeError('Array or DataSet expected');
  733. }
  734. if (oldNodesData) {
  735. // unsubscribe from old dataset
  736. util.forEach(this.nodesListeners, function (callback, event) {
  737. oldNodesData.off(event, callback);
  738. });
  739. }
  740. // remove drawn nodes
  741. this.body.nodes = {};
  742. if (this.body.data.nodes) {
  743. // subscribe to new dataset
  744. var me = this;
  745. util.forEach(this.nodesListeners, function (callback, event) {
  746. me.body.data.nodes.on(event, callback);
  747. });
  748. // draw all new nodes
  749. var ids = this.body.data.nodes.getIds();
  750. this._addNodes(ids);
  751. }
  752. this._updateSelection();
  753. };
  754. /**
  755. * Add nodes
  756. * @param {Number[] | String[]} ids
  757. * @private
  758. */
  759. Network.prototype._addNodes = function(ids) {
  760. var id;
  761. for (var i = 0, len = ids.length; i < len; i++) {
  762. id = ids[i];
  763. var data = this.body.data.nodes.get(id);
  764. var node = new Node(data, this.images, this.groups, this.constants);
  765. this.body.nodes[id] = node; // note: this may replace an existing node
  766. if ((node.xFixed == false || node.yFixed == false) && (node.x === null || node.y === null)) {
  767. var radius = 10 * 0.1*ids.length + 10;
  768. var angle = 2 * Math.PI * Math.random();
  769. if (node.xFixed == false) {node.x = radius * Math.cos(angle);}
  770. if (node.yFixed == false) {node.y = radius * Math.sin(angle);}
  771. }
  772. this.moving = true;
  773. }
  774. this._updateNodeIndexList();
  775. if (this.constants.hierarchicalLayout.enabled == true && this.initializing == false) {
  776. this._resetLevels();
  777. this._setupHierarchicalLayout();
  778. }
  779. this.physics._updateCalculationNodes();
  780. this._reconnectEdges();
  781. this._updateValueRange(this.body.nodes);
  782. };
  783. /**
  784. * Update existing nodes, or create them when not yet existing
  785. * @param {Number[] | String[]} ids
  786. * @private
  787. */
  788. Network.prototype._updateNodes = function(ids,changedData) {
  789. var nodes = this.body.nodes;
  790. for (var i = 0, len = ids.length; i < len; i++) {
  791. var id = ids[i];
  792. var node = nodes[id];
  793. var data = changedData[i];
  794. if (node) {
  795. // update node
  796. node.setProperties(data, this.constants);
  797. }
  798. else {
  799. // create node
  800. node = new Node(properties, this.images, this.groups, this.constants);
  801. nodes[id] = node;
  802. }
  803. }
  804. this.moving = true;
  805. if (this.constants.hierarchicalLayout.enabled == true && this.initializing == false) {
  806. this._resetLevels();
  807. this._setupHierarchicalLayout();
  808. }
  809. this._updateNodeIndexList();
  810. this._updateValueRange(nodes);
  811. this._markAllEdgesAsDirty();
  812. };
  813. Network.prototype._markAllEdgesAsDirty = function() {
  814. for (var edgeId in this.body.edges) {
  815. this.body.edges[edgeId].colorDirty = true;
  816. }
  817. }
  818. /**
  819. * Remove existing nodes. If nodes do not exist, the method will just ignore it.
  820. * @param {Number[] | String[]} ids
  821. * @private
  822. */
  823. Network.prototype._removeNodes = function(ids) {
  824. var nodes = this.body.nodes;
  825. // remove from selection
  826. for (var i = 0, len = ids.length; i < len; i++) {
  827. if (this.selectionObj.nodes[ids[i]] !== undefined) {
  828. this.body.nodes[ids[i]].unselect();
  829. this._removeFromSelection(this.body.nodes[ids[i]]);
  830. }
  831. }
  832. for (var i = 0, len = ids.length; i < len; i++) {
  833. var id = ids[i];
  834. delete nodes[id];
  835. }
  836. this._updateNodeIndexList();
  837. if (this.constants.hierarchicalLayout.enabled == true && this.initializing == false) {
  838. this._resetLevels();
  839. this._setupHierarchicalLayout();
  840. }
  841. this.physics._updateCalculationNodes();
  842. this._reconnectEdges();
  843. this._updateSelection();
  844. this._updateValueRange(nodes);
  845. };
  846. /**
  847. * Load edges by reading the data table
  848. * @param {Array | DataSet | DataView} edges The data containing the edges.
  849. * @private
  850. * @private
  851. */
  852. Network.prototype._setEdges = function(edges) {
  853. var oldEdgesData = this.body.data.edges;
  854. if (edges instanceof DataSet || edges instanceof DataView) {
  855. this.body.data.edges = edges;
  856. }
  857. else if (Array.isArray(edges)) {
  858. this.body.data.edges = new DataSet();
  859. this.body.data.edges.add(edges);
  860. }
  861. else if (!edges) {
  862. this.body.data.edges = new DataSet();
  863. }
  864. else {
  865. throw new TypeError('Array or DataSet expected');
  866. }
  867. if (oldEdgesData) {
  868. // unsubscribe from old dataset
  869. util.forEach(this.edgesListeners, function (callback, event) {
  870. oldEdgesData.off(event, callback);
  871. });
  872. }
  873. // remove drawn edges
  874. this.body.edges = {};
  875. if (this.body.data.edges) {
  876. // subscribe to new dataset
  877. var me = this;
  878. util.forEach(this.edgesListeners, function (callback, event) {
  879. me.body.data.edges.on(event, callback);
  880. });
  881. // draw all new nodes
  882. var ids = this.body.data.edges.getIds();
  883. this._addEdges(ids);
  884. }
  885. this._reconnectEdges();
  886. };
  887. /**
  888. * Add edges
  889. * @param {Number[] | String[]} ids
  890. * @private
  891. */
  892. Network.prototype._addEdges = function (ids) {
  893. var edges = this.body.edges,
  894. edgesData = this.body.data.edges;
  895. for (var i = 0, len = ids.length; i < len; i++) {
  896. var id = ids[i];
  897. var oldEdge = edges[id];
  898. if (oldEdge) {
  899. oldEdge.disconnect();
  900. }
  901. var data = edgesData.get(id, {"showInternalIds" : true});
  902. edges[id] = new Edge(data, this.body, this.constants);
  903. }
  904. this.moving = true;
  905. this._updateValueRange(edges);
  906. this._createBezierNodes();
  907. this.physics._updateCalculationNodes();
  908. if (this.constants.hierarchicalLayout.enabled == true && this.initializing == false) {
  909. this._resetLevels();
  910. this._setupHierarchicalLayout();
  911. }
  912. };
  913. /**
  914. * Update existing edges, or create them when not yet existing
  915. * @param {Number[] | String[]} ids
  916. * @private
  917. */
  918. Network.prototype._updateEdges = function (ids) {
  919. var edges = this.body.edges;
  920. var edgesData = this.body.data.edges;
  921. for (var i = 0, len = ids.length; i < len; i++) {
  922. var id = ids[i];
  923. var data = edgesData.get(id);
  924. var edge = edges[id];
  925. if (edge) {
  926. // update edge
  927. edge.disconnect();
  928. edge.setProperties(data);
  929. edge.connect();
  930. }
  931. else {
  932. // create edge
  933. edge = new Edge(data, this.body, this.constants);
  934. this.body.edges[id] = edge;
  935. }
  936. }
  937. this._createBezierNodes();
  938. if (this.constants.hierarchicalLayout.enabled == true && this.initializing == false) {
  939. this._resetLevels();
  940. this._setupHierarchicalLayout();
  941. }
  942. this.moving = true;
  943. this._updateValueRange(edges);
  944. };
  945. /**
  946. * Remove existing edges. Non existing ids will be ignored
  947. * @param {Number[] | String[]} ids
  948. * @private
  949. */
  950. Network.prototype._removeEdges = function (ids) {
  951. var edges = this.body.edges;
  952. // remove from selection
  953. for (var i = 0, len = ids.length; i < len; i++) {
  954. if (this.selectionObj.edges[ids[i]] !== undefined) {
  955. edges[ids[i]].unselect();
  956. this._removeFromSelection(edges[ids[i]]);
  957. }
  958. }
  959. for (var i = 0, len = ids.length; i < len; i++) {
  960. var id = ids[i];
  961. var edge = edges[id];
  962. if (edge) {
  963. if (edge.via != null) {
  964. delete this.body.supportNodes[edge.via.id];
  965. }
  966. edge.disconnect();
  967. delete edges[id];
  968. }
  969. }
  970. this.moving = true;
  971. this._updateValueRange(edges);
  972. if (this.constants.hierarchicalLayout.enabled == true && this.initializing == false) {
  973. this._resetLevels();
  974. this._setupHierarchicalLayout();
  975. }
  976. this.physics._updateCalculationNodes();
  977. };
  978. /**
  979. * Reconnect all edges
  980. * @private
  981. */
  982. Network.prototype._reconnectEdges = function() {
  983. var id,
  984. nodes = this.body.nodes,
  985. edges = this.body.edges;
  986. for (id in nodes) {
  987. if (nodes.hasOwnProperty(id)) {
  988. nodes[id].edges = [];
  989. }
  990. }
  991. for (id in edges) {
  992. if (edges.hasOwnProperty(id)) {
  993. var edge = edges[id];
  994. edge.from = null;
  995. edge.to = null;
  996. edge.connect();
  997. }
  998. }
  999. };
  1000. /**
  1001. * Update the values of all object in the given array according to the current
  1002. * value range of the objects in the array.
  1003. * @param {Object} obj An object containing a set of Edges or Nodes
  1004. * The objects must have a method getValue() and
  1005. * setValueRange(min, max).
  1006. * @private
  1007. */
  1008. Network.prototype._updateValueRange = function(obj) {
  1009. var id;
  1010. // determine the range of the objects
  1011. var valueMin = undefined;
  1012. var valueMax = undefined;
  1013. var valueTotal = 0;
  1014. for (id in obj) {
  1015. if (obj.hasOwnProperty(id)) {
  1016. var value = obj[id].getValue();
  1017. if (value !== undefined) {
  1018. valueMin = (valueMin === undefined) ? value : Math.min(value, valueMin);
  1019. valueMax = (valueMax === undefined) ? value : Math.max(value, valueMax);
  1020. valueTotal += value;
  1021. }
  1022. }
  1023. }
  1024. // adjust the range of all objects
  1025. if (valueMin !== undefined && valueMax !== undefined) {
  1026. for (id in obj) {
  1027. if (obj.hasOwnProperty(id)) {
  1028. obj[id].setValueRange(valueMin, valueMax, valueTotal);
  1029. }
  1030. }
  1031. }
  1032. };
  1033. /**
  1034. * Set the translation of the network
  1035. * @param {Number} offsetX Horizontal offset
  1036. * @param {Number} offsetY Vertical offset
  1037. * @private
  1038. */
  1039. Network.prototype._setTranslation = function(offsetX, offsetY) {
  1040. if (this.translation === undefined) {
  1041. this.translation = {
  1042. x: 0,
  1043. y: 0
  1044. };
  1045. }
  1046. if (offsetX !== undefined) {
  1047. this.translation.x = offsetX;
  1048. }
  1049. if (offsetY !== undefined) {
  1050. this.translation.y = offsetY;
  1051. }
  1052. this.emit('viewChanged');
  1053. };
  1054. /**
  1055. * Get the translation of the network
  1056. * @return {Object} translation An object with parameters x and y, both a number
  1057. * @private
  1058. */
  1059. Network.prototype._getTranslation = function() {
  1060. return {
  1061. x: this.translation.x,
  1062. y: this.translation.y
  1063. };
  1064. };
  1065. /**
  1066. * Scale the network
  1067. * @param {Number} scale Scaling factor 1.0 is unscaled
  1068. * @private
  1069. */
  1070. Network.prototype._setScale = function(scale) {
  1071. this.scale = scale;
  1072. };
  1073. /**
  1074. * Get the current scale of the network
  1075. * @return {Number} scale Scaling factor 1.0 is unscaled
  1076. * @private
  1077. */
  1078. Network.prototype._getScale = function() {
  1079. return this.scale;
  1080. };
  1081. /**
  1082. * Move the network according to the keyboard presses.
  1083. *
  1084. * @private
  1085. */
  1086. Network.prototype._handleNavigation = function() {
  1087. if (this.xIncrement != 0 || this.yIncrement != 0) {
  1088. var translation = this._getTranslation();
  1089. this._setTranslation(translation.x+this.xIncrement, translation.y+this.yIncrement);
  1090. }
  1091. if (this.zoomIncrement != 0) {
  1092. var center = {
  1093. x: this.frame.canvas.clientWidth / 2,
  1094. y: this.frame.canvas.clientHeight / 2
  1095. };
  1096. this.zoom(this.scale*(1 + this.zoomIncrement), center);
  1097. }
  1098. };
  1099. /**
  1100. * Freeze the _animationStep
  1101. */
  1102. Network.prototype.freezeSimulation = function(freeze) {
  1103. if (freeze == true) {
  1104. this.freezeSimulationEnabled = true;
  1105. this.moving = false;
  1106. }
  1107. else {
  1108. this.freezeSimulationEnabled = false;
  1109. this.moving = true;
  1110. this.start();
  1111. }
  1112. };
  1113. /**
  1114. * This function cleans the support nodes if they are not needed and adds them when they are.
  1115. *
  1116. * @param {boolean} [disableStart]
  1117. * @private
  1118. */
  1119. Network.prototype._configureSmoothCurves = function(disableStart = true) {
  1120. if (this.constants.smoothCurves.enabled == true && this.constants.smoothCurves.dynamic == true) {
  1121. this._createBezierNodes();
  1122. // cleanup unused support nodes
  1123. for (let i = 0; i < this.body.supportNodeIndices.length; i++) {
  1124. let nodeId = this.body.supportNodeIndices[i];
  1125. // delete support nodes for edges that have been deleted
  1126. if (this.body.edges[this.body.supportNodes[nodeId].parentEdgeId] === undefined) {
  1127. delete this.body.supportNodes[nodeId];
  1128. }
  1129. }
  1130. }
  1131. else {
  1132. // delete the support nodes
  1133. this.body.supportNodes = {};
  1134. for (var edgeId in this.body.edges) {
  1135. if (this.body.edges.hasOwnProperty(edgeId)) {
  1136. this.body.edges[edgeId].via = null;
  1137. }
  1138. }
  1139. }
  1140. this._updateNodeIndexList();
  1141. this.physics._updateCalculationNodes();
  1142. if (!disableStart) {
  1143. this.moving = true;
  1144. this.start();
  1145. }
  1146. };
  1147. /**
  1148. * Bezier curves require an anchor point to calculate the smooth flow. These points are nodes. These nodes are invisible but
  1149. * are used for the force calculation.
  1150. *
  1151. * @private
  1152. */
  1153. Network.prototype._createBezierNodes = function(specificEdges = this.body.edges) {
  1154. if (this.constants.smoothCurves.enabled == true && this.constants.smoothCurves.dynamic == true) {
  1155. for (var edgeId in specificEdges) {
  1156. if (specificEdges.hasOwnProperty(edgeId)) {
  1157. var edge = specificEdges[edgeId];
  1158. if (edge.via == null) {
  1159. var nodeId = "edgeId:".concat(edge.id);
  1160. var node = new Node(
  1161. {id:nodeId,
  1162. mass:1,
  1163. shape:'circle',
  1164. image:"",
  1165. internalMultiplier:1
  1166. },{},{},this.constants);
  1167. this.body.supportNodes[nodeId] = node;
  1168. edge.via = node;
  1169. edge.via.parentEdgeId = edge.id;
  1170. edge.positionBezierNode();
  1171. }
  1172. }
  1173. }
  1174. this._updateNodeIndexList();
  1175. }
  1176. };
  1177. /**
  1178. * load the functions that load the mixins into the prototype.
  1179. *
  1180. * @private
  1181. */
  1182. Network.prototype._initializeMixinLoaders = function () {
  1183. for (var mixin in MixinLoader) {
  1184. if (MixinLoader.hasOwnProperty(mixin)) {
  1185. Network.prototype[mixin] = MixinLoader[mixin];
  1186. }
  1187. }
  1188. };
  1189. /**
  1190. * Load the XY positions of the nodes into the dataset.
  1191. */
  1192. Network.prototype.storePosition = function() {
  1193. console.log("storePosition is depricated: use .storePositions() from now on.")
  1194. this.storePositions();
  1195. };
  1196. /**
  1197. * Load the XY positions of the nodes into the dataset.
  1198. */
  1199. Network.prototype.storePositions = function() {
  1200. var dataArray = [];
  1201. for (var nodeId in this.body.nodes) {
  1202. if (this.body.nodes.hasOwnProperty(nodeId)) {
  1203. var node = this.body.nodes[nodeId];
  1204. var allowedToMoveX = !this.body.nodes.xFixed;
  1205. var allowedToMoveY = !this.body.nodes.yFixed;
  1206. if (this.body.data.nodes._data[nodeId].x != Math.round(node.x) || this.body.data.nodes._data[nodeId].y != Math.round(node.y)) {
  1207. dataArray.push({id:nodeId,x:Math.round(node.x),y:Math.round(node.y),allowedToMoveX:allowedToMoveX,allowedToMoveY:allowedToMoveY});
  1208. }
  1209. }
  1210. }
  1211. this.body.data.nodes.update(dataArray);
  1212. };
  1213. /**
  1214. * Return the positions of the nodes.
  1215. */
  1216. Network.prototype.getPositions = function(ids) {
  1217. var dataArray = {};
  1218. if (ids !== undefined) {
  1219. if (Array.isArray(ids) == true) {
  1220. for (var i = 0; i < ids.length; i++) {
  1221. if (this.body.nodes[ids[i]] !== undefined) {
  1222. var node = this.body.nodes[ids[i]];
  1223. dataArray[ids[i]] = {x: Math.round(node.x), y: Math.round(node.y)};
  1224. }
  1225. }
  1226. }
  1227. else {
  1228. if (this.body.nodes[ids] !== undefined) {
  1229. var node = this.body.nodes[ids];
  1230. dataArray[ids] = {x: Math.round(node.x), y: Math.round(node.y)};
  1231. }
  1232. }
  1233. }
  1234. else {
  1235. for (var nodeId in this.body.nodes) {
  1236. if (this.body.nodes.hasOwnProperty(nodeId)) {
  1237. var node = this.body.nodes[nodeId];
  1238. dataArray[nodeId] = {x: Math.round(node.x), y: Math.round(node.y)};
  1239. }
  1240. }
  1241. }
  1242. return dataArray;
  1243. };
  1244. /**
  1245. * Returns true when the Network is active.
  1246. * @returns {boolean}
  1247. */
  1248. Network.prototype.isActive = function () {
  1249. return !this.activator || this.activator.active;
  1250. };
  1251. /**
  1252. * Sets the scale
  1253. * @returns {Number}
  1254. */
  1255. Network.prototype.setScale = function () {
  1256. return this._setScale();
  1257. };
  1258. /**
  1259. * Returns the scale
  1260. * @returns {Number}
  1261. */
  1262. Network.prototype.getScale = function () {
  1263. return this._getScale();
  1264. };
  1265. /**
  1266. * Check if a node is a cluster.
  1267. * @param nodeId
  1268. * @returns {*}
  1269. */
  1270. Network.prototype.isCluster = function(nodeId) {
  1271. if (this.body.nodes[nodeId] !== undefined) {
  1272. return this.body.nodes[nodeId].isCluster;
  1273. }
  1274. else {
  1275. console.log("Node does not exist.")
  1276. return false;
  1277. }
  1278. };
  1279. /**
  1280. * Returns the scale
  1281. * @returns {Number}
  1282. */
  1283. Network.prototype.getCenterCoordinates = function () {
  1284. return this.DOMtoCanvas({x: 0.5 * this.frame.canvas.clientWidth, y: 0.5 * this.frame.canvas.clientHeight});
  1285. };
  1286. Network.prototype.getBoundingBox = function(nodeId) {
  1287. if (this.body.nodes[nodeId] !== undefined) {
  1288. return this.body.nodes[nodeId].boundingBox;
  1289. }
  1290. }
  1291. Network.prototype.getConnectedNodes = function(nodeId) {
  1292. var nodeList = [];
  1293. if (this.body.nodes[nodeId] !== undefined) {
  1294. var node = this.body.nodes[nodeId];
  1295. var nodeObj = {nodeId : true}; // used to quickly check if node already exists
  1296. for (var i = 0; i < node.edges.length; i++) {
  1297. var edge = node.edges[i];
  1298. if (edge.toId == nodeId) {
  1299. if (nodeObj[edge.fromId] === undefined) {
  1300. nodeList.push(edge.fromId);
  1301. nodeObj[edge.fromId] = true;
  1302. }
  1303. }
  1304. else if (edge.fromId == nodeId) {
  1305. if (nodeObj[edge.toId] === undefined) {
  1306. nodeList.push(edge.toId)
  1307. nodeObj[edge.toId] = true;
  1308. }
  1309. }
  1310. }
  1311. }
  1312. return nodeList;
  1313. }
  1314. Network.prototype.getEdgesFromNode = function(nodeId) {
  1315. var edgesList = [];
  1316. if (this.body.nodes[nodeId] !== undefined) {
  1317. var node = this.body.nodes[nodeId];
  1318. for (var i = 0; i < node.edges.length; i++) {
  1319. edgesList.push(node.edges[i].id);
  1320. }
  1321. }
  1322. return edgesList;
  1323. }
  1324. Network.prototype.generateColorObject = function(color) {
  1325. return util.parseColor(color);
  1326. }
  1327. module.exports = Network;