StringUtils.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /**
  2. * Licensed Materials - Property of IBM IBM Cognos Products: Modeling UI (C) Copyright IBM Corp. 2016, 2019
  3. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP
  4. * Schedule Contract with IBM Corp.
  5. */
  6. define([], function() {
  7. //offset value attached to the label shortener if filter icon is present
  8. var MAX_DIAGRAM_LABEL_LENGTH = 20;
  9. //offset value attached to the label shortener if filter icon is present
  10. var MAX_DIAGRAM_LABEL_LENGTH_FILTER = 16;
  11. //offset value attached to the label shortener if capitalized letters are present
  12. var MAX_DIAGRAM_LABEL_LENGTH_CAPTALIZED = 15;
  13. //offset value attached to the label shortener if capitalized letters are and a filter is present
  14. var MAX_DIAGRAM_LABEL_LENGTH_CAPTALIZED_FILTER = 12;
  15. var StringUtils = {
  16. /**
  17. * Return the max length of a text string for the diagram
  18. *
  19. * @param isFiter
  20. * Boolean value for if a filter is applied to the diagram
  21. * node (ie. filter icon visible)
  22. * @param label
  23. * Label of the diagram node
  24. */
  25. getMaxDiagramLabelLength : function(isFilter, label) {
  26. if (isFilter) {
  27. if ((label.match(/[A-Z]/g) || []).length >= 12) {
  28. return MAX_DIAGRAM_LABEL_LENGTH_CAPTALIZED_FILTER;
  29. }
  30. return MAX_DIAGRAM_LABEL_LENGTH_FILTER;
  31. }
  32. if ((label.match(/[A-Z]/g) || []).length > 15) {
  33. return MAX_DIAGRAM_LABEL_LENGTH_CAPTALIZED;
  34. }
  35. return MAX_DIAGRAM_LABEL_LENGTH;
  36. },
  37. /**
  38. * Shorten a passed string that will be formatted with
  39. * the ellipsis in the middle of the string eg:
  40. * Hollywood Movies Csv -> Hollywo...ies Csv
  41. *
  42. * @param text
  43. * The text item that is to be shortened
  44. * @param maxSize
  45. * Max size allowed (number of characters)
  46. */
  47. shortenTextMidEllipsis : function(text, maxSize) {
  48. var shortenedText = text;
  49. var length = text.length;
  50. // subtract 2 so we don't exceed the max val (adding
  51. // in 3 chars with the ...)
  52. var subLength = maxSize / 2 - 2;
  53. if (length > maxSize) {
  54. shortenedText = text.substring(0, subLength);
  55. shortenedText += '...';
  56. shortenedText += text.substring(length - subLength, length);
  57. }
  58. return shortenedText;
  59. }
  60. };
  61. return StringUtils;
  62. });