theia-ide / sprotty

A next-gen web-based graphics framework
Apache License 2.0
138 stars 23 forks source link

How to implement onclick event on any node or edge? #240

Open Dipinrajc opened 5 years ago

JanKoehnlein commented 5 years ago

Usually, you have to create a mouse listener, implement the method for the event and then bind it as TYPES.IMouseListener in your di.config.ts. The actual consequence of the click usually is an action. If the SModel should be changed, create and register a command handling the action.

Dipinrajc commented 5 years ago

Could please provide an example for the same?

JanKoehnlein commented 5 years ago

Could you be more specific about what you're trying to achieve? A mouse click is usually already consumed as a select action. Maybe you're rather interested in selection changes? Or you want to add some button like behavior?

Dipinrajc commented 5 years ago

I need to show a modal/popup while clicking on an edge.

JanKoehnlein commented 5 years ago

I'd try to add an action handler for SelectAction (caution: unchecked code)

export class MyActionHandler implements IActionHandler {
  handleAction(Action a) {
     if(a instanceof SelectAction) {
       // investigate if a single edge is selected and in this case open the dialog
     }
  }
}

export MyActionHandlerInitializer implements IActionHandlerInitializer {
    initialize(registry: ActionHandlerRegistry) {
       registry.register(SelectAction.KIND, new MyActionHandler())
    }
}

In your di.config.ts

bind(TYPES.IActionHandlerInitializer).to(MyActionHandlerIntializer)
Dipinrajc commented 5 years ago

OK, Thanks.

JanKoehnlein commented 5 years ago

If you need to access the model as well, then I'd rather bind a custom model source, have a look at the register and handle methods in https://github.com/TypeFox/theia-xtext-sprotty-example/blob/master/theia/states-dsl/src/browser/diagram/states-diagram-server.ts

Dipinrajc commented 5 years ago

Ok