Search code examples
gojs

Gojs PanelExpanderButton custom icon


In Gojs, Is it possible to give a custom icon? In my case, I wanted to use an SVG/PNG image as PanelExpanderButton icon.

go.GraphObject.defineBuilder("PanelExpanderButton", function(args) {
      var eltname = /** @type {string} */ (go.GraphObject.takeBuilderArgument(args, "COLLAPSIBLE"));

      var button = /** @type {Panel} */ (
        $("Button",
          { // set these values for the visible binding conversion
            "_buttonExpandedFigure": $(go.Picture, {
                // desiredSize: new go.Size(10, 10),
                source: "assets/images/expand.png"
              },
            ),
            "_buttonCollapsedFigure": $(go.Picture, {
                // desiredSize: new go.Size(10, 10),
                source: "assets/images/collapse.png"
              },
            )
          },
          $(go.Picture, { source: "assets/images/expand.png", desiredSize: new go.Size(10, 10) },
            new go.Binding("figure", "visible", function(vis) { return vis ? button["_buttonExpandedFigure"] : button["_buttonCollapsedFigure"]; })
            .ofObject(eltname)
          )
        )
      );

      var border = button.findObject("ButtonBorder");
      if (border instanceof go.Shape) {
        border.stroke = null;
        border.fill = "transparent";
      }

      button.click = function(e, button) {
        var diagram = button.diagram;
        if (diagram === null) return;
        if (diagram.isReadOnly) return;
        var elt = button.findTemplateBinder();
        if (elt === null) elt = button.part;
        if (elt !== null) {
          var pan = elt.findObject(eltname);
          if (pan !== null) {
            diagram.startTransaction('Collapse/Expand Panel');
            pan.visible = !pan.visible;
            diagram.commitTransaction('Collapse/Expand Panel');
          }
        }
      }

      return button;
    });

I'm not able to change the icon when the panel is expanded/collapsed

Here I'm trying to use the picture but not successful

Happy coding!!!


Solution

  • "Figures" must be one of the names defined by Shape.defineFigureGenerator, including all of those figure names predefined in the library. So you cannot use a Picture that way.

    But you could copy and adapt the definition of the "PanelExpanderButton", which is provided at https://gojs.net/latest/extensions/Buttons.js. That way you can size it and provide arbitrary Shapes for its states. If you want to use SVG, please be aware of platform-specific limitations: https://gojs.net/latest/intro/pictures.html#UsingSVGAsPictureSource