VIPRBundleHelper.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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 Business Analytics (C) Copyright IBM Corp. 2019, 2020
  6. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  7. */
  8. /**
  9. * @class VIPRBundleHelper
  10. * @classdesc implementation helper methods to get all bundles and add, update, and delete custom visualization.
  11. */
  12. define(['./VIPRConfig', '../../DynamicFileLoader', '../../widgets/livewidget/nls/StringResources', 'underscore'], function (VIPRConfig, DynamicFileLoader, StringResources, _) {
  13. var _class, _temp;
  14. var baseUrl = 'v1/visualizations';
  15. var _logError = function _logError(dashboardApi, error) {
  16. var logger = dashboardApi.getGlassCoreSvc('.Logger');
  17. logger.error(error);
  18. };
  19. return _temp = _class = function () {
  20. function VIPRBundleHelper() {
  21. _classCallCheck(this, VIPRBundleHelper);
  22. }
  23. /**
  24. * @function VIPRBundleHelper#_loadVIPRLibrariesClass
  25. * @private
  26. * @description load module VIPRLibraries.
  27. *
  28. * @return {Promise} Promise that will resolve once the class VIPRLibraries has been loaded
  29. */
  30. VIPRBundleHelper._loadVIPRLibrariesClass = function _loadVIPRLibrariesClass() {
  31. return DynamicFileLoader.load(['dashboard-analytics/visualizations/vipr/VIPRLibraries']).then(function (modules) {
  32. return modules[0];
  33. }).catch(function (err) {
  34. console.log(err);
  35. });
  36. };
  37. /**
  38. * @function VIPRBundleHelper#_getVIPRLibraries
  39. * @private
  40. * @description get list of vipr visualizations.
  41. *
  42. * @return {Promise} Promise that will resolve once the vipr visualization are loaded
  43. */
  44. VIPRBundleHelper._getVIPRLibraries = function _getVIPRLibraries(options) {
  45. return VIPRBundleHelper._loadVIPRLibrariesClass().then(function (LibrariesClass) {
  46. var viprLibrariesClass = new LibrariesClass({ dashboardApi: options.dashboardApi });
  47. return viprLibrariesClass.setVIPRLibraries();
  48. }).catch(function (err) {
  49. _logError(options.dashboardApi, err);
  50. });
  51. };
  52. /**
  53. * @function _getOnlyEnabledSystemVisualizations
  54. * @private
  55. * @description Remove any visualizations that are not feature enabled.
  56. * @param {Array} arrayOfVisDescriptions - array of visualzations that should be checked.
  57. * Any feature with the following property will be checked:
  58. * "featureFlag": {
  59. * "id": "mentionsVis",
  60. * "enabledVal": "enabled"
  61. *}
  62. * @param {Object} options - hold dashboardApi as a prop which is used for feature checker.
  63. */
  64. VIPRBundleHelper._getOnlyEnabledSystemVisualizations = function _getOnlyEnabledSystemVisualizations(arrayOfVisDescriptions, options) {
  65. var aEnabledVizs = arrayOfVisDescriptions.filter(function (vis) {
  66. if (vis.featureFlag) {
  67. var featureFlag = vis.featureFlag ? vis.featureFlag.id : '';
  68. var valueToMatch = vis.featureFlag ? vis.featureFlag.enabledVal : 'enabled';
  69. return options.dashboardApi.getGlassCoreSvc('.FeatureChecker').checkValue('dashboard', featureFlag, valueToMatch);
  70. } else {
  71. return true;
  72. }
  73. });
  74. return aEnabledVizs;
  75. };
  76. /**
  77. * @function VIPRBundleHelper#getALLVIPRLibraries
  78. * @private
  79. * @description get list of all available visualizations(custom, vipr).
  80. *
  81. * @return {Promise} Promise that will resolve once the all visualization are loaded
  82. */
  83. VIPRBundleHelper.getALLVIPRLibraries = function getALLVIPRLibraries() {
  84. var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  85. var libraries = [];
  86. var promises = [VIPRBundleHelper._getVIPRLibraries(options)];
  87. promises.push(VIPRBundleHelper.getCustomBundleVisualizations(options));
  88. return Promise.all(promises).then(function (results) {
  89. var standardLibraries = results[0];
  90. var customLibraries = results[1];
  91. // Remove any visualizations that are not feature enabled.
  92. var enabledSystemVizs = VIPRBundleHelper._getOnlyEnabledSystemVisualizations(standardLibraries.system, options);
  93. libraries.push.apply(libraries, enabledSystemVizs);
  94. if (standardLibraries['custom'].length > 0) {
  95. VIPRBundleHelper._buildCustomLibVIPRConfig(standardLibraries.custom || []);
  96. libraries.push.apply(libraries, standardLibraries['custom']);
  97. }
  98. if (customLibraries.length > 0) {
  99. libraries.push.apply(libraries, customLibraries);
  100. }
  101. return libraries;
  102. }).catch(function (err) {
  103. _logError(options.dashboardApi, err);
  104. throw new Error(err);
  105. });
  106. };
  107. /**
  108. * @function VIPRBundleHelper#getCustomBundleVisualizations
  109. * @public
  110. * @description Make rest call to get list of custom visualizations bundle information from the server.
  111. *
  112. * @returns {Promise} Returns a promise resolved with a JSON object contains custom bundles information from server
  113. */
  114. VIPRBundleHelper.getCustomBundleVisualizations = function getCustomBundleVisualizations(options) {
  115. var ajaxSvc = options.dashboardApi.getGlassCoreSvc('.Ajax');
  116. return ajaxSvc.ajax({
  117. url: baseUrl + '/custom',
  118. type: 'GET',
  119. contentType: 'application/json'
  120. }).then(function (customVisualizations) {
  121. return VIPRBundleHelper._buildCustomBundleVisualizationsLib(ajaxSvc, customVisualizations);
  122. }).catch(function (error) {
  123. _logError(options.dashboardApi, error);
  124. return Promise.resolve([]);
  125. });
  126. };
  127. VIPRBundleHelper._buildCustomBundleVisualizationsLib = function _buildCustomBundleVisualizationsLib(ajaxSvc) {
  128. var customVisualizationslib = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  129. var customCustomVisualizationsLib = [];
  130. var data = customVisualizationslib.data || [];
  131. var promise = void 0;
  132. if (data.length === 0) {
  133. promise = Promise.resolve(customCustomVisualizationsLib);
  134. } else {
  135. _.forEach(data, function (lib) {
  136. customCustomVisualizationsLib.push({
  137. id: lib.id,
  138. location: lib.type === 'schematic' ? 'vizbundles/rave2schematics' : 'v1/visualizations/' + lib.id + '/content/',
  139. assetId: lib.storeId,
  140. icon: lib.icon || null,
  141. placeholderIcon: 'v1/visualizations/' + lib.id + '/content/',
  142. modificationTime: lib.modificationTime,
  143. name: lib.name,
  144. type: lib.id,
  145. isSchematic: lib.type === 'schematic',
  146. isCustomVis: lib.type !== 'schematic'
  147. });
  148. });
  149. promise = VIPRBundleHelper._loadIcon(ajaxSvc, customCustomVisualizationsLib);
  150. }
  151. return promise.then(VIPRBundleHelper._buildCustomLibVIPRConfig);
  152. };
  153. VIPRBundleHelper._loadIcon = function _loadIcon(ajaxSvc) {
  154. var customCustomVisualizationsLib = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
  155. var promises = [];
  156. customCustomVisualizationsLib.forEach(function (lib) {
  157. if (!lib.icon && !lib.isSchematic) {
  158. //Load only if one not found,
  159. //This happens when the icon is defined at nested folder levels and not at the root
  160. //when the custom vis was storeed in CM
  161. var url = baseUrl + '/id/' + lib.id;
  162. promises.push(ajaxSvc.ajax({
  163. url: url,
  164. type: 'GET',
  165. contentType: 'application/json'
  166. }));
  167. } else {
  168. promises.push(Promise.resolve(null));
  169. }
  170. });
  171. return Promise.all(promises).then(function () {
  172. var responses = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  173. responses.forEach(function (response, idx) {
  174. if (response) {
  175. var data = response.data || [];
  176. var icon = data.find(function (fileDesc) {
  177. fileDesc = fileDesc || {};
  178. var file = fileDesc.file ? fileDesc.file.toLowerCase() : '';
  179. return VIPRBundleHelper.isValidIconFileExt(file);
  180. });
  181. if (icon) {
  182. var nameParts = icon.file.split('\\');
  183. customCustomVisualizationsLib[idx].icon = nameParts.join('/');
  184. }
  185. }
  186. });
  187. return customCustomVisualizationsLib;
  188. });
  189. };
  190. VIPRBundleHelper._buildCustomLibVIPRConfig = function _buildCustomLibVIPRConfig() {
  191. var customCustomVisualizationsLibs = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  192. customCustomVisualizationsLibs.forEach(function (lib) {
  193. VIPRConfig.removeCustomVisConfiguration(lib.id);
  194. VIPRConfig.addCustomVisConfiguration(lib);
  195. });
  196. return customCustomVisualizationsLibs;
  197. };
  198. /**
  199. * @function VIPRBundleHelper#loadCustomBundleVisulization
  200. * @public
  201. * @description Make rest call to load custom visualization to the server.
  202. *
  203. * @param {Object} options contains dashboardApi and data of type FromData of selected visualization
  204. * @returns {Promise} Returns a promise resolved with id of custom bundles information added on server
  205. */
  206. VIPRBundleHelper.loadCustomBundleVisualization = function loadCustomBundleVisualization(options) {
  207. return VIPRBundleHelper.submitAjaxAddOrUpdate(options, 'POST', baseUrl + '?maskResponses=true&updateAction=FAIL');
  208. };
  209. /**
  210. * @function VIPRBundleHelper#updateCustomBundleVisualization
  211. * @public
  212. * @description Make rest call to load custom visualization to the server.
  213. *
  214. * @param {Object} options contains dashboardApi and data of type FromData of selected visualization
  215. * @returns {Promise} Returns a promise resolved with id of custom bundles information added on server
  216. */
  217. //@todo for R3 will use POST for updating, VIDA will fix the ajax call to use put with dedicated update endpoint
  218. VIPRBundleHelper.updateCustomBundleVisualization = function updateCustomBundleVisualization() {
  219. var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  220. return VIPRBundleHelper.submitAjaxAddOrUpdate(options, 'PUT', baseUrl + '/id/' + options.bundleId);
  221. };
  222. /**
  223. * @function VIPRBundleHelper#deleteCustomBundleVisulization
  224. * @public
  225. * @description Make rest call to delete custom visualization bundle information from the server.
  226. *
  227. * @returns {Promise} Returns a promise resolved if custom visualization bundle information deleted from the server,
  228. * promise rejected if custom visualization bundle information is not deleted from the server
  229. */
  230. VIPRBundleHelper.deleteCustomBundleVisulization = function deleteCustomBundleVisulization() {
  231. var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  232. var ajaxSvc = options.dashboardApi.getGlassCoreSvc('.Ajax');
  233. var userCapabilities = options.dashboardApi.getGlassCoreSvc('.UserProfile').capabilities;
  234. var hasPermission = userCapabilities.indexOf('canManageVisualizations') !== -1;
  235. if (hasPermission) {
  236. return ajaxSvc.ajax({
  237. url: 'v1/objects/' + encodeURIComponent(options.assetId),
  238. type: 'DELETE'
  239. }).then(function () {
  240. VIPRConfig.removeCustomVisConfiguration(options.id);
  241. });
  242. } else {
  243. return Promise.reject(new Error(StringResources.get('noPermissionToManage')));
  244. }
  245. };
  246. VIPRBundleHelper.submitAjaxAddOrUpdate = function submitAjaxAddOrUpdate() {
  247. var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  248. var type = arguments[1];
  249. var url = arguments[2];
  250. var userCapabilities = options.dashboardApi.getGlassCoreSvc('.UserProfile').capabilities;
  251. var hasPermission = userCapabilities.indexOf('canManageVisualizations') !== -1;
  252. if (hasPermission) {
  253. var ajaxSvc = options.dashboardApi.getGlassCoreSvc('.Ajax');
  254. return ajaxSvc.ajax({
  255. url: url,
  256. type: type,
  257. data: options.data,
  258. enctype: 'multipart/form-data',
  259. processData: false,
  260. contentType: false
  261. }).then(function (info) {
  262. var data = JSON.parse(info.data);
  263. if (data.Status === 200 || data.Status === 201) {
  264. return data;
  265. } else if (info.jqXHR && info.jqXHR.responseJSON) {
  266. return Promise.reject(new Error(info.jqXHR.responseJSON.msg));
  267. } else if (info.jqXHR && info.jqXHR.responseText) {
  268. return Promise.reject(new Error(info.jqXHR.responseText));
  269. } else {
  270. return Promise.reject(new Error(data.Description));
  271. }
  272. });
  273. } else {
  274. return Promise.reject(new Error(StringResources.get('noPermissionToManage')));
  275. }
  276. };
  277. return VIPRBundleHelper;
  278. }(), _class.isValidIconFileExt = function (file) {
  279. var supportIconFileExts = ['.png', '.svg'];
  280. return file && !!supportIconFileExts.find(function (ext) {
  281. return file.indexOf(ext) !== -1;
  282. });
  283. }, _temp;
  284. });
  285. //# sourceMappingURL=VIPRBundleHelper.js.map