'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. 2019 * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp. */ /* global FormData */ define(['../../../visualizations/vipr/VIPRBundleHelper', '../../../widgets/livewidget/nls/StringResources', '../../../lib/@waca/core-client/js/core-client/ui/ProgressToast'], function (VIPRBundleHelper, StringResources, ProgressToast) { var _logError = function _logError(dashboardApi, error) { var logger = dashboardApi.getGlassCoreSvc('.Logger'); logger.error(error); }; var _getErrorDescription = function _getErrorDescription(error) { var message = JSON.parse(error.message); return message && message.Description ? message.Description : ''; }; return function () { function CustomUtil() { _classCallCheck(this, CustomUtil); } CustomUtil.deleteCustomVis = function deleteCustomVis(dashboardApi, visId, refeshCustomVisCallback) { try { var definition = dashboardApi.getFeature('VisDefinitions').getById(visId); if (!definition) { throw new Error('Cannot update bundle: ' + visId); } var assetId = definition.getProperty('assetId'); return VIPRBundleHelper.deleteCustomBundleVisulization({ id: visId, assetId: assetId, dashboardApi: dashboardApi }).then(refeshCustomVisCallback).then(function (items) { dashboardApi.showToast(StringResources.get('extVisDeleteConfirmation'), { type: 'success' }); return items; }).catch(function (error) { dashboardApi.showToast(StringResources.get('extVisDeleteError', { msg: _getErrorDescription(error) }), { type: 'error' }); _logError(dashboardApi, error); }); } catch (error) { _logError(dashboardApi, error); } }; CustomUtil.addCustomVis = function addCustomVis(fileEvent, dashboardApi, refreshCustomVisDefinitions) { if (fileEvent.target.files.length === 0) return refreshCustomVisDefinitions(); if (!(window.File && window.FileReader && window.FileList && window.Blob)) { console.log('The File APIs are not fully supported in this browser.'); return refreshCustomVisDefinitions(); } var toast = CustomUtil.getProgressToast(fileEvent.target.files[0].name); var data = CustomUtil.getDataFromFileEvent(fileEvent); return VIPRBundleHelper.loadCustomBundleVisualization({ data: data, dashboardApi: dashboardApi }).then(function () { return refreshCustomVisDefinitions( /*refreshAll*/false).then(function (items) { toast.setComplete(100, { duration: 0, completeMsg: StringResources.get('extVisAdd'), isComplete: true }); toast.remove(); return items; }); }).catch(function (error) { toast.fail(StringResources.get('extVisAddError', { msg: _getErrorDescription(error) })); _logError(dashboardApi, error); }); }; CustomUtil.updateCustomVis = function updateCustomVis(fileEvent, dashboardApi, refreshCustomVisDefinitions) { var item = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {}; if (fileEvent.target.files.length === 0) return refreshCustomVisDefinitions(); if (!(window.File && window.FileReader && window.FileList && window.Blob)) { console.log('The File APIs are not fully supported in this browser.'); return refreshCustomVisDefinitions(); } var toast = CustomUtil.getProgressToast(item.label); try { var definition = dashboardApi.getFeature('VisDefinitions').getById(item.name); if (!definition) { throw new Error('Cannot update bundle: ' + item.name); } var bundleId = definition.getId(); var data = CustomUtil.getDataFromFileEvent(fileEvent); return VIPRBundleHelper.updateCustomBundleVisualization({ data: data, dashboardApi: dashboardApi, bundleId: bundleId, assetId: definition.getProperty('assetId') }).then(function () { return refreshCustomVisDefinitions( /*refreshAll*/false).then(function (items) { toast.setComplete(100, { duration: 0, completeMsg: StringResources.get('extVisUpdate'), isComplete: true }); void items; toast.remove(); return items; }); }).catch(function (error) { toast.fail(StringResources.get('extVisUpdateError', { msg: _getErrorDescription(error) })); _logError(dashboardApi, error); }); } catch (error) { _logError(dashboardApi, error); } }; CustomUtil.getDataFromFileEvent = function getDataFromFileEvent(fileEvent) { var file = fileEvent.target.files[0]; var formData = new FormData(); formData.append(file.name, file); return formData; }; CustomUtil.getProgressToast = function getProgressToast(name) { var toast = new ProgressToast({ noCancelBtn: true }); var addProgressString = StringResources.get('extVisUpdateProgress', { 'name': name }); toast.show(addProgressString); toast.indefinite(addProgressString); return toast; }; return CustomUtil; }(); }); //# sourceMappingURL=CustomUtil.js.map