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.

54 lines
1.8 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. export default ComponentUtil;