outline.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. define(function(require, exports, module) {
  2. var kity = require('../core/kity');
  3. var utils = require('../core/utils');
  4. var Minder = require('../core/minder');
  5. var MinderNode = require('../core/node');
  6. var Command = require('../core/command');
  7. var Module = require('../core/module');
  8. var Renderer = require('../core/render');
  9. var OutlineRenderer = kity.createClass('OutlineRenderer', {
  10. base: Renderer,
  11. create: function(node) {
  12. var outline = new kity.Rect()
  13. .setId(utils.uuid('node_outline'));
  14. this.bringToBack = true;
  15. return outline;
  16. },
  17. update: function(outline, node, box) {
  18. var shape = node.getStyle('shape');
  19. var paddingLeft = node.getStyle('padding-left'),
  20. paddingRight = node.getStyle('padding-right'),
  21. paddingTop = node.getStyle('padding-top'),
  22. paddingBottom = node.getStyle('padding-bottom');
  23. var outlineBox = {
  24. x: box.x - paddingLeft,
  25. y: box.y - paddingTop,
  26. width: box.width + paddingLeft + paddingRight,
  27. height: box.height + paddingTop + paddingBottom
  28. };
  29. var radius = node.getStyle('radius');
  30. // 天盘图圆形的情况
  31. if (shape && shape == 'circle') {
  32. var p = Math.pow;
  33. var r = Math.round;
  34. radius = r(Math.sqrt(p(outlineBox.width, 2) + p(outlineBox.height, 2)) / 2);
  35. outlineBox.x = box.cx - radius;
  36. outlineBox.y = box.cy - radius;
  37. outlineBox.width = 2 * radius;
  38. outlineBox.height = 2 * radius;
  39. }
  40. var prefix = node.isSelected() ? (node.getMinder().isFocused() ? 'selected-' : 'blur-selected-') : '';
  41. outline
  42. .setPosition(outlineBox.x, outlineBox.y)
  43. .setSize(outlineBox.width, outlineBox.height)
  44. .setRadius(radius)
  45. .fill(node.getData('background') || node.getStyle(prefix + 'background') || node.getStyle('background'))
  46. .stroke(node.getStyle(prefix + 'stroke' || node.getStyle('stroke')),
  47. node.getStyle(prefix + 'stroke-width'));
  48. return new kity.Box(outlineBox);
  49. }
  50. });
  51. var ShadowRenderer = kity.createClass('ShadowRenderer', {
  52. base: Renderer,
  53. create: function(node) {
  54. this.bringToBack = true;
  55. return new kity.Rect();
  56. },
  57. shouldRender: function(node) {
  58. return node.getStyle('shadow');
  59. },
  60. update: function(shadow, node, box) {
  61. shadow.setPosition(box.x + 4, box.y + 5)
  62. .fill(node.getStyle('shadow'));
  63. var shape = node.getStyle('shape');
  64. if(!shape){
  65. shadow.setSize(box.width, box.height);
  66. shadow.setRadius(node.getStyle('radius'));
  67. }else if(shape=='circle'){
  68. var width= Math.max(box.width,box.height);
  69. shadow.setSize(width, width);
  70. shadow.setRadius(width/2);
  71. }
  72. }
  73. });
  74. var marker = new kity.Marker();
  75. marker.setWidth(10);
  76. marker.setHeight(12);
  77. marker.setRef(0, 0);
  78. marker.setViewBox(-6, -4, 8, 10);
  79. marker.addShape(new kity.Path().setPathData('M-5-3l5,3,-5,3').stroke('#33ffff'));
  80. var wireframeOption = /wire/.test(window.location.href);
  81. var WireframeRenderer = kity.createClass('WireframeRenderer', {
  82. base: Renderer,
  83. create: function() {
  84. var wireframe = new kity.Group();
  85. var oxy = this.oxy = new kity.Path()
  86. .stroke('#f6f')
  87. .setPathData('M0,-50L0,50M-50,0L50,0');
  88. var box = this.wireframe = new kity.Rect()
  89. .stroke('lightgreen');
  90. var vectorIn = this.vectorIn = new kity.Path()
  91. .stroke('#66ffff');
  92. var vectorOut = this.vectorOut = new kity.Path()
  93. .stroke('#66ffff');
  94. vectorIn.setMarker(marker, 'end');
  95. vectorOut.setMarker(marker, 'end');
  96. return wireframe.addShapes([oxy, box, vectorIn, vectorOut]);
  97. },
  98. shouldRender: function() {
  99. return wireframeOption;
  100. },
  101. update: function(created, node, box) {
  102. this.wireframe
  103. .setPosition(box.x, box.y)
  104. .setSize(box.width, box.height);
  105. var pin = node.getVertexIn();
  106. var pout = node.getVertexOut();
  107. var vin = node.getLayoutVectorIn().normalize(30);
  108. var vout = node.getLayoutVectorOut().normalize(30);
  109. this.vectorIn.setPathData(['M', pin.offset(vin.reverse()), 'L', pin]);
  110. this.vectorOut.setPathData(['M', pout, 'l', vout]);
  111. }
  112. });
  113. Module.register('OutlineModule', function() {
  114. return {
  115. events: (!wireframeOption ? null : {
  116. 'ready': function() {
  117. this.getPaper().addResource(marker);
  118. },
  119. 'layoutallfinish': function() {
  120. this.getRoot().traverse(function(node) {
  121. node.getRenderer('WireframeRenderer').update(null, node, node.getContentBox());
  122. });
  123. }
  124. }),
  125. renderers: {
  126. outline: OutlineRenderer,
  127. outside: [ShadowRenderer, WireframeRenderer]
  128. }
  129. };
  130. });
  131. });