2015-07-12 99 views
10

我想要做的是使用自定义类的端口和路径的元素,以便我可以添加自定义路径和我自己的标记为ports.This方法当我创建一个元素,我会通过动态它的形状路径就像路径类的元素行为一样,因为我也从PortsModelInterface扩展了,我也将有我自己的端口标记。 这整个努力是使svg可缩放zomming。以前我是用HTML自定义元素与工作正常,但自定义元素的HTML我的自定义端口不结垢缩放Joint.js添加具有路径类的自定义端口。为自定义元素

var graph = new joint.dia. 
var paper = new joint.dia.Paper({ 
    el: $('#paper'), 
    width: 800, 
    height: 600, 
    gridSize: 1, 
    model: graph, 
    snapLinks: true, 
    embeddingMode: true 
}); 
joint.shapes.custom1={}; 
joint.shapes.custom1.Element = joint.shapes.basic.Generic.extend(_.extend({}, joint.shapes.basic.PortsModelInterface, { 
     markup: '<g class="rotatable"><g class="scalable"><rect class = "myrect"/></g><g class="inPorts"/><g class="outPorts"/></g>', 
     portMarkup: '<g class="port<%= id %>"><circle class="port-body"/></g>', 
     defaults: joint.util.deepSupplement({ 
      type: 'html.Element', 
      size: { width: 200, height: 110 }, 
      inPorts: [], 
      outPorts: [], 
      attrs: { 
       '.': { magnet: true}, 
       rect: { 
        stroke: 'none', 'fill-opacity': 0, width: 300, height: 210, 
       }, 
       circle: { 
        r: 6, //circle radius 
        magnet: true, 
      left:0, 
        stroke: 'gray' 
       }, 

       '.inPorts circle': { fill: 'gray', magnet: 'passive', type: 'input', y: 0}, 
       '.outPorts circle': { fill: 'gray', type: 'output' } 
      } 
     }, joint.shapes.basic.Generic.prototype.defaults), 
     getPortAttrs: function (portName, index, total, selector, type) { 

      var attrs = {}; 
      var portClass = 'port' + index; 
      var portSelector = selector + '>.' + portClass; 
      var portCircleSelector = portSelector + '>circle'; 
      attrs[portCircleSelector] = { port: { id: portName || _.uniqueId(type), type: type } }; 
      attrs[portSelector] = { ref: 'rect', 'ref-x': (index + 1) * (0.55/total)}; 
      if (selector === '.outPorts') { 
      attrs[portSelector]['ref-dy'] = 15; 
     } 
      return attrs; 
     } 
    })); 
joint.shapes.custom1.Atomic = joint.shapes.custom1.Element.extend({ 

    markup: '<g class="rotatable"><g class="scalable"><path/></g><text/></g>', 

    defaults: joint.util.deepSupplement({ 

     type: 'basic.Path', 
     size: { width: 60, height: 60 }, 
     attrs: { 
      'path': { fill: '#FFFFFF', stroke: 'black' }, 
      'text': { 'font-size': 14, text: '', 'text-anchor': 'middle', 'ref-x': .5, 'ref-dy': 20, ref: 'path', 'y-alignment': 'middle', fill: 'black', 'font-family': 'Arial, helvetica, sans-serif' } 
     } 
    }, joint.shapes.basic.Generic.prototype.defaults) 

}); 

var a2 = new joint.shapes.custom1.Atomic({ 
    position: { x: 50, y: 260 }, 
    size: { width: 100, height: 100 }, 
    attrs: { 
     path: { d: 'M 30 0 L 60 30 30 60 0 30 z' }, 
     text: { 
      text: 'Diamond', 
      'ref-y': .5 // basic.Path text is originally positioned under the element 
     } 
    }, 
    inPorts: ['in'], 
    outPorts: ['out'] 
}); 
graph.addCells([a2]) 

元素在图形添加,但某些端口如何不显示。 我没有适当的概念添加类,所以请任何帮助将不胜感激。谢谢。 Fiddle example

+0

这是什么都与'backbone.js'办? – ivarni

+2

Joint.js构建在backbone.js之上,这个扩展类的整个机制源于骨干。不应该添加这个标签? – Achilles

+1

是否有无论如何你可以做一个小提琴或什么东西,所以我们可以运行代码,看看它失败,然后尝试看看我们是否可以使它工作?我不确定你会找到一个有joint.js的人,但是如果你给我们一个玩的机会,我们可能会发现它。 –

回答

5

我建议为形状和端口定义具有自定义标记的元素。这两个标记都应该包含SVG路径,因此您可以通过model.attr()设置任意路径数据d

joint.shapes.devs.GenericModel = joint.shapes.devs.Model.extend({ 

    markup: '<g class="rotatable"><g class="scalable"><path class="body"/></g><text class="label"/><g class="inPorts"/><g class="outPorts"/></g>', 
    portMarkup: '<g class="port port<%= id %>"><path class="port-body"/><text class="port-label"/></g>', 

    defaults: joint.util.deepSupplement({ 
     type: 'devs.GenericModel' 
    }, joint.shapes.devs.Model.prototype.defaults) 
}); 

告诉纸使用devs.ModelView进行渲染。

joint.shapes.devs.GenericModelView = joint.shapes.devs.ModelView; 

现在,您可以设置或更改任何时候你想要的形状和端口d属性。

var model = new joint.shapes.devs.GenericModel({ 
    attrs: { 
     '.body': { d: 'M 0 0 0 50 50 50 z'}, 
     '.port-body': { d: 'M 0 0 10 0 10 10 0 10 z'} 
    } 
}); 

model.attr('.body/d', 'M 25 0 50 50 0 50 z'); 

JS小提琴:http://jsfiddle.net/kumilingus/kge023bc/

+0

这正是我渴望的。谢谢 。 – Achilles