DataSourcesController.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. 'use strict';
  2. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  3. function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
  4. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
  5. /**
  6. * Licensed Materials - Property of IBM
  7. * IBM Cognos Products: Dashboard
  8. * (C) Copyright IBM Corp. 2019, 2020
  9. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  10. */
  11. define(['underscore', '../ExtensionBaseController', './DataSourcesView'], function (_, BaseController, DataSourcesView) {
  12. 'use strict';
  13. var SOURCE_TYPE = {
  14. PACKAGE: 'package'
  15. };
  16. var DataSourcesController = function (_BaseController) {
  17. _inherits(DataSourcesController, _BaseController);
  18. function DataSourcesController(dashboard, appSettings, eventRouter) {
  19. _classCallCheck(this, DataSourcesController);
  20. var _this = _possibleConstructorReturn(this, _BaseController.call(this, dashboard, appSettings, eventRouter));
  21. _this.dataSourcesFeature = _this.dashboard.getFeature('DataSources');
  22. _this.emptyDatasetRenderer = DataSourcesView.renderEmptyDataset;
  23. _this.MUIDataGridRender = DataSourcesView.renderMUIGrid;
  24. return _this;
  25. }
  26. /**
  27. * @see ExtensionBaseController
  28. */
  29. DataSourcesController.prototype.getTabContrib = function getTabContrib(event, eventName, currentTabId) {
  30. var _this2 = this;
  31. return Promise.all([this._prepareDataSources(), this._getSelectedContent()]).then(function (deps) {
  32. var tabContribs = deps[0];
  33. var content = deps[1];
  34. if (!tabContribs.length) {
  35. return null;
  36. }
  37. var selectedTab = null;
  38. var activeSourceId = null;
  39. switch (eventName) {
  40. case 'dataSourcePanel:dataSourceSelected':
  41. if (event && event.sender) {
  42. selectedTab = _.findWhere(tabContribs, { id: event.sender });
  43. selectedTab.isSelectedOnTrayRender = true;
  44. } else {
  45. selectedTab = _.findWhere(tabContribs, { id: currentTabId });
  46. if (selectedTab) {
  47. selectedTab.isSelectedOnTrayRender = true;
  48. }
  49. }
  50. break;
  51. case 'shapingmodel:changed':
  52. activeSourceId = _this2.dashboard.getActiveDataSourceId();
  53. if (!activeSourceId) {
  54. throw new Error('shapingmodel:changed triggered, but there is no active data source');
  55. }
  56. selectedTab = _.findWhere(tabContribs, { id: activeSourceId });
  57. if (currentTabId === activeSourceId) {
  58. selectedTab.select = _this2.select.bind(_this2, { id: activeSourceId, forceRender: true });
  59. selectedTab.isSelectedOnTrayRender = true;
  60. }
  61. break;
  62. case 'widget:rerendered':
  63. selectedTab = _.findWhere(tabContribs, { id: currentTabId });
  64. //Widget rerendered but a datasource view is the current tab.
  65. //The data source view should still be the active view
  66. if (selectedTab) {
  67. selectedTab.isSelectedOnTrayRender = true;
  68. }
  69. break;
  70. case 'splitterPanel:show':
  71. selectedTab = _.findWhere(tabContribs, { id: currentTabId });
  72. if (selectedTab) {
  73. selectedTab.isSelectedOnTrayRender = true;
  74. selectedTab.select = _this2.select.bind(_this2, { id: currentTabId, forceRender: true });
  75. }
  76. break;
  77. case 'remove':
  78. selectedTab = _.findWhere(tabContribs, { id: currentTabId });
  79. if (selectedTab) {
  80. selectedTab.isSelectedOnTrayRender = true;
  81. selectedTab.select = _this2.select.bind(_this2, { id: currentTabId, forceRender: true });
  82. }
  83. break;
  84. }
  85. if (!selectedTab && !content) {
  86. var _activeSourceId = _this2.dashboard.getActiveDataSourceId();
  87. if (!_activeSourceId) {
  88. selectedTab = tabContribs[0];
  89. } else {
  90. selectedTab = _.findWhere(tabContribs, { id: _activeSourceId });
  91. }
  92. selectedTab.isSelectedOnTrayRender = true;
  93. }
  94. return tabContribs;
  95. });
  96. };
  97. /**
  98. * Given an array of source model object, returns and array of source objects that the tab control expects.
  99. * @return {Promise} promise that will resolve with an array of source object ready for the tab control
  100. */
  101. DataSourcesController.prototype._prepareDataSources = function _prepareDataSources() {
  102. var _this3 = this;
  103. var whenNamesReady = [];
  104. var aPrepDataSources = [];
  105. var getName = function getName(source, item) {
  106. return source.getLocalizedName().then(function (name) {
  107. item.name = name;
  108. item.state = source.getState(); //State can be updated by the getLocalizedName() call, so set it after that call has finished.
  109. }).fail(function () {
  110. item.state = 'error';
  111. });
  112. };
  113. var dataSources = this.dataSourcesFeature.getDataSourceList() || [];
  114. dataSources.forEach(function (dataSource) {
  115. var id = dataSource.getId();
  116. var item = {
  117. type: dataSource.getType(),
  118. id: id,
  119. select: _this3.select.bind(_this3, { id: id })
  120. };
  121. aPrepDataSources.push(item);
  122. whenNamesReady.push(getName(dataSource, item));
  123. });
  124. return Promise.all(whenNamesReady).then(function () {
  125. return aPrepDataSources;
  126. });
  127. };
  128. /**
  129. * Renders the data source grid view.
  130. */
  131. DataSourcesController.prototype.render = function render(context, container) {
  132. var _this4 = this;
  133. var source = this.dataSourcesFeature.getDataSource(context.id);
  134. if (source && source.getType() === SOURCE_TYPE.PACKAGE) {
  135. return this.emptyDatasetRenderer(container);
  136. }
  137. return source.getModule().then(function (module) {
  138. // enableDataQuality is a string
  139. var enableDataQuality = _this4.appSettings.options && _this4.appSettings.options.config ? _this4.appSettings.options.config.enableDataQuality == 'true' : true;
  140. return _this4.MUIDataGridRender(module, enableDataQuality, container, _this4.dashboard);
  141. });
  142. };
  143. /**
  144. * @see ExtensionBaseController
  145. */
  146. DataSourcesController.prototype.select = function select(context, currentTabId, container) {
  147. if (currentTabId === context.id && !context.forceRender) {
  148. return Promise.resolve(false);
  149. }
  150. this.dashboard.getFeature('DataSources').setActiveDataSourceId(context.id);
  151. this.eventRouter.trigger('dataSourceGrid:dataSourceSelected', {
  152. 'sender': context.id
  153. });
  154. return this.render(context, container);
  155. };
  156. return DataSourcesController;
  157. }(BaseController);
  158. return DataSourcesController;
  159. });
  160. //# sourceMappingURL=DataSourcesController.js.map