cytoscape-cose-bilkent.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. (function webpackUniversalModuleDefinition(root, factory) {
  2. if(typeof exports === 'object' && typeof module === 'object')
  3. module.exports = factory(require("cose-base"));
  4. else if(typeof define === 'function' && define.amd)
  5. define(["cose-base"], factory);
  6. else if(typeof exports === 'object')
  7. exports["cytoscapeCoseBilkent"] = factory(require("cose-base"));
  8. else
  9. root["cytoscapeCoseBilkent"] = factory(root["coseBase"]);
  10. })(this, function(__WEBPACK_EXTERNAL_MODULE_0__) {
  11. return /******/ (function(modules) { // webpackBootstrap
  12. /******/ // The module cache
  13. /******/ var installedModules = {};
  14. /******/
  15. /******/ // The require function
  16. /******/ function __webpack_require__(moduleId) {
  17. /******/
  18. /******/ // Check if module is in cache
  19. /******/ if(installedModules[moduleId]) {
  20. /******/ return installedModules[moduleId].exports;
  21. /******/ }
  22. /******/ // Create a new module (and put it into the cache)
  23. /******/ var module = installedModules[moduleId] = {
  24. /******/ i: moduleId,
  25. /******/ l: false,
  26. /******/ exports: {}
  27. /******/ };
  28. /******/
  29. /******/ // Execute the module function
  30. /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
  31. /******/
  32. /******/ // Flag the module as loaded
  33. /******/ module.l = true;
  34. /******/
  35. /******/ // Return the exports of the module
  36. /******/ return module.exports;
  37. /******/ }
  38. /******/
  39. /******/
  40. /******/ // expose the modules object (__webpack_modules__)
  41. /******/ __webpack_require__.m = modules;
  42. /******/
  43. /******/ // expose the module cache
  44. /******/ __webpack_require__.c = installedModules;
  45. /******/
  46. /******/ // identity function for calling harmony imports with the correct context
  47. /******/ __webpack_require__.i = function(value) { return value; };
  48. /******/
  49. /******/ // define getter function for harmony exports
  50. /******/ __webpack_require__.d = function(exports, name, getter) {
  51. /******/ if(!__webpack_require__.o(exports, name)) {
  52. /******/ Object.defineProperty(exports, name, {
  53. /******/ configurable: false,
  54. /******/ enumerable: true,
  55. /******/ get: getter
  56. /******/ });
  57. /******/ }
  58. /******/ };
  59. /******/
  60. /******/ // getDefaultExport function for compatibility with non-harmony modules
  61. /******/ __webpack_require__.n = function(module) {
  62. /******/ var getter = module && module.__esModule ?
  63. /******/ function getDefault() { return module['default']; } :
  64. /******/ function getModuleExports() { return module; };
  65. /******/ __webpack_require__.d(getter, 'a', getter);
  66. /******/ return getter;
  67. /******/ };
  68. /******/
  69. /******/ // Object.prototype.hasOwnProperty.call
  70. /******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
  71. /******/
  72. /******/ // __webpack_public_path__
  73. /******/ __webpack_require__.p = "";
  74. /******/
  75. /******/ // Load entry module and return exports
  76. /******/ return __webpack_require__(__webpack_require__.s = 1);
  77. /******/ })
  78. /************************************************************************/
  79. /******/ ([
  80. /* 0 */
  81. /***/ (function(module, exports) {
  82. module.exports = __WEBPACK_EXTERNAL_MODULE_0__;
  83. /***/ }),
  84. /* 1 */
  85. /***/ (function(module, exports, __webpack_require__) {
  86. "use strict";
  87. var LayoutConstants = __webpack_require__(0).layoutBase.LayoutConstants;
  88. var FDLayoutConstants = __webpack_require__(0).layoutBase.FDLayoutConstants;
  89. var CoSEConstants = __webpack_require__(0).CoSEConstants;
  90. var CoSELayout = __webpack_require__(0).CoSELayout;
  91. var CoSENode = __webpack_require__(0).CoSENode;
  92. var PointD = __webpack_require__(0).layoutBase.PointD;
  93. var DimensionD = __webpack_require__(0).layoutBase.DimensionD;
  94. var defaults = {
  95. // Called on `layoutready`
  96. ready: function ready() {},
  97. // Called on `layoutstop`
  98. stop: function stop() {},
  99. // 'draft', 'default' or 'proof"
  100. // - 'draft' fast cooling rate
  101. // - 'default' moderate cooling rate
  102. // - "proof" slow cooling rate
  103. quality: 'default',
  104. // include labels in node dimensions
  105. nodeDimensionsIncludeLabels: false,
  106. // number of ticks per frame; higher is faster but more jerky
  107. refresh: 30,
  108. // Whether to fit the network view after when done
  109. fit: true,
  110. // Padding on fit
  111. padding: 10,
  112. // Whether to enable incremental mode
  113. randomize: true,
  114. // Node repulsion (non overlapping) multiplier
  115. nodeRepulsion: 4500,
  116. // Ideal edge (non nested) length
  117. idealEdgeLength: 50,
  118. // Divisor to compute edge forces
  119. edgeElasticity: 0.45,
  120. // Nesting factor (multiplier) to compute ideal edge length for nested edges
  121. nestingFactor: 0.1,
  122. // Gravity force (constant)
  123. gravity: 0.25,
  124. // Maximum number of iterations to perform
  125. numIter: 2500,
  126. // For enabling tiling
  127. tile: true,
  128. // Type of layout animation. The option set is {'during', 'end', false}
  129. animate: 'end',
  130. // Duration for animate:end
  131. animationDuration: 500,
  132. // Represents the amount of the vertical space to put between the zero degree members during the tiling operation(can also be a function)
  133. tilingPaddingVertical: 10,
  134. // Represents the amount of the horizontal space to put between the zero degree members during the tiling operation(can also be a function)
  135. tilingPaddingHorizontal: 10,
  136. // Gravity range (constant) for compounds
  137. gravityRangeCompound: 1.5,
  138. // Gravity force (constant) for compounds
  139. gravityCompound: 1.0,
  140. // Gravity range (constant)
  141. gravityRange: 3.8,
  142. // Initial cooling factor for incremental layout
  143. initialEnergyOnIncremental: 0.5
  144. };
  145. function extend(defaults, options) {
  146. var obj = {};
  147. for (var i in defaults) {
  148. obj[i] = defaults[i];
  149. }
  150. for (var i in options) {
  151. obj[i] = options[i];
  152. }
  153. return obj;
  154. };
  155. function _CoSELayout(_options) {
  156. this.options = extend(defaults, _options);
  157. getUserOptions(this.options);
  158. }
  159. var getUserOptions = function getUserOptions(options) {
  160. if (options.nodeRepulsion != null) CoSEConstants.DEFAULT_REPULSION_STRENGTH = FDLayoutConstants.DEFAULT_REPULSION_STRENGTH = options.nodeRepulsion;
  161. if (options.idealEdgeLength != null) CoSEConstants.DEFAULT_EDGE_LENGTH = FDLayoutConstants.DEFAULT_EDGE_LENGTH = options.idealEdgeLength;
  162. if (options.edgeElasticity != null) CoSEConstants.DEFAULT_SPRING_STRENGTH = FDLayoutConstants.DEFAULT_SPRING_STRENGTH = options.edgeElasticity;
  163. if (options.nestingFactor != null) CoSEConstants.PER_LEVEL_IDEAL_EDGE_LENGTH_FACTOR = FDLayoutConstants.PER_LEVEL_IDEAL_EDGE_LENGTH_FACTOR = options.nestingFactor;
  164. if (options.gravity != null) CoSEConstants.DEFAULT_GRAVITY_STRENGTH = FDLayoutConstants.DEFAULT_GRAVITY_STRENGTH = options.gravity;
  165. if (options.numIter != null) CoSEConstants.MAX_ITERATIONS = FDLayoutConstants.MAX_ITERATIONS = options.numIter;
  166. if (options.gravityRange != null) CoSEConstants.DEFAULT_GRAVITY_RANGE_FACTOR = FDLayoutConstants.DEFAULT_GRAVITY_RANGE_FACTOR = options.gravityRange;
  167. if (options.gravityCompound != null) CoSEConstants.DEFAULT_COMPOUND_GRAVITY_STRENGTH = FDLayoutConstants.DEFAULT_COMPOUND_GRAVITY_STRENGTH = options.gravityCompound;
  168. if (options.gravityRangeCompound != null) CoSEConstants.DEFAULT_COMPOUND_GRAVITY_RANGE_FACTOR = FDLayoutConstants.DEFAULT_COMPOUND_GRAVITY_RANGE_FACTOR = options.gravityRangeCompound;
  169. if (options.initialEnergyOnIncremental != null) CoSEConstants.DEFAULT_COOLING_FACTOR_INCREMENTAL = FDLayoutConstants.DEFAULT_COOLING_FACTOR_INCREMENTAL = options.initialEnergyOnIncremental;
  170. if (options.quality == 'draft') LayoutConstants.QUALITY = 0;else if (options.quality == 'proof') LayoutConstants.QUALITY = 2;else LayoutConstants.QUALITY = 1;
  171. CoSEConstants.NODE_DIMENSIONS_INCLUDE_LABELS = FDLayoutConstants.NODE_DIMENSIONS_INCLUDE_LABELS = LayoutConstants.NODE_DIMENSIONS_INCLUDE_LABELS = options.nodeDimensionsIncludeLabels;
  172. CoSEConstants.DEFAULT_INCREMENTAL = FDLayoutConstants.DEFAULT_INCREMENTAL = LayoutConstants.DEFAULT_INCREMENTAL = !options.randomize;
  173. CoSEConstants.ANIMATE = FDLayoutConstants.ANIMATE = LayoutConstants.ANIMATE = options.animate;
  174. CoSEConstants.TILE = options.tile;
  175. CoSEConstants.TILING_PADDING_VERTICAL = typeof options.tilingPaddingVertical === 'function' ? options.tilingPaddingVertical.call() : options.tilingPaddingVertical;
  176. CoSEConstants.TILING_PADDING_HORIZONTAL = typeof options.tilingPaddingHorizontal === 'function' ? options.tilingPaddingHorizontal.call() : options.tilingPaddingHorizontal;
  177. };
  178. _CoSELayout.prototype.run = function () {
  179. var ready;
  180. var frameId;
  181. var options = this.options;
  182. var idToLNode = this.idToLNode = {};
  183. var layout = this.layout = new CoSELayout();
  184. var self = this;
  185. self.stopped = false;
  186. this.cy = this.options.cy;
  187. this.cy.trigger({ type: 'layoutstart', layout: this });
  188. var gm = layout.newGraphManager();
  189. this.gm = gm;
  190. var nodes = this.options.eles.nodes();
  191. var edges = this.options.eles.edges();
  192. this.root = gm.addRoot();
  193. this.processChildrenList(this.root, this.getTopMostNodes(nodes), layout);
  194. for (var i = 0; i < edges.length; i++) {
  195. var edge = edges[i];
  196. var sourceNode = this.idToLNode[edge.data("source")];
  197. var targetNode = this.idToLNode[edge.data("target")];
  198. if (sourceNode !== targetNode && sourceNode.getEdgesBetween(targetNode).length == 0) {
  199. var e1 = gm.add(layout.newEdge(), sourceNode, targetNode);
  200. e1.id = edge.id();
  201. }
  202. }
  203. var getPositions = function getPositions(ele, i) {
  204. if (typeof ele === "number") {
  205. ele = i;
  206. }
  207. var theId = ele.data('id');
  208. var lNode = self.idToLNode[theId];
  209. return {
  210. x: lNode.getRect().getCenterX(),
  211. y: lNode.getRect().getCenterY()
  212. };
  213. };
  214. /*
  215. * Reposition nodes in iterations animatedly
  216. */
  217. var iterateAnimated = function iterateAnimated() {
  218. // Thigs to perform after nodes are repositioned on screen
  219. var afterReposition = function afterReposition() {
  220. if (options.fit) {
  221. options.cy.fit(options.eles, options.padding);
  222. }
  223. if (!ready) {
  224. ready = true;
  225. self.cy.one('layoutready', options.ready);
  226. self.cy.trigger({ type: 'layoutready', layout: self });
  227. }
  228. };
  229. var ticksPerFrame = self.options.refresh;
  230. var isDone;
  231. for (var i = 0; i < ticksPerFrame && !isDone; i++) {
  232. isDone = self.stopped || self.layout.tick();
  233. }
  234. // If layout is done
  235. if (isDone) {
  236. // If the layout is not a sublayout and it is successful perform post layout.
  237. if (layout.checkLayoutSuccess() && !layout.isSubLayout) {
  238. layout.doPostLayout();
  239. }
  240. // If layout has a tilingPostLayout function property call it.
  241. if (layout.tilingPostLayout) {
  242. layout.tilingPostLayout();
  243. }
  244. layout.isLayoutFinished = true;
  245. self.options.eles.nodes().positions(getPositions);
  246. afterReposition();
  247. // trigger layoutstop when the layout stops (e.g. finishes)
  248. self.cy.one('layoutstop', self.options.stop);
  249. self.cy.trigger({ type: 'layoutstop', layout: self });
  250. if (frameId) {
  251. cancelAnimationFrame(frameId);
  252. }
  253. ready = false;
  254. return;
  255. }
  256. var animationData = self.layout.getPositionsData(); // Get positions of layout nodes note that all nodes may not be layout nodes because of tiling
  257. // Position nodes, for the nodes whose id does not included in data (because they are removed from their parents and included in dummy compounds)
  258. // use position of their ancestors or dummy ancestors
  259. options.eles.nodes().positions(function (ele, i) {
  260. if (typeof ele === "number") {
  261. ele = i;
  262. }
  263. // If ele is a compound node, then its position will be defined by its children
  264. if (!ele.isParent()) {
  265. var theId = ele.id();
  266. var pNode = animationData[theId];
  267. var temp = ele;
  268. // If pNode is undefined search until finding position data of its first ancestor (It may be dummy as well)
  269. while (pNode == null) {
  270. pNode = animationData[temp.data('parent')] || animationData['DummyCompound_' + temp.data('parent')];
  271. animationData[theId] = pNode;
  272. temp = temp.parent()[0];
  273. if (temp == undefined) {
  274. break;
  275. }
  276. }
  277. if (pNode != null) {
  278. return {
  279. x: pNode.x,
  280. y: pNode.y
  281. };
  282. } else {
  283. return {
  284. x: ele.position('x'),
  285. y: ele.position('y')
  286. };
  287. }
  288. }
  289. });
  290. afterReposition();
  291. frameId = requestAnimationFrame(iterateAnimated);
  292. };
  293. /*
  294. * Listen 'layoutstarted' event and start animated iteration if animate option is 'during'
  295. */
  296. layout.addListener('layoutstarted', function () {
  297. if (self.options.animate === 'during') {
  298. frameId = requestAnimationFrame(iterateAnimated);
  299. }
  300. });
  301. layout.runLayout(); // Run cose layout
  302. /*
  303. * If animate option is not 'during' ('end' or false) perform these here (If it is 'during' similar things are already performed)
  304. */
  305. if (this.options.animate !== "during") {
  306. self.options.eles.nodes().not(":parent").layoutPositions(self, self.options, getPositions); // Use layout positions to reposition the nodes it considers the options parameter
  307. ready = false;
  308. }
  309. return this; // chaining
  310. };
  311. //Get the top most ones of a list of nodes
  312. _CoSELayout.prototype.getTopMostNodes = function (nodes) {
  313. var nodesMap = {};
  314. for (var i = 0; i < nodes.length; i++) {
  315. nodesMap[nodes[i].id()] = true;
  316. }
  317. var roots = nodes.filter(function (ele, i) {
  318. if (typeof ele === "number") {
  319. ele = i;
  320. }
  321. var parent = ele.parent()[0];
  322. while (parent != null) {
  323. if (nodesMap[parent.id()]) {
  324. return false;
  325. }
  326. parent = parent.parent()[0];
  327. }
  328. return true;
  329. });
  330. return roots;
  331. };
  332. _CoSELayout.prototype.processChildrenList = function (parent, children, layout) {
  333. var size = children.length;
  334. for (var i = 0; i < size; i++) {
  335. var theChild = children[i];
  336. var children_of_children = theChild.children();
  337. var theNode;
  338. var dimensions = theChild.layoutDimensions({
  339. nodeDimensionsIncludeLabels: this.options.nodeDimensionsIncludeLabels
  340. });
  341. if (theChild.outerWidth() != null && theChild.outerHeight() != null) {
  342. theNode = parent.add(new CoSENode(layout.graphManager, new PointD(theChild.position('x') - dimensions.w / 2, theChild.position('y') - dimensions.h / 2), new DimensionD(parseFloat(dimensions.w), parseFloat(dimensions.h))));
  343. } else {
  344. theNode = parent.add(new CoSENode(this.graphManager));
  345. }
  346. // Attach id to the layout node
  347. theNode.id = theChild.data("id");
  348. // Attach the paddings of cy node to layout node
  349. theNode.paddingLeft = parseInt(theChild.css('padding'));
  350. theNode.paddingTop = parseInt(theChild.css('padding'));
  351. theNode.paddingRight = parseInt(theChild.css('padding'));
  352. theNode.paddingBottom = parseInt(theChild.css('padding'));
  353. //Attach the label properties to compound if labels will be included in node dimensions
  354. if (this.options.nodeDimensionsIncludeLabels) {
  355. if (theChild.isParent()) {
  356. var labelWidth = theChild.boundingBox({ includeLabels: true, includeNodes: false }).w;
  357. var labelHeight = theChild.boundingBox({ includeLabels: true, includeNodes: false }).h;
  358. var labelPos = theChild.css("text-halign");
  359. theNode.labelWidth = labelWidth;
  360. theNode.labelHeight = labelHeight;
  361. theNode.labelPos = labelPos;
  362. }
  363. }
  364. // Map the layout node
  365. this.idToLNode[theChild.data("id")] = theNode;
  366. if (isNaN(theNode.rect.x)) {
  367. theNode.rect.x = 0;
  368. }
  369. if (isNaN(theNode.rect.y)) {
  370. theNode.rect.y = 0;
  371. }
  372. if (children_of_children != null && children_of_children.length > 0) {
  373. var theNewGraph;
  374. theNewGraph = layout.getGraphManager().add(layout.newGraph(), theNode);
  375. this.processChildrenList(theNewGraph, children_of_children, layout);
  376. }
  377. }
  378. };
  379. /**
  380. * @brief : called on continuous layouts to stop them before they finish
  381. */
  382. _CoSELayout.prototype.stop = function () {
  383. this.stopped = true;
  384. return this; // chaining
  385. };
  386. var register = function register(cytoscape) {
  387. // var Layout = getLayout( cytoscape );
  388. cytoscape('layout', 'cose-bilkent', _CoSELayout);
  389. };
  390. // auto reg for globals
  391. if (typeof cytoscape !== 'undefined') {
  392. register(cytoscape);
  393. }
  394. module.exports = register;
  395. /***/ })
  396. /******/ ]);
  397. });