BaseRenderer.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. var DEFAULT_RENDER_PRIORITY = 1000;
  2. /**
  3. * The base implementation of shape and connection renderers.
  4. *
  5. * @param {EventBus} eventBus
  6. * @param {number} [renderPriority=1000]
  7. */
  8. export default function BaseRenderer(eventBus, renderPriority) {
  9. var self = this;
  10. renderPriority = renderPriority || DEFAULT_RENDER_PRIORITY;
  11. eventBus.on([ 'render.shape', 'render.connection' ], renderPriority, function(evt, context) {
  12. var type = evt.type,
  13. element = context.element,
  14. visuals = context.gfx,
  15. attrs = context.attrs;
  16. if (self.canRender(element)) {
  17. if (type === 'render.shape') {
  18. return self.drawShape(visuals, element, attrs);
  19. } else {
  20. return self.drawConnection(visuals, element, attrs);
  21. }
  22. }
  23. });
  24. eventBus.on([ 'render.getShapePath', 'render.getConnectionPath' ], renderPriority, function(evt, element) {
  25. if (self.canRender(element)) {
  26. if (evt.type === 'render.getShapePath') {
  27. return self.getShapePath(element);
  28. } else {
  29. return self.getConnectionPath(element);
  30. }
  31. }
  32. });
  33. }
  34. /**
  35. * Should check whether *this* renderer can render
  36. * the element/connection.
  37. *
  38. * @param {element} element
  39. *
  40. * @returns {boolean}
  41. */
  42. BaseRenderer.prototype.canRender = function() {};
  43. /**
  44. * Provides the shape's snap svg element to be drawn on the `canvas`.
  45. *
  46. * @param {djs.Graphics} visuals
  47. * @param {Shape} shape
  48. *
  49. * @returns {Snap.svg} [returns a Snap.svg paper element ]
  50. */
  51. BaseRenderer.prototype.drawShape = function() {};
  52. /**
  53. * Provides the shape's snap svg element to be drawn on the `canvas`.
  54. *
  55. * @param {djs.Graphics} visuals
  56. * @param {Connection} connection
  57. *
  58. * @returns {Snap.svg} [returns a Snap.svg paper element ]
  59. */
  60. BaseRenderer.prototype.drawConnection = function() {};
  61. /**
  62. * Gets the SVG path of a shape that represents it's visual bounds.
  63. *
  64. * @param {Shape} shape
  65. *
  66. * @return {string} svg path
  67. */
  68. BaseRenderer.prototype.getShapePath = function() {};
  69. /**
  70. * Gets the SVG path of a connection that represents it's visual bounds.
  71. *
  72. * @param {Connection} connection
  73. *
  74. * @return {string} svg path
  75. */
  76. BaseRenderer.prototype.getConnectionPath = function() {};