SourcesCollectionManager.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. 'use strict';
  2. /*
  3. *+------------------------------------------------------------------------+
  4. *| Licensed Materials - Property of IBM
  5. *| IBM Cognos Products: Dashboard
  6. *| (C) Copyright IBM Corp. 2017 - 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(['../../../lib/@waca/core-client/js/core-client/ui/core/Class', '../SourceModelAPI', '../../models/sources/SourceModel', './EmbeddedModuleManager'], function (Class, SourceModelAPI, SourceModel, EmbeddedModuleManager) {
  13. 'use strict';
  14. var SourcesCollectionManager = Class.extend({
  15. init: function init(options) {
  16. SourcesCollectionManager.inherited('init', this, arguments);
  17. this.sourcesCollection = options.sourcesCollection;
  18. this.dashboardApi = options.dashboardApi;
  19. this._sourceModelAPIs = {};
  20. //Session usage state
  21. this.widgetSources = {};
  22. this.sourceUsage = {};
  23. },
  24. /**
  25. * Returns a sourceAPI object for the given id
  26. * @param {String} id the id of the source
  27. * @return {Object} sourceAPI object
  28. */
  29. getSource: function getSource(id) {
  30. if (!this._sourceModelAPIs[id]) {
  31. var sourceModel = this.sourcesCollection.get(id);
  32. if (!sourceModel) {
  33. return null;
  34. }
  35. this._sourceModelAPIs[id] = new SourceModelAPI({
  36. sourceModel: sourceModel,
  37. dashboardApi: this.dashboardApi,
  38. sourcesCollectionManager: this
  39. });
  40. }
  41. return this._sourceModelAPIs[id];
  42. },
  43. /**
  44. * Returns all the sources with a certain assetId
  45. * @param {String} assetId
  46. * @return {[SourceModel]}
  47. */
  48. getSourceModelsByAssetId: function getSourceModelsByAssetId(assetId) {
  49. var models = this.sourcesCollection.getModels();
  50. var sources = [];
  51. for (var i = 0; i < models.length; i++) {
  52. if (models[i].get('assetId') === assetId) {
  53. sources.push(models[i]);
  54. }
  55. }
  56. return sources;
  57. },
  58. /**
  59. * Returns the list of sourceId that match a given assetId. This is needed since we can have multiple sources that point to the same assetId.
  60. * These sources should still talk to each via events.
  61. * @param {String} assetId
  62. * @return {[String]} The sourceIds
  63. */
  64. getSourceIdsFromAssetId: function getSourceIdsFromAssetId(assetId) {
  65. var models = this.sourcesCollection.getModels();
  66. var sourceIds = [];
  67. for (var i = 0; i < models.length; i++) {
  68. if (models[i].get('assetId') === assetId) {
  69. sourceIds.push(models[i].id);
  70. }
  71. }
  72. return sourceIds;
  73. },
  74. /**
  75. * Returns an array of sourceAPI objects
  76. * @return {Array} array of sourceAPI objects
  77. */
  78. getSources: function getSources() {
  79. var _this = this;
  80. var sources = [];
  81. var models = this.sourcesCollection.getModels();
  82. if (models) {
  83. models.forEach(function (model) {
  84. sources.push(_this.getSource(model.get('id')));
  85. });
  86. }
  87. return sources;
  88. },
  89. /**
  90. * Remove a source model from the collection.
  91. * @param {String} id The id of the source to remove
  92. * @param {object} options These options will be passed to the collection remove method. Look at Collection.js for description.
  93. */
  94. removeSource: function removeSource(id, options) {
  95. if (this.isSourceUsed(id)) {
  96. return; //Do not allow the removal of a source that is in use.
  97. }
  98. var source = this.sourcesCollection.get(id);
  99. if (source) {
  100. if (source.shaping) {
  101. EmbeddedModuleManager.deleteEmbeddedModule(source.shaping, {
  102. ajaxSvc: this.dashboardApi.getGlassCoreSvc('.Ajax'),
  103. logger: this.logger
  104. });
  105. source.shaping.destroy();
  106. }
  107. this.sourcesCollection.remove(source, options);
  108. delete this._sourceModelAPIs[id];
  109. }
  110. },
  111. /**
  112. * Register a source as being used by the specified widget. If any other source was previously registered, remove that registration first.
  113. *
  114. * @param {String} widgetId The id of the widget.
  115. * @param {String} sourceId The id of the data source
  116. */
  117. registerUsage: function registerUsage(widgetId, sourceId) {
  118. if (this.widgetSources[widgetId]) {
  119. this.sourceUsage[this.widgetSources[widgetId]]--;
  120. }
  121. this.widgetSources[widgetId] = sourceId;
  122. if (!this.sourceUsage[sourceId]) {
  123. this.sourceUsage[sourceId] = 1;
  124. } else {
  125. this.sourceUsage[sourceId]++;
  126. }
  127. },
  128. /**
  129. * De-register any source used by the specified widget.
  130. *
  131. * @param {String} widgetId The id of the widget.
  132. */
  133. deregisterUsage: function deregisterUsage(widgetId) {
  134. if (this.widgetSources[widgetId]) {
  135. this.sourceUsage[this.widgetSources[widgetId]]--;
  136. delete this.widgetSources[widgetId];
  137. }
  138. },
  139. /**
  140. * Check if a source is currently being used.
  141. * @param {String} sourceId The id of the data source
  142. * @returns {Boolean} True if the source is used.
  143. */
  144. isSourceUsed: function isSourceUsed(sourceId) {
  145. return this.sourceUsage[sourceId] > 0;
  146. },
  147. /**
  148. * Retrieve the id of the source being used by a widget.
  149. * @param {String} widgetId The id of the widget.
  150. * @returns {String} The id of the source used by the widget, if any.
  151. */
  152. usesSource: function usesSource(widgetId) {
  153. return this.widgetSources[widgetId];
  154. },
  155. /**
  156. * Adds the needed sources for the pin being added to the dashboard
  157. * @param {Object} modelFragmentSpec the modelFragment from the dropped pin
  158. * @param {Object} options contains the payload
  159. * @return {Object} Returns a map of sourceIDs pinnedSourceId to existingSourceId
  160. */
  161. addSourcesForPin: function addSourcesForPin(modelFragmentSpec, options) {
  162. var _this2 = this;
  163. var pinnedSources = modelFragmentSpec.dataSources ? modelFragmentSpec.dataSources.sources : [];
  164. var pinnedSourceIdMap = {};
  165. pinnedSources.forEach(function (pinnedSource) {
  166. var pinnedAssetId = pinnedSource.assetId;
  167. var existingSources = _this2.getSourceModelsByAssetId(pinnedAssetId);
  168. var newSourceModel = null;
  169. var shapingId = pinnedSource.shaping ? pinnedSource.shaping.shapingId : undefined;
  170. // Get rid of all traces of the embedded module so that we use the moserJSON in the spec
  171. if (pinnedSource.shaping) {
  172. delete pinnedSource.shaping.embeddedModuleId;
  173. delete pinnedSource.shaping.embeddedModuleName;
  174. // In a copy paste or a pin scenario, we are guaranteed to be in authoring mode.
  175. // However, an API user may be doing an addContent from consumption mode as custom apps do not have this notion.
  176. // Ensures we hit case #4 in ShapingModelManager meaning the shaping model gets loaded.
  177. pinnedSource.shaping.embeddedModuleUpToDate = false;
  178. }
  179. // If there's no existing source with the same assetId simply add the pinned one as a new source
  180. if (existingSources.length === 0) {
  181. // Keep track of the source dashboard name of where the pin came from in the source has shaping
  182. if (shapingId) {
  183. pinnedSource.pinSourceName = modelFragmentSpec.sourceName;
  184. }
  185. newSourceModel = new SourceModel(pinnedSource);
  186. _this2.sourcesCollection.add(newSourceModel, options);
  187. } else {
  188. var matchingSource = null;
  189. for (var i = 0; i < existingSources.length; i++) {
  190. var existingSource = existingSources[i];
  191. var existingSourceShapingId = existingSource.shaping ? existingSource.shaping.shapingId : undefined;
  192. // Only link the pin to an existing source if they both have the same shapingId
  193. if (shapingId === existingSourceShapingId) {
  194. matchingSource = existingSource;
  195. break;
  196. }
  197. }
  198. if (matchingSource) {
  199. pinnedSourceIdMap[pinnedSource.id] = matchingSource.id;
  200. } else {
  201. var pinnedSourceInitialId = pinnedSource.id;
  202. // Delete the pinned source ID so a new gets generated
  203. pinnedSource.id = undefined;
  204. // Keep track of the source dashboard name of where the pin came from
  205. pinnedSource.pinSourceName = modelFragmentSpec.sourceName;
  206. newSourceModel = new SourceModel(pinnedSource);
  207. _this2.sourcesCollection.add(newSourceModel, options);
  208. pinnedSourceIdMap[pinnedSourceInitialId] = newSourceModel.id;
  209. }
  210. }
  211. });
  212. return pinnedSourceIdMap;
  213. }
  214. });
  215. return SourcesCollectionManager;
  216. });
  217. //# sourceMappingURL=SourcesCollectionManager.js.map