documentationfor yFiles for HTML 2.6

Deleting Items

The easiest way to control which items should be deleted is setting the deletableItems or deletablePredicate property as mentioned in the Deleting Items section. For example, the following code snippet will prevent nodes from being deleted when they still have edges connected to them:

// Prevent deletion of nodes that are connected to other nodes
graphEditorInputMode.deletablePredicate = (item) => {
  if (item instanceof INode) {
    if (graph.edgesAt(item).size !== 0) {
      return false
    }
  }
  return true
}
// Prevent deletion of nodes that are connected to other nodes
graphEditorInputMode.deletablePredicate = (item: IModelItem): boolean => {
  if (item instanceof INode) {
    if (graph.edgesAt(item).size !== 0) {
      return false
    }
  }
  return true
}

Alternatively to setting the deletablePredicate property you can also override the shouldDelete method from which you can also access other protected members of GraphEditorInputMode.

Note that the shouldDelete method in its default implementation delegates to the deletableItems and deletablePredicate properties. The latter is used to constrain the former.Overriding the method without taking into account those properties means that they are ignored for whether items are considered deletable. However, deletableItems is still used as a shortcut to decide whether the DELETE command should be enabled or not.

The deleteSelection method is called in response to the DELETE command and actually deletes the selected items after querying the shouldDelete method for every item. It also raises the events listed below. You can customize the entire process by overriding this method.

Related events
Event Occurs …​
DeletingSelection…​ before the items are deleted.
DeletedItem…​ for items that have been deleted. Note that this will not include items that are deleted implicitly, e.g. labels belonging to nodes when deleting a node, not even when those dependent items were in the selection to be deleted.
DeletedSelection…​ after the items have been deleted.

The shouldDelete method may be called more than once for the same item.