LiveWidgetService.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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: BI Cloud (C) Copyright IBM Corp. 2018
  6. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  7. */
  8. define(['../../DynamicFileLoader'], function (DynamicFileLoader) {
  9. /**
  10. * Live widget service that can be used to provide a live widget functionality
  11. * outside of the dashboard environment
  12. */
  13. var LiveWidgetService = function () {
  14. function LiveWidgetService(options) {
  15. _classCallCheck(this, LiveWidgetService);
  16. this._liveWidgetPreviewClass = options && options.liveWidgetPreviewClass;
  17. }
  18. LiveWidgetService.prototype.initialize = function initialize(glassContext) {
  19. this.glassContext = glassContext;
  20. this.logger = this.glassContext.getCoreSvc('.Logger');
  21. return Promise.resolve();
  22. };
  23. LiveWidgetService.prototype._getLiveWidgetPreviewClass = function _getLiveWidgetPreviewClass() {
  24. var _this = this;
  25. return new Promise(function (resolve) {
  26. if (_this._liveWidgetPreviewClass) {
  27. return resolve(_this._liveWidgetPreviewClass);
  28. } else {
  29. return DynamicFileLoader.load(['dashboard-analytics/preview/LiveWidgetPreview']).then(function (modules) {
  30. resolve(modules[0]);
  31. });
  32. }
  33. });
  34. };
  35. LiveWidgetService.prototype._getEnvironment = function _getEnvironment() {
  36. return this.glassContext.getSvc('.DashboardRuntime').then(function (svc) {
  37. return svc.getRuntimeEnvironment();
  38. });
  39. };
  40. /**
  41. * Transform a list of recommendations returned by the smarts recommender into an array of live widget specs
  42. *
  43. * @param {object[]} - array of recommendations
  44. * @param {object} - model information
  45. *
  46. * {
  47. * "assetId": "iD5075523AA154AD091DACC72F97823DC",
  48. * "type": "uploadedFile" // Optional - will save a CM query if available.
  49. * }
  50. *
  51. */
  52. LiveWidgetService.prototype.transformRecommendationsToLiveWidgetSpecs = function transformRecommendationsToLiveWidgetSpecs(recommendations, modelInfo) {
  53. var _this2 = this;
  54. return this._getEnvironment().then(function (env) {
  55. var dataSources = env.dashboardApi.getFeature('dataSources.deprecated');
  56. return _this2._getModule(dataSources, modelInfo).then(function (module) {
  57. var recommender = env.dashboardApi.getFeature('SmartsRecommender.deprecated');
  58. return recommender.transformRecommendationsToLiveWidgetSpecs(recommendations, module.getSourceId()).then(function (results) {
  59. var widgetSpecs = [];
  60. // Cleanup the internal model ref and replace it with an inline model info
  61. results.forEach(function (_ref) {
  62. var spec = _ref.spec;
  63. if (spec.data && spec.data.dataViews) {
  64. spec.data.dataViews.forEach(function (dataView) {
  65. dataView.model = {
  66. assetId: modelInfo.assetId,
  67. type: modelInfo.type
  68. };
  69. delete dataView.modelRef;
  70. });
  71. }
  72. widgetSpecs.push(spec);
  73. });
  74. return widgetSpecs;
  75. });
  76. });
  77. });
  78. };
  79. LiveWidgetService.prototype._getModule = function _getModule(dataSources, modelInfo) {
  80. var sourceCollection = dataSources.getSourcesCollection();
  81. var sourcesId = sourceCollection.getSourceIdsFromAssetId(modelInfo.assetId);
  82. var modelRef = void 0;
  83. if (sourcesId.length > 0) {
  84. modelRef = sourcesId[0];
  85. } else {
  86. modelRef = sourceCollection.addSource(modelInfo);
  87. }
  88. return dataSources.getModule(modelRef);
  89. };
  90. LiveWidgetService.prototype.renderLiveWidgetPreview = function renderLiveWidgetPreview(options) {
  91. return this._getLiveWidgetPreviewClass().then(function (_LiveWidgetClass) {
  92. var preview = new _LiveWidgetClass(options);
  93. return preview.render().then(function () {
  94. return preview;
  95. });
  96. });
  97. };
  98. return LiveWidgetService;
  99. }();
  100. return LiveWidgetService;
  101. });
  102. //# sourceMappingURL=LiveWidgetService.js.map