mxClipboard

Singleton that implements a clipboard for graph cells.

Example

mxClipboard.copy(graph);
mxClipboard.paste(graph2);

This copies the selection cells from the graph to the clipboard and pastes them into graph2.

For fine-grained control of the clipboard data the mxGraph.canExportCell and mxGraph.canImportCell functions can be overridden.

To restore previous parents for pasted cells, the implementation for copy and paste can be changed as follows.

mxClipboard.copy = function(graph, cells)
{
  cells = cells || graph.getSelectionCells();
  var result = graph.getExportableCells(cells);

  mxClipboard.parents = new Object();

  for (var i = 0; i < result.length; i++)
  {
    mxClipboard.parents[i] = graph.model.getParent(cells[i]);
  }

  mxClipboard.insertCount = 1;
  mxClipboard.setCells(graph.cloneCells(result));

  return result;
};

mxClipboard.paste = function(graph)
{
  if (!mxClipboard.isEmpty())
  {
    var cells = graph.getImportableCells(mxClipboard.getCells());
    var delta = mxClipboard.insertCount * mxClipboard.STEPSIZE;
    var parent = graph.getDefaultParent();

    graph.model.beginUpdate();
    try
    {
      for (var i = 0; i < cells.length; i++)
      {
        var tmp = (mxClipboard.parents != null && graph.model.contains(mxClipboard.parents[i])) ?
             mxClipboard.parents[i] : parent;
        cells[i] = graph.importCells([cells[i]], delta, delta, tmp)[0];
      }
    }
    finally
    {
      graph.model.endUpdate();
    }

    // Increments the counter and selects the inserted cells
    mxClipboard.insertCount++;
    graph.setSelectionCells(cells);
  }
};
Summary
mxClipboardSingleton that implements a clipboard for graph cells.
Variables
STEPSIZEDefines the step size to offset the cells after each paste operation.
insertCountCounts the number of times the clipboard data has been inserted.
cellsHolds the array of mxCells currently in the clipboard.
Functions
setCellsSets the cells in the clipboard.
getCellsReturns the cells in the clipboard.
isEmptyReturns true if the clipboard currently has not data stored.
cutCuts the given array of mxCells from the specified graph.
removeCellsHook to remove the given cells from the given graph after a cut operation.
copyCopies the given array of mxCells from the specified graph to cells.
pastePastes the cells into the specified graph restoring the relation to <parents>, if possible.

Variables

STEPSIZE

STEPSIZE: 10

Defines the step size to offset the cells after each paste operation.  Default is 10.

insertCount

insertCount: 1

Counts the number of times the clipboard data has been inserted.

cells

cells: null

Holds the array of mxCells currently in the clipboard.

Functions

setCells

setCells: function(cells)

Sets the cells in the clipboard.  Fires a mxEvent.CHANGE event.

getCells

getCells: function()

Returns the cells in the clipboard.

isEmpty

isEmpty: function()

Returns true if the clipboard currently has not data stored.

cut

cut: function(graph,
cells)

Cuts the given array of mxCells from the specified graph.  If cells is null then the selection cells of the graph will be used.  Returns the cells that have been cut from the graph.

Parameters

graphmxGraph that contains the cells to be cut.
cellsOptional array of mxCells to be cut.

removeCells

removeCells: function(graph,
cells)

Hook to remove the given cells from the given graph after a cut operation.

Parameters

graphmxGraph that contains the cells to be cut.
cellsArray of mxCells to be cut.

copy

copy: function(graph,
cells)

Copies the given array of mxCells from the specified graph to cells.  Returns the original array of cells that has been cloned.  Descendants of cells in the array are ignored.

Parameters

graphmxGraph that contains the cells to be copied.
cellsOptional array of mxCells to be copied.

paste

paste: function(graph)

Pastes the cells into the specified graph restoring the relation to <parents>, if possible.  If the parents are no longer in the graph or invisible then the cells are added to the graph’s default or into the swimlane under the cell’s new location if one exists.  The cells are added to the graph using mxGraph.importCells and returned.

Parameters

graphmxGraph to paste the cells into.
STEPSIZE: 10
Defines the step size to offset the cells after each paste operation.
insertCount: 1
Counts the number of times the clipboard data has been inserted.
cells: null
Holds the array of mxCells currently in the clipboard.
Cells are the elements of the graph model.
setCells: function(cells)
Sets the cells in the clipboard.
getCells: function()
Returns the cells in the clipboard.
isEmpty: function()
Returns true if the clipboard currently has not data stored.
cut: function(graph,
cells)
Cuts the given array of mxCells from the specified graph.
removeCells: function(graph,
cells)
Hook to remove the given cells from the given graph after a cut operation.
copy: function(graph,
cells)
Copies the given array of mxCells from the specified graph to cells.
paste: function(graph)
Pastes the cells into the specified graph restoring the relation to parents, if possible.
mxGraph.prototype.canExportCell = function(cell)
Returns true if the given cell may be exported to the clipboard.
mxGraph.prototype.canImportCell = function(cell)
Returns true if the given cell may be imported from the clipboard.
CHANGE: 'change'
Specifies the event name for change.
Extends mxEventSource to implement a graph component for the browser.
mxGraph.prototype.importCells = function(cells,
dx,
dy,
target,
evt,
mapping)
Clones and inserts the given cells into the graph using the move method and returns the inserted cells.
Close