Skip to content

allow graph manipulation via callbacks #15

Description

@simontaurus

Draft

general notes:

  • each important action of the graph should have a callback
  • each callback should have a 'before' and 'after' version if that makes sense
  • each callback shoudl have a single object 'params' as parameter
  • 'params' should always provide a self reference to the graph
  • if a 'before' callback returns false, the action is aborted
  • lists of callbacks are configured with this.registerCallback({name, func}), unregisterCallback(...)

callbacks

  • onBefore/onAfterNodeCreated({this, newNode}): List of callbacks
  • onBefore/onAfterEdgeCreated({this, newNode}): List of callbacks
  • use Events (Single click, double click, ...): single callback
  • onAfterGraphChanged (called after all changes are applied, before the UI gets refreshed), provides the option to register multiple postprocessing steps: List of callbacks
class Graph...
defaultConfig = {
  callbacks: {
    onBeforeAddNode: [ (graph, node) => true ]
  }
}

registerCallback(params) {
  this.config.callbacks[params.name].push(params.func)
}

handleCallbacks(params) {
  let result = true; 
  if (!this.config.callbacks[params.id]) return true;
  for (const callback of this.config.callbacks[params.name]) {
    if (!callback(params.params)) {
      result = false
      break;
    }
  }
  return result;
}

addNode(node) {
  if (handleCallbacks({id: 'onBeforeAddNode', params: {graph: this, node: node}}) {
    //add the node
  }
  if (handleCallbacks({id: 'onAfterAddNode', params: {graph: this, node: node}})
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions