'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, 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', './LifeCycleManager', '../../lib/@waca/dashboard-common/dist/utils/dnd/DnDManager', '../../lib/@waca/baglass/js/baglass/services/ServiceRegistry', './DashboardStringService', '../../features/FeatureLoader', './InlineFeatures', './DeprecatedInlineFeatures'], function (ClassFactory, LifeCycleManager, DnDManager, ServiceRegistry, DashboardStringService, FeatureLoader, inlineFeatures, deprecatedInlineFeatures) { /** * Helper class used to create the core dashboard services and services registry */ var ServicesHelper = function () { function ServicesHelper(options) { _classCallCheck(this, ServicesHelper); this.glassContext = options.glassContext; this.eventRouter = options.eventRouter; this.appSettings = options.appSettings; this.logger = options.logger; this.dashboardAPI = options.dashboardAPI; this.inlineFeatures = options.inlineFeatures || inlineFeatures; this.internalDashboardAPI = options.internalDashboardAPI; } ServicesHelper.prototype.createServices = function createServices() { var _this = this; var response = {}; response.lifeCycleManager = new LifeCycleManager(); response.dnDManager = new DnDManager(); response.stringService = new DashboardStringService(); var lifeCycleFeature = { registerHandler: response.lifeCycleManager.registerLifeCycleHandler.bind(response.lifeCycleManager), invokeHandlers: response.lifeCycleManager.invokeLifeCycleHandlers.bind(response.lifeCycleManager) }; this.featureLoader = new FeatureLoader({ lifeCycleManager: lifeCycleFeature, featureNamePrefix: 'Dashboard' }); // Temporary -- needed by the live widget priview in order to create a content API // TODO - cleanup the live widget preview to be created without needing this. this.featureLoader.registerFeature('DashboardFeatureLoader.internal', { getAPI: function getAPI() { return _this.featureLoader; } }, null, true); // Some feature require the dashboard api (or the internal API).. // let us added so it can be requested through dependencies this.featureLoader.registerFeature('API', { getAPI: function getAPI() { return _this.dashboardAPI; } }, null, true); this.featureLoader.registerFeature('internal', { getAPI: function getAPI() { return _this.internalDashboardAPI; } }, null, true); // Make the logger availale as a native feature so it could be required as a dependency this.featureLoader.registerFeature('Logger', { getAPI: function getAPI() { return _this.logger; } }, null, true); this.featureLoader.registerFeature('FeatureRegistry.internal', { getAPI: function getAPI() { return _this.featureLoader.getAPI(); } }, null, true); this.featureLoader.registerFeature('LifeCycle', { getAPI: function getAPI() { return lifeCycleFeature; } }, null, true); // Core dashboard services - can be called using getSvcSync this.services = new ServiceRegistry({ services: { 'Notification': { showError: this.glassContext.appController.showErrorMessage.bind(this.glassContext.appController) }, 'Logger': this.logger, '.LifeCycleManager': response.lifeCycleManager, '.DndManager': response.dnDManager, '.StringResources': response.stringService } }); // TODO - remove this services and cleanup the code that calls it this.services.biGlass = { glassContext: this.glassContext }; response.serviceRegistry = this.services; response.featureLoader = this.featureLoader; return response; }; ServicesHelper.prototype._registerServiceCollection = function _registerServiceCollection(dashboardApi, collection, isFeature) { var promises = []; if (collection) { collection.forEach(function (serviceExtension) { if (!serviceExtension.name || !serviceExtension.class) { return; } promises.push(ClassFactory.loadModule(serviceExtension.class).then(function (ServiceExtension) { var _this2 = this; var options = serviceExtension.options || {}; options.services = this.services; options.eventRouter = this.eventRouter; options.glassContext = this.glassContext; options.dashboardApi = dashboardApi; var extension = new ServiceExtension(options); var whenInitialized = void 0; if (isFeature && extension.initialize && typeof extension.initialize === 'function') { whenInitialized = extension.initialize(); } else { whenInitialized = Promise.resolve(); } return whenInitialized.then(function () { var extensionAPI = null; if (isFeature && extension.getAPI && typeof extension.getAPI === 'function') { extensionAPI = extension.getAPI(); } _this2.services.register(serviceExtension.name, extensionAPI || extension); _this2._registerServiceHooks(extension); }); }.bind(this)).catch(function (err) { //continue working even if an extension failed to load this.logger.error(err); // recover // TODO: maybe we don't need to recover. // Please check }.bind(this))); }.bind(this)); } return Promise.all(promises); }; /** * Create runtime services: Register services/features to be used * @public * @param {Object} options Create runtime services options * @param {Object} options.dashboardApi Dashboard API * @param {Object} options.collections Collections used to inject features * @param {Array} options.collections.templates Array of template collection items for injecting template feature * @function ServicesHelper#createRuntimeServices */ ServicesHelper.prototype.createRuntimeServices = function createRuntimeServices(options) { var _this3 = this; var dashboardApi = options.dashboardApi; return dashboardApi.findGlassCollection('com.ibm.bi.dashboard.features').then(function (collectionItems) { var items = collectionItems || []; items = items.concat(_this3.inlineFeatures); items.push({ name: 'InternalProperties', class: 'dashboard-core/js/features/dashboard/dashboardProperties/DashboardInternalProperties', options: { templateCollection: options.collections ? options.collections.templates : [] }, dependencies: ['Properties'] }); items.push({ name: 'ContentTypeRegistry', class: 'dashboard-core/js/features/dashboard/contentTypeRegistry/api/impl/ContentTypeRegistry', options: options.collections ? options.collections.contentTypes : [], dependencies: [] }); items.push({ name: 'DashboardContentProvider', class: 'dashboard-core/js/features/dashboard/contentProvider/api/impl/DashboardContentProvider', dependencies: ['ContentFactory'] }); return _this3.featureLoader.loadFeaturesFromArray(items, dashboardApi.getFeature('internal').getBoardModel().getContentModel().features); }).then(function () { return _this3._getServiceExtensions().then(function (servicesCollection) { return _this3._getFeatureExtensions().then(function (featuresCollection) { var promises = []; promises.push(_this3._registerServiceCollection(dashboardApi, servicesCollection, false)); // TODO - removed once all features under "core-features" are moved to the correct "features" collection promises.push(_this3._registerServiceCollection(dashboardApi, featuresCollection, true)); return Promise.all(promises); }); }); }); }; /** * Deprecated service collection. The new core-features collecion must e userd for new features. */ ServicesHelper.prototype._getServiceExtensions = function _getServiceExtensions() { var promise = Promise.resolve(); if (this.appSettings.options && this.appSettings.options.collections.serviceExtension) { var collectionId = this.appSettings.options.collections.serviceExtension.id; promise = Promise.all([this.glassContext.appController.findCollection(collectionId)]).then(function (extensions) { return extensions[0]; }); } return promise; }; ServicesHelper.prototype._getFeatureExtensions = function _getFeatureExtensions() { var promise = Promise.resolve(deprecatedInlineFeatures); if (this.appSettings.options && this.appSettings.options.collections.featureExtension) { var collectionId = this.appSettings.options.collections.featureExtension.id; promise = Promise.all([this.glassContext.appController.findCollection(collectionId)]).then(function (extensions) { var collection = extensions[0] || []; return deprecatedInlineFeatures.concat(collection); }); } return promise; }; ServicesHelper.prototype._registerServiceHooks = function _registerServiceHooks(serviceExtension) { if (typeof serviceExtension.getLifeCycleHandlers === 'function') { var lifeCycleManager = this.services.getSvcSync('.LifeCycleManager'); var handlers = serviceExtension.getLifeCycleHandlers(); if (!Array.isArray(handlers)) { throw new Error('serviceExtension getLifeCycleHandlers method is expected to return an array'); } handlers.forEach(function (handler) { if (!handler.name || !handler.action) { throw new Error('serviceExtension handlers are expected to have a name and action property'); } lifeCycleManager.registerLifeCycleHandler(handler.name, handler.action, handler.priority); }); } }; return ServicesHelper; }(); return ServicesHelper; }); //# sourceMappingURL=ServicesHelper.js.map