CustomUtil.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. 'use strict';
  2. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  3. /**
  4. * Licensed Materials - Property of IBM
  5. * IBM Cognos Products: BI Cloud (C) Copyright IBM Corp. 2019
  6. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  7. */
  8. /* global FormData */
  9. define(['../../../visualizations/vipr/VIPRBundleHelper', '../../../widgets/livewidget/nls/StringResources', '../../../lib/@waca/core-client/js/core-client/ui/ProgressToast'], function (VIPRBundleHelper, StringResources, ProgressToast) {
  10. var _logError = function _logError(dashboardApi, error) {
  11. var logger = dashboardApi.getGlassCoreSvc('.Logger');
  12. logger.error(error);
  13. };
  14. var _getErrorDescription = function _getErrorDescription(error) {
  15. var message = JSON.parse(error.message);
  16. return message && message.Description ? message.Description : '';
  17. };
  18. return function () {
  19. function CustomUtil() {
  20. _classCallCheck(this, CustomUtil);
  21. }
  22. CustomUtil.deleteCustomVis = function deleteCustomVis(dashboardApi, visId, refeshCustomVisCallback) {
  23. try {
  24. var definition = dashboardApi.getFeature('VisDefinitions').getById(visId);
  25. if (!definition) {
  26. throw new Error('Cannot update bundle: ' + visId);
  27. }
  28. var assetId = definition.getProperty('assetId');
  29. return VIPRBundleHelper.deleteCustomBundleVisulization({ id: visId, assetId: assetId, dashboardApi: dashboardApi }).then(refeshCustomVisCallback).then(function (items) {
  30. dashboardApi.showToast(StringResources.get('extVisDeleteConfirmation'), { type: 'success' });
  31. return items;
  32. }).catch(function (error) {
  33. dashboardApi.showToast(StringResources.get('extVisDeleteError', { msg: _getErrorDescription(error) }), { type: 'error' });
  34. _logError(dashboardApi, error);
  35. });
  36. } catch (error) {
  37. _logError(dashboardApi, error);
  38. }
  39. };
  40. CustomUtil.addCustomVis = function addCustomVis(fileEvent, dashboardApi, refreshCustomVisDefinitions) {
  41. if (fileEvent.target.files.length === 0) return refreshCustomVisDefinitions();
  42. if (!(window.File && window.FileReader && window.FileList && window.Blob)) {
  43. console.log('The File APIs are not fully supported in this browser.');
  44. return refreshCustomVisDefinitions();
  45. }
  46. var toast = CustomUtil.getProgressToast(fileEvent.target.files[0].name);
  47. var data = CustomUtil.getDataFromFileEvent(fileEvent);
  48. return VIPRBundleHelper.loadCustomBundleVisualization({ data: data, dashboardApi: dashboardApi }).then(function () {
  49. return refreshCustomVisDefinitions( /*refreshAll*/false).then(function (items) {
  50. toast.setComplete(100, {
  51. duration: 0,
  52. completeMsg: StringResources.get('extVisAdd'),
  53. isComplete: true
  54. });
  55. toast.remove();
  56. return items;
  57. });
  58. }).catch(function (error) {
  59. toast.fail(StringResources.get('extVisAddError', { msg: _getErrorDescription(error) }));
  60. _logError(dashboardApi, error);
  61. });
  62. };
  63. CustomUtil.updateCustomVis = function updateCustomVis(fileEvent, dashboardApi, refreshCustomVisDefinitions) {
  64. var item = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
  65. if (fileEvent.target.files.length === 0) return refreshCustomVisDefinitions();
  66. if (!(window.File && window.FileReader && window.FileList && window.Blob)) {
  67. console.log('The File APIs are not fully supported in this browser.');
  68. return refreshCustomVisDefinitions();
  69. }
  70. var toast = CustomUtil.getProgressToast(item.label);
  71. try {
  72. var definition = dashboardApi.getFeature('VisDefinitions').getById(item.name);
  73. if (!definition) {
  74. throw new Error('Cannot update bundle: ' + item.name);
  75. }
  76. var bundleId = definition.getId();
  77. var data = CustomUtil.getDataFromFileEvent(fileEvent);
  78. return VIPRBundleHelper.updateCustomBundleVisualization({ data: data, dashboardApi: dashboardApi, bundleId: bundleId, assetId: definition.getProperty('assetId') }).then(function () {
  79. return refreshCustomVisDefinitions( /*refreshAll*/false).then(function (items) {
  80. toast.setComplete(100, {
  81. duration: 0,
  82. completeMsg: StringResources.get('extVisUpdate'),
  83. isComplete: true
  84. });
  85. void items;
  86. toast.remove();
  87. return items;
  88. });
  89. }).catch(function (error) {
  90. toast.fail(StringResources.get('extVisUpdateError', { msg: _getErrorDescription(error) }));
  91. _logError(dashboardApi, error);
  92. });
  93. } catch (error) {
  94. _logError(dashboardApi, error);
  95. }
  96. };
  97. CustomUtil.getDataFromFileEvent = function getDataFromFileEvent(fileEvent) {
  98. var file = fileEvent.target.files[0];
  99. var formData = new FormData();
  100. formData.append(file.name, file);
  101. return formData;
  102. };
  103. CustomUtil.getProgressToast = function getProgressToast(name) {
  104. var toast = new ProgressToast({
  105. noCancelBtn: true
  106. });
  107. var addProgressString = StringResources.get('extVisUpdateProgress', { 'name': name });
  108. toast.show(addProgressString);
  109. toast.indefinite(addProgressString);
  110. return toast;
  111. };
  112. return CustomUtil;
  113. }();
  114. });
  115. //# sourceMappingURL=CustomUtil.js.map