FeatureLoader.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. 'use strict';
  2. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  3. /**
  4. * Licensed Materials - Property of IBM
  5. * IBM Cognos Products: Dashboard
  6. * (C) Copyright IBM Corp. 2018, 2019
  7. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  8. *
  9. */
  10. define(['../../../DynamicFileLoader', './InlineFeatures', 'underscore'], function (DynamicFileLoader, inlineFeatures, _) {
  11. 'use strict';
  12. /**
  13. * @deprecated Replaced by dashboard-core/js/features/FeatureLoader.js
  14. */
  15. var FeatureLoader = function () {
  16. function FeatureLoader(widgetApi, dashboardApi) {
  17. _classCallCheck(this, FeatureLoader);
  18. this.widgetApi = widgetApi;
  19. this.dashboardApi = dashboardApi;
  20. this.features = {};
  21. this.disabledFeatures = {};
  22. }
  23. /**
  24. * Load the live widget features
  25. * @param Array(String) featureSet - list of feature to be loaded. If no featureSet is defined, then we load everything.
  26. *
  27. */
  28. FeatureLoader.prototype.loadFeatures = function loadFeatures(featureSet) {
  29. var _this = this;
  30. return this.dashboardApi.findGlassCollection('com.ibm.bi.dashboard.live-features').then(function (collectionItems) {
  31. var promises = [];
  32. if (collectionItems) {
  33. // Adde the inline features. Inline features are a quick and easy way to test features without having to add the perspective json
  34. collectionItems = collectionItems.concat(inlineFeatures);
  35. collectionItems.forEach(function (feature) {
  36. var identifier = feature.name || feature.id;
  37. if (!featureSet || featureSet.indexOf(identifier) !== -1) {
  38. promises.push(DynamicFileLoader.load([feature.class]).then(function (modules) {
  39. _this.features[identifier] = new modules[0](_this.widgetApi, _this.dashboardApi);
  40. }));
  41. }
  42. });
  43. }
  44. return Promise.all(promises);
  45. });
  46. };
  47. FeatureLoader.prototype.setFeatureEnabled = function setFeatureEnabled(name, isEnabled) {
  48. if (isEnabled) {
  49. delete this.disabledFeatures[name];
  50. } else {
  51. this.disabledFeatures[name] = true;
  52. }
  53. };
  54. FeatureLoader.prototype.unloadFeatures = function unloadFeatures() {
  55. var _this2 = this;
  56. _.each(_.keys(this.features), function (featureName) {
  57. if (_this2.features[featureName].destroy) {
  58. _this2.features[featureName].destroy();
  59. }
  60. delete _this2.features[featureName];
  61. });
  62. this.features = {};
  63. };
  64. FeatureLoader.prototype.setFeature = function setFeature(name, feature) {
  65. this.features[name] = feature;
  66. };
  67. FeatureLoader.prototype.getFeature = function getFeature(name) {
  68. var api;
  69. if (this.features[name] && this.isFeatureEnabled(name) && this.features[name].getAPI) {
  70. api = this.features[name].getAPI();
  71. }
  72. return api;
  73. };
  74. FeatureLoader.prototype.isFeatureEnabled = function isFeatureEnabled(name) {
  75. return !this.disabledFeatures[name] && (!this.features[name].isEnabled || this.features[name].isEnabled());
  76. };
  77. /**
  78. * @param tagToMatch - a tag that any feature must include in its getTags() api.
  79. * @returns any features that match the tag passed in as an array (empty array is returned if no features match).
  80. */
  81. FeatureLoader.prototype.getMatchingFeatures = function getMatchingFeatures(tagToMatch) {
  82. var _this3 = this;
  83. var matchingFeatures = [];
  84. _.each(_.keys(this.features), function (featureName) {
  85. var feature = _this3.features[featureName];
  86. if (feature && _this3.isFeatureEnabled(featureName) && feature.getFeatureTags && feature.getFeatureTags().indexOf(tagToMatch) >= 0) {
  87. var featureAPI = _this3.getFeature(featureName);
  88. if (featureAPI) {
  89. matchingFeatures.push(featureAPI);
  90. }
  91. }
  92. });
  93. return matchingFeatures;
  94. };
  95. /**
  96. * Enable or disable a feature that matches the supplied tag
  97. * @param tagToMatch - a tag that any feature must include in its getTags() api.
  98. * @param isEnabled
  99. */
  100. FeatureLoader.prototype.setMatchingFeaturesEnabled = function setMatchingFeaturesEnabled(tagToMatch, isEnabled) {
  101. var _this4 = this;
  102. _.each(_.keys(this.features), function (featureName) {
  103. var feature = _this4.features[featureName];
  104. if (feature && feature.getFeatureTags && feature.getFeatureTags().indexOf(tagToMatch) >= 0) {
  105. _this4.setFeatureEnabled(featureName, isEnabled);
  106. }
  107. });
  108. };
  109. FeatureLoader.prototype.getExtraRenderSequenceSteps = function getExtraRenderSequenceSteps() {
  110. var steps = [];
  111. for (var name in this.features) {
  112. if (this.isFeatureEnabled(name) && this.features[name].getExtraRenderSequenceSteps) {
  113. var extraSteps = this.features[name].getExtraRenderSequenceSteps();
  114. if (extraSteps) {
  115. steps.push.apply(steps, Array.isArray(extraSteps) ? extraSteps : [extraSteps]);
  116. }
  117. }
  118. }
  119. return steps;
  120. };
  121. /**
  122. * Return an array of render sequence step names that are associated with the supplied tag.
  123. * If enabledFeaturesOnly is set to true, only return the steps associated with enabled features.
  124. * @param tagToMatch - a tag hat any feature must include in its getTags() api.
  125. * @param enabledFeaturesOnly (default false) - if set, only steps for enabled features will be returned.
  126. * @returns an array of step ids for the renderSequence steps that are associated with the features that match the tag.
  127. */
  128. FeatureLoader.prototype.getMatchingFeatureStepIds = function getMatchingFeatureStepIds(tagToMatch) {
  129. var enabledFeaturesOnly = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  130. var stepIds = [];
  131. for (var name in this.features) {
  132. if (!enabledFeaturesOnly || this.isFeatureEnabled(name)) {
  133. var feature = this.features[name];
  134. if (feature && feature.getFeatureTags && feature.getExtraRenderSequenceSteps && feature.getFeatureTags().indexOf(tagToMatch) >= 0) {
  135. var extraSteps = this.features[name].getExtraRenderSequenceSteps();
  136. if (extraSteps) {
  137. extraSteps = Array.isArray(extraSteps) ? extraSteps : [extraSteps];
  138. stepIds.push.apply(stepIds, extraSteps.map(function (step) {
  139. return step.id;
  140. }));
  141. }
  142. }
  143. }
  144. }
  145. return stepIds;
  146. };
  147. return FeatureLoader;
  148. }();
  149. return FeatureLoader;
  150. });
  151. //# sourceMappingURL=FeatureLoader.js.map