I ran into the same needs. Instead of adding the ContextMenu in the Bean. I was able to get it working using <f:facet> in the xhtml file like the following:
- Create an new Class to use as the Data
public class NodeElement implements Serializable {
private Long id;
private String title;
private String type;
//... other variables
// getter and setters and other methods
}
- Example creating an Element
Element element = new Element(new NodeElement(1L, "Element Title", "type1"), "30em", "16em");
...
<h:form id="patent-form" enctype="multipart/form-data">
<p:growl id="msgs" showDetail="true" skipDetailIfEqualsSummary="true" />
<p:diagram id="sample-graph" value="#{diagramBasicView.model}" styleClass="ui-widget-content" var="el">
<f:facet name="element">
<h:panelGroup layout="block" rendered="#{el.type == 'type1'}" id="type1-node" styleClass="type1-node" ps:data-id="#{el.id}">
<h:panelGroup laybout="block" styleClass="graph-node-content" ps:data-id="#{el.id}">
<h:outputText ps:title="Node Title: #{el.title}" ps:data-id="#{el.id}" value="#{el.title.length() gt 45 ? el.title.substring(0, 45) : el.title}" styleClass="graph-node-title"/>
<!-- ... OTHER data (make sure to use h:outputText or other JSF tags) -->
</h:panelGroup>
</h:panelGroup>
...
</h:panelGroup>
<f:facet>
</p:diagram>
<!-- from https://stackoverflow.com/questions/35812682/jsf-primefaces-eventing-in-diagram-elements -->
<p:remoteCommand name="elementClicked" actionListener="#{diagramBasicView.onSelection()}"/>
<p:contextMenu for="type1-node" style="width: 220px; height: 150px;">
<p:menuitem value="Show Info"
update="sample-graph"
id="show-info-menu-item"/>
<p:menuitem value="Second Menu"
update="sample-graph"
id="second-menu-item"/>
</p:contextMenu>
</h:form>
<!-- use to get the id from the element as data attribute -->
<script>
$('.ui-diagram > .ui-diagram-element').contextmenu(function(info){
// console.log($(info.target).data('id'));
elementClicked([ {
name : 'elementId',
value : $(info.target).data('id')
} ]);
});
</script>
...