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.

119 lines
3.7 KiB

  1. let util = require("../../../../util");
  2. /**
  3. * Helper functions for components
  4. * @class
  5. */
  6. class ComponentUtil {
  7. /**
  8. * Determine values to use for (sub)options of 'chosen'.
  9. *
  10. * This option is either a boolean or an object whose values should be examined further.
  11. * The relevant structures are:
  12. *
  13. * - chosen: <boolean value>
  14. * - chosen: { subOption: <boolean or function> }
  15. *
  16. * Where subOption is 'node', 'edge' or 'label'.
  17. *
  18. * The intention of this method appears to be to set a specific priority to the options;
  19. * Since most properties are either bridged or merged into the local options objects, there
  20. * is not much point in handling them separately.
  21. * TODO: examine if 'most' in previous sentence can be replaced with 'all'. In that case, we
  22. * should be able to get rid of this method.
  23. *
  24. * @param {string} subOption option within object 'chosen' to consider; either 'node', 'edge' or 'label'
  25. * @param {Object} pile array of options objects to consider
  26. *
  27. * @return {boolean|function} value for passed subOption of 'chosen' to use
  28. */
  29. static choosify(subOption, pile) {
  30. // allowed values for subOption
  31. let allowed = [ 'node', 'edge', 'label'];
  32. let value = true;
  33. let chosen = util.topMost(pile, 'chosen');
  34. if (typeof chosen === 'boolean') {
  35. value = chosen;
  36. } else if (typeof chosen === 'object') {
  37. if (allowed.indexOf(subOption) === -1 ) {
  38. throw new Error('choosify: subOption \'' + subOption + '\' should be one of '
  39. + "'" + allowed.join("', '") + "'");
  40. }
  41. let chosenEdge = util.topMost(pile, ['chosen', subOption]);
  42. if ((typeof chosenEdge === 'boolean') || (typeof chosenEdge === 'function')) {
  43. value = chosenEdge;
  44. }
  45. }
  46. return value;
  47. }
  48. /**
  49. * Check if the point falls within the given rectangle.
  50. *
  51. * @param {rect} rect
  52. * @param {point} point
  53. * @param {rotationPoint} [rotationPoint] if specified, the rotation that applies to the rectangle.
  54. * @returns {boolean} true if point within rectangle, false otherwise
  55. * @static
  56. */
  57. static pointInRect(rect, point, rotationPoint) {
  58. if (rect.width <= 0 || rect.height <= 0) {
  59. return false; // early out
  60. }
  61. if (rotationPoint !== undefined) {
  62. // Rotate the point the same amount as the rectangle
  63. var tmp = {
  64. x: point.x - rotationPoint.x,
  65. y: point.y - rotationPoint.y
  66. };
  67. if (rotationPoint.angle !== 0) {
  68. // In order to get the coordinates the same, you need to
  69. // rotate in the reverse direction
  70. var angle = -rotationPoint.angle;
  71. var tmp2 = {
  72. x: Math.cos(angle)*tmp.x - Math.sin(angle)*tmp.y,
  73. y: Math.sin(angle)*tmp.x + Math.cos(angle)*tmp.y
  74. };
  75. point = tmp2;
  76. } else {
  77. point = tmp;
  78. }
  79. // Note that if a rotation is specified, the rectangle coordinates
  80. // are **not* the full canvas coordinates. They are relative to the
  81. // rotationPoint. Hence, the point coordinates need not be translated
  82. // back in this case.
  83. }
  84. var right = rect.x + rect.width;
  85. var bottom = rect.y + rect.width;
  86. return (
  87. rect.left < point.x &&
  88. right > point.x &&
  89. rect.top < point.y &&
  90. bottom > point.y
  91. );
  92. }
  93. /**
  94. * Check if given value is acceptable as a label text.
  95. *
  96. * @param {*} text value to check; can be anything at this point
  97. * @returns {boolean} true if valid label value, false otherwise
  98. */
  99. static isValidLabel(text) {
  100. // Note that this is quite strict: types that *might* be converted to string are disallowed
  101. return (typeof text === 'string' && text !== '');
  102. }
  103. }
  104. export default ComponentUtil;