Search code examples
javascriptgraphiti-js

How to add click event on rectangle in Graphiti.js


I want to register click event on rectangle in graphiti. i tried this

var innerrect = new graphiti.shape.basic.Rectangle(100, 20);
        innerrect.onClick = function(){
            alert("Hi");
        }
rect.addFigure(innerrect , new graphiti.layout.locator.BottomLocator(rect));
canvas.addFigure(rect, 100, 100);   

but wont work. plz let me know abut it?


Solution

  • create your own shape inherited from Rectangle like this. This looks at the first moment a little bit unconfortable, but normaly you write your own shapes with a lot of custom code. In this case it is a one time effort to crate a new class.

    Keep in mind that the examples are a good starting point. There is a an examples with a click event listener as well.

    MyFigure = graphiti.shape.basic.Rectangle.extend({
    
    NAME : "MyFigure",
    
    init : function()
    {
        this._super();
    
        this.createPort("output");
        this.setDimension(30,30);
        this.setResizeable(false);
    
        this.value=false;
    
        this.colors={};
        this.colors[true]="#00f000";
        this.colors[false]="#f00000";
    
        this.setBackgroundColor(this.colors[this.value]);
    },
    
    /**
     * @method
     * Change the color and the internal value of the figure.
     * Post the new value to related input ports.
     * 
     */
    onClick: function(){
        this.value = !this.value;
        this.setBackgroundColor(this.colors[this.value]);
    
        var connections = this.getOutputPort(0).getConnections();
        connections.each($.proxy(function(i, conn){
            var targetPort = conn.getTarget();
            targetPort.setValue(this.value);
            conn.setColor(this.getBackgroundColor());
        },this));
    }
    
    });