How to remove elements from context menu?

I am using the following code to successfully remove elements from palette:

    const _getPaletteEntries = PaletteProvider.prototype.getPaletteEntries;
    PaletteProvider.prototype.getPaletteEntries = function() {
      const entries = _getPaletteEntries.apply(this);
      delete entries["create.data-store"];
      return entries;
    };

when I try to do the same thing to remove elements from context menu:

    const _getContextPadEntries =
      ContextPadProvider.prototype.getContextPadEntries;
    ContextPadProvider.prototype.getContextPadEntries = function() {
      const entries = _getContextPadEntries.apply(this);
      // yet to be determined
      return entries;
    };

then I get the following error (as soon as I right click context menu element):

  54 | translate = this._translate;
  55 | var actions = {};
  56 | 
> 57 | if (element.type === "label") {
     | ^  58 |   return actions;
  59 | }
  60 | 

The error is referring to line 95 of bpmn-js/lib/features/context-pad/ContextPadProvider.js

What is the errror you’re getting?

When you have a look at the ContextPadProvider you will see that you need to input an element to retrieve the context pad entries for it.

I’d assume your error is related to the fact element is not defined.

My mistake!

TypeError: Cannot read property 'type' of undefined

A little more background here is that I am attempting to execute this block of code from within a React component jut before I initialize a modeler:

Not sure if that might be relevant or not.

You’re right.

Any idea how I might be able to pass element?

I’m not really used to attempting to modify a prototype in this manner, so I’m not sure how to approach it.

Also, if anyone knows a better way to go about removing an element from context menu then please share.

How’s that supposed to work? There are no elements before you haven’t created the modeler and imported a diagram.

The truth is that I am trying something rather blindly based on a code example for something that I thought was similar.

Please let me know in case you have any suggestions for how to remove elements from context menu.

Thanks

Try this:

    const _getContextPadEntries =
      ContextPadProvider.prototype.getContextPadEntries;
    ContextPadProvider.prototype.getContextPadEntries = function(element) {
      const entries = _getContextPadEntries.apply(this, [ element ]);
      // yet to be determined
      return entries;
    };

So now your function will actually accept the element as a parameter and can pass it to the parent’s function.

Thanks a lot.

doing:

    const _getContextPadEntries =
      ContextPadProvider.prototype.getContextPadEntries;
    ContextPadProvider.prototype.getContextPadEntries = function(element) {
      const entries = _getContextPadEntries.apply(this, [element]);
      delete entries["append.end-event"];
      delete entries["append.intermediate-event"];
      delete entries["append.gateway"];
      delete entries["append.append-task"];
      delete entries["append.text-annotation"];
      delete entries["replace"];
      return entries;
    };

does the trick.

1 Like