'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. 2020 * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp. */ define(['../../lib/@waca/core-client/js/core-client/utils/ClassFactory', '../../lib/@waca/core-client/js/core-client/ui/core/Events', '../impl/Dashboard', '../../dashboard/glass/services/DashboardGlassContext', '../deprecated/CanvasAPI', '../../dashboard/layout/authoring/interaction/Controller', '../../dashboard/model/BoardModelFactory', '../../dashboard/widgets/WidgetRegistry', 'jquery', 'underscore'], function (ClassFactory, Events, DashboardController, DashboardGlassContext, DeprecatedCanvasAPI, Controller, BoardModelFactory, WidgetRegistry, $, _) { /** * Dashboard service that can be used to provide a dashboard runtime environment * that is needed to run code that is dashboard dependant (e.g. live widget) but without a dashboard Perspective * The service will require the dashboard perspective json, and use it to provide all the needed extensions and collections */ var RuntimeEnvironment = function () { function RuntimeEnvironment(bootstrapFeatures) { _classCallCheck(this, RuntimeEnvironment); this.bootstrapFeatures = bootstrapFeatures; this.logger = this.bootstrapFeatures['Logger'].getAPI(); //TODO: Convert all dependencies to use glass features instead of the glassContext this.glassFeature = this.bootstrapFeatures['Glass'].getAPI(); } RuntimeEnvironment.prototype._getCSSLinks = function _getCSSLinks(model) { var links = document.getElementsByTagName('link'); var loaded = {}; Array.from(links).forEach(function (item) { loaded[item.getAttribute('href')] = true; loaded[item.getAttribute('href').split('?')[0]] = true; }); var cssStyles = []; if (model.cssStyles) { model.cssStyles.forEach(function (style) { if (!loaded[style]) { cssStyles.push(style); } }); } return cssStyles; }; RuntimeEnvironment.prototype._registerBootstrapFeatures = function _registerBootstrapFeatures(featureLoader) { var _this = this; Object.keys(this.bootstrapFeatures).forEach(function (featureName) { featureLoader.registerFeature(featureName, _this.bootstrapFeatures[featureName], undefined, true); }); }; RuntimeEnvironment.prototype.getRuntimeEnvironment = function getRuntimeEnvironment(options, cbGetDefaultSpec) { var _this2 = this; return this.glassFeature.getCoreService('.Perspective').getModel('dashboard').then(function (model) { // Load all required classes and create the services. return Promise.all(['dashboard-core/js/dashboard/services/ServicesHelper', 'dashboard-core/js/extension/Extensions', 'dashboard-core/js/canvas/CanvasFactory', 'dashboard-core/js/dashboard/loader/BoardLoaderHelper'].map(function (path) { return ClassFactory.loadModule(path); })).then(function (_ref) { var _ServicesHelper = _ref[0], _ExtensionsHelper = _ref[1], CanvasFactory = _ref[2], _BoardModelHelper = _ref[3]; var eventRouter = new Events(); var ServicesHelper = _this2._serviceHelperClass || _ServicesHelper; var ExtensionsHelper = _this2._extensionsHelperClass || _ExtensionsHelper; var BoardModelHelper = _this2._boardModelHelperClass || _BoardModelHelper; var glassContext = _this2.bootstrapFeatures['Glass'].getAPI('internal').getGlassContext(); _this2.dashboardGlassContext = new DashboardGlassContext({ model: model, glassContext: glassContext }); var whenDashboardReady = null; var canvas = void 0; var boardView = { // TODO: this needs to be refactored // When dashboardRuntime service is invoked from outside of dashboard (where no canvas is present) // we still need to make sure the consumers get a canvas API (even though empty), or that might // prevent them from getting access to the runtime entirely. getDeprecatedCanvasWhenReady: function getDeprecatedCanvasWhenReady() { return whenDashboardReady.then(function () { return canvas; }); }, getDeprecatedCanvas: function getDeprecatedCanvas() { return canvas; }, canAuthor: function canAuthor() { return false; }, getBoardInfo: function getBoardInfo() { return { type: 'exploration' }; }, getContent: function getContent() { return {}; }, setPermissions: function setPermissions() {}, setSavedInstance: function setSavedInstance() {} }; var widgetRegistry = new WidgetRegistry(); var dashboardController = new DashboardController({ glassContext: _this2.dashboardGlassContext, view: boardView, eventRouter: eventRouter, appSettings: model.content, widgetRegistry: widgetRegistry, featuresOverride: options && options.featuresOverride }); var internalDashboardAPI = dashboardController.getInternalAPI(); var dashboardApi = dashboardController.getAPI(); var servicesHelper = new ServicesHelper({ appSettings: model.content, logger: _this2.logger, glassContext: _this2.dashboardGlassContext, dashboardAPI: dashboardApi, internalDashboardAPI: internalDashboardAPI }); var services = servicesHelper.createServices(); _this2.dashboardGlassContext.setServices(services.serviceRegistry); dashboardController.setServices(services.serviceRegistry); dashboardController.setFeatureLoader(services.featureLoader); var boardSpec = void 0, assetId = void 0; if (!options) { boardSpec = cbGetDefaultSpec(); } else { boardSpec = options.spec; assetId = options.assetId; } var extensionsHelper = new ExtensionsHelper(dashboardApi); var boardModelFactory = void 0; var env = { internalDashboardAPI: internalDashboardAPI, dashboardApi: dashboardApi, glassContext: _this2.dashboardGlassContext, services: services, serviceRegistry: services.serviceRegistry, eventRouter: eventRouter, cssStyles: _this2._getCSSLinks(model), widgetRegistry: widgetRegistry, dashboardFeatureLoader: services.featureLoader }; whenDashboardReady = widgetRegistry.initialize(dashboardApi).then(function () { boardModelFactory = new BoardModelFactory({ dashboardAPI: dashboardApi, glassContext: glassContext, boardSpec: boardSpec, extensions: extensionsHelper, stringResources: dashboardApi.getDashboardCoreSvc('.StringResources'), eventRouter: eventRouter, logger: _this2.logger, boardId: assetId, widgetRegistry: widgetRegistry }); }).then(extensionsHelper.load.bind(extensionsHelper)).then(function () { env.boardModelExtensions = extensionsHelper.getBoardModelExtensions(); env.layoutExtensions = extensionsHelper.getLayoutViewExtensions() || {}; env.canvasExtensions = extensionsHelper.getCanvasExtensions() || []; }).then(function () { return boardModelFactory.createBoardModel(); }).then(function (boardModelInfo) { env.boardModel = boardModelInfo.boardModel; boardView.boardModel = env.boardModel; }).then(_this2._registerBootstrapFeatures.bind(_this2, dashboardController.featureLoader)).then(servicesHelper.createRuntimeServices.bind(servicesHelper, { dashboardApi: dashboardApi })).then(function () { dashboardController.createContent(env.boardModel); }).then(function () { var factory = new CanvasFactory({ dashboardAPI: dashboardApi, services: env.serviceRegistry, extensions: extensionsHelper, glassContext: env.glassContext, eventRouter: env.eventRouter, appSettings: {}, logger: _this2.logger, boardModel: env.boardModel, featureLoader: services.featureLoader, dashboardContent: dashboardController.getContent() }); return factory.createCanvas().then(function (factoryResponse) { canvas = _.extend(new DeprecatedCanvasAPI(), factoryResponse.canvas); // Add the canvas to the environment env.canvas = canvas; env.internalCanvas = factoryResponse.internalCanvas; // TODO: evaluate where we use this and if we need this stored on the instance or not _this2.interactionController = new Controller({ dashboardAPI: env.dashboardApi, toolbarConfig: env.dashboardApi.getAppConfig('toolbar'), transaction: env.dashboardApi.getFeature('Transaction'), canvas: env.canvas, services: env.serviceRegistry, glassContext: env.glassContext, boardModel: env.boardModel, layoutController: null, eventRouter: env.eventRouter, $el: $('body'), appSettings: {} }); var boardModelHelper = new BoardModelHelper(env.dashboardApi); boardModelHelper.createBoardServices({ boardModel: env.boardModel }); return env; }); }); return whenDashboardReady; }); }); }; return RuntimeEnvironment; }(); return RuntimeEnvironment; }); //# sourceMappingURL=RuntimeEnvironment.js.map