PropertiesHelper.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. 'use strict';
  2. var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
  3. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  4. /**
  5. * Licensed Materials - Property of IBM
  6. * IBM Cognos Products: BI Cloud (C) Copyright IBM Corp. 2019
  7. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  8. */
  9. define(function () {
  10. var booleanProperties = ['weight', 'style'];
  11. var suffixToPart = {
  12. 'size': 'fontSize',
  13. 'family': 'font',
  14. 'weight': 'bold',
  15. 'style': 'italic'
  16. };
  17. var PropertiesHelper = function () {
  18. function PropertiesHelper() {
  19. _classCallCheck(this, PropertiesHelper);
  20. }
  21. PropertiesHelper.buildVidaFontPropertiesFromParts = function buildVidaFontPropertiesFromParts(VIPRUtils, fontChanges, currentFontProperty) {
  22. var vidaFont = {
  23. 'family': [],
  24. 'size': null,
  25. 'style': null,
  26. 'weight': null
  27. };
  28. var parsedCurrentVidaFontProperty = currentFontProperty;
  29. if (typeof currentFontProperty === 'string') {
  30. parsedCurrentVidaFontProperty = VIPRUtils.parseVidaFontProperty(currentFontProperty);
  31. }
  32. parsedCurrentVidaFontProperty = parsedCurrentVidaFontProperty ? parsedCurrentVidaFontProperty : vidaFont;
  33. for (var fontPart in parsedCurrentVidaFontProperty) {
  34. if (parsedCurrentVidaFontProperty.hasOwnProperty(fontPart)) {
  35. var part = suffixToPart[fontPart];
  36. //the steps to apply the value: 1) If changed states is valid, apply it;2)Then override the missing values from old property value;3) If changed states is null, apply null
  37. if (booleanProperties.indexOf(fontPart) !== -1) {
  38. if (fontChanges[part]) {
  39. vidaFont[fontPart] = part;
  40. } else if (typeof fontChanges[part] === 'undefined' && parsedCurrentVidaFontProperty[fontPart]) {
  41. vidaFont[fontPart] = parsedCurrentVidaFontProperty[fontPart].toString();
  42. }
  43. } else if (Array.isArray(vidaFont[fontPart])) {
  44. //in text toolbar, we only supports one family option at one time, so the array length is no more than 1
  45. vidaFont[fontPart].length = 0;
  46. if (fontChanges[part]) {
  47. vidaFont[fontPart].push(fontChanges[part]);
  48. } else if (typeof fontChanges[part] === 'undefined' && parsedCurrentVidaFontProperty[fontPart].length) {
  49. vidaFont[fontPart].push(parsedCurrentVidaFontProperty[fontPart][0].toString());
  50. }
  51. } else {
  52. if (fontChanges[part]) {
  53. vidaFont[fontPart] = fontChanges[part];
  54. } else if (typeof fontChanges[part] === 'undefined' && parsedCurrentVidaFontProperty[fontPart]) {
  55. vidaFont[fontPart] = parsedCurrentVidaFontProperty[fontPart].toString();
  56. }
  57. }
  58. }
  59. }
  60. var vidaFontValues = Object.keys(vidaFont).map(function (fontPart) {
  61. return vidaFont[fontPart];
  62. });
  63. var vidaFontProperty = VIPRUtils.createVidaFontProperty(vidaFontValues);
  64. //vida does not accept empty string, need to convert the empty string to null value
  65. var vidaFontString = vidaFontProperty && vidaFontProperty.toString ? vidaFontProperty.toString() : null;
  66. var propertyValueToApply = vidaFontString === '' ? null : vidaFontString;
  67. return propertyValueToApply;
  68. };
  69. PropertiesHelper.getVidaFontPropertiesPart = function getVidaFontPropertiesPart(VIPRUtils, part, fontPropertyValue) {
  70. var vidaFontValue = fontPropertyValue;
  71. if (typeof fontPropertyValue === 'string') {
  72. vidaFontValue = VIPRUtils.parseVidaFontProperty(fontPropertyValue);
  73. }
  74. var fontPropValue = vidaFontValue && vidaFontValue[part] ? vidaFontValue[part] : null;
  75. var isBooleanProperty = booleanProperties.indexOf(part) !== -1;
  76. if (fontPropValue) {
  77. if (isBooleanProperty) {
  78. return true;
  79. } else if (part === 'size' && (typeof fontPropValue === 'undefined' ? 'undefined' : _typeof(fontPropValue)) === 'object') {
  80. var fontvalue = fontPropValue['value'].toString() + fontPropValue['unit'].toString();
  81. return fontvalue;
  82. } else {
  83. return fontPropValue.toString();
  84. }
  85. } else {
  86. return isBooleanProperty ? false : null;
  87. }
  88. };
  89. PropertiesHelper.splitEscapedString = function splitEscapedString(input, delimiter, outputs) {
  90. var index = input.indexOf(delimiter);
  91. if (index !== -1 && index !== input.length && input[index - 1] !== '\\') {
  92. var result = input.slice(0, index);
  93. input = input.slice(index + 1, input.length);
  94. outputs.push(result);
  95. PropertiesHelper.splitEscapedString(input, delimiter, outputs);
  96. } else {
  97. outputs.push(input);
  98. }
  99. };
  100. return PropertiesHelper;
  101. }();
  102. return PropertiesHelper;
  103. });
  104. //# sourceMappingURL=PropertiesHelper.js.map