mxClipboard.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /**
  2. * Copyright (c) 2006-2015, JGraph Ltd
  3. * Copyright (c) 2006-2015, Gaudenz Alder
  4. */
  5. var mxClipboard =
  6. {
  7. /**
  8. * Class: mxClipboard
  9. *
  10. * Singleton that implements a clipboard for graph cells.
  11. *
  12. * Example:
  13. *
  14. * (code)
  15. * mxClipboard.copy(graph);
  16. * mxClipboard.paste(graph2);
  17. * (end)
  18. *
  19. * This copies the selection cells from the graph to the clipboard and
  20. * pastes them into graph2.
  21. *
  22. * For fine-grained control of the clipboard data the <mxGraph.canExportCell>
  23. * and <mxGraph.canImportCell> functions can be overridden.
  24. *
  25. * To restore previous parents for pasted cells, the implementation for
  26. * <copy> and <paste> can be changed as follows.
  27. *
  28. * (code)
  29. * mxClipboard.copy = function(graph, cells)
  30. * {
  31. * cells = cells || graph.getSelectionCells();
  32. * var result = graph.getExportableCells(cells);
  33. *
  34. * mxClipboard.parents = new Object();
  35. *
  36. * for (var i = 0; i < result.length; i++)
  37. * {
  38. * mxClipboard.parents[i] = graph.model.getParent(cells[i]);
  39. * }
  40. *
  41. * mxClipboard.insertCount = 1;
  42. * mxClipboard.setCells(graph.cloneCells(result));
  43. *
  44. * return result;
  45. * };
  46. *
  47. * mxClipboard.paste = function(graph)
  48. * {
  49. * if (!mxClipboard.isEmpty())
  50. * {
  51. * var cells = graph.getImportableCells(mxClipboard.getCells());
  52. * var delta = mxClipboard.insertCount * mxClipboard.STEPSIZE;
  53. * var parent = graph.getDefaultParent();
  54. *
  55. * graph.model.beginUpdate();
  56. * try
  57. * {
  58. * for (var i = 0; i < cells.length; i++)
  59. * {
  60. * var tmp = (mxClipboard.parents != null && graph.model.contains(mxClipboard.parents[i])) ?
  61. * mxClipboard.parents[i] : parent;
  62. * cells[i] = graph.importCells([cells[i]], delta, delta, tmp)[0];
  63. * }
  64. * }
  65. * finally
  66. * {
  67. * graph.model.endUpdate();
  68. * }
  69. *
  70. * // Increments the counter and selects the inserted cells
  71. * mxClipboard.insertCount++;
  72. * graph.setSelectionCells(cells);
  73. * }
  74. * };
  75. * (end)
  76. *
  77. * Variable: STEPSIZE
  78. *
  79. * Defines the step size to offset the cells after each paste operation.
  80. * Default is 10.
  81. */
  82. STEPSIZE: 10,
  83. /**
  84. * Variable: insertCount
  85. *
  86. * Counts the number of times the clipboard data has been inserted.
  87. */
  88. insertCount: 1,
  89. /**
  90. * Variable: cells
  91. *
  92. * Holds the array of <mxCells> currently in the clipboard.
  93. */
  94. cells: null,
  95. /**
  96. * Function: setCells
  97. *
  98. * Sets the cells in the clipboard. Fires a <mxEvent.CHANGE> event.
  99. */
  100. setCells: function(cells)
  101. {
  102. mxClipboard.cells = cells;
  103. },
  104. /**
  105. * Function: getCells
  106. *
  107. * Returns the cells in the clipboard.
  108. */
  109. getCells: function()
  110. {
  111. return mxClipboard.cells;
  112. },
  113. /**
  114. * Function: isEmpty
  115. *
  116. * Returns true if the clipboard currently has not data stored.
  117. */
  118. isEmpty: function()
  119. {
  120. return mxClipboard.getCells() == null;
  121. },
  122. /**
  123. * Function: cut
  124. *
  125. * Cuts the given array of <mxCells> from the specified graph.
  126. * If cells is null then the selection cells of the graph will
  127. * be used. Returns the cells that have been cut from the graph.
  128. *
  129. * Parameters:
  130. *
  131. * graph - <mxGraph> that contains the cells to be cut.
  132. * cells - Optional array of <mxCells> to be cut.
  133. */
  134. cut: function(graph, cells)
  135. {
  136. cells = mxClipboard.copy(graph, cells);
  137. mxClipboard.insertCount = 0;
  138. mxClipboard.removeCells(graph, cells);
  139. return cells;
  140. },
  141. /**
  142. * Function: removeCells
  143. *
  144. * Hook to remove the given cells from the given graph after
  145. * a cut operation.
  146. *
  147. * Parameters:
  148. *
  149. * graph - <mxGraph> that contains the cells to be cut.
  150. * cells - Array of <mxCells> to be cut.
  151. */
  152. removeCells: function(graph, cells)
  153. {
  154. graph.removeCells(cells);
  155. },
  156. /**
  157. * Function: copy
  158. *
  159. * Copies the given array of <mxCells> from the specified
  160. * graph to <cells>. Returns the original array of cells that has
  161. * been cloned. Descendants of cells in the array are ignored.
  162. *
  163. * Parameters:
  164. *
  165. * graph - <mxGraph> that contains the cells to be copied.
  166. * cells - Optional array of <mxCells> to be copied.
  167. */
  168. copy: function(graph, cells)
  169. {
  170. cells = cells || graph.getSelectionCells();
  171. var result = graph.getExportableCells(graph.model.getTopmostCells(cells));
  172. mxClipboard.insertCount = 1;
  173. mxClipboard.setCells(graph.cloneCells(result));
  174. return result;
  175. },
  176. /**
  177. * Function: paste
  178. *
  179. * Pastes the <cells> into the specified graph restoring
  180. * the relation to <parents>, if possible. If the parents
  181. * are no longer in the graph or invisible then the
  182. * cells are added to the graph's default or into the
  183. * swimlane under the cell's new location if one exists.
  184. * The cells are added to the graph using <mxGraph.importCells>
  185. * and returned.
  186. *
  187. * Parameters:
  188. *
  189. * graph - <mxGraph> to paste the <cells> into.
  190. */
  191. paste: function(graph)
  192. {
  193. var cells = null;
  194. if (!mxClipboard.isEmpty())
  195. {
  196. cells = graph.getImportableCells(mxClipboard.getCells());
  197. var delta = mxClipboard.insertCount * mxClipboard.STEPSIZE;
  198. var parent = graph.getDefaultParent();
  199. cells = graph.importCells(cells, delta, delta, parent);
  200. // Increments the counter and selects the inserted cells
  201. mxClipboard.insertCount++;
  202. graph.setSelectionCells(cells);
  203. }
  204. return cells;
  205. }
  206. };
  207. __mxOutput.mxClipboard = typeof mxClipboard !== 'undefined' ? mxClipboard : undefined;