'use strict'; function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } /** * Licensed Materials - Property of IBM * IBM Cognos Products: BI Cloud (C) Copyright IBM Corp. 2018 * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp. */ define(['../../DynamicFileLoader'], function (DynamicFileLoader) { /** * Live widget service that can be used to provide a live widget functionality * outside of the dashboard environment */ var LiveWidgetService = function () { function LiveWidgetService(options) { _classCallCheck(this, LiveWidgetService); this._liveWidgetPreviewClass = options && options.liveWidgetPreviewClass; } LiveWidgetService.prototype.initialize = function initialize(glassContext) { this.glassContext = glassContext; this.logger = this.glassContext.getCoreSvc('.Logger'); return Promise.resolve(); }; LiveWidgetService.prototype._getLiveWidgetPreviewClass = function _getLiveWidgetPreviewClass() { var _this = this; return new Promise(function (resolve) { if (_this._liveWidgetPreviewClass) { return resolve(_this._liveWidgetPreviewClass); } else { return DynamicFileLoader.load(['dashboard-analytics/preview/LiveWidgetPreview']).then(function (modules) { resolve(modules[0]); }); } }); }; LiveWidgetService.prototype._getEnvironment = function _getEnvironment() { return this.glassContext.getSvc('.DashboardRuntime').then(function (svc) { return svc.getRuntimeEnvironment(); }); }; /** * Transform a list of recommendations returned by the smarts recommender into an array of live widget specs * * @param {object[]} - array of recommendations * @param {object} - model information * * { * "assetId": "iD5075523AA154AD091DACC72F97823DC", * "type": "uploadedFile" // Optional - will save a CM query if available. * } * */ LiveWidgetService.prototype.transformRecommendationsToLiveWidgetSpecs = function transformRecommendationsToLiveWidgetSpecs(recommendations, modelInfo) { var _this2 = this; return this._getEnvironment().then(function (env) { var dataSources = env.dashboardApi.getFeature('dataSources.deprecated'); return _this2._getModule(dataSources, modelInfo).then(function (module) { var recommender = env.dashboardApi.getFeature('SmartsRecommender.deprecated'); return recommender.transformRecommendationsToLiveWidgetSpecs(recommendations, module.getSourceId()).then(function (results) { var widgetSpecs = []; // Cleanup the internal model ref and replace it with an inline model info results.forEach(function (_ref) { var spec = _ref.spec; if (spec.data && spec.data.dataViews) { spec.data.dataViews.forEach(function (dataView) { dataView.model = { assetId: modelInfo.assetId, type: modelInfo.type }; delete dataView.modelRef; }); } widgetSpecs.push(spec); }); return widgetSpecs; }); }); }); }; LiveWidgetService.prototype._getModule = function _getModule(dataSources, modelInfo) { var sourceCollection = dataSources.getSourcesCollection(); var sourcesId = sourceCollection.getSourceIdsFromAssetId(modelInfo.assetId); var modelRef = void 0; if (sourcesId.length > 0) { modelRef = sourcesId[0]; } else { modelRef = sourceCollection.addSource(modelInfo); } return dataSources.getModule(modelRef); }; LiveWidgetService.prototype.renderLiveWidgetPreview = function renderLiveWidgetPreview(options) { return this._getLiveWidgetPreviewClass().then(function (_LiveWidgetClass) { var preview = new _LiveWidgetClass(options); return preview.render().then(function () { return preview; }); }); }; return LiveWidgetService; }(); return LiveWidgetService; }); //# sourceMappingURL=LiveWidgetService.js.map