Override groupselection

Hi i’m trying to override the groupselection function to add some functionality to it, but i’m running into a problem.

i’ve looked at the Extensions intro in the API (http://gojs.net/latest/intro/extensions.html) and tried to do something similar, here’s the code:

var tool = diagram.commandHandler;
tool.groupSelection = function() {	

I’m getting stuck at the point of calling the GroupSelection prototype, getting the message ‘Cannot read property ‘prototype’ of undefined’, i’ve tried reaching it through a couple of other (possible) ways but haven’t succeeded.

Some help, and explanation would be helpful!

Thanks in advance

If you are trying to change the behavior of the CommandHandler.groupSelection command, you probably don’t want to do it that way.

I suggest that you instead implement a “SelectionGrouped” DiagramEvent listener in which you make changes to the new Group. But it isn’t clear to me what you are trying to do – what does graphWorker do in your code?

Hi Walter,

Thanks for your reply. What i’m doing with the groups is: when a selection is grouped, the grouped object must make links with all the nodes that are connected to the nodes in the group. Therefore i’m first setting the selectedObjectKeys in the graphworker before calling the groupselection command. The group command can be called from a regular menu, from a context menu, and from the CTRL+G command. The commands from the regular menu and the context menu are working (with that little code segment) so now i was trying to override the groupSelection command to do the same.

Listening to the SelectionGrouped command makes sense. Then i just have to set the selected object keys an other way (by finding the nodes that are inside the group). So i guess that should be the best way to do it.


Yes, I think that should work.

By the way, if you don’t mind my pointing out some naming clarifications:

  • The CommandHandler is not a Tool
  • “SelectionGrouped” is a name for a DiagramEvent, not a command

Hi Walter,

Yes i know, should’ve given the variable another name

Also knew it , but thanks anyway for pointing it out :)

I’ve refactored it so it listenes to the selectionGrouped event. Much nicer code this way !


One more question, is it possible to disable the keycommands for the commandHandler? i’ve fixed the grouping command but the ungrouping gives me problems (stuff inside the code which is’nt refactored very easy and it’s almost release time) so i would like to disable ungrouping with ctrl+shift+g

Do you only want to disable Ctrl-Shift-g from invoking CommandHandler.ungroupSelection? If so, you can override CommandHandler.doKeyDown as shown in http://gojs.net/latest/intro/commands.html. Check for e.control && e.shift && e.key === "G" and do nothing; otherwise call the base method.

But I suspect that you want to disable the command, no matter how it is invoked. The obvious answer is to set Diagram.allowUngroup to false. That will cause CommandHandler.canUngroupSelection() to return false, thereby disabling the command in all circumstances.

But the easiest solution would be to not set Group.ungroupable to true in your Group template. The default value is false. This property is like Link.relinkableTo and Part.rotatable – they default to false so that that functionality can be easily enabled for those particular Parts. This allows all of the “Diagram.allow…” properties to be true by default (with two exceptions), making diagram-wide disablement easy when desired.

No, overriding the dokeydown event is what i wanted to do. In my current ungroup functions i call the ‘ungroupselection’ after doing some other logic (like i also did in the groupselection function).

I managed to rewrite the groupselection function to be compatible with the standard groupselection event, but rewriting my ungroupselection function gave me problems, so i wanted to override the standard ctrl+shift+g keycommand to be ignored. But since i caught it now, with the diagram.commandHandler.doKeyDown, i can call my own ungroupselection function instead of the default.

Thanks for the help!