vis.js is a dynamic, browser-based visualization library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
3.1 KiB

  1. class NodeBase {
  2. constructor(options, body, labelModule) {
  3. this.body = body;
  4. this.labelModule = labelModule;
  5. this.setOptions(options);
  6. this.top = undefined;
  7. this.left = undefined;
  8. this.height = undefined;
  9. this.width = undefined;
  10. this.radius = undefined;
  11. this.margin = undefined;
  12. this.refreshNeeded = true;
  13. this.boundingBox = {top: 0, left: 0, right: 0, bottom: 0};
  14. }
  15. setOptions(options) {
  16. this.options = options;
  17. }
  18. _setMargins(labelModule) {
  19. this.margin = {};
  20. if (this.options.margin) {
  21. if (typeof this.options.margin == 'object') {
  22. this.margin.top = this.options.margin.top;
  23. this.margin.right = this.options.margin.right;
  24. this.margin.bottom = this.options.margin.bottom;
  25. this.margin.left = this.options.margin.left;
  26. } else {
  27. this.margin.top = this.options.margin;
  28. this.margin.right = this.options.margin;
  29. this.margin.bottom = this.options.margin;
  30. this.margin.left = this.options.margin;
  31. }
  32. }
  33. labelModule.adjustSizes(this.margin)
  34. }
  35. _distanceToBorder(ctx,angle) {
  36. var borderWidth = this.options.borderWidth;
  37. this.resize(ctx);
  38. return Math.min(
  39. Math.abs(this.width / 2 / Math.cos(angle)),
  40. Math.abs(this.height / 2 / Math.sin(angle))) + borderWidth;
  41. }
  42. enableShadow(ctx, values) {
  43. if (values.shadow) {
  44. ctx.shadowColor = values.shadowColor;
  45. ctx.shadowBlur = values.shadowSize;
  46. ctx.shadowOffsetX = values.shadowX;
  47. ctx.shadowOffsetY = values.shadowY;
  48. }
  49. }
  50. disableShadow(ctx, values) {
  51. if (values.shadow) {
  52. ctx.shadowColor = 'rgba(0,0,0,0)';
  53. ctx.shadowBlur = 0;
  54. ctx.shadowOffsetX = 0;
  55. ctx.shadowOffsetY = 0;
  56. }
  57. }
  58. enableBorderDashes(ctx, values) {
  59. if (values.borderDashes !== false) {
  60. if (ctx.setLineDash !== undefined) {
  61. let dashes = values.borderDashes;
  62. if (dashes === true) {
  63. dashes = [5,15]
  64. }
  65. ctx.setLineDash(dashes);
  66. }
  67. else {
  68. console.warn("setLineDash is not supported in this browser. The dashed borders cannot be used.");
  69. this.options.shapeProperties.borderDashes = false;
  70. values.borderDashes = false;
  71. }
  72. }
  73. }
  74. disableBorderDashes(ctx, values) {
  75. if (values.borderDashes !== false) {
  76. if (ctx.setLineDash !== undefined) {
  77. ctx.setLineDash([0]);
  78. }
  79. else {
  80. console.warn("setLineDash is not supported in this browser. The dashed borders cannot be used.");
  81. this.options.shapeProperties.borderDashes = false;
  82. values.borderDashes = false;
  83. }
  84. }
  85. }
  86. /**
  87. * Determine if the shape of a node needs to be recalculated.
  88. *
  89. * @protected
  90. */
  91. needsRefresh(selected, hover) {
  92. if (this.refreshNeeded === true) {
  93. // This is probably not the best location to reset this member.
  94. // However, in the current logic, it is the most convenient one.
  95. this.refreshNeeded = false;
  96. return true;
  97. }
  98. return (this.width === undefined) || (this.labelModule.differentState(selected, hover));
  99. }
  100. }
  101. export default NodeBase;