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