VisModelManagerLeftover.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. 'use strict';
  2. /**
  3. * Licensed Materials - Property of IBM
  4. * IBM Cognos Products: Dashboard
  5. * (C) Copyright IBM Corp. 2013, 2019
  6. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  7. *
  8. * VisModelManager
  9. * INTENT: VisModelManager object is a top-level manager for the LiveWidgetModel and its sub-managers.
  10. * It has an api for use by external callers and the renderer.
  11. * TODO: The functionality in this class should be investigated and moved into more focused sub-objects.
  12. *
  13. * The VisModelManager owns VisDataSlots and VisProperties that the renderer pieces render,
  14. * interactivity modifies and save persists.
  15. *
  16. * It also has utilities to ease access for Visualization items to data-oriented actions
  17. * (like querying/filtering).
  18. */
  19. define(['underscore', '../../../../lib/@waca/core-client/js/core-client/ui/core/Events'], function (_, Events) {
  20. 'use strict';
  21. var VisModelManagerLeftover = null; // class declaration
  22. VisModelManagerLeftover = Events.extend({
  23. init: function init(options) {
  24. VisModelManagerLeftover.inherited('init', this, arguments);
  25. this.content = options.content;
  26. this.widgetModel = options.widgetModel;
  27. this.ownerWidget = options.ownerWidget;
  28. this.dashboardApi = options.dashboardApi;
  29. },
  30. /**
  31. *
  32. * @returns customDataSelection is enable or not
  33. */
  34. _isCustomDataSelectionEnabled: function _isCustomDataSelectionEnabled() {
  35. return this.dashboardApi.getAppConfig('enableCustomDataSelection') || this.supportsCustomDataSelection();
  36. },
  37. supportsCustomDataSelection: function supportsCustomDataSelection() {
  38. return this.content.getFeature('Visualization').getDefinition().getProperty('supportsCustomDataSelection');
  39. },
  40. /**
  41. *
  42. * @returns an array of object with the id of the custom data and the value of the decoration
  43. */
  44. getCustomDataDecoration: function getCustomDataDecoration(decoration) {
  45. var decorations = [];
  46. if (this._isCustomDataSelectionEnabled()) {
  47. var customData = this.widgetModel.get('customData');
  48. if (customData && customData[decoration]) {
  49. customData[decoration].forEach(function (item) {
  50. decorations.push({
  51. id: item.id,
  52. value: item.value
  53. });
  54. });
  55. }
  56. }
  57. return decorations;
  58. },
  59. /**
  60. * Sets a decoration value for a give array of custom data ids.
  61. * Any other exiting custom data that had that decoration will be cleared.
  62. * The decorations will be saved in the widget model and persisted.
  63. * @param {String} decorationName - decoration name
  64. * @param {Array} customDataDecorations - array of object that contain the id of the customData and the value of the decoration
  65. * @param {Object} options - model set options (optional)
  66. *
  67. */
  68. setCustomDataDecoration: function setCustomDataDecoration(decorationName, customDataDecorations, options) {
  69. if (this._isCustomDataSelectionEnabled() && this._isCustomDataUpdateNeeded(customDataDecorations)) {
  70. // Save to the widget model
  71. var idArray = customDataDecorations.map(function (decoration) {
  72. return {
  73. id: decoration.id,
  74. value: decoration.value
  75. };
  76. });
  77. var payload = {};
  78. payload[decorationName] = idArray;
  79. this.widgetModel.set({
  80. 'customData': payload
  81. }, options);
  82. }
  83. },
  84. /**
  85. * Get all custom data that have the given decoration set
  86. * @param {String} decorationname
  87. * @return {Array} array of custom data objects
  88. */
  89. getDecoratedCustomData: function getDecoratedCustomData(decorationName) {
  90. var decorations = this.getCustomDataDecoration(decorationName);
  91. var ids = decorations.map(function (decoration) {
  92. return decoration.id;
  93. });
  94. return this.getCustomData(ids);
  95. },
  96. /**
  97. * Get the custom data Listen
  98. * @param {Array} ids - optional array of ids to match the custom data
  99. */
  100. getCustomData: function getCustomData(ids) {
  101. var customData = this.ownerWidget.getCustomData() || [];
  102. // If we have an explicit list of ids, filter the custom data using that list
  103. if (ids) {
  104. customData = customData.filter(function (payload) {
  105. return ids.find(function (id) {
  106. return id === payload.id;
  107. });
  108. });
  109. }
  110. return customData;
  111. },
  112. /**
  113. * Verifies if custom data in the model should be updated
  114. * @param {Map} Map containing custom data
  115. * @return {Boolean} True if model custom data needs to be updated, false otherwise
  116. */
  117. _isCustomDataUpdateNeeded: function _isCustomDataUpdateNeeded(customDataDecorations) {
  118. var currCustomData = this.widgetModel.get('customData');
  119. return currCustomData && currCustomData.selected && currCustomData.selected.length || customDataDecorations && customDataDecorations.length;
  120. }
  121. });
  122. return VisModelManagerLeftover;
  123. });
  124. //# sourceMappingURL=VisModelManagerLeftover.js.map