Extending DrawCommandHandler without defining a subclass

I need to extend a function defined in my DrawCommandHandler and that works fine if I’m overriding it but how can I actually extend it. Now being forced to duplicate.

mainDiagram.commandHandler._getGroupNodes = function(group) {
   //this next line doesn't work
   var allParts = DrawCommandHandler.prototype._getGroupNodes.call(this, group);
   var linkIt = group.findLinksConnected();

   while (linkIt.next()) {
        ... add some links as grouped nodes
    }
   return allParts;
};

So you want to replace the standard Diagram.commandHandler with a custom DrawCommandHandler? Just subclass DrawCommandHandler and replace the Diagram.commandHandler with an instance of the new class.

Not exactly.

I have a custom DrawCommandHandler. But in some diagrams I need to provide custom handling. In the example the groupNodes are both the normal ones (as defined in my DrawCommandHandler) but should also include some link attached nodes for navigation purpose.

Like I said, right now I duplicate the getGroupNodes from my DrawCommandHandler at the var allParts line, but that doesn’t sound like the right way to do things.

Oh, so your question isn’t where to put the code, but how to write the code? I don’t understand what you want to do, so I am unable to help you with the latter. I have no idea of how your _getGroupNodes method should behave or how it is called.

What is the syntax for this line to work:

so that it calls the function defined in DrawCommandHandler to perform the normal behavior.

For example if I was using the standard CommandHandler I would do something like this to call the original function:

go.CommandHandler.prototype.increaseZoom.call(this, actualZoomFactor);

Thanks

Unless you have modified the definition of DrawCommandHandler, there is no _getGroupNodes method. So of course you cannot call the super method.

Walter,

If I am trying to call a function in my customized DrawCommandHandler, it does exist. And when it is invoked from within DrawCommandHandler, it will correctly trigger the diagram version that I showed.

The question is, what is the syntax to call an inherited function in this case, similar to what I would do if I wanted to extend CommandHandler as shown in my previous post.

So that method does exist in your copy of DrawCommandHandler.js? That’s confusing, but OK. Then that should work, if that is actually what you have done and have loaded.

Having a bit of a hard time being on the same page

Ok, in DrawCommandHandler I have this function:

DrawCommandHandler.prototype._getGroupNodes = function(group) {
  var allParts = new Array();

  group.memberParts.each(function(member) {
    if (member instanceof go.Node) {
      allParts.push(member); 
    }
  });
  // note that this ignores Links
  return allParts;
};

and it gets call like this:

var grpTopLeft = getHandler().topLeftPart(originalPart, this._getGroupNodes(originalPart));
this._processSelection(diagram, grpTopLeft, e);

in one specific diagram I need to extend the normal behavior. So I implemented this version:

mainDiagram.commandHandler._getGroupNodes = function(group) {
  var allParts = new Array();

  group.memberParts.each(function(member) {
    if (member instanceof go.Node) {
      allParts.push(member); 
    }
  });

//		var allParts = DrawCommandHandler.prototype._getGroupNodes.call(this, group);
  var linkIt = group.findLinksConnected();

  while (linkIt.next()) { // for each link get the link text and toNode text
    var link = linkIt.value;
    if (link.category == ENTRY_CRITERIA_LINK_CATEGORY || link.category == EXIT_CRITERIA_LINK_CATEGORY) {
      if (link.fromNode != group) {
        allParts.push(link.fromNode); 
      }
      else {
        allParts.push(link.toNode); 
      }
    }
  }
  return allParts;
};

which works, but notice that I had to replicate the code from DrawCommandHandler.

My attempt to invoke the parent/base/inherited, call it what you want, version of the method as shown in the commented line, doesn’t work. It actually seems to completely ignore this extended method.

I hope this makes it a bit clearer.
Thanks

OK. It appears to me that your code calling the super method is correct. I hope you set mainDiagram.commandHandler = new DrawCommandHandler() before you set its _getGroupNodes method.

This is what I have:

drawCommandHandler = new DrawCommandHandler();
commandContextMenuTool = new CommandContextMenuTool();

mainDiagram = $g(go.Diagram, "mainDiagramDiv",
{
  commandHandler: drawCommandHandler,
  contextMenuTool: commandContextMenuTool,
  initialContentAlignment : go.Spot.Center, // center Diagram contents
  "undoManager.isEnabled" : true, // enable Ctrl-Z to undo and Ctrl-Y to
  allowDrop : true,
  allowCopy: isEditable()
});

That still looks good to me. You’ll need to step through the code carefully to debug it.

Ok thanks, I’ll try to debug in finer details.

No change and it’s working now. False alert I guess. Wasn’t sure if I was using the right syntax, seemed logical to me, but never know when it fails.

Thanks
Alain