explore.js 10 KB

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