DrillThroughService.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. 'use strict';
  2. /**
  3. * Licensed Materials - Property of IBM
  4. * IBM Cognos Products: dashboard
  5. * (C) Copyright IBM Corp. 2018, 2020
  6. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  7. */
  8. define(['../../lib/@waca/core-client/js/core-client/ui/core/Class', '../controls/DrillThroughController', '../managers/DrillThroughMappingManager', '../../util/JumpToActionHelper', 'underscore'], function (BaseClass, DrillThroughController, DrillThroughMappingManager, JumpToActionHelper, _) {
  9. var DrillThroughService = BaseClass.extend({
  10. /**
  11. * The drill through service extension which manages access to the drill through APIs and managers
  12. * initialized with its model (as defined in the boardModelExtension).
  13. */
  14. init: function init(options) {
  15. DrillThroughService.inherited('init', this, arguments);
  16. this.dashboardApi = options.dashboardApi;
  17. this._drillThroughController = new DrillThroughController(this.dashboardApi, this);
  18. this._mappingManager = new DrillThroughMappingManager(this.dashboardApi);
  19. },
  20. /**
  21. * Get public api of drill through model object
  22. */
  23. getDrillThroughModelApi: function getDrillThroughModelApi() {
  24. // TODO - livewidget_cleanup -- there is no need for this to be asynch
  25. if (!this.drillThrough) {
  26. var boardModel = this.dashboardApi.getFeature('internal').getBoardModel();
  27. this.drillThrough = boardModel.get('drillThrough');
  28. }
  29. return Promise.resolve(this.drillThrough.getAPI());
  30. },
  31. /**
  32. * Get public api of drill through model object synchronously
  33. */
  34. getDrillThroughModelApiSync: function getDrillThroughModelApiSync() {
  35. if (this.drillThrough) {
  36. return this.drillThrough.getAPI();
  37. }
  38. this.drillThrough = this.dashboardApi.getFeature('internal').getBoardModel().get('drillThrough');
  39. return this.drillThrough.getAPI();
  40. },
  41. /**
  42. * Get drill through definition by widget and widget groups.
  43. * @param {object} widgetModel - the widget model to get the drill through definitions
  44. */
  45. getDrillThroughByWidgetAndGroups: function getDrillThroughByWidgetAndGroups(widgetModel) {
  46. var boardModel = this.dashboardApi.getFeature('internal').getBoardModel();
  47. var associatedDrillDefinitions = [];
  48. var drillThroughDefinitions = boardModel.drillThrough.getModels();
  49. var eventGroup = boardModel.eventGroups.findGroup(widgetModel.get('id'));
  50. drillThroughDefinitions.forEach(function (drillDefinition) {
  51. if (drillDefinition.scope === 'visualization' && drillDefinition.ownerId === widgetModel.get('id') || drillDefinition.scope === 'connection' && eventGroup && eventGroup.widgetIds.indexOf(drillDefinition.ownerId) > -1 || drillDefinition.scope === 'dashboard') {
  52. // Clean mappings from current dashboard if it doesn't included in the widgetModel
  53. if (drillDefinition.mappings && drillDefinition.mappings.length) {
  54. drillDefinition.mappings = JSON.parse(JSON.stringify(drillDefinition.mappings));
  55. drillDefinition.mappings.forEach(function (mapping) {
  56. if (mapping.mapTo) {
  57. // Search in projected data items
  58. var widgetDataViews = widgetModel.data && widgetModel.data.dataViews;
  59. var dataItemInWidget = void 0;
  60. _.find(widgetDataViews, function (dataView) {
  61. return _.find(dataView && dataView.dataItems, function (dataItem) {
  62. var mappedToDataItem = dataItem && dataItem.itemId === mapping.mapTo;
  63. if (mappedToDataItem) {
  64. dataItemInWidget = dataItem;
  65. }
  66. return mappedToDataItem;
  67. });
  68. });
  69. if (!dataItemInWidget) {
  70. // pageContext are converted to local filters and copied to the new widget so look at the local filters only
  71. // Search in filters
  72. var localFilters = widgetModel.localFilters;
  73. dataItemInWidget = _.find(localFilters, function (localFilter) {
  74. return localFilter.id === mapping.mapTo || localFilter.columnId === mapping.mapTo;
  75. });
  76. if (!dataItemInWidget) {
  77. delete mapping.mapTo;
  78. }
  79. }
  80. }
  81. });
  82. }
  83. associatedDrillDefinitions.push(drillDefinition);
  84. }
  85. });
  86. return associatedDrillDefinitions;
  87. },
  88. /**
  89. * Add drill through definitions when add dashboard fragment
  90. */
  91. addDrillThroughOnAddFragment: function addDrillThroughOnAddFragment(boardModel, sourceIdMap, widgetIdMap) {
  92. var drillThroughEntries = boardModel && boardModel.drillThrough;
  93. if (drillThroughEntries && drillThroughEntries.length) {
  94. var drillThroughModelApi = this.getDrillThroughModelApiSync();
  95. drillThroughEntries.forEach(function (drillDefinition) {
  96. // Update the modelRef to the new sourceId (currently only one source is supported)
  97. var drillThroughDataSource = drillDefinition.modelRefs[0];
  98. if (sourceIdMap && sourceIdMap[drillThroughDataSource]) {
  99. drillDefinition.modelRefs = [sourceIdMap[drillThroughDataSource]];
  100. }
  101. // Preserve the new widget Id as the new drill through definition owner
  102. // when pasting to a new Dashboard.
  103. // For the scenarios such as pasting a widget to same tab or different tab within a Dashboard,
  104. // just do a simple update, i.e. no ownership will be changed.
  105. var options = {};
  106. var newWidgetId = widgetIdMap[drillDefinition.ownerId];
  107. if (newWidgetId) {
  108. options.newOwnerId = newWidgetId;
  109. }
  110. drillThroughModelApi.addUpdateDrillDefinitionEntry(drillDefinition, options);
  111. });
  112. }
  113. },
  114. getDrillThroughController: function getDrillThroughController() {
  115. return this._drillThroughController;
  116. },
  117. getDrillThroughMappingManager: function getDrillThroughMappingManager() {
  118. return this._mappingManager;
  119. },
  120. getJumpToTargets: function getJumpToTargets(options) {
  121. return JumpToActionHelper.getJumpToTargets(options);
  122. },
  123. getDeploymentReferences: function getDeploymentReferences() {
  124. var drillEntries = this.getDrillThroughModelApiSync().getDrillDefinitionEntries();
  125. var userProfile = this.dashboardApi.getGlassCoreSvc('.UserProfile');
  126. var userPreferences = userProfile && userProfile.preferences;
  127. var contentLocale = userPreferences && userPreferences.contentLocale || 'en-us';
  128. var deploymentReferences = [];
  129. drillEntries.forEach(function (entry) {
  130. var _name;
  131. deploymentReferences.push({
  132. objects: [{
  133. type: entry.getType(),
  134. searchPath: 'storeID("' + entry.getAssetId() + '")'
  135. }],
  136. name: (_name = {}, _name[contentLocale] = entry.getAssetId(), _name)
  137. });
  138. });
  139. return deploymentReferences;
  140. }
  141. });
  142. return DrillThroughService;
  143. });
  144. //# sourceMappingURL=DrillThroughService.js.map