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.

553 lines
18 KiB

  1. var util = require('../../util');
  2. var Node = require('../Node');
  3. /**
  4. * Creation of the SectorMixin var.
  5. *
  6. * This contains all the functions the Network object can use to employ the sector system.
  7. * The sector system is always used by Network, though the benefits only apply to the use of clustering.
  8. * If clustering is not used, there is no overhead except for a duplicate object with references to nodes and edges.
  9. */
  10. /**
  11. * This function is only called by the setData function of the Network object.
  12. * This loads the global references into the active sector. This initializes the sector.
  13. *
  14. * @private
  15. */
  16. exports._putDataInSector = function() {
  17. this.sectors["active"][this._sector()].nodes = this.nodes;
  18. this.sectors["active"][this._sector()].edges = this.edges;
  19. this.sectors["active"][this._sector()].nodeIndices = this.nodeIndices;
  20. };
  21. /**
  22. * /**
  23. * This function sets the global references to nodes, edges and nodeIndices back to
  24. * those of the supplied (active) sector. If a type is defined, do the specific type
  25. *
  26. * @param {String} sectorId
  27. * @param {String} [sectorType] | "active" or "frozen"
  28. * @private
  29. */
  30. exports._switchToSector = function(sectorId, sectorType) {
  31. if (sectorType === undefined || sectorType == "active") {
  32. this._switchToActiveSector(sectorId);
  33. }
  34. else {
  35. this._switchToFrozenSector(sectorId);
  36. }
  37. };
  38. /**
  39. * This function sets the global references to nodes, edges and nodeIndices back to
  40. * those of the supplied active sector.
  41. *
  42. * @param sectorId
  43. * @private
  44. */
  45. exports._switchToActiveSector = function(sectorId) {
  46. this.nodeIndices = this.sectors["active"][sectorId]["nodeIndices"];
  47. this.nodes = this.sectors["active"][sectorId]["nodes"];
  48. this.edges = this.sectors["active"][sectorId]["edges"];
  49. };
  50. /**
  51. * This function sets the global references to nodes, edges and nodeIndices back to
  52. * those of the supplied active sector.
  53. *
  54. * @private
  55. */
  56. exports._switchToSupportSector = function() {
  57. this.nodeIndices = this.sectors["support"]["nodeIndices"];
  58. this.nodes = this.sectors["support"]["nodes"];
  59. this.edges = this.sectors["support"]["edges"];
  60. };
  61. /**
  62. * This function sets the global references to nodes, edges and nodeIndices back to
  63. * those of the supplied frozen sector.
  64. *
  65. * @param sectorId
  66. * @private
  67. */
  68. exports._switchToFrozenSector = function(sectorId) {
  69. this.nodeIndices = this.sectors["frozen"][sectorId]["nodeIndices"];
  70. this.nodes = this.sectors["frozen"][sectorId]["nodes"];
  71. this.edges = this.sectors["frozen"][sectorId]["edges"];
  72. };
  73. /**
  74. * This function sets the global references to nodes, edges and nodeIndices back to
  75. * those of the currently active sector.
  76. *
  77. * @private
  78. */
  79. exports._loadLatestSector = function() {
  80. this._switchToSector(this._sector());
  81. };
  82. /**
  83. * This function returns the currently active sector Id
  84. *
  85. * @returns {String}
  86. * @private
  87. */
  88. exports._sector = function() {
  89. return this.activeSector[this.activeSector.length-1];
  90. };
  91. /**
  92. * This function returns the previously active sector Id
  93. *
  94. * @returns {String}
  95. * @private
  96. */
  97. exports._previousSector = function() {
  98. if (this.activeSector.length > 1) {
  99. return this.activeSector[this.activeSector.length-2];
  100. }
  101. else {
  102. throw new TypeError('there are not enough sectors in the this.activeSector array.');
  103. }
  104. };
  105. /**
  106. * We add the active sector at the end of the this.activeSector array
  107. * This ensures it is the currently active sector returned by _sector() and it reaches the top
  108. * of the activeSector stack. When we reverse our steps we move from the end to the beginning of this stack.
  109. *
  110. * @param newId
  111. * @private
  112. */
  113. exports._setActiveSector = function(newId) {
  114. this.activeSector.push(newId);
  115. };
  116. /**
  117. * We remove the currently active sector id from the active sector stack. This happens when
  118. * we reactivate the previously active sector
  119. *
  120. * @private
  121. */
  122. exports._forgetLastSector = function() {
  123. this.activeSector.pop();
  124. };
  125. /**
  126. * This function creates a new active sector with the supplied newId. This newId
  127. * is the expanding node id.
  128. *
  129. * @param {String} newId | Id of the new active sector
  130. * @private
  131. */
  132. exports._createNewSector = function(newId) {
  133. // create the new sector
  134. this.sectors["active"][newId] = {"nodes":{},
  135. "edges":{},
  136. "nodeIndices":[],
  137. "formationScale": this.scale,
  138. "drawingNode": undefined};
  139. // create the new sector render node. This gives visual feedback that you are in a new sector.
  140. this.sectors["active"][newId]['drawingNode'] = new Node(
  141. {id:newId,
  142. color: {
  143. background: "#eaefef",
  144. border: "495c5e"
  145. }
  146. },{},{},this.constants);
  147. this.sectors["active"][newId]['drawingNode'].clusterSize = 2;
  148. };
  149. /**
  150. * This function removes the currently active sector. This is called when we create a new
  151. * active sector.
  152. *
  153. * @param {String} sectorId | Id of the active sector that will be removed
  154. * @private
  155. */
  156. exports._deleteActiveSector = function(sectorId) {
  157. delete this.sectors["active"][sectorId];
  158. };
  159. /**
  160. * This function removes the currently active sector. This is called when we reactivate
  161. * the previously active sector.
  162. *
  163. * @param {String} sectorId | Id of the active sector that will be removed
  164. * @private
  165. */
  166. exports._deleteFrozenSector = function(sectorId) {
  167. delete this.sectors["frozen"][sectorId];
  168. };
  169. /**
  170. * Freezing an active sector means moving it from the "active" object to the "frozen" object.
  171. * We copy the references, then delete the active entree.
  172. *
  173. * @param sectorId
  174. * @private
  175. */
  176. exports._freezeSector = function(sectorId) {
  177. // we move the set references from the active to the frozen stack.
  178. this.sectors["frozen"][sectorId] = this.sectors["active"][sectorId];
  179. // we have moved the sector data into the frozen set, we now remove it from the active set
  180. this._deleteActiveSector(sectorId);
  181. };
  182. /**
  183. * This is the reverse operation of _freezeSector. Activating means moving the sector from the "frozen"
  184. * object to the "active" object.
  185. *
  186. * @param sectorId
  187. * @private
  188. */
  189. exports._activateSector = function(sectorId) {
  190. // we move the set references from the frozen to the active stack.
  191. this.sectors["active"][sectorId] = this.sectors["frozen"][sectorId];
  192. // we have moved the sector data into the active set, we now remove it from the frozen stack
  193. this._deleteFrozenSector(sectorId);
  194. };
  195. /**
  196. * This function merges the data from the currently active sector with a frozen sector. This is used
  197. * in the process of reverting back to the previously active sector.
  198. * The data that is placed in the frozen (the previously active) sector is the node that has been removed from it
  199. * upon the creation of a new active sector.
  200. *
  201. * @param sectorId
  202. * @private
  203. */
  204. exports._mergeThisWithFrozen = function(sectorId) {
  205. // copy all nodes
  206. for (var nodeId in this.nodes) {
  207. if (this.nodes.hasOwnProperty(nodeId)) {
  208. this.sectors["frozen"][sectorId]["nodes"][nodeId] = this.nodes[nodeId];
  209. }
  210. }
  211. // copy all edges (if not fully clustered, else there are no edges)
  212. for (var edgeId in this.edges) {
  213. if (this.edges.hasOwnProperty(edgeId)) {
  214. this.sectors["frozen"][sectorId]["edges"][edgeId] = this.edges[edgeId];
  215. }
  216. }
  217. // merge the nodeIndices
  218. for (var i = 0; i < this.nodeIndices.length; i++) {
  219. this.sectors["frozen"][sectorId]["nodeIndices"].push(this.nodeIndices[i]);
  220. }
  221. };
  222. /**
  223. * This clusters the sector to one cluster. It was a single cluster before this process started so
  224. * we revert to that state. The clusterToFit function with a maximum size of 1 node does this.
  225. *
  226. * @private
  227. */
  228. exports._collapseThisToSingleCluster = function() {
  229. this.clusterToFit(1,false);
  230. };
  231. /**
  232. * We create a new active sector from the node that we want to open.
  233. *
  234. * @param node
  235. * @private
  236. */
  237. exports._addSector = function(node) {
  238. // this is the currently active sector
  239. var sector = this._sector();
  240. // // this should allow me to select nodes from a frozen set.
  241. // if (this.sectors['active'][sector]["nodes"].hasOwnProperty(node.id)) {
  242. // console.log("the node is part of the active sector");
  243. // }
  244. // else {
  245. // console.log("I dont know what the fuck happened!!");
  246. // }
  247. // when we switch to a new sector, we remove the node that will be expanded from the current nodes list.
  248. delete this.nodes[node.id];
  249. var unqiueIdentifier = util.randomUUID();
  250. // we fully freeze the currently active sector
  251. this._freezeSector(sector);
  252. // we create a new active sector. This sector has the Id of the node to ensure uniqueness
  253. this._createNewSector(unqiueIdentifier);
  254. // we add the active sector to the sectors array to be able to revert these steps later on
  255. this._setActiveSector(unqiueIdentifier);
  256. // we redirect the global references to the new sector's references. this._sector() now returns unqiueIdentifier
  257. this._switchToSector(this._sector());
  258. // finally we add the node we removed from our previous active sector to the new active sector
  259. this.nodes[node.id] = node;
  260. };
  261. /**
  262. * We close the sector that is currently open and revert back to the one before.
  263. * If the active sector is the "default" sector, nothing happens.
  264. *
  265. * @private
  266. */
  267. exports._collapseSector = function() {
  268. // the currently active sector
  269. var sector = this._sector();
  270. // we cannot collapse the default sector
  271. if (sector != "default") {
  272. if ((this.nodeIndices.length == 1) ||
  273. (this.sectors["active"][sector]["drawingNode"].width*this.scale < this.constants.clustering.screenSizeThreshold * this.frame.canvas.clientWidth) ||
  274. (this.sectors["active"][sector]["drawingNode"].height*this.scale < this.constants.clustering.screenSizeThreshold * this.frame.canvas.clientHeight)) {
  275. var previousSector = this._previousSector();
  276. // we collapse the sector back to a single cluster
  277. this._collapseThisToSingleCluster();
  278. // we move the remaining nodes, edges and nodeIndices to the previous sector.
  279. // This previous sector is the one we will reactivate
  280. this._mergeThisWithFrozen(previousSector);
  281. // the previously active (frozen) sector now has all the data from the currently active sector.
  282. // we can now delete the active sector.
  283. this._deleteActiveSector(sector);
  284. // we activate the previously active (and currently frozen) sector.
  285. this._activateSector(previousSector);
  286. // we load the references from the newly active sector into the global references
  287. this._switchToSector(previousSector);
  288. // we forget the previously active sector because we reverted to the one before
  289. this._forgetLastSector();
  290. // finally, we update the node index list.
  291. this._updateNodeIndexList();
  292. // we refresh the list with calulation nodes and calculation node indices.
  293. this._updateCalculationNodes();
  294. }
  295. }
  296. };
  297. /**
  298. * This runs a function in all active sectors. This is used in _redraw() and the _initializeForceCalculation().
  299. *
  300. * @param {String} runFunction | This is the NAME of a function we want to call in all active sectors
  301. * | we dont pass the function itself because then the "this" is the window object
  302. * | instead of the Network object
  303. * @param {*} [argument] | Optional: arguments to pass to the runFunction
  304. * @private
  305. */
  306. exports._doInAllActiveSectors = function(runFunction,argument) {
  307. var returnValues = [];
  308. if (argument === undefined) {
  309. for (var sector in this.sectors["active"]) {
  310. if (this.sectors["active"].hasOwnProperty(sector)) {
  311. // switch the global references to those of this sector
  312. this._switchToActiveSector(sector);
  313. returnValues.push( this[runFunction]() );
  314. }
  315. }
  316. }
  317. else {
  318. for (var sector in this.sectors["active"]) {
  319. if (this.sectors["active"].hasOwnProperty(sector)) {
  320. // switch the global references to those of this sector
  321. this._switchToActiveSector(sector);
  322. var args = Array.prototype.splice.call(arguments, 1);
  323. if (args.length > 1) {
  324. returnValues.push( this[runFunction](args[0],args[1]) );
  325. }
  326. else {
  327. returnValues.push( this[runFunction](argument) );
  328. }
  329. }
  330. }
  331. }
  332. // we revert the global references back to our active sector
  333. this._loadLatestSector();
  334. return returnValues;
  335. };
  336. /**
  337. * This runs a function in all active sectors. This is used in _redraw() and the _initializeForceCalculation().
  338. *
  339. * @param {String} runFunction | This is the NAME of a function we want to call in all active sectors
  340. * | we dont pass the function itself because then the "this" is the window object
  341. * | instead of the Network object
  342. * @param {*} [argument] | Optional: arguments to pass to the runFunction
  343. * @private
  344. */
  345. exports._doInSupportSector = function(runFunction,argument) {
  346. var returnValues = false;
  347. if (argument === undefined) {
  348. this._switchToSupportSector();
  349. returnValues = this[runFunction]();
  350. }
  351. else {
  352. this._switchToSupportSector();
  353. var args = Array.prototype.splice.call(arguments, 1);
  354. if (args.length > 1) {
  355. returnValues = this[runFunction](args[0],args[1]);
  356. }
  357. else {
  358. returnValues = this[runFunction](argument);
  359. }
  360. }
  361. // we revert the global references back to our active sector
  362. this._loadLatestSector();
  363. return returnValues;
  364. };
  365. /**
  366. * This runs a function in all frozen sectors. This is used in the _redraw().
  367. *
  368. * @param {String} runFunction | This is the NAME of a function we want to call in all active sectors
  369. * | we don't pass the function itself because then the "this" is the window object
  370. * | instead of the Network object
  371. * @param {*} [argument] | Optional: arguments to pass to the runFunction
  372. * @private
  373. */
  374. exports._doInAllFrozenSectors = function(runFunction,argument) {
  375. if (argument === undefined) {
  376. for (var sector in this.sectors["frozen"]) {
  377. if (this.sectors["frozen"].hasOwnProperty(sector)) {
  378. // switch the global references to those of this sector
  379. this._switchToFrozenSector(sector);
  380. this[runFunction]();
  381. }
  382. }
  383. }
  384. else {
  385. for (var sector in this.sectors["frozen"]) {
  386. if (this.sectors["frozen"].hasOwnProperty(sector)) {
  387. // switch the global references to those of this sector
  388. this._switchToFrozenSector(sector);
  389. var args = Array.prototype.splice.call(arguments, 1);
  390. if (args.length > 1) {
  391. this[runFunction](args[0],args[1]);
  392. }
  393. else {
  394. this[runFunction](argument);
  395. }
  396. }
  397. }
  398. }
  399. this._loadLatestSector();
  400. };
  401. /**
  402. * This runs a function in all sectors. This is used in the _redraw().
  403. *
  404. * @param {String} runFunction | This is the NAME of a function we want to call in all active sectors
  405. * | we don't pass the function itself because then the "this" is the window object
  406. * | instead of the Network object
  407. * @param {*} [argument] | Optional: arguments to pass to the runFunction
  408. * @private
  409. */
  410. exports._doInAllSectors = function(runFunction,argument) {
  411. var args = Array.prototype.splice.call(arguments, 1);
  412. if (argument === undefined) {
  413. this._doInAllActiveSectors(runFunction);
  414. this._doInAllFrozenSectors(runFunction);
  415. }
  416. else {
  417. if (args.length > 1) {
  418. this._doInAllActiveSectors(runFunction,args[0],args[1]);
  419. this._doInAllFrozenSectors(runFunction,args[0],args[1]);
  420. }
  421. else {
  422. this._doInAllActiveSectors(runFunction,argument);
  423. this._doInAllFrozenSectors(runFunction,argument);
  424. }
  425. }
  426. };
  427. /**
  428. * This clears the nodeIndices list. We cannot use this.nodeIndices = [] because we would break the link with the
  429. * active sector. Thus we clear the nodeIndices in the active sector, then reconnect the this.nodeIndices to it.
  430. *
  431. * @private
  432. */
  433. exports._clearNodeIndexList = function() {
  434. var sector = this._sector();
  435. this.sectors["active"][sector]["nodeIndices"] = [];
  436. this.nodeIndices = this.sectors["active"][sector]["nodeIndices"];
  437. };
  438. /**
  439. * Draw the encompassing sector node
  440. *
  441. * @param ctx
  442. * @param sectorType
  443. * @private
  444. */
  445. exports._drawSectorNodes = function(ctx,sectorType) {
  446. var minY = 1e9, maxY = -1e9, minX = 1e9, maxX = -1e9, node;
  447. for (var sector in this.sectors[sectorType]) {
  448. if (this.sectors[sectorType].hasOwnProperty(sector)) {
  449. if (this.sectors[sectorType][sector]["drawingNode"] !== undefined) {
  450. this._switchToSector(sector,sectorType);
  451. minY = 1e9; maxY = -1e9; minX = 1e9; maxX = -1e9;
  452. for (var nodeId in this.nodes) {
  453. if (this.nodes.hasOwnProperty(nodeId)) {
  454. node = this.nodes[nodeId];
  455. node.resize(ctx);
  456. if (minX > node.x - 0.5 * node.width) {minX = node.x - 0.5 * node.width;}
  457. if (maxX < node.x + 0.5 * node.width) {maxX = node.x + 0.5 * node.width;}
  458. if (minY > node.y - 0.5 * node.height) {minY = node.y - 0.5 * node.height;}
  459. if (maxY < node.y + 0.5 * node.height) {maxY = node.y + 0.5 * node.height;}
  460. }
  461. }
  462. node = this.sectors[sectorType][sector]["drawingNode"];
  463. node.x = 0.5 * (maxX + minX);
  464. node.y = 0.5 * (maxY + minY);
  465. node.width = 2 * (node.x - minX);
  466. node.height = 2 * (node.y - minY);
  467. node.options.radius = Math.sqrt(Math.pow(0.5*node.width,2) + Math.pow(0.5*node.height,2));
  468. node.setScale(this.scale);
  469. node._drawCircle(ctx);
  470. }
  471. }
  472. }
  473. };
  474. exports._drawAllSectorNodes = function(ctx) {
  475. this._drawSectorNodes(ctx,"frozen");
  476. this._drawSectorNodes(ctx,"active");
  477. this._loadLatestSector();
  478. };