explore.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /**
  2. * Explore plugin.
  3. */
  4. Draw.loadPlugin(function(ui)
  5. {
  6. // Adds resource for action
  7. mxResources.parse('exploreFromHere=Explore from here...');
  8. // Max number of edges per page
  9. var pageSize = 20;
  10. var uiCreatePopupMenu = ui.menus.createPopupMenu;
  11. ui.menus.createPopupMenu = function(menu, cell, evt)
  12. {
  13. uiCreatePopupMenu.apply(this, arguments);
  14. var graph = ui.editor.graph;
  15. if (graph.model.isVertex(graph.getSelectionCell()))
  16. {
  17. this.addMenuItems(menu, ['-', 'exploreFromHere'], null, evt);
  18. }
  19. };
  20. // Adds action
  21. ui.actions.addAction('exploreFromHere', function()
  22. {
  23. var sourceGraph = ui.editor.graph;
  24. var container = document.createElement('div');
  25. container.style.position = 'absolute';
  26. container.style.display = 'block';
  27. container.style.background = '#ffffff';
  28. container.style.width = '100%';
  29. container.style.height = '100%';
  30. container.style.zIndex = 2;
  31. var deleteImage = document.createElement('img');
  32. deleteImage.setAttribute('src', IMAGE_PATH + '/delete.png');
  33. deleteImage.style.position = 'absolute';
  34. deleteImage.style.cursor = 'pointer';
  35. deleteImage.style.right = '10px';
  36. deleteImage.style.top = '10px';
  37. container.appendChild(deleteImage);
  38. document.body.appendChild(container);
  39. var keyHandler = function(evt)
  40. {
  41. if (evt.keyCode == 27)
  42. {
  43. deleteImage.click();
  44. }
  45. };
  46. mxEvent.addListener(document, 'keydown', keyHandler);
  47. // Global variable to make sure each cell in a response has
  48. // a unique ID throughout the complete life of the program,
  49. // in a real-life setup each cell should have an external
  50. // ID on the business object or else the cell ID should be
  51. // globally unique for the lifetime of the graph model.
  52. var requestId = 0;
  53. function main(container)
  54. {
  55. // Checks if browser is supported
  56. if (!mxClient.isBrowserSupported())
  57. {
  58. // Displays an error message if the browser is
  59. // not supported.
  60. mxUtils.error('Browser is not supported!', 200, false);
  61. }
  62. else
  63. {
  64. // Creates the graph inside the given container
  65. var graph = new Graph(container);
  66. graph.keepEdgesInBackground = true;
  67. graph.isCellResizable = function()
  68. {
  69. return false;
  70. };
  71. // Workaround to hide custom handles
  72. graph.isCellRotatable = function()
  73. {
  74. return false;
  75. };
  76. // Shows hand cursor for all vertices
  77. graph.getCursorForCell = function(cell)
  78. {
  79. if (this.model.isVertex(cell))
  80. {
  81. return 'pointer';
  82. }
  83. return null;
  84. };
  85. graph.getFoldingImage = function()
  86. {
  87. return null;
  88. };
  89. mxEvent.addListener(deleteImage, 'click', function()
  90. {
  91. mxEvent.removeListener(document, 'keydown', keyHandler);
  92. container.parentNode.removeChild(container);
  93. sourceGraph.setSelectionCell(sourceGraph.model.getCell(graph.rootCell.sourceCellId));
  94. // FIXME: Does not work
  95. sourceGraph.scrollCellToVisible(sourceGraph.getSelectionCell());
  96. });
  97. // Disables all built-in interactions
  98. graph.setEnabled(false);
  99. // Handles clicks on cells
  100. graph.click = function(me)
  101. {
  102. var evt = me.getEvent();
  103. var cell = me.getCell();
  104. if (cell != null)
  105. {
  106. load(graph, cell);
  107. }
  108. };
  109. // Gets the default parent for inserting new cells. This
  110. // is normally the first child of the root (ie. layer 0).
  111. var parent = graph.getDefaultParent();
  112. var cx = graph.container.scrollWidth / 2;
  113. var cy = graph.container.scrollHeight / 3;
  114. var sourceCell = sourceGraph.getSelectionCell();
  115. graph.model.beginUpdate();
  116. var cell = graph.importCells([sourceCell])[0];
  117. cell.sourceCellId = sourceCell.id;
  118. cell.geometry.x = cx - cell.geometry.width / 2;
  119. cell.geometry.y = cy - cell.geometry.height / 2;
  120. graph.model.endUpdate();
  121. // Animates the changes in the graph model
  122. graph.getModel().addListener(mxEvent.CHANGE, function(sender, evt)
  123. {
  124. var changes = evt.getProperty('edit').changes;
  125. var prev = mxText.prototype.enableBoundingBox;
  126. mxText.prototype.enableBoundingBox = false;
  127. graph.labelsVisible = false;
  128. mxEffects.animateChanges(graph, changes, function()
  129. {
  130. mxText.prototype.prev = true;
  131. graph.labelsVisible = true;
  132. graph.refresh();
  133. graph.setSelectionCell(graph.rootCell);
  134. graph.tooltipHandler.hide();
  135. });
  136. });
  137. load(graph, cell);
  138. }
  139. };
  140. // Loads the links for the given cell into the given graph
  141. // by requesting the respective data in the server-side
  142. // (implemented for this demo using the server-function)
  143. function load(graph, cell)
  144. {
  145. if (graph.getModel().isVertex(cell))
  146. {
  147. var cx = graph.container.scrollWidth / 2;
  148. var cy = graph.container.scrollHeight / 3;
  149. // Gets the default parent for inserting new cells. This
  150. // is normally the first child of the root (ie. layer 0).
  151. var parent = graph.getDefaultParent();
  152. graph.rootCell = cell.referenceCell || cell;
  153. // Adds cells to the model in a single step
  154. graph.getModel().beginUpdate();
  155. try
  156. {
  157. var cells = rootChanged(graph, cell);
  158. // Removes all cells except the new root
  159. for (var key in graph.getModel().cells)
  160. {
  161. var tmp = graph.getModel().getCell(key);
  162. if (tmp != graph.rootCell && graph.getModel().isVertex(tmp))
  163. {
  164. graph.removeCells([tmp]);
  165. }
  166. }
  167. // Merges the response model with the client model
  168. //graph.getModel().mergeChildren(model.getRoot().getChildAt(0), parent);
  169. graph.addCells(cells);
  170. // Moves the given cell to the center
  171. var geo = graph.getModel().getGeometry(graph.rootCell);
  172. if (geo != null)
  173. {
  174. geo = geo.clone();
  175. geo.x = cx - geo.width / 2;
  176. geo.y = cy - geo.height / 3;
  177. graph.getModel().setGeometry(graph.rootCell, geo);
  178. }
  179. // Creates a list of the new vertices, if there is more
  180. // than the center vertex which might have existed
  181. // previously, then this needs to be changed to analyze
  182. // the target model before calling mergeChildren above
  183. var vertices = [];
  184. for (var key in graph.getModel().cells)
  185. {
  186. var tmp = graph.getModel().getCell(key);
  187. if (tmp != graph.rootCell && graph.getModel().isVertex(tmp) &&
  188. graph.getModel().getParent(tmp) == graph.getDefaultParent())
  189. {
  190. vertices.push(tmp);
  191. // Changes the initial location "in-place"
  192. // to get a nice animation effect from the
  193. // center to the radius of the circle
  194. var geo = graph.getModel().getGeometry(tmp);
  195. if (geo != null)
  196. {
  197. geo.x = cx - geo.width / 2;
  198. geo.y = cy - geo.height / 2;
  199. }
  200. }
  201. }
  202. // Arranges the response in a circle
  203. var cellCount = vertices.length;
  204. var phi = 2 * Math.PI / cellCount;
  205. var r = Math.min(graph.container.scrollWidth / 3 - 80,
  206. graph.container.scrollHeight / 3 - 80);
  207. for (var i = 0; i < cellCount; i++)
  208. {
  209. var geo = graph.getModel().getGeometry(vertices[i]);
  210. if (geo != null)
  211. {
  212. geo = geo.clone();
  213. geo.x += r * Math.sin(i * phi);
  214. geo.y += r * Math.cos(i * phi);
  215. graph.getModel().setGeometry(vertices[i], geo);
  216. }
  217. }
  218. // Keeps parallel edges apart
  219. var layout = new mxParallelEdgeLayout(graph);
  220. layout.spacing = 60;
  221. layout.execute(graph.getDefaultParent());
  222. }
  223. finally
  224. {
  225. // Updates the display
  226. graph.getModel().endUpdate();
  227. }
  228. }
  229. };
  230. // Gets the edges from the source cell and adds the targets
  231. function rootChanged(graph, cell)
  232. {
  233. // TODO: Keep existing cells, probably best via XML to redirect IDs
  234. var realCell = cell.referenceCell || cell;
  235. var sourceCell = sourceGraph.model.getCell(realCell.sourceCellId);
  236. var edges = sourceGraph.getEdges(sourceCell, null, true, true, false, true);
  237. var cells = edges;
  238. // Paging by selecting a window in the edges array
  239. if (cell.startIndex != null || (pageSize > 0 && edges.length > pageSize))
  240. {
  241. var start = cell.startIndex || 0;
  242. cells = edges.slice(Math.max(0, start), Math.min(edges.length, start + pageSize));
  243. }
  244. cells = cells.concat(sourceGraph.getOpposites(cells, sourceCell));
  245. var clones = graph.cloneCells(cells);
  246. var edgeStyle = ';curved=1;noEdgeStyle=1;entryX=none;entryY=none;exitX=none;exitY=none;';
  247. var btnStyle = 'fillColor=green;fontColor=white;strokeColor=green;';
  248. for (var i = 0; i < cells.length; i++)
  249. {
  250. clones[i].sourceCellId = cells[i].id;
  251. if (graph.model.isEdge(clones[i]))
  252. {
  253. // Removes waypoints, edge styles, constraints and centers the label
  254. clones[i].geometry.x = 0;
  255. clones[i].geometry.y = 0;
  256. clones[i].geometry.points = null;
  257. clones[i].setStyle(clones[i].getStyle() + edgeStyle);
  258. clones[i].setTerminal(realCell, clones[i].getTerminal(true) == null);
  259. }
  260. }
  261. if (cell.startIndex > 0)
  262. {
  263. var backCell = graph.createVertex(null, null, 'Back...', 0, 0, 80, 30, btnStyle);
  264. backCell.referenceCell = realCell;
  265. backCell.startIndex = Math.max(0, (cell.startIndex || 0) - pageSize);
  266. clones.splice(0, 0, backCell);
  267. }
  268. if (edges.length > (cell.startIndex || 0) + pageSize)
  269. {
  270. var moreCell = graph.createVertex(null, null, 'More...', 0, 0, 80, 30, btnStyle);
  271. moreCell.referenceCell = realCell;
  272. moreCell.startIndex = (cell.startIndex || 0) + pageSize;
  273. clones.splice(0, 0, moreCell);
  274. }
  275. return clones;
  276. };
  277. main(container);
  278. });
  279. });