'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 Business Analytics (C) Copyright IBM Corp. 2019, 2020 * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp. */ /** * @class VIPRBundleHelper * @classdesc implementation helper methods to get all bundles and add, update, and delete custom visualization. */ define(['./VIPRConfig', '../../DynamicFileLoader', '../../widgets/livewidget/nls/StringResources', 'underscore'], function (VIPRConfig, DynamicFileLoader, StringResources, _) { var _class, _temp; var baseUrl = 'v1/visualizations'; var _logError = function _logError(dashboardApi, error) { var logger = dashboardApi.getGlassCoreSvc('.Logger'); logger.error(error); }; return _temp = _class = function () { function VIPRBundleHelper() { _classCallCheck(this, VIPRBundleHelper); } /** * @function VIPRBundleHelper#_loadVIPRLibrariesClass * @private * @description load module VIPRLibraries. * * @return {Promise} Promise that will resolve once the class VIPRLibraries has been loaded */ VIPRBundleHelper._loadVIPRLibrariesClass = function _loadVIPRLibrariesClass() { return DynamicFileLoader.load(['dashboard-analytics/visualizations/vipr/VIPRLibraries']).then(function (modules) { return modules[0]; }).catch(function (err) { console.log(err); }); }; /** * @function VIPRBundleHelper#_getVIPRLibraries * @private * @description get list of vipr visualizations. * * @return {Promise} Promise that will resolve once the vipr visualization are loaded */ VIPRBundleHelper._getVIPRLibraries = function _getVIPRLibraries(options) { return VIPRBundleHelper._loadVIPRLibrariesClass().then(function (LibrariesClass) { var viprLibrariesClass = new LibrariesClass({ dashboardApi: options.dashboardApi }); return viprLibrariesClass.setVIPRLibraries(); }).catch(function (err) { _logError(options.dashboardApi, err); }); }; /** * @function _getOnlyEnabledSystemVisualizations * @private * @description Remove any visualizations that are not feature enabled. * @param {Array} arrayOfVisDescriptions - array of visualzations that should be checked. * Any feature with the following property will be checked: * "featureFlag": { * "id": "mentionsVis", * "enabledVal": "enabled" *} * @param {Object} options - hold dashboardApi as a prop which is used for feature checker. */ VIPRBundleHelper._getOnlyEnabledSystemVisualizations = function _getOnlyEnabledSystemVisualizations(arrayOfVisDescriptions, options) { var aEnabledVizs = arrayOfVisDescriptions.filter(function (vis) { if (vis.featureFlag) { var featureFlag = vis.featureFlag ? vis.featureFlag.id : ''; var valueToMatch = vis.featureFlag ? vis.featureFlag.enabledVal : 'enabled'; return options.dashboardApi.getGlassCoreSvc('.FeatureChecker').checkValue('dashboard', featureFlag, valueToMatch); } else { return true; } }); return aEnabledVizs; }; /** * @function VIPRBundleHelper#getALLVIPRLibraries * @private * @description get list of all available visualizations(custom, vipr). * * @return {Promise} Promise that will resolve once the all visualization are loaded */ VIPRBundleHelper.getALLVIPRLibraries = function getALLVIPRLibraries() { var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var libraries = []; var promises = [VIPRBundleHelper._getVIPRLibraries(options)]; promises.push(VIPRBundleHelper.getCustomBundleVisualizations(options)); return Promise.all(promises).then(function (results) { var standardLibraries = results[0]; var customLibraries = results[1]; // Remove any visualizations that are not feature enabled. var enabledSystemVizs = VIPRBundleHelper._getOnlyEnabledSystemVisualizations(standardLibraries.system, options); libraries.push.apply(libraries, enabledSystemVizs); if (standardLibraries['custom'].length > 0) { VIPRBundleHelper._buildCustomLibVIPRConfig(standardLibraries.custom || []); libraries.push.apply(libraries, standardLibraries['custom']); } if (customLibraries.length > 0) { libraries.push.apply(libraries, customLibraries); } return libraries; }).catch(function (err) { _logError(options.dashboardApi, err); throw new Error(err); }); }; /** * @function VIPRBundleHelper#getCustomBundleVisualizations * @public * @description Make rest call to get list of custom visualizations bundle information from the server. * * @returns {Promise} Returns a promise resolved with a JSON object contains custom bundles information from server */ VIPRBundleHelper.getCustomBundleVisualizations = function getCustomBundleVisualizations(options) { var ajaxSvc = options.dashboardApi.getGlassCoreSvc('.Ajax'); return ajaxSvc.ajax({ url: baseUrl + '/custom', type: 'GET', contentType: 'application/json' }).then(function (customVisualizations) { return VIPRBundleHelper._buildCustomBundleVisualizationsLib(ajaxSvc, customVisualizations); }).catch(function (error) { _logError(options.dashboardApi, error); return Promise.resolve([]); }); }; VIPRBundleHelper._buildCustomBundleVisualizationsLib = function _buildCustomBundleVisualizationsLib(ajaxSvc) { var customVisualizationslib = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; var customCustomVisualizationsLib = []; var data = customVisualizationslib.data || []; var promise = void 0; if (data.length === 0) { promise = Promise.resolve(customCustomVisualizationsLib); } else { _.forEach(data, function (lib) { customCustomVisualizationsLib.push({ id: lib.id, location: lib.type === 'schematic' ? 'vizbundles/rave2schematics' : 'v1/visualizations/' + lib.id + '/content/', assetId: lib.storeId, icon: lib.icon || null, placeholderIcon: 'v1/visualizations/' + lib.id + '/content/', modificationTime: lib.modificationTime, name: lib.name, type: lib.id, isSchematic: lib.type === 'schematic', isCustomVis: lib.type !== 'schematic' }); }); promise = VIPRBundleHelper._loadIcon(ajaxSvc, customCustomVisualizationsLib); } return promise.then(VIPRBundleHelper._buildCustomLibVIPRConfig); }; VIPRBundleHelper._loadIcon = function _loadIcon(ajaxSvc) { var customCustomVisualizationsLib = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : []; var promises = []; customCustomVisualizationsLib.forEach(function (lib) { if (!lib.icon && !lib.isSchematic) { //Load only if one not found, //This happens when the icon is defined at nested folder levels and not at the root //when the custom vis was storeed in CM var url = baseUrl + '/id/' + lib.id; promises.push(ajaxSvc.ajax({ url: url, type: 'GET', contentType: 'application/json' })); } else { promises.push(Promise.resolve(null)); } }); return Promise.all(promises).then(function () { var responses = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; responses.forEach(function (response, idx) { if (response) { var data = response.data || []; var icon = data.find(function (fileDesc) { fileDesc = fileDesc || {}; var file = fileDesc.file ? fileDesc.file.toLowerCase() : ''; return VIPRBundleHelper.isValidIconFileExt(file); }); if (icon) { var nameParts = icon.file.split('\\'); customCustomVisualizationsLib[idx].icon = nameParts.join('/'); } } }); return customCustomVisualizationsLib; }); }; VIPRBundleHelper._buildCustomLibVIPRConfig = function _buildCustomLibVIPRConfig() { var customCustomVisualizationsLibs = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; customCustomVisualizationsLibs.forEach(function (lib) { VIPRConfig.removeCustomVisConfiguration(lib.id); VIPRConfig.addCustomVisConfiguration(lib); }); return customCustomVisualizationsLibs; }; /** * @function VIPRBundleHelper#loadCustomBundleVisulization * @public * @description Make rest call to load custom visualization to the server. * * @param {Object} options contains dashboardApi and data of type FromData of selected visualization * @returns {Promise} Returns a promise resolved with id of custom bundles information added on server */ VIPRBundleHelper.loadCustomBundleVisualization = function loadCustomBundleVisualization(options) { return VIPRBundleHelper.submitAjaxAddOrUpdate(options, 'POST', baseUrl + '?maskResponses=true&updateAction=FAIL'); }; /** * @function VIPRBundleHelper#updateCustomBundleVisualization * @public * @description Make rest call to load custom visualization to the server. * * @param {Object} options contains dashboardApi and data of type FromData of selected visualization * @returns {Promise} Returns a promise resolved with id of custom bundles information added on server */ //@todo for R3 will use POST for updating, VIDA will fix the ajax call to use put with dedicated update endpoint VIPRBundleHelper.updateCustomBundleVisualization = function updateCustomBundleVisualization() { var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; return VIPRBundleHelper.submitAjaxAddOrUpdate(options, 'PUT', baseUrl + '/id/' + options.bundleId); }; /** * @function VIPRBundleHelper#deleteCustomBundleVisulization * @public * @description Make rest call to delete custom visualization bundle information from the server. * * @returns {Promise} Returns a promise resolved if custom visualization bundle information deleted from the server, * promise rejected if custom visualization bundle information is not deleted from the server */ VIPRBundleHelper.deleteCustomBundleVisulization = function deleteCustomBundleVisulization() { var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var ajaxSvc = options.dashboardApi.getGlassCoreSvc('.Ajax'); var userCapabilities = options.dashboardApi.getGlassCoreSvc('.UserProfile').capabilities; var hasPermission = userCapabilities.indexOf('canManageVisualizations') !== -1; if (hasPermission) { return ajaxSvc.ajax({ url: 'v1/objects/' + encodeURIComponent(options.assetId), type: 'DELETE' }).then(function () { VIPRConfig.removeCustomVisConfiguration(options.id); }); } else { return Promise.reject(new Error(StringResources.get('noPermissionToManage'))); } }; VIPRBundleHelper.submitAjaxAddOrUpdate = function submitAjaxAddOrUpdate() { var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var type = arguments[1]; var url = arguments[2]; var userCapabilities = options.dashboardApi.getGlassCoreSvc('.UserProfile').capabilities; var hasPermission = userCapabilities.indexOf('canManageVisualizations') !== -1; if (hasPermission) { var ajaxSvc = options.dashboardApi.getGlassCoreSvc('.Ajax'); return ajaxSvc.ajax({ url: url, type: type, data: options.data, enctype: 'multipart/form-data', processData: false, contentType: false }).then(function (info) { var data = JSON.parse(info.data); if (data.Status === 200 || data.Status === 201) { return data; } else if (info.jqXHR && info.jqXHR.responseJSON) { return Promise.reject(new Error(info.jqXHR.responseJSON.msg)); } else if (info.jqXHR && info.jqXHR.responseText) { return Promise.reject(new Error(info.jqXHR.responseText)); } else { return Promise.reject(new Error(data.Description)); } }); } else { return Promise.reject(new Error(StringResources.get('noPermissionToManage'))); } }; return VIPRBundleHelper; }(), _class.isValidIconFileExt = function (file) { var supportIconFileExts = ['.png', '.svg']; return file && !!supportIconFileExts.find(function (ext) { return file.indexOf(ext) !== -1; }); }, _temp; }); //# sourceMappingURL=VIPRBundleHelper.js.map