Search API

- Preparing search index...
- The search index is not available

Show Summaries
GoJS® Diagramming Components
version 1.8.35 for JavaScript/HTML
by Northwoods Software®

## Class UndoManager

UndoManager observes and records model and diagram changes in transactions and supports undo/redo operations. You will need to set the isEnabled property to true in order for users to perform an undo or a redo.

Typically an operation will call startTransaction, make some changes to the Model or Diagram, and then call commitTransaction. Any ChangedEvents that occur will be recorded in a Transaction object. If for some reason you do not wish to complete the transaction successfully, you can call rollbackTransaction instead of commitTransaction.

The history property is a list of Transactions. commitTransaction will add the currentTransaction to the history list. rollbackTransaction will undo the changes remembered in the currentTransaction and then discard it, without changing the history. You can limit how many transactions are remembered in the history by setting maxHistoryLength.

Transactions may be nested. Be sure to call either commitTransaction or rollbackTransaction for each call to startTransaction. Avoid repeated start-commit-start-commit calls as a result of a user's actions. Instead, start, make all changes, and then commit.

If you want to restore the diagram to the state before the latest complete transaction, call undo. Call redo to change the diagram to a later state. If after some number of undo's you start a transaction, all of the history after the current state is discarded, and a new transaction may be recorded. You cannot undo or redo during a transaction.

Initially each Model has its own UndoManager. UndoManagers may be shared by multiple Models by replacing the standard Model.undoManager created by the model constructor.

There are several informational properties:

- isInTransaction is true when a top-level transaction has been started that has not yet been committed or rolled-back.
- currentTransaction holds the flattened list of all ChangedEvents that have happened within the current transaction.
- transactionLevel indicates the current depth of nesting.
- nestedTransactionNames holds the stack of transaction names supplied to startTransaction calls.
- history holds only complete top-level transactions.
- isUndoingRedoing is true during a call to undo or redo.
- historyIndex indicates which Transaction in the history is the next to be "undone"; this is decremented by each undo and incremented by a redo.
- transactionToUndo and transactionToRedo indicate which Transaction may be undone or redone next, if any.
- models returns an iterator over all of the Models that this UndoManager is handling.

## Constructor Summary Details

Name | Description
--- | ---
UndoManager() | The constructor produces an empty UndoManager with no transaction history.

## Properties Summary Details

Name, Value Type | Description
--- | ---
currentTransaction {Transaction} | This read-only property returns the current Transaction for recording additional model change events.More... This is initialized and augmented by handleChanged before it is added to history by a top-level call to commitTransaction. The value will be null between transactions.
history {List.<Transaction>} | This read-only property returns the whole history, a list of all of the Transactions, each representing a transaction with some number of ChangedEvents.More... You should not modify this List.
historyIndex {number} | This read-only property returns the index into history for the current undoable Transaction.More... The value is -1 if there is no undoable Transaction to be undone.
isEnabled {boolean} | Gets or sets whether this UndoManager records any changes.More... The default value is false -- you need to set this to true if you want the user to be able to undo or redo. You can temporarily turn off recording by setting Diagram.skipsUndoManager and Model.skipsUndoManager to true.
isInTransaction {boolean} | This read-only property is true after the first call to startTransaction and before a corresponding call to commitTransaction or rollbackTransaction.More... During a transaction canUndo and canRedo will be false. currentTransaction may be non-null if any ChangedEvents were recorded.
isUndoingRedoing {boolean} | This read-only property is true during a call to undo or redo.

maxHistoryLength
{number}

Gets or sets the maximum number of transactions that this undo manager will remember.More... When a transaction is committed and the number exceeds this value, the UndoManager will discard the oldest transaction(s). The initial value is 999. A negative value is treated as if there were no limit.

This property is useful in helping limit the memory consumption of typical applications. But this does not limit the number of ChangedEvents that are recorded, because there may be an unlimited number of those within each Transaction. Decreasing this value will not necessarily remove any existing Transactions if there currently exist more in history than the new value permits.

models
{Iterator.<Model>}

This read-only property returns an iterator for all of the Models that this UndoManager is handling.More...

See also:

- addModel
- removeModel

nestedTransactionNames {List.<string>} | This read-only property returns a stack of ongoing transaction names.More... The outermost transaction name will be the first item in the list. The last one will be the name of the most recent (nested) call to startTransaction. You should not modify this List.

transactionLevel
{number}

This read-only property returns the current transaction level.More... The value is zero when there is no ongoing transaction. The initial value is zero. startTransaction will increment this value; commitTransaction or rollbackTransaction will decrement it. When this value is greater than zero, canUndo and canRedo will be false, because additional logically related model change events may occur.

transactionToRedo
{Transaction}

This read-only property returns the Transaction in the history to be redone next.More... The value may be null if the UndoManager is not ready to perform a redo.

See also:

- transactionToUndo

transactionToUndo
{Transaction}

This read-only property returns the Transaction in the history to be undone next.More... The value may be null if the UndoManager is not ready to perform an undo.

See also:

- transactionToRedo

## Method Summary Details

Name, Return Type | Description
--- | ---

addModel(model)

Make sure this UndoManager knows about a Model for which it may receive ChangedEvents when the given Model is changed.More... The model will also receive notifications about transactions and undo or redo operations.

You should not call this method during a transaction.

See also:

- models
- removeModel

Parameters:
{Model} model
  A Model that this UndoManager is managing.

canRedo()
{boolean}

This predicate returns true if you can call redo.More... This will return false if isEnabled is false (as it is by default), if any transaction is ongoing, or if there is no transactionToRedo that can be redone.

Returns:
{boolean} true if ready for redo to be called.

canUndo()
{boolean}

This predicate returns true if you can call undo.More... This will return false if isEnabled is false (as it is by default), if any transaction is ongoing, or if there is no transactionToUndo that can be undone.

Returns:
{boolean} true if ready for undo to be called.

clear() | Clear all of the Transactions and clear all other state, including any ongoing transaction without rolling back.More... However, this maintains its references to its Models. You should not call this method during a transaction.

commitTransaction(tname)
{boolean}

Commit the current transaction started by a call to startTransaction.More...

For convenience, this method is called by Model.commitTransaction and Diagram.commitTransaction.

If this call stops a top-level transaction, we mark the currentTransaction as complete (Transaction.isComplete), we add the Transaction to the history list, and we return true. Committing a transaction when there have been some undos without corresponding redos will throw away the Transactions holding changes that happened after the current state, before adding the new Transaction to the history list.

See also:

- startTransaction
- rollbackTransaction

Parameters:
{string=} tname
  a short string describing the transaction; this is recorded as the Transaction.name and need not be the same as the string passed to startTransaction. If the value is an empty string or not supplied, this will use the name given to startTransaction.

Returns:
{boolean} true if ending a top-level transaction.

handleChanged(e)

Maybe record a ChangedEvent in the currentTransaction.More... This calls skipsEvent to see if this should ignore the change. If skipsEvent returns false, this creates a copy of the ChangedEvent and adds it to the currentTransaction. If there is no currentTransaction, this first creates and remembers it.

This method always ignores all changes while performing an undo or redo. This method is also a no-op if isEnabled is false.

Parameters:
{ChangedEvent} e
  a ChangedEvent.

redo()

After an undo, re-perform the changes in transactionToRedo.More... canRedo must be true for this method to have any effect.

This is called by CommandHandler.redo.

This will raise a "StartingRedo" ChangedEvent of type ChangedEvent.Transaction, perform the Transaction.redo on the transactionToRedo, and then raise a "FinishedRedo" ChangedEvent of type ChangedEvent.Transaction. The two ChangedEvents are to let model listeners know that a redo is about to take place and that it just finished. isUndoingRedoing will temporarily be set to true during this operation.

removeModel(model)

Inform this UndoManager that it will no longer be receiving ChangedEvents when the given Model is changed.More... The model will no longer receive notifications about transactions and undo or redo operations.

You should not call this method during a transaction. If you call this method between transactions when there is a transaction history, you should be careful that there are no ChangedEvents referring to that model in any Transactions.

See also:

- models
- addModel

Parameters:
{Model} model
  A Model that this UndoManager should no longer manage.

rollbackTransaction()
{boolean}

Rollback the current transaction started by a call to startTransaction, undoing any changes.More...

For convenience, this method is called by Model.rollbackTransaction and Diagram.rollbackTransaction.

This undoes and then discards the changes in the currentTransaction. You must have started a transaction previously.

See also:

- startTransaction
- commitTransaction

Returns:
{boolean} true if ending a top-level transaction.

skipsEvent(e)
{boolean}

This predicate is called by handleChanged to decide if a ChangedEvent is not interesting enough to be remembered.More...

Transactional events (of change type ChangedEvent.Transaction) are always skipped. Changed events for GraphObjects that are in Layer.isTemporary layers are also skipped.

Sometimes changed events do not even get to handleChanged because Model.skipsUndoManager or Diagram.skipsUndoManager is true.

Parameters:
{ChangedEvent} e
  the ChangedEvent received by handleChanged.

Returns:
{boolean} true to not record the change.

startTransaction(tname)
{boolean}

Begin a transaction, where the changes are held by a Transaction object as the value of currentTransaction.More... You must call either commitTransaction or rollbackTransaction afterwards.

For convenience, this method is called by Model.startTransaction and Diagram.startTransaction.

Transactions can be nested. Starting or ending a nested transaction will return false. Nested transactions will share the same Transaction list of ChangedEvents.

Starting a transaction will not necessarily cause currentTransaction to be non-null. A Transaction object is usually only created by handleChanged when a ChangedEvent first occurs.

See also:

- commitTransaction
- rollbackTransaction

Parameters:
{string=} tname
  a short string describing the transaction, pushed onto the nestedTransactionNames stack.

Returns:
{boolean} true if starting a top-level transaction.

undo()

Reverse the effects of the transactionToUndo.More... canUndo must be true for this method to have any effect.

This is called by CommandHandler.undo.

This will raise a "StartingUndo" ChangedEvent of type ChangedEvent.Transaction, perform the Transaction.undo on the transactionToUndo, and then raise a "FinishedUndo" ChangedEvent of type ChangedEvent.Transaction. The two ChangedEvents are to let model listeners know that an undo is about to take place and that it just finished. isUndoingRedoing will temporarily be set to true during this operation.