documentationfor yFiles for HTML 3.0.0.1

Deleting Items

The easiest way to control which items can be deleted is by setting the deletableItems or deletablePredicate property, as mentioned in the Deleting Items section. For example, the following code snippet prevents 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
}

Note that the default deletablePredicate delegates to the deletableItems.Setting a custom deletablePredicate without considering the deletableItems means that the latter is ignored when determining whether items can be deleted. However, deletableItems is still used as a shortcut to decide whether the DELETE command should be enabled.

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

Related events
Event Occurs …​
deleting-selection…​ before the items are deleted.
deleted-item…​ for items that have been deleted. Note that this will not include items that are deleted implicitly. For example, labels belonging to nodes are deleted when deleting a node, even if those dependent items were in the selection to be deleted.
deleted-selection…​ after the items have been deleted.

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