I managed to enhance my SVG-shape with ports via:
joint.shapes.devs.Element = joint.shapes.basic.Generic.extend(_.extend({},
joint.shapes.basic.PortsMod
The key was this plugin. The author extended the PortsModelInterface with own code for a move-, resize- and ports-tool. I extended it further by implementing the delete functionality. This way the devs-model is open to any extension in case of functionality.
In tooledViewPlugin.js there is joint.plugins.TooledModelInterface = {}. In there I added:
deleteToolMarkup: 'Remove this element from the model ',
Below in joint.plugins.TooledViewInterface = {} I wrote
renderDeleteTool: function () {
var deleteContainer = this.$('.deleteTool').empty();
var markup = V(this.model.deleteToolMarkup);
for(var id in markup)
deleteContainer.append(markup[id].node);
}
An example shape with special SVG markup other than a simple rectangle. Note the in the markup:
joint.shapes.devs.UnspecifiedProcess = joint.shapes.devs.Model.extend(_.extend({}, joint.plugins.TooledModelInterface, {
markup: ['',
'',
' ',
'',
'',
'',
'',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' '].join(''),
defaults: joint.util.deepSupplement({
type: 'devs.UnspecifiedProcess',
inPorts: [''],
outPorts: [''],
moveTool: true,
resizeTool: true,
size: { width: 100, height: 31},
attrs: {
'.inPorts circle': { fill: '#fff' },
'.outPorts circle': { fill: '#fff' },
'.body': {
width: 67, height: 21,
stroke: 'none'
},
}
}, joint.shapes.devs.Model.prototype.defaults),
}));
joint.shapes.devs.UnspecifiedProcessView = joint.shapes.devs.ModelView.extend(joint.plugins.TooledViewInterface);
The final part is instantiating the element with new joint.shapes.devs.UnspecifiedProcess. I present it to you with my drag and drop logic as it might be useful for you too:
//Drag and drop shapes
if (Modernizr.draganddrop) {
// Mouse position
var posX = 0,
posY = 0;
// Selected Element with start of dragging
var selectedEl = "";
var selectedObj = null;
var oldObj = null;
//
$(".draggable-svg").on("dragstart", function(e) {
selectedEl = this.id;
console.log(selectedEl);
});
$("#drawing-area").on("dragover", function(e) {
e.preventDefault();
posX = e.originalEvent.pageX - sideBarW;
posY = e.originalEvent.pageY - topBarH;
});
$("#drawing-area").on("drop", function(e) {
e.preventDefault();
var element = new joint.shapes.devs[selectedEl]({
position: { x: posX, y: posY }
});
graph.addCell(element);
selectedEl = "";
oldObj = selectedObj;
selectedObj = element;
});
} else {
alert("Your browser is very old. Please update.");
}