BaseRenderer.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. if (self.canRender(element)) {
  16. if (type === 'render.shape') {
  17. return self.drawShape(visuals, element);
  18. } else {
  19. return self.drawConnection(visuals, element);
  20. }
  21. }
  22. });
  23. eventBus.on([ 'render.getShapePath', 'render.getConnectionPath'], renderPriority, function(evt, element) {
  24. if (self.canRender(element)) {
  25. if (evt.type === 'render.getShapePath') {
  26. return self.getShapePath(element);
  27. } else {
  28. return self.getConnectionPath(element);
  29. }
  30. }
  31. });
  32. }
  33. /**
  34. * Should check whether *this* renderer can render
  35. * the element/connection.
  36. *
  37. * @param {element} element
  38. *
  39. * @returns {Boolean}
  40. */
  41. BaseRenderer.prototype.canRender = function() {};
  42. /**
  43. * Provides the shape's snap svg element to be drawn on the `canvas`.
  44. *
  45. * @param {djs.Graphics} visuals
  46. * @param {Shape} shape
  47. *
  48. * @returns {Snap.svg} [returns a Snap.svg paper element ]
  49. */
  50. BaseRenderer.prototype.drawShape = function() {};
  51. /**
  52. * Provides the shape's snap svg element to be drawn on the `canvas`.
  53. *
  54. * @param {djs.Graphics} visuals
  55. * @param {Connection} connection
  56. *
  57. * @returns {Snap.svg} [returns a Snap.svg paper element ]
  58. */
  59. BaseRenderer.prototype.drawConnection = function() {};
  60. /**
  61. * Gets the SVG path of a shape that represents it's visual bounds.
  62. *
  63. * @param {Shape} shape
  64. *
  65. * @return {string} svg path
  66. */
  67. BaseRenderer.prototype.getShapePath = function() {};
  68. /**
  69. * Gets the SVG path of a connection that represents it's visual bounds.
  70. *
  71. * @param {Connection} connection
  72. *
  73. * @return {string} svg path
  74. */
  75. BaseRenderer.prototype.getConnectionPath = function() {};