'use strict'; var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; /** *+------------------------------------------------------------------------+ *| Licensed Materials - Property of IBM *| IBM Cognos Products: Dashboard *| (C) Copyright IBM Corp. 2017, 2020 *| *| US Government Users Restricted Rights - Use, duplication or disclosure *| restricted by GSA ADP Schedule Contract with IBM Corp. *+------------------------------------------------------------------------+ */ define(['underscore', '../../lib/@waca/core-client/js/core-client/ui/core/Class', 'com/ibm/vida/control/vida', './VIPRConfig', './VIPRLibraries', './VIPRUtils', './VIPRBundleHelper'], function (_, Class, vida, VIPRConfig, VIPRLibraries, VIPRUtils, VIPRBundleHelper) { 'use strict'; var CUSTOM_PREVIEWS = ['visualizationPreview', 'com.ibm.vis.schematicsPreview']; var VIPRProxy = Class.extend({ init: function init() { this._setInitialStates(); }, _setInitialStates: function _setInitialStates() { this._libs = undefined; //Only reset for custom libs so that they can be refreshed this._customLibs = undefined; if (!this.libraries) { this.libraries = { system: [], custom: [] }; } else { //Only reset for custom libs so that they can be refreshed this.libraries.custom = []; } }, initialize: function initialize() { var _this = this; var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; this.dashboardApi = params.dashboardApi; if (params.refresh) { this._setInitialStates(); } if (!this._libs) { this.ajaxSvc = params.dashboardApi.getGlassCoreSvc('.Ajax'); return VIPRBundleHelper.getALLVIPRLibraries({ dashboardApi: params.dashboardApi }).then(function (libraries) { var libsToCreate = _this._getLibsBundleToCreate(libraries, params.refresh); return _this._setRequiredConfigurationDefaults(libsToCreate).then(function () { var configuredLibraries = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; configuredLibraries.forEach(function (lib) { var bundleinfo = VIPRConfig.getConfig(lib.id); lib.config = {}; // Add properties config if there is any if (bundleinfo) { if (bundleinfo.isCustomVis && bundleinfo.bundleInclude) { lib.config['properties'] = bundleinfo.bundleInclude; } else if (bundleinfo.isCustomVis && !bundleinfo.bundleInclude) { // never has to get to this case var logger = _this.dashboardApi.getGlassCoreSvc('.Logger'); logger.error('`Failed to set lib.config.properties'); } else if (!bundleinfo.isCustomVis && bundleinfo.config) { lib.config['properties'] = bundleinfo.config; } } // Add slots config if there is any. if (bundleinfo && bundleinfo.slotConfig) { lib.config['slots'] = bundleinfo.slotConfig; } }); _this._createLibs(configuredLibraries, params.refresh); _this._setLibraries(configuredLibraries); }); }); } return Promise.resolve(); }, updateCustomLibrary: function updateCustomLibrary(id) { var library = this._customLibs[id]; if (library) { this._updateLibrary(library); } }, getLibraries: function getLibraries() { if (!this._libs) { var libs = _extends({}, this._systemLibs, this._customLibs); this._libs = _.indexBy(libs, 'id'); } return this._libs; }, getSystemLibIds: function getSystemLibIds() { return this.libraries ? this.libraries.system.map(function (library) { return library.id; }) : []; }, getCustomLibIds: function getCustomLibIds() { return this.libraries ? this.libraries.custom.map(function (library) { return library.id; }) : []; }, getBundleIds: function getBundleIds() { var libType = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : VIPRLibraries.LIB_TYPE_ALL; var ids = []; if (libType === VIPRLibraries.LIB_TYPE_ALL || libType === VIPRLibraries.LIB_TYPE_SYSTEM) { ids.push.apply(ids, this.getSystemLibIds()); } if (libType === VIPRLibraries.LIB_TYPE_ALL || libType === VIPRLibraries.LIB_TYPE_CUSTOM) { ids.push.apply(ids, this.getCustomLibIds()); } return ids; }, _setLibraries: function _setLibraries() { var _this2 = this; var libraries = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; if (!this._customLibs) { this._customLibs = {}; } if (!this._systemLibs) { this._systemLibs = {}; } libraries.forEach(function (library) { if (library.isSchematic || library.isCustomVis || CUSTOM_PREVIEWS.indexOf(library.id) !== -1) { _this2._customLibs[library.id] = library; _this2.libraries.custom.push(library); } else { _this2._systemLibs[library.id] = library; _this2.libraries.system.push(library); } }); }, _getLibsBundleToCreate: function _getLibsBundleToCreate() { var libraries = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; var refresh = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; if (!refresh) { return libraries; } var libsToCreate = []; libraries.forEach(function (library) { if (library.isSchematic || library.isCustomVis || CUSTOM_PREVIEWS.indexOf(library.id) !== -1) { libsToCreate.push(library); } }); return libsToCreate; }, _createLibs: function _createLibs() { var _this3 = this; var configuredLibraries = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; var refresh = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; if (refresh) { configuredLibraries.forEach(function (library) { _this3._updateLibrary(library); }); } else { _.extend(this, vida.VIPR.create({ bundles: configuredLibraries })); } }, _updateLibrary: function _updateLibrary(library) { var bundle = library || {}; var isSchematic = bundle.isSchematic; if (bundle.id && !isSchematic) { this.unregister(bundle.id); } if (bundle.id && (isSchematic && !this.isRegistered(bundle.id) || !isSchematic)) { this.register(bundle.id, bundle.location, bundle.config, bundle.fileName, bundle); } }, /** * @param {String} viprDefinitionId - Vis bundle id * @param {Object} requiredConfig - contains a list of required configuration tokens/properties * @returns {String} a definition id for a library that should be removed as the user/server doesnt have the required configuration(e.g. without mapbox tokens, the user will not be able to use maps) * Makes a rest call searching for specific configuration tokens on a user's server and sets the corresponding default properties */ _fetchAndApplyRequiredConfigurationDefaults: function _fetchAndApplyRequiredConfigurationDefaults(_ref) { var _this4 = this; var viprDefinitionId = _ref.viprDefinitionId, requiredConfig = _ref.requiredConfig; return Promise.reduce(requiredConfig, function (values, key) { return _this4.dashboardApi.getGlassCoreSvc('.Config').getConfigValue(key).then(function (value) { values[key] = value; return values; }); }, {}).then(function (response) { if (Object.values(response).indexOf('default') === -1) { VIPRUtils.applyRequiredConfiguration(viprDefinitionId, requiredConfig, response); return null; } return viprDefinitionId; }).catch(function () { return viprDefinitionId; }); }, /* * Check each library to see if there is a required configuration. If so, then call fetchAndApplyRequiredConfigurationDefaults * which will gather up any viprdefinitionids that do not have the corresponding configuration tokens--in this case, the id will * be removed from the libraries so that the user cannot use that type of visualization. */ _setRequiredConfigurationDefaults: function _setRequiredConfigurationDefaults(libraries) { var _this5 = this; var libraryRequiredConfigs = libraries.map(function (lib) { return { viprDefinitionId: lib.id, requiredConfig: VIPRUtils.getRequiredConfig(lib.id) }; }).filter(function (libraryRequiredConfig) { return !!libraryRequiredConfig.requiredConfig; }); return Promise.map(libraryRequiredConfigs, function (config) { return _this5._fetchAndApplyRequiredConfigurationDefaults(config); }).filter(function (definition) { return !!definition; }).then(function (definitionsToBeRemoved) { return libraries.filter(function (lib) { return !_.contains(definitionsToBeRemoved, lib.id); }); }); }, createWidget: function createWidget(nodeId, hostAPI) { return new vida.SmallMultipleGrid({ vipr: this, hostAPI: hostAPI }, nodeId); }, unloadBundle: function unloadBundle(id) { this.unload(id); }, loadBundle: function loadBundle(id) { var _this6 = this; return this.load(id).then(function (bundle) { return vida.SmallMultipleGrid.extendVizBundle(bundle); }).catch(function (error) { var logger = _this6.dashboardApi.getGlassCoreSvc('.Logger'); if (id !== 'visualizationPreview') { logger.error(error); } }); } }); // create a singleton instance return new VIPRProxy(); }); //# sourceMappingURL=VIPR.js.map