SaveActionUtil.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. 2020
  6. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  7. */
  8. define(['underscore'], function (_) {
  9. var SaveActionUtil = function () {
  10. function SaveActionUtil() {
  11. _classCallCheck(this, SaveActionUtil);
  12. }
  13. /**
  14. * Saves the dashboard spec to the specified location
  15. *
  16. * @param {object} options
  17. * @param {object} options.ajaxService ajax service
  18. * @param {string} options.name dashboard name
  19. * @param {string} options.url url location to save the dashboard spec
  20. * @param {object} options.dashboard the dashboard api object to be saved
  21. * @param {object} options.overwrite whether or not to overwrite a dashboard with same name and given location
  22. */
  23. SaveActionUtil.saveAs = function saveAs(options) {
  24. var ajaxService = options.ajaxService,
  25. name = options.name,
  26. dashboard = options.dashboard,
  27. overwrite = options.overwrite;
  28. var url = options.url;
  29. var boardModel = dashboard.getFeature('internal').getBoardModel();
  30. var spec = dashboard.getFeature('Serializer').toJSON();
  31. SaveActionUtil.cleanUnusedCustomColors(dashboard, spec, boardModel);
  32. if (name) {
  33. spec.name = name;
  34. }
  35. var cmSpec = SaveActionUtil.getCMSpec(spec, boardModel, true);
  36. url = url + (overwrite ? '?updateAction=replace&ignoreInvalidObjectReference=true' : '?ignoreInvalidObjectReference=true');
  37. return new Promise(function (resolve, reject) {
  38. ajaxService.post(url, {
  39. contentType: 'application/json',
  40. processData: false,
  41. dataType: 'text',
  42. data: JSON.stringify(cmSpec)
  43. }).done(function (data, status, request) {
  44. var url = request.getResponseHeader('location');
  45. var assetId = url.substring(url.lastIndexOf('/') + 1);
  46. boardModel.set({ id: assetId, name: name }, { silent: true });
  47. resolve({
  48. 'status': 'success',
  49. 'name': name,
  50. 'saveRequest': request,
  51. 'assetId': assetId,
  52. 'url': url,
  53. 'savedSpec': cmSpec.specification
  54. });
  55. }).fail(function (ajaxRequestDeferred, request) {
  56. reject({
  57. 'status': 'fail',
  58. 'saveRequest': request,
  59. 'cmSpec': cmSpec
  60. });
  61. });
  62. });
  63. };
  64. SaveActionUtil.getCMSpec = function getCMSpec(spec, boardModel, isSaveAs) {
  65. var contentReferences = boardModel.getContentReferences().map(function (refInfo) {
  66. return refInfo.value;
  67. });
  68. var cmSpec = {
  69. defaultName: spec.name,
  70. type: SaveActionUtil.getAssetType(boardModel.dashboardApi),
  71. specification: JSON.stringify(spec),
  72. deploymentReferences: SaveActionUtil._getAllDeploymentReferences(boardModel.dashboardApi, isSaveAs),
  73. references: contentReferences
  74. };
  75. // Asset tags are used to distinguish the sub types of a dashboard (ie. explore).
  76. var assetTags = SaveActionUtil._getAssetTags(boardModel.dashboardApi);
  77. if (assetTags) {
  78. cmSpec.tags = assetTags;
  79. }
  80. //this is a HACK for WA TODO fix me ... AHHHH HACK! Hacks like this keep me up at night.
  81. //now that you're finished panicking after reading that this is a hack, the reason for it is
  82. //CAL stores properties/metadata and content seperatly, CM doesn't. it was easier to add the hack on
  83. //the dashboard side than the content service side. Sooo.. one day when we have a dashboard server, this should be
  84. //aggregated on there.
  85. if (cmSpec.type === 'page_asset' || cmSpec.type === 'card_group') {
  86. delete cmSpec.specification;
  87. cmSpec.content = spec;
  88. }
  89. return cmSpec;
  90. };
  91. SaveActionUtil.getAssetType = function getAssetType(dashboard) {
  92. return dashboard.getConfiguration('assetType') || 'exploration';
  93. };
  94. SaveActionUtil._getAllDeploymentReferences = function _getAllDeploymentReferences(dashboard, isSaveAs) {
  95. //TODO For cleanup, 'Datasource' and DrillThroughService
  96. //features can register themselves as providers to the DeploymentReferences feature
  97. //TODO rm when ContentStoreReference is avaiable for system tests
  98. var contentStoreReferences = dashboard.getFeature('ContentStoreReferences');
  99. var contentStoreReferencesContrib = contentStoreReferences ? contentStoreReferences.getContentStoreReferences() : [];
  100. return [].concat(dashboard.getFeature('dataSources.deprecated').getDeploymentReferences(isSaveAs), dashboard.getFeature('DrillThroughService').getDeploymentReferences(), contentStoreReferencesContrib);
  101. };
  102. SaveActionUtil._getAssetTags = function _getAssetTags(dashboard) {
  103. return dashboard.getConfiguration('assetTags');
  104. };
  105. /*
  106. * Clean unused custom colors and override the saved spec.
  107. */
  108. SaveActionUtil.cleanUnusedCustomColors = function cleanUnusedCustomColors(dashboard, spec, boardModel) {
  109. var customColor = dashboard.getFeature('CustomColor');
  110. var customPalette = boardModel.properties.customColors.getCustomPaletteDefinition();
  111. if (customPalette && customPalette.ids.length) {
  112. var _colorsUsed;
  113. var colorsUsed = boardModel.getUsedCustomColors(customPalette);
  114. (_colorsUsed = colorsUsed).push.apply(_colorsUsed, customColor.getUsedCustomColors());
  115. colorsUsed = _.uniq(colorsUsed);
  116. if (spec && spec.properties && spec.properties.customColors) {
  117. spec.properties.customColors.colors = colorsUsed;
  118. }
  119. }
  120. };
  121. return SaveActionUtil;
  122. }();
  123. return SaveActionUtil;
  124. });
  125. //# sourceMappingURL=SaveActionUtil.js.map