Options
All
  • Public
  • Public/Protected
  • All
Menu

Extends mxEventSource to implement a graph model. The graph model acts as a wrapper around the cells which are in charge of storing the actual graph datastructure. The model acts as a transactional wrapper with event notification for all changes, whereas the cells contain the atomic operations for updating the actual datastructure.

Layers

The cell hierarchy in the model must have a top-level root cell which contains the layers (typically one default layer), which in turn contain the top-level cells of the layers. This means each cell is contained in a layer. If no layers are required, then all new cells should be added to the default layer.

Layers are useful for hiding and showing groups of cells, or for placing groups of cells on top of other cells in the display. To identify a layer, the isLayer function is used. It returns true if the parent of the given cell is the root of the model.

Events

See events section for more details. There is a new set of events for tracking transactional changes as they happen. The events are called startEdit for the initial beginUpdate, executed for each executed change and endEdit for the terminal endUpdate. The executed event contains a property called change which represents the change after execution.

Encoding the model

To encode a graph model, use the following code:

var enc = new mxCodec();
var node = enc.encode(graph.getModel());

This will create an XML node that contains all the model information.

Encoding and decoding changes:

For the encoding of changes, a graph model listener is required that encodes each change from the given array of changes.

model.addListener(mxEvent.CHANGE, function(sender, evt)
{
  var changes = evt.getProperty('edit').changes;
  var nodes = [];
  var codec = new mxCodec();

  for (var i = 0; i < changes.length; i++)
  {
    nodes.push(codec.encode(changes[i]));
  }
  // do something with the nodes
});

For the decoding and execution of changes, the codec needs a lookup function that allows it to resolve cell IDs as follows:

var codec = new mxCodec();
codec.lookup(id)
{
  return model.getCell(id);
}

For each encoded change (represented by a node), the following code can be used to carry out the decoding and create a change object.

var changes = [];
var change = codec.decode(node);
change.model = model;
change.execute();
changes.push(change);

The changes can then be dispatched using the model as follows.

var edit = new mxUndoableEdit(model, false);
edit.changes = changes;

edit.notify()
{
  edit.source.fireEvent(new mxEventObject(mxEvent.CHANGE,
      'edit', edit, 'changes', edit.changes));
  edit.source.fireEvent(new mxEventObject(mxEvent.NOTIFY,
      'edit', edit, 'changes', edit.changes));
}

model.fireEvent(new mxEventObject(mxEvent.UNDO, 'edit', edit));
model.fireEvent(new mxEventObject(mxEvent.CHANGE,
   'edit', edit, 'changes', changes));

Event: mxEvent.CHANGE

Fires when an undoable edit is dispatched. The edit property contains the mxUndoableEdit. The changes property contains the array of atomic changes inside the undoable edit. The changes property is deprecated, please use edit.changes instead.

Example

For finding newly inserted cells, the following code can be used:

graph.model.addListener(mxEvent.CHANGE, function(sender, evt)
{
  var changes = evt.getProperty('edit').changes;

  for (var i = 0; i < changes.length; i++)
  {
    var change = changes[i];

    if (change instanceof mxChildChange &&
      change.change.previous == null)
    {
      graph.startEditingAtCell(change.child);
      break;
    }
  }
});

Event: mxEvent.NOTIFY

Same as <mxEvent.CHANGE>, this event can be used for classes that need to implement a sync mechanism between this model and, say, a remote model. In such a setup, only local changes should trigger a notify event and all changes should trigger a change event.

Event: mxEvent.EXECUTE

Fires between begin- and endUpdate and after an atomic change was executed in the model. The change property contains the atomic change that was executed.

Event: mxEvent.EXECUTED

Fires between START_EDIT and END_EDIT after an atomic change was executed. The change property contains the change that was executed.

Event: mxEvent.BEGIN_UPDATE

Fires after the updateLevel was incremented in beginUpdate. This event contains no properties.

Event: mxEvent.START_EDIT

Fires after the updateLevel was changed from 0 to 1. This event contains no properties.

Event: mxEvent.END_UPDATE

Fires after the updateLevel was decreased in endUpdate but before any notification or change dispatching. The edit property contains the currentEdit.

Event: mxEvent.END_EDIT

Fires after the updateLevel was changed from 1 to 0. This event contains no properties.

Event: mxEvent.BEFORE_UNDO

Fires before the change is dispatched after the update level has reached 0 in endUpdate. The edit property contains the {@link curreneEdit}.

Event: mxEvent.UNDO

Fires after the change was dispatched in endUpdate. The edit property contains the currentEdit.

Hierarchy

Index

Constructors

constructor

Properties

cells

cells: any

Maps from Ids to cells.

createIds

createIds: boolean

Specifies if the model should automatically create Ids for new cells. Default is true.

currentEdit

currentEdit: any

Holds the changes for the current transaction. If the transaction is closed then a new object is created for this variable using createUndoableEdit.

endingUpdate

endingUpdate: boolean

True if the program flow is currently inside endUpdate.

eventListeners

eventListeners: any[]

Variable: eventListeners

Holds the event names and associated listeners in an array. The array contains the event name followed by the respective listener for each registered listener.

eventSource

eventSource: any

Variable: eventSource

Optional source for events. Default is null.

eventsEnabled

eventsEnabled: boolean

Variable: eventsEnabled

Specifies if events can be fired. Default is true.

ignoreRelativeEdgeParent

ignoreRelativeEdgeParent: boolean

Specifies if relative edge parents should be ignored for finding the nearest common ancestors of an edge's terminals. Default is true.

maintainEdgeParent

maintainEdgeParent: boolean

Specifies if edges should automatically be moved into the nearest common ancestor of their terminals. Default is true.

nextId

nextId: string | number

Specifies the next Id to be created. Initial value is 0.

postfix

postfix: string

Defines the postfix of new Ids. Default is an empty string.

prefix

prefix: string

Defines the prefix of new Ids. Default is an empty string.

root

root: mxCell

Holds the root cell, which in turn contains the cells that represent the layers of the diagram as child cells. That is, the actual elements of the diagram are supposed to live in the third generation of cells and below.

updateLevel

updateLevel: number

Counter for the depth of nested transactions. Each call to beginUpdate will increment this number and each call to endUpdate will decrement it. When the counter reaches 0, the transaction is closed and the respective events are fired. Initial value is 0.

Methods

add

  • Adds the specified child to the parent at the given index using mxChildChange and adds the change to the current transaction. If no index is specified then the child is appended to the parent's array of children. Returns the inserted child.

    Parameters

    • parent: mxCell

      that specifies the parent to contain the child.

    • child: mxCell

      that specifies the child to be inserted.

    • Optional index: number

      Optional integer that specifies the index of the child.

    Returns mxCell

addListener

  • addListener(name: string, funct: (...args: any[]) => any): void
  • Function: addListener

    Binds the specified function to the given event name. If no event name is given, then the listener is registered for all events.

    The parameters of the listener are the sender and an .

    Parameters

    • name: string
    • funct: (...args: any[]) => any
        • (...args: any[]): any
        • Parameters

          • Rest ...args: any[]

          Returns any

    Returns void

beginUpdate

  • beginUpdate(): void
  • Increments the updateLevel by one. The event notification is queued until updateLevel reaches 0 by use of endUpdate.

    All changes on mxGraphModel are transactional, that is, they are executed in a single undoable change on the model (without transaction isolation). Therefore, if you want to combine any number of changes into a single undoable change, you should group any two or more API calls that modify the graph model between beginUpdate and endUpdate calls as shown here:

    var model = graph.getModel();
    var parent = graph.getDefaultParent();
    var index = model.getChildCount(parent);
    model.beginUpdate();
    try
    {
      model.add(parent, v1, index);
      model.add(parent, v2, index+1);
    }
    finally
    {
      model.endUpdate();
    }
    

    Of course there is a shortcut for appending a sequence of cells into the default parent:

    graph.addCells([v1, v2]).
    

    Returns void

cellAdded

  • cellAdded(cell: mxCell): void
  • Inner callback to update cells when a cell has been added. This implementation resolves collisions by creating new Ids. To change the ID of a cell after it was inserted into the model, use the following code:

    (code delete model.cells[cell.getId()]; cell.setId(newId); model.cells[cell.getId()] = cell;

    
    If the change of the ID should be part of the command history, then the
    cell should be removed from the model and a clone with the new ID should
    be reinserted into the model instead.
    
    @param {mxCell} cell  that specifies the cell that has been added.
    

    Parameters

    Returns void

cellCloned

cellRemoved

  • cellRemoved(cell: mxCell): void

clear

  • clear(): void

cloneCell

  • Returns a deep clone of the given mxCell (including the children) which is created using <a href="mxgraphmodel.html#clonecells">cloneCells</a>.

    Parameters

    Returns mxCell

cloneCellImpl

  • cloneCellImpl(cell: mxCell, mapping?: any, includeChildren?: boolean): mxCell

cloneCells

  • cloneCells(cells: mxCell[], includeChildren?: boolean, mapping?: any): mxCell[]
  • Returns an array of clones for the given array of mxCell`. Depending on the value of includeChildren, a deep clone is created for each cell. Connections are restored based if the corresponding cell is contained in the passed in array.

    Parameters

    • cells: mxCell[]

      Array of mxCell` to be cloned.

    • Optional includeChildren: boolean

      Boolean indicating if the cells should be cloned with all descendants.

    • Optional mapping: any

      Optional mapping for existing clones.

    Returns mxCell[]

collapsedStateForCellChanged

  • collapsedStateForCellChanged(cell: mxCell, collapsed: boolean): boolean
  • Inner callback to update the collapsed state of the given mxCell using <mxCell.setCollapsed> and return the previous collapsed state.

    Parameters

    • cell: mxCell

      that specifies the cell to be updated.

    • collapsed: boolean

      Boolean that specifies the new collpased state.

    Returns boolean

contains

  • contains(cell: mxCell): boolean

createId

  • createId(cell: mxCell): string
  • Hook method to create an Id for the specified cell. This implementation concatenates prefix, id and postfix to create the Id and increments nextId. The cell is ignored by this implementation, but can be used in overridden methods to prefix the Ids with eg. the cell type.

    Parameters

    • cell: mxCell

      to create the Id for.

    Returns string

createRoot

createUndoableEdit

endUpdate

  • endUpdate(): void

execute

  • execute(change: any): void
  • Executes the given edit and fires events if required. The edit object requires an execute function which is invoked. The edit is added to the currentEdit between beginUpdate and endUpdate calls, so that events will be fired if this execute is an individual transaction, that is, if no previous beginUpdate calls have been made without calling endUpdate. This implementation fires an execute event before executing the given change.

    Parameters

    • change: any

      Object that described the change.

    Returns void

filterCells

  • filterCells(cells: mxCell[], filter: (...args: any) => boolean): mxCell[]
  • Returns the cells from the given array where the given filter function returns true.

    Parameters

    • cells: mxCell[]
    • filter: (...args: any) => boolean
        • (...args: any): boolean
        • Parameters

          • Rest ...args: any

          Returns boolean

    Returns mxCell[]

filterDescendants

  • filterDescendants(filter: (...args: any) => boolean, parent?: mxCell): mxCell[]
  • Visits all cells recursively and applies the specified filter function to each cell. If the function returns true then the cell is added to the resulting array. The parent and result paramters are optional. If parent is not specified then the recursion starts at root.

    Example: The following example extracts all vertices from a given model:

    var filter(cell)
    {
        return model.isVertex(cell);
    }
    var vertices = model.filterDescendants(filter);
    

    Parameters

    • filter: (...args: any) => boolean

      JavaScript function that takes an mxCell as an argument and returns a boolean.

        • (...args: any): boolean
        • Parameters

          • Rest ...args: any

          Returns boolean

    • Optional parent: mxCell

      Optional mxCell that is used as the root of the recursion.

    Returns mxCell[]

fireEvent

  • Function: fireEvent

    Dispatches the given event to the listeners which are registered for the event. The sender argument is optional. The current execution scope ("this") is used for the listener invocation (see <mxUtils.bind>).

    Example:

    (code) fireEvent(new mxEventObject("eventName", key1, val1, .., keyN, valN)) (end)

    Parameters:

    evt - that represents the event. sender - Optional sender to be passed to the listener. Default value is the return value of .

    Parameters

    Returns void

geometryForCellChanged

getCell

  • Returns the mxCell for the specified Id or null if no cell can be found for the given Id.

    Parameters

    • id: string

      A string representing the Id of the cell.

    Returns mxCell

getChildAt

  • Returns the child of the given mxCell at the given index.

    Parameters

    • cell: mxCell

      that represents the parent.

    • index: number

      Integer that specifies the index of the child to be returned.

    Returns mxCell

getChildCells

  • getChildCells(parent: mxCell, vertices: boolean, edges: boolean): mxCell[]
  • Returns the children of the given cell that are vertices and/or edges depending on the arguments.

    Parameters

    • parent: mxCell
    • vertices: boolean

      Boolean indicating if child vertices should be returned. Default is false.

    • edges: boolean

      Boolean indicating if child edges should be returned. Default is false.

    Returns mxCell[]

getChildCount

  • getChildCount(cell?: mxCell): number
  • Returns the number of children in the given cell.

    Parameters

    • Optional cell: mxCell

      whose number of children should be returned.

    Returns number

getChildEdges

getChildVertices

getChildren

getConnections

getDescendants

  • Returns all descendants of the given cell and the cell itself in an array.

    Parameters

    • parent: mxCell

      whose descendants should be returned.

    Returns mxCell[]

getDirectedEdgeCount

  • getDirectedEdgeCount(cell: mxCell, outgoing: boolean, ignoredEdge: boolean): number
  • Returns the number of incoming or outgoing edges, ignoring the given edge.

    Parameters

    • cell: mxCell

      whose edge count should be returned.

    • outgoing: boolean

      Boolean that specifies if the number of outgoing or incoming edges should be returned.

    • ignoredEdge: boolean

      that represents an edge to be ignored.

    Returns number

getEdgeAt

  • Returns the edge of cell at the given index.

    Parameters

    • cell: mxCell

      that specifies the vertex.

    • index: number

      Integer that specifies the index of the edge to return.

    Returns mxCell

getEdgeCount

  • getEdgeCount(cell: mxCell): number
  • Returns the number of distinct edges connected to the given cell.

    Parameters

    • cell: mxCell

      that represents the vertex.

    Returns number

getEdges

  • getEdges(cell: mxCell, incoming?: boolean, outgoing?: boolean, includeLoops?: boolean): mxCell[]
  • Returns all distinct edges connected to this cell as a new array of mxCell. If at least one of incoming or outgoing is true, then loops are ignored, otherwise if both are false, then all edges connected to the given cell are returned including loops.

    Parameters

    • cell: mxCell

      that specifies the cell.

    • Optional incoming: boolean

      Optional boolean that specifies if incoming edges should be returned. Default is true.

    • Optional outgoing: boolean

      Optional boolean that specifies if outgoing edges should be returned. Default is true.

    • Optional includeLoops: boolean

      Optional boolean that specifies if loops should be returned. Default is true.

    Returns mxCell[]

getEdgesBetween

  • Returns all edges between the given source and target pair. If directed is true, then only edges from the source to the target are returned, otherwise, all edges between the two cells are returned.

    Parameters

    • source: mxCell

      that defines the source terminal of the edge to be returned.

    • target: mxCell

      that defines the target terminal of the edge to be returned.

    • Optional directed: boolean

      Optional boolean that specifies if the direction of the edge should be taken into account. Default is false.

    Returns mxCell[]

getEventSource

  • getEventSource(): any

getGeometry

getIncomingEdges

getNearestCommonAncestor

  • Returns the nearest common ancestor for the specified cells.

    Parameters

    • cell1: mxCell

      that specifies the first cell in the tree.

    • cell2: mxCell

      that specifies the second cell in the tree.

    Returns mxCell

getOpposites

  • getOpposites(edges: mxCell[], terminal: mxCell, sources?: boolean, targets?: boolean): mxCell[]
  • Returns all opposite vertices wrt terminal for the given edges, only returning sources and/or targets as specified. The result is returned as an array of mxCell.

    Parameters

    • edges: mxCell[]

      Array of mxCell that contain the edges to be examined.

    • terminal: mxCell

      that specifies the known end of the edges.

    • Optional sources: boolean

      Boolean that specifies if source terminals should be contained in the result. Default is true.

    • Optional targets: boolean

      Boolean that specifies if target terminals should be contained in the result. Default is true.

    Returns mxCell[]

getOrigin

getOutgoingEdges

getParent

getParents

  • Returns an array that represents the set (no duplicates) of all parents for the given array of cells.

    Parameters

    • cells: mxCell[]

      Array of cells whose parents should be returned.

    Returns mxCell[]

getRoot

getStyle

  • getStyle(cell: mxCell): null | string

getTerminal

  • Returns the source or target mxCell of the given edge depending on the value of the boolean parameter.

    Parameters

    • edge: mxCell

      that specifies the edge.

    • isSource: boolean

      Boolean indicating which end of the edge should be returned.

    Returns mxCell

getTopmostCells

  • Returns the topmost cells of the hierarchy in an array that contains no descendants for each mxCell that it contains. Duplicates should be removed in the cells array to improve performance.

    Parameters

    • cells: mxCell[]

      Array of mxCell whose topmost ancestors should be returned.

    Returns mxCell[]

getValue

isAncestor

  • Returns true if the given parent is an ancestor of the given child. Note returns true if child == parent.

    Parameters

    • parent: mxCell

      that specifies the parent.

    • child: mxCell

      that specifies the child.

    Returns boolean

isCollapsed

  • isCollapsed(cell: mxCell): boolean

isConnectable

  • isConnectable(cell: mxCell): boolean
  • Returns true if the given mxCell is connectable. If {@link edgesConnectable} is false, then this function returns false for all edges else it returns the return value of <mxCell.isConnectable>.

    Parameters

    • cell: mxCell

      whose connectable state should be returned.

    Returns boolean

isCreateIds

  • isCreateIds(): boolean

isEdge

  • isEdge(cell: mxCell): boolean

isEventsEnabled

  • isEventsEnabled(): boolean

isLayer

  • isLayer(cell: mxCell): boolean

isRoot

  • isRoot(cell: mxCell): boolean
  • Returns true if the given cell is the root of the model and a non-null value.

    Parameters

    • cell: mxCell

      that represents the possible root.

    Returns boolean

isVertex

  • isVertex(cell: mxCell): boolean

isVisible

  • isVisible(cell: mxCell): boolean

mergeChildren

  • Merges the children of the given cell into the given target cell inside this model. All cells are cloned unless there is a corresponding cell in the model with the same id, in which case the source cell is ignored and all edges are connected to the corresponding cell in this model. Edges are considered to have no identity and are always cloned unless the cloneAllEdges flag is set to false, in which case edges with the same id in the target model are reconnected to reflect the terminals of the source edges.

    Parameters

    Returns void

mergeChildrenImpl

  • Clones the children of the source cell into the given target cell in this model and adds an entry to the mapping that maps from the source cell to the target cell with the same id or the clone of the source cell that was inserted into this model.

    Parameters

    Returns void

parentForCellChanged

  • Inner callback to update the parent of a cell using <mxCell.insert> on the parent and return the previous parent.

    Parameters

    • cell: mxCell

      to update the parent for.

    • parent: mxCell

      that specifies the new parent of the cell.

    • index: number

      Optional integer that defines the index of the child in the parent's child array.

    Returns mxCell

remove

  • Removes the specified cell from the model using mxChildChange and adds the change to the current transaction. This operation will remove the cell and all of its children from the model. Returns the removed cell.

    Parameters

    • cell: mxCell

      that should be removed.

    Returns mxCell

removeListener

  • removeListener(funct: (...args: any[]) => any): void
  • Function: removeListener

    Removes all occurrences of the given listener from .

    Parameters

    • funct: (...args: any[]) => any
        • (...args: any[]): any
        • Parameters

          • Rest ...args: any[]

          Returns any

    Returns void

restoreClone

  • restoreClone(clone: mxCell, cell: mxCell, mapping?: any): void

rootChanged

setCollapsed

  • setCollapsed(cell: mxCell, collapsed: boolean): boolean
  • Sets the collapsed state of the given mxCell using mxCollapseChange and adds the change to the current transaction.

    Parameters

    • cell: mxCell

      whose collapsed state should be changed.

    • collapsed: boolean

      Boolean that specifies the new collpased state.

    Returns boolean

setCreateIds

  • setCreateIds(value: boolean): void

setEventSource

  • setEventSource(value: any): void

setEventsEnabled

  • setEventsEnabled(value: boolean): void

setGeometry

setRoot

  • Sets the root of the model using mxRootChange and adds the change to the current transaction. This resets all datastructures in the model and is the preferred way of clearing an existing model. Returns the new root.

    Example:

    var root = new mxCell();
    root.insert(new mxCell());
    model.setRoot(root);
    

    Parameters

    • root: mxCell

      that specifies the new root.

    Returns mxCell

setStyle

  • setStyle(cell: mxCell, style: string): string
  • Sets the style of the given mxCell using mxStyleChange and adds the change to the current transaction.

    Parameters

    • cell: mxCell

      whose style should be changed.

    • style: string

      String of the form [stylename;|key=value;] to specify the new cell style.

    Returns string

setTerminal

  • Sets the source or target terminal of the given mxCell using mxTerminalChange and adds the change to the current transaction. This implementation updates the parent of the edge using updateEdgeParent if required.

    Parameters

    • edge: mxCell

      that specifies the edge.

    • terminal: mxCell

      that specifies the new terminal.

    • isSource: boolean

      Boolean indicating if the terminal is the new source or target terminal of the edge.

    Returns mxCell

setTerminals

setValue

  • setValue(cell: mxCell, value: any): any
  • Sets the user object of then given mxCell using mxValueChange and adds the change to the current transaction.

    Parameters

    • cell: mxCell

      whose user object should be changed.

    • value: any

      Object that defines the new user object.

    Returns any

setVisible

  • setVisible(cell: mxCell, visible: boolean): boolean
  • Sets the visible state of the given mxCell using mxVisibleChange and adds the change to the current transaction.

    Parameters

    • cell: mxCell

      whose visible state should be changed.

    • visible: boolean

      Boolean that specifies the new visible state.

    Returns boolean

styleForCellChanged

  • styleForCellChanged(cell: mxCell, style: string): string
  • Inner callback to update the style of the given mxCell using <mxCell.setStyle> and return the previous style.

    Parameters

    • cell: mxCell

      that specifies the cell to be updated.

    • style: string

      String of the form [stylename;|key=value;] to specify the new cell style.

    Returns string

terminalForCellChanged

  • Inner helper function to update the terminal of the edge using <mxCell.insertEdge> and return the previous terminal.

    Parameters

    • edge: mxCell

      that specifies the edge to be updated.

    • terminal: mxCell

      that specifies the new terminal.

    • isSource: boolean

      Boolean indicating if the terminal is the new source or target terminal of the edge.

    Returns mxCell

updateEdgeParent

  • Inner callback to update the parent of the specified mxCell to the nearest-common-ancestor of its two terminals.

    Parameters

    • edge: mxCell

      that specifies the edge.

    • root: mxCell

      that represents the current root of the model.

    Returns void

updateEdgeParents

valueForCellChanged

  • valueForCellChanged(cell: mxCell, value: any): any
  • Inner callback to update the user object of the given mxCell using <mxCell.valueChanged> and return the previous value, that is, the return value of <mxCell.valueChanged>.

    To change a specific attribute in an XML node, the following code can be used.

    graph.getModel().valueForCellChanged(cell, value)
    {
      var previous = cell.value.getAttribute('label');
      cell.value.setAttribute('label', value);
    
      return previous;
    };
    

    Parameters

    Returns any

visibleStateForCellChanged

  • visibleStateForCellChanged(cell: mxCell, visible: boolean): boolean
  • Inner callback to update the visible state of the given mxCell using <mxCell.setCollapsed> and return the previous visible state.

    Parameters

    • cell: mxCell

      that specifies the cell to be updated.

    • visible: boolean

      Boolean that specifies the new visible state.

    Returns boolean

Generated using TypeDoc