PropertiesCreatorFromVisDefinition.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. 'use strict';
  2. /**
  3. *+------------------------------------------------------------------------+
  4. *| Licensed Materials - Property of IBM
  5. *| IBM Cognos Products: Dashboard
  6. *| (C) Copyright IBM Corp. 2018, 2020
  7. *|
  8. *| US Government Users Restricted Rights - Use, duplication or disclosure
  9. *| restricted by GSA ADP Schedule Contract with IBM Corp.
  10. *+------------------------------------------------------------------------+
  11. */
  12. define(['underscore', 'dashboard-analytics/widgets/livewidget/nls/StringResources', 'dashboard-analytics/visualizations/properties/PropertiesCreator', 'dashboard-analytics/visualizations/vipr/properties/ColorPropertiesCreator', 'dashboard-analytics/visualizations/vipr/VIPRUtils', '../VIPRConfig'], function (_, StringResources, PropertiesCreator, ColorPropertiesCreator, VIPRUtils, VIPRConfig) {
  13. 'use strict';
  14. /*
  15. * This purpose of this singleton is to create generic vida property descriptions
  16. * from the Vis bundles that can be stored in our model.
  17. */
  18. var PropertiesCreatorFromVisDef = function () {
  19. /**
  20. * Instance stores a reference to the Singleton
  21. * @type {object}
  22. */
  23. var instance = null;
  24. function init() {
  25. return {
  26. /**
  27. * We only need generic descriptions for properties in the model cache.
  28. * @param {String} propId - id of the property of interest
  29. * @param {Object} simple object with id and name defined
  30. */
  31. _createGenericProperty: function _createGenericProperty(propId) {
  32. return {
  33. 'id': propId,
  34. 'name': propId
  35. };
  36. },
  37. /**
  38. * @returns a list of vipr properties as an array in a format useable by
  39. * the properties model.
  40. */
  41. getPropertiesFromDefintion: function getPropertiesFromDefintion(visId) {
  42. var PropCreatorInstance = PropertiesCreator.getInstance();
  43. var ColorPropertiesCreatorInstance = ColorPropertiesCreator.getInstance();
  44. var items = [];
  45. // We need to use known property IDs so they match what we have supported
  46. // previously or have added. Vida handles property upgrade on the fly.
  47. var configuration = VIPRConfig.getConfig(visId) || {};
  48. if (configuration && configuration.config && configuration.config.include) {
  49. configuration.config.include.forEach(function (propertyId) {
  50. var propDesc = instance._createGenericProperty(propertyId);
  51. var property = VIPRUtils.overrideProperty(visId, propDesc, propertyId);
  52. items.push(property);
  53. });
  54. }
  55. items.push(PropCreatorInstance.getMaintainAxisScaleProperty());
  56. // If there are single palettes create the individual props from config.
  57. var singlePaletteProps = VIPRUtils.getSinglePaletteProperties(visId);
  58. if (singlePaletteProps) {
  59. Object.values(singlePaletteProps).forEach(function (palettePropDesc) {
  60. items.push(ColorPropertiesCreatorInstance.getSinglePaletteColorProperty(palettePropDesc));
  61. });
  62. } else {
  63. // Otherwise create the default one.
  64. items.push(ColorPropertiesCreatorInstance.getDefaultPaletteIndexProperty());
  65. }
  66. var colorPaletteProperties = ColorPropertiesCreatorInstance.getColorPalettePropertiesFromVisId(visId);
  67. if (colorPaletteProperties && colorPaletteProperties.length) {
  68. items = items.concat(colorPaletteProperties);
  69. }
  70. if (configuration.propertyList) {
  71. items = items.concat(configuration.propertyList);
  72. }
  73. return items;
  74. },
  75. getVizDefPropertiesFromDefinition: function getVizDefPropertiesFromDefinition(viprDefinition) {
  76. var items = [];
  77. var configuration = VIPRConfig.getConfig(viprDefinition.id) || {};
  78. if (configuration && configuration.config && configuration.config.include) {
  79. configuration.config.include.forEach(function (propertyId) {
  80. var propDesc = instance._createGenericProperty(propertyId);
  81. var vizDefPropDesc = viprDefinition.vizDef.getProperty(propertyId) || {};
  82. propDesc.active = true;
  83. propDesc = _.extend(vizDefPropDesc, propDesc);
  84. var property = VIPRUtils.overrideProperty(viprDefinition.id, propDesc, propertyId);
  85. items.push(property);
  86. });
  87. }
  88. // augment this array to match viprwidget.properties object functionality
  89. items.get = function (id) {
  90. return items.find(function (item) {
  91. return item.id === id;
  92. });
  93. };
  94. return items;
  95. }
  96. };
  97. }
  98. return {
  99. // Get the Singleton instance if one exists
  100. // or create one if it doesn't
  101. getInstance: function getInstance() {
  102. if (!instance) {
  103. instance = init();
  104. }
  105. return instance;
  106. }
  107. };
  108. }();
  109. return PropertiesCreatorFromVisDef;
  110. });
  111. //# sourceMappingURL=PropertiesCreatorFromVisDefinition.js.map