connect.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. define(function(require, exports, module) {
  2. var kity = require('./kity');
  3. var utils = require('./utils');
  4. var Module = require('./module');
  5. var Minder = require('./minder');
  6. var MinderNode = require('./node');
  7. // 连线提供方
  8. var _connectProviders = {};
  9. function register(name, provider) {
  10. _connectProviders[name] = provider;
  11. }
  12. register('default', function(node, parent, connection) {
  13. connection.setPathData([
  14. 'M', parent.getLayoutVertexOut(),
  15. 'L', node.getLayoutVertexIn()
  16. ]);
  17. });
  18. kity.extendClass(MinderNode, {
  19. /**
  20. * @private
  21. * @method getConnect()
  22. * @for MinderNode
  23. * @description 获取当前节点的连线类型
  24. *
  25. * @grammar getConnect() => {string}
  26. */
  27. getConnect: function() {
  28. return this.data.connect || 'default';
  29. },
  30. getConnectProvider: function() {
  31. return _connectProviders[this.getConnect()] || _connectProviders['default'];
  32. },
  33. /**
  34. * @private
  35. * @method getConnection()
  36. * @for MinderNode
  37. * @description 获取当前节点的连线对象
  38. *
  39. * @grammar getConnection() => {kity.Path}
  40. */
  41. getConnection: function() {
  42. return this._connection || null;
  43. }
  44. });
  45. kity.extendClass(Minder, {
  46. getConnectContainer: function() {
  47. return this._connectContainer;
  48. },
  49. createConnect: function(node) {
  50. if (node.isRoot()) return;
  51. var connection = new kity.Path();
  52. node._connection = connection;
  53. this._connectContainer.addShape(connection);
  54. this.updateConnect(node);
  55. },
  56. removeConnect: function(node) {
  57. var me = this;
  58. node.traverse(function(node) {
  59. me._connectContainer.removeShape(node._connection);
  60. node._connection = null;
  61. });
  62. },
  63. updateConnect: function(node) {
  64. var connection = node._connection;
  65. var parent = node.parent;
  66. if (!parent || !connection) return;
  67. if (parent.isCollapsed()) {
  68. connection.setVisible(false);
  69. return;
  70. }
  71. connection.setVisible(true);
  72. var provider = node.getConnectProvider();
  73. var strokeColor = node.getStyle('connect-color') || 'white',
  74. strokeWidth = node.getStyle('connect-width') || 2;
  75. connection.stroke(strokeColor, strokeWidth);
  76. provider(node, parent, connection, strokeWidth, strokeColor);
  77. if (strokeWidth % 2 === 0) {
  78. connection.setTranslate(0.5, 0.5);
  79. } else {
  80. connection.setTranslate(0, 0);
  81. }
  82. }
  83. });
  84. Module.register('Connect', {
  85. init: function() {
  86. this._connectContainer = new kity.Group().setId(utils.uuid('minder_connect_group'));
  87. this.getRenderContainer().prependShape(this._connectContainer);
  88. },
  89. events: {
  90. 'nodeattach': function(e) {
  91. this.createConnect(e.node);
  92. },
  93. 'nodedetach': function(e) {
  94. this.removeConnect(e.node);
  95. },
  96. 'layoutapply layoutfinish noderender': function(e) {
  97. this.updateConnect(e.node);
  98. }
  99. }
  100. });
  101. exports.register = register;
  102. });