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.

802 lines
24 KiB

10 years ago
9 years ago
10 years ago
10 years ago
  1. // Load custom shapes into CanvasRenderingContext2D
  2. require('./shapes');
  3. var Emitter = require('emitter-component');
  4. var Hammer = require('../module/hammer');
  5. var util = require('../util');
  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 Popup = require('./Popup');
  13. var Activator = require('../shared/Activator');
  14. var locales = require('./locales');
  15. import NodesHandler from './modules/NodesHandler';
  16. import EdgesHandler from './modules/EdgesHandler';
  17. import PhysicsEngine from './modules/PhysicsEngine';
  18. import ClusterEngine from './modules/Clustering';
  19. import CanvasRenderer from './modules/CanvasRenderer';
  20. import Canvas from './modules/Canvas';
  21. import View from './modules/View';
  22. import InteractionHandler from './modules/InteractionHandler';
  23. import SelectionHandler from "./modules/SelectionHandler";
  24. import LayoutEngine from "./modules/LayoutEngine";
  25. /**
  26. * @constructor Network
  27. * Create a network visualization, displaying nodes and edges.
  28. *
  29. * @param {Element} container The DOM element in which the Network will
  30. * be created. Normally a div element.
  31. * @param {Object} data An object containing parameters
  32. * {Array} nodes
  33. * {Array} edges
  34. * @param {Object} options Options
  35. */
  36. function Network (container, data, options) {
  37. if (!(this instanceof Network)) {
  38. throw new SyntaxError('Constructor must be called with the new operator');
  39. }
  40. // set constant values
  41. this.remainingOptions = {
  42. dataManipulation: {
  43. enabled: false,
  44. initiallyVisible: false
  45. },
  46. hierarchicalLayout: {
  47. enabled:false,
  48. levelSeparation: 150,
  49. nodeSpacing: 100,
  50. direction: "UD", // UD, DU, LR, RL
  51. layout: "hubsize" // hubsize, directed
  52. },
  53. locale: 'en',
  54. locales: locales,
  55. useDefaultGroups: true
  56. };
  57. // containers for nodes and edges
  58. this.body = {
  59. nodes: {},
  60. nodeIndices: [],
  61. edges: {},
  62. edgeIndices: [],
  63. data: {
  64. nodes: null, // A DataSet or DataView
  65. edges: null // A DataSet or DataView
  66. },
  67. functions:{
  68. createNode: () => {},
  69. createEdge: () => {}
  70. },
  71. emitter: {
  72. on: this.on.bind(this),
  73. off: this.off.bind(this),
  74. emit: this.emit.bind(this),
  75. once: this.once.bind(this)
  76. },
  77. eventListeners: {
  78. onTap: function() {},
  79. onTouch: function() {},
  80. onDoubleTap: function() {},
  81. onHold: function() {},
  82. onDragStart: function() {},
  83. onDrag: function() {},
  84. onDragEnd: function() {},
  85. onMouseWheel: function() {},
  86. onPinch: function() {},
  87. onMouseMove: function() {},
  88. onRelease: function() {}
  89. },
  90. container: container,
  91. view: {
  92. scale:1,
  93. translation:{x:0,y:0}
  94. }
  95. };
  96. // todo think of good comment for this set
  97. var groups = new Groups(); // object with groups
  98. var images = new Images(() => this.body.emitter.emit("_requestRedraw")); // object with images
  99. // data handling modules
  100. this.canvas = new Canvas(this.body); // DOM handler
  101. this.selectionHandler = new SelectionHandler(this.body, this.canvas); // Selection handler
  102. this.interactionHandler = new InteractionHandler(this.body, this.canvas, this.selectionHandler); // Interaction handler handles all the hammer bindings (that are bound by canvas), key
  103. this.view = new View(this.body, this.canvas); // camera handler, does animations and zooms
  104. this.renderer = new CanvasRenderer(this.body, this.canvas); // renderer, starts renderloop, has events that modules can hook into
  105. this.physics = new PhysicsEngine(this.body); // physics engine, does all the simulations
  106. this.layoutEngine = new LayoutEngine(this.body);
  107. this.clustering = new ClusterEngine(this.body); // clustering api
  108. this.nodesHandler = new NodesHandler(this.body, images, groups, this.layoutEngine); // Handle adding, deleting and updating of nodes as well as global options
  109. this.edgesHandler = new EdgesHandler(this.body, images, groups); // Handle adding, deleting and updating of edges as well as global options
  110. // this event will trigger a rebuilding of the cache everything. Used when nodes or edges have been added or removed.
  111. this.body.emitter.on("_dataChanged", (params) => {
  112. var t0 = new Date().valueOf();
  113. // update shortcut lists
  114. this._updateVisibleIndices();
  115. this.physics._updatePhysicsIndices();
  116. // update values
  117. this._updateValueRange(this.body.nodes);
  118. this._updateValueRange(this.body.edges);
  119. // update edges
  120. this._reconnectEdges();
  121. this._markAllEdgesAsDirty();
  122. // start simulation (can be called safely, even if already running)
  123. this.body.emitter.emit("startSimulation");
  124. console.log("_dataChanged took:", new Date().valueOf() - t0);
  125. })
  126. // this is called when options of EXISTING nodes or edges have changed.
  127. this.body.emitter.on("_dataUpdated", () => {
  128. var t0 = new Date().valueOf();
  129. // update values
  130. this._updateValueRange(this.body.nodes);
  131. this._updateValueRange(this.body.edges);
  132. // update edges
  133. this._reconnectEdges();
  134. this._markAllEdgesAsDirty();
  135. // start simulation (can be called safely, even if already running)
  136. this.body.emitter.emit("startSimulation");
  137. console.log("_dataUpdated took:", new Date().valueOf() - t0);
  138. });
  139. // create the DOM elements
  140. this.canvas.create();
  141. // apply options
  142. this.setOptions(options);
  143. // load data (the disable start variable will be the same as the enabled clustering)
  144. this.setData(data);
  145. }
  146. // Extend Network with an Emitter mixin
  147. Emitter(Network.prototype);
  148. /**
  149. * Update the this.body.nodeIndices with the most recent node index list
  150. * @private
  151. */
  152. Network.prototype._updateVisibleIndices = function() {
  153. let nodes = this.body.nodes;
  154. let edges = this.body.edges;
  155. this.body.nodeIndices = [];
  156. this.body.edgeIndices = [];
  157. for (let nodeId in nodes) {
  158. if (nodes.hasOwnProperty(nodeId)) {
  159. if (nodes[nodeId].options.hidden === false) {
  160. this.body.nodeIndices.push(nodeId);
  161. }
  162. }
  163. }
  164. for (let edgeId in edges) {
  165. if (edges.hasOwnProperty(edgeId)) {
  166. if (edges[edgeId].options.hidden === false) {
  167. this.body.edgeIndices.push(edgeId);
  168. }
  169. }
  170. }
  171. };
  172. /**
  173. * Set nodes and edges, and optionally options as well.
  174. *
  175. * @param {Object} data Object containing parameters:
  176. * {Array | DataSet | DataView} [nodes] Array with nodes
  177. * {Array | DataSet | DataView} [edges] Array with edges
  178. * {String} [dot] String containing data in DOT format
  179. * {String} [gephi] String containing data in gephi JSON format
  180. * {Options} [options] Object with options
  181. * @param {Boolean} [disableStart] | optional: disable the calling of the start function.
  182. */
  183. Network.prototype.setData = function(data) {
  184. // reset the physics engine.
  185. this.body.emitter.emit("resetPhysics");
  186. this.body.emitter.emit("_resetData");
  187. // unselect all to ensure no selections from old data are carried over.
  188. this.selectionHandler.unselectAll();
  189. if (data && data.dot && (data.nodes || data.edges)) {
  190. throw new SyntaxError('Data must contain either parameter "dot" or ' +
  191. ' parameter pair "nodes" and "edges", but not both.');
  192. }
  193. // set options
  194. this.setOptions(data && data.options);
  195. // set all data
  196. if (data && data.dot) {
  197. // parse DOT file
  198. if(data && data.dot) {
  199. var dotData = dotparser.DOTToGraph(data.dot);
  200. this.setData(dotData);
  201. return;
  202. }
  203. }
  204. else if (data && data.gephi) {
  205. // parse DOT file
  206. if(data && data.gephi) {
  207. var gephiData = gephiParser.parseGephi(data.gephi);
  208. this.setData(gephiData);
  209. return;
  210. }
  211. }
  212. else {
  213. this.nodesHandler.setData(data && data.nodes);
  214. this.edgesHandler.setData(data && data.edges);
  215. }
  216. // find a stable position or start animating to a stable position
  217. this.body.emitter.emit("initPhysics");
  218. };
  219. /**
  220. * Set options
  221. * @param {Object} options
  222. */
  223. Network.prototype.setOptions = function (options) {
  224. if (options) {
  225. //var fields = ['nodes','edges','smoothCurves','hierarchicalLayout','navigation',
  226. // 'keyboard','dataManipulation','onAdd','onEdit','onEditEdge','onConnect','onDelete','clickToUse'
  227. //];
  228. // extend all but the values in fields
  229. //util.selectiveNotDeepExtend(fields,this.constants, options);
  230. //util.selectiveNotDeepExtend(['color'],this.constants.nodes, options.nodes);
  231. //util.selectiveNotDeepExtend(['color','length'],this.constants.edges, options.edges);
  232. //this.groups.useDefaultGroups = this.constants.useDefaultGroups;
  233. this.nodesHandler.setOptions(options.nodes);
  234. this.edgesHandler.setOptions(options.edges);
  235. this.physics.setOptions(options.physics);
  236. this.canvas.setOptions(options.canvas);
  237. this.renderer.setOptions(options.rendering);
  238. this.view.setOptions(options.view);
  239. this.interactionHandler.setOptions(options.interaction);
  240. this.selectionHandler.setOptions(options.selection);
  241. this.layoutEngine.setOptions(options.layout);
  242. this.clustering.setOptions(options.clustering);
  243. //util.mergeOptions(this.constants, options,'smoothCurves');
  244. //util.mergeOptions(this.constants, options,'hierarchicalLayout');
  245. //util.mergeOptions(this.constants, options,'clustering');
  246. //util.mergeOptions(this.constants, options,'navigation');
  247. //util.mergeOptions(this.constants, options,'keyboard');
  248. //util.mergeOptions(this.constants, options,'dataManipulation');
  249. //if (options.dataManipulation) {
  250. // this.editMode = this.constants.dataManipulation.initiallyVisible;
  251. //}
  252. //// TODO: work out these options and document them
  253. //if (options.edges) {
  254. // if (options.edges.color !== undefined) {
  255. // if (util.isString(options.edges.color)) {
  256. // this.constants.edges.color = {};
  257. // this.constants.edges.color.color = options.edges.color;
  258. // this.constants.edges.color.highlight = options.edges.color;
  259. // this.constants.edges.color.hover = options.edges.color;
  260. // }
  261. // else {
  262. // if (options.edges.color.color !== undefined) {this.constants.edges.color.color = options.edges.color.color;}
  263. // if (options.edges.color.highlight !== undefined) {this.constants.edges.color.highlight = options.edges.color.highlight;}
  264. // if (options.edges.color.hover !== undefined) {this.constants.edges.color.hover = options.edges.color.hover;}
  265. // }
  266. // this.constants.edges.inheritColor = false;
  267. // }
  268. //
  269. // if (!options.edges.fontColor) {
  270. // if (options.edges.color !== undefined) {
  271. // if (util.isString(options.edges.color)) {this.constants.edges.fontColor = options.edges.color;}
  272. // else if (options.edges.color.color !== undefined) {this.constants.edges.fontColor = options.edges.color.color;}
  273. // }
  274. // }
  275. //}
  276. //
  277. //if (options.nodes) {
  278. // if (options.nodes.color) {
  279. // var newColorObj = util.parseColor(options.nodes.color);
  280. // this.constants.nodes.color.background = newColorObj.background;
  281. // this.constants.nodes.color.border = newColorObj.border;
  282. // this.constants.nodes.color.highlight.background = newColorObj.highlight.background;
  283. // this.constants.nodes.color.highlight.border = newColorObj.highlight.border;
  284. // this.constants.nodes.color.hover.background = newColorObj.hover.background;
  285. // this.constants.nodes.color.hover.border = newColorObj.hover.border;
  286. // }
  287. //}
  288. //if (options.groups) {
  289. // for (var groupname in options.groups) {
  290. // if (options.groups.hasOwnProperty(groupname)) {
  291. // var group = options.groups[groupname];
  292. // this.groups.add(groupname, group);
  293. // }
  294. // }
  295. //}
  296. //
  297. //if (options.tooltip) {
  298. // for (prop in options.tooltip) {
  299. // if (options.tooltip.hasOwnProperty(prop)) {
  300. // this.constants.tooltip[prop] = options.tooltip[prop];
  301. // }
  302. // }
  303. // if (options.tooltip.color) {
  304. // this.constants.tooltip.color = util.parseColor(options.tooltip.color);
  305. // }
  306. //}
  307. if ('clickToUse' in options) {
  308. if (options.clickToUse === true) {
  309. if (this.activator === undefined) {
  310. this.activator = new Activator(this.frame);
  311. this.activator.on('change', this._createKeyBinds.bind(this));
  312. }
  313. }
  314. else {
  315. if (this.activator !== undefined) {
  316. this.activator.destroy();
  317. delete this.activator;
  318. }
  319. this.body.emitter.emit("activate");
  320. }
  321. }
  322. else {
  323. this.body.emitter.emit("activate");
  324. }
  325. this.canvas.setSize();
  326. }
  327. };
  328. /**
  329. * Cleans up all bindings of the network, removing it fully from the memory IF the variable is set to null after calling this function.
  330. * var network = new vis.Network(..);
  331. * network.destroy();
  332. * network = null;
  333. */
  334. Network.prototype.destroy = function() {
  335. this.body.emitter.emit("destroy");
  336. // clear events
  337. this.body.emitter.off();
  338. // remove the container and everything inside it recursively
  339. this.util.recursiveDOMDelete(this.body.container);
  340. };
  341. /**
  342. * Check if there is an element on the given position in the network
  343. * (a node or edge). If so, and if this element has a title,
  344. * show a popup window with its title.
  345. *
  346. * @param {{x:Number, y:Number}} pointer
  347. * @private
  348. */
  349. Network.prototype._checkShowPopup = function (pointer) {
  350. var obj = {
  351. left: this._XconvertDOMtoCanvas(pointer.x),
  352. top: this._YconvertDOMtoCanvas(pointer.y),
  353. right: this._XconvertDOMtoCanvas(pointer.x),
  354. bottom: this._YconvertDOMtoCanvas(pointer.y)
  355. };
  356. var id;
  357. var previousPopupObjId = this.popupObj === undefined ? "" : this.popupObj.id;
  358. var nodeUnderCursor = false;
  359. var popupType = "node";
  360. if (this.popupObj == undefined) {
  361. // search the nodes for overlap, select the top one in case of multiple nodes
  362. var nodes = this.body.nodes;
  363. var overlappingNodes = [];
  364. for (id in nodes) {
  365. if (nodes.hasOwnProperty(id)) {
  366. var node = nodes[id];
  367. if (node.isOverlappingWith(obj)) {
  368. if (node.getTitle() !== undefined) {
  369. overlappingNodes.push(id);
  370. }
  371. }
  372. }
  373. }
  374. if (overlappingNodes.length > 0) {
  375. // if there are overlapping nodes, select the last one, this is the
  376. // one which is drawn on top of the others
  377. this.popupObj = this.body.nodes[overlappingNodes[overlappingNodes.length - 1]];
  378. // if you hover over a node, the title of the edge is not supposed to be shown.
  379. nodeUnderCursor = true;
  380. }
  381. }
  382. if (this.popupObj === undefined && nodeUnderCursor == false) {
  383. // search the edges for overlap
  384. var edges = this.body.edges;
  385. var overlappingEdges = [];
  386. for (id in edges) {
  387. if (edges.hasOwnProperty(id)) {
  388. var edge = edges[id];
  389. if (edge.connected === true && (edge.getTitle() !== undefined) &&
  390. edge.isOverlappingWith(obj)) {
  391. overlappingEdges.push(id);
  392. }
  393. }
  394. }
  395. if (overlappingEdges.length > 0) {
  396. this.popupObj = this.body.edges[overlappingEdges[overlappingEdges.length - 1]];
  397. popupType = "edge";
  398. }
  399. }
  400. if (this.popupObj) {
  401. // show popup message window
  402. if (this.popupObj.id != previousPopupObjId) {
  403. if (this.popup === undefined) {
  404. this.popup = new Popup(this.frame, this.constants.tooltip);
  405. }
  406. this.popup.popupTargetType = popupType;
  407. this.popup.popupTargetId = this.popupObj.id;
  408. // adjust a small offset such that the mouse cursor is located in the
  409. // bottom left location of the popup, and you can easily move over the
  410. // popup area
  411. this.popup.setPosition(pointer.x + 3, pointer.y - 5);
  412. this.popup.setText(this.popupObj.getTitle());
  413. this.popup.show();
  414. }
  415. }
  416. else {
  417. if (this.popup) {
  418. this.popup.hide();
  419. }
  420. }
  421. };
  422. /**
  423. * Check if the popup must be hidden, which is the case when the mouse is no
  424. * longer hovering on the object
  425. * @param {{x:Number, y:Number}} pointer
  426. * @private
  427. */
  428. Network.prototype._checkHidePopup = function (pointer) {
  429. var pointerObj = {
  430. left: this._XconvertDOMtoCanvas(pointer.x),
  431. top: this._YconvertDOMtoCanvas(pointer.y),
  432. right: this._XconvertDOMtoCanvas(pointer.x),
  433. bottom: this._YconvertDOMtoCanvas(pointer.y)
  434. };
  435. var stillOnObj = false;
  436. if (this.popup.popupTargetType == 'node') {
  437. stillOnObj = this.body.nodes[this.popup.popupTargetId].isOverlappingWith(pointerObj);
  438. if (stillOnObj === true) {
  439. var overNode = this.getNodeAt(pointer);
  440. stillOnObj = overNode.id == this.popup.popupTargetId;
  441. }
  442. }
  443. else {
  444. if (this.getNodeAt(pointer) === null) {
  445. stillOnObj = this.body.edges[this.popup.popupTargetId].isOverlappingWith(pointerObj);
  446. }
  447. }
  448. if (stillOnObj === false) {
  449. this.popupObj = undefined;
  450. this.popup.hide();
  451. }
  452. };
  453. Network.prototype._markAllEdgesAsDirty = function() {
  454. for (var edgeId in this.body.edges) {
  455. this.body.edges[edgeId].colorDirty = true;
  456. }
  457. }
  458. /**
  459. * Reconnect all edges
  460. * @private
  461. */
  462. Network.prototype._reconnectEdges = function() {
  463. var id,
  464. nodes = this.body.nodes,
  465. edges = this.body.edges;
  466. for (id in nodes) {
  467. if (nodes.hasOwnProperty(id)) {
  468. nodes[id].edges = [];
  469. }
  470. }
  471. for (id in edges) {
  472. if (edges.hasOwnProperty(id)) {
  473. var edge = edges[id];
  474. edge.from = null;
  475. edge.to = null;
  476. edge.connect();
  477. }
  478. }
  479. };
  480. /**
  481. * Update the values of all object in the given array according to the current
  482. * value range of the objects in the array.
  483. * @param {Object} obj An object containing a set of Edges or Nodes
  484. * The objects must have a method getValue() and
  485. * setValueRange(min, max).
  486. * @private
  487. */
  488. Network.prototype._updateValueRange = function(obj) {
  489. var id;
  490. // determine the range of the objects
  491. var valueMin = undefined;
  492. var valueMax = undefined;
  493. var valueTotal = 0;
  494. for (id in obj) {
  495. if (obj.hasOwnProperty(id)) {
  496. var value = obj[id].getValue();
  497. if (value !== undefined) {
  498. valueMin = (valueMin === undefined) ? value : Math.min(value, valueMin);
  499. valueMax = (valueMax === undefined) ? value : Math.max(value, valueMax);
  500. valueTotal += value;
  501. }
  502. }
  503. }
  504. // adjust the range of all objects
  505. if (valueMin !== undefined && valueMax !== undefined) {
  506. for (id in obj) {
  507. if (obj.hasOwnProperty(id)) {
  508. obj[id].setValueRange(valueMin, valueMax, valueTotal);
  509. }
  510. }
  511. }
  512. };
  513. /**
  514. * Set the translation of the network
  515. * @param {Number} offsetX Horizontal offset
  516. * @param {Number} offsetY Vertical offset
  517. * @private
  518. */
  519. Network.prototype._setTranslation = function(offsetX, offsetY) {
  520. if (this.translation === undefined) {
  521. this.translation = {
  522. x: 0,
  523. y: 0
  524. };
  525. }
  526. if (offsetX !== undefined) {
  527. this.translation.x = offsetX;
  528. }
  529. if (offsetY !== undefined) {
  530. this.translation.y = offsetY;
  531. }
  532. this.emit('viewChanged');
  533. };
  534. /**
  535. * Get the translation of the network
  536. * @return {Object} translation An object with parameters x and y, both a number
  537. * @private
  538. */
  539. Network.prototype._getTranslation = function() {
  540. return {
  541. x: this.translation.x,
  542. y: this.translation.y
  543. };
  544. };
  545. /**
  546. * Scale the network
  547. * @param {Number} scale Scaling factor 1.0 is unscaled
  548. * @private
  549. */
  550. Network.prototype._setScale = function(scale) {
  551. this.scale = scale;
  552. };
  553. /**
  554. * Get the current scale of the network
  555. * @return {Number} scale Scaling factor 1.0 is unscaled
  556. * @private
  557. */
  558. Network.prototype._getScale = function() {
  559. return this.scale;
  560. };
  561. /**
  562. * load the functions that load the mixins into the prototype.
  563. *
  564. * @private
  565. */
  566. Network.prototype._initializeMixinLoaders = function () {
  567. for (var mixin in MixinLoader) {
  568. if (MixinLoader.hasOwnProperty(mixin)) {
  569. Network.prototype[mixin] = MixinLoader[mixin];
  570. }
  571. }
  572. };
  573. /**
  574. * Load the XY positions of the nodes into the dataset.
  575. */
  576. Network.prototype.storePositions = function() {
  577. var dataArray = [];
  578. for (var nodeId in this.body.nodes) {
  579. if (this.body.nodes.hasOwnProperty(nodeId)) {
  580. var node = this.body.nodes[nodeId];
  581. var allowedToMoveX = !this.body.nodes.xFixed;
  582. var allowedToMoveY = !this.body.nodes.yFixed;
  583. if (this.body.data.nodes._data[nodeId].x != Math.round(node.x) || this.body.data.nodes._data[nodeId].y != Math.round(node.y)) {
  584. dataArray.push({id:nodeId,x:Math.round(node.x),y:Math.round(node.y),allowedToMoveX:allowedToMoveX,allowedToMoveY:allowedToMoveY});
  585. }
  586. }
  587. }
  588. this.body.data.nodes.update(dataArray);
  589. };
  590. /**
  591. * Return the positions of the nodes.
  592. */
  593. Network.prototype.getPositions = function(ids) {
  594. var dataArray = {};
  595. if (ids !== undefined) {
  596. if (Array.isArray(ids) == true) {
  597. for (var i = 0; i < ids.length; i++) {
  598. if (this.body.nodes[ids[i]] !== undefined) {
  599. var node = this.body.nodes[ids[i]];
  600. dataArray[ids[i]] = {x: Math.round(node.x), y: Math.round(node.y)};
  601. }
  602. }
  603. }
  604. else {
  605. if (this.body.nodes[ids] !== undefined) {
  606. var node = this.body.nodes[ids];
  607. dataArray[ids] = {x: Math.round(node.x), y: Math.round(node.y)};
  608. }
  609. }
  610. }
  611. else {
  612. for (var nodeId in this.body.nodes) {
  613. if (this.body.nodes.hasOwnProperty(nodeId)) {
  614. var node = this.body.nodes[nodeId];
  615. dataArray[nodeId] = {x: Math.round(node.x), y: Math.round(node.y)};
  616. }
  617. }
  618. }
  619. return dataArray;
  620. };
  621. /**
  622. * Returns true when the Network is active.
  623. * @returns {boolean}
  624. */
  625. Network.prototype.isActive = function () {
  626. return !this.activator || this.activator.active;
  627. };
  628. /**
  629. * Sets the scale
  630. * @returns {Number}
  631. */
  632. Network.prototype.setScale = function () {
  633. return this._setScale();
  634. };
  635. /**
  636. * Returns the scale
  637. * @returns {Number}
  638. */
  639. Network.prototype.getScale = function () {
  640. return this._getScale();
  641. };
  642. /**
  643. * Check if a node is a cluster.
  644. * @param nodeId
  645. * @returns {*}
  646. */
  647. Network.prototype.isCluster = function(nodeId) {
  648. if (this.body.nodes[nodeId] !== undefined) {
  649. return this.body.nodes[nodeId].isCluster;
  650. }
  651. else {
  652. console.log("Node does not exist.")
  653. return false;
  654. }
  655. };
  656. /**
  657. * Returns the scale
  658. * @returns {Number}
  659. */
  660. Network.prototype.getCenterCoordinates = function () {
  661. return this.DOMtoCanvas({x: 0.5 * this.frame.canvas.clientWidth, y: 0.5 * this.frame.canvas.clientHeight});
  662. };
  663. Network.prototype.getBoundingBox = function(nodeId) {
  664. if (this.body.nodes[nodeId] !== undefined) {
  665. return this.body.nodes[nodeId].boundingBox;
  666. }
  667. }
  668. Network.prototype.getConnectedNodes = function(nodeId) {
  669. var nodeList = [];
  670. if (this.body.nodes[nodeId] !== undefined) {
  671. var node = this.body.nodes[nodeId];
  672. var nodeObj = {nodeId : true}; // used to quickly check if node already exists
  673. for (var i = 0; i < node.edges.length; i++) {
  674. var edge = node.edges[i];
  675. if (edge.toId == nodeId) {
  676. if (nodeObj[edge.fromId] === undefined) {
  677. nodeList.push(edge.fromId);
  678. nodeObj[edge.fromId] = true;
  679. }
  680. }
  681. else if (edge.fromId == nodeId) {
  682. if (nodeObj[edge.toId] === undefined) {
  683. nodeList.push(edge.toId)
  684. nodeObj[edge.toId] = true;
  685. }
  686. }
  687. }
  688. }
  689. return nodeList;
  690. }
  691. Network.prototype.getEdgesFromNode = function(nodeId) {
  692. var edgesList = [];
  693. if (this.body.nodes[nodeId] !== undefined) {
  694. var node = this.body.nodes[nodeId];
  695. for (var i = 0; i < node.edges.length; i++) {
  696. edgesList.push(node.edges[i].id);
  697. }
  698. }
  699. return edgesList;
  700. }
  701. Network.prototype.generateColorObject = function(color) {
  702. return util.parseColor(color);
  703. }
  704. module.exports = Network;