VIPR.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. 'use strict';
  2. var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
  3. /**
  4. *+------------------------------------------------------------------------+
  5. *| Licensed Materials - Property of IBM
  6. *| IBM Cognos Products: Dashboard
  7. *| (C) Copyright IBM Corp. 2017, 2020
  8. *|
  9. *| US Government Users Restricted Rights - Use, duplication or disclosure
  10. *| restricted by GSA ADP Schedule Contract with IBM Corp.
  11. *+------------------------------------------------------------------------+
  12. */
  13. define(['underscore', '../../lib/@waca/core-client/js/core-client/ui/core/Class', 'com/ibm/vida/control/vida', './VIPRConfig', './VIPRLibraries', './VIPRUtils', './VIPRBundleHelper'], function (_, Class, vida, VIPRConfig, VIPRLibraries, VIPRUtils, VIPRBundleHelper) {
  14. 'use strict';
  15. var CUSTOM_PREVIEWS = ['visualizationPreview', 'com.ibm.vis.schematicsPreview'];
  16. var VIPRProxy = Class.extend({
  17. init: function init() {
  18. this._setInitialStates();
  19. },
  20. _setInitialStates: function _setInitialStates() {
  21. this._libs = undefined;
  22. //Only reset for custom libs so that they can be refreshed
  23. this._customLibs = undefined;
  24. if (!this.libraries) {
  25. this.libraries = {
  26. system: [],
  27. custom: []
  28. };
  29. } else {
  30. //Only reset for custom libs so that they can be refreshed
  31. this.libraries.custom = [];
  32. }
  33. },
  34. initialize: function initialize() {
  35. var _this = this;
  36. var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  37. this.dashboardApi = params.dashboardApi;
  38. if (params.refresh) {
  39. this._setInitialStates();
  40. }
  41. if (!this._libs) {
  42. this.ajaxSvc = params.dashboardApi.getGlassCoreSvc('.Ajax');
  43. return VIPRBundleHelper.getALLVIPRLibraries({ dashboardApi: params.dashboardApi }).then(function (libraries) {
  44. var libsToCreate = _this._getLibsBundleToCreate(libraries, params.refresh);
  45. return _this._setRequiredConfigurationDefaults(libsToCreate).then(function () {
  46. var configuredLibraries = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  47. configuredLibraries.forEach(function (lib) {
  48. var bundleinfo = VIPRConfig.getConfig(lib.id);
  49. lib.config = {};
  50. // Add properties config if there is any
  51. if (bundleinfo) {
  52. if (bundleinfo.isCustomVis && bundleinfo.bundleInclude) {
  53. lib.config['properties'] = bundleinfo.bundleInclude;
  54. } else if (bundleinfo.isCustomVis && !bundleinfo.bundleInclude) {
  55. // never has to get to this case
  56. var logger = _this.dashboardApi.getGlassCoreSvc('.Logger');
  57. logger.error('`Failed to set lib.config.properties');
  58. } else if (!bundleinfo.isCustomVis && bundleinfo.config) {
  59. lib.config['properties'] = bundleinfo.config;
  60. }
  61. }
  62. // Add slots config if there is any.
  63. if (bundleinfo && bundleinfo.slotConfig) {
  64. lib.config['slots'] = bundleinfo.slotConfig;
  65. }
  66. });
  67. _this._createLibs(configuredLibraries, params.refresh);
  68. _this._setLibraries(configuredLibraries);
  69. });
  70. });
  71. }
  72. return Promise.resolve();
  73. },
  74. updateCustomLibrary: function updateCustomLibrary(id) {
  75. var library = this._customLibs[id];
  76. if (library) {
  77. this._updateLibrary(library);
  78. }
  79. },
  80. getLibraries: function getLibraries() {
  81. if (!this._libs) {
  82. var libs = _extends({}, this._systemLibs, this._customLibs);
  83. this._libs = _.indexBy(libs, 'id');
  84. }
  85. return this._libs;
  86. },
  87. getSystemLibIds: function getSystemLibIds() {
  88. return this.libraries ? this.libraries.system.map(function (library) {
  89. return library.id;
  90. }) : [];
  91. },
  92. getCustomLibIds: function getCustomLibIds() {
  93. return this.libraries ? this.libraries.custom.map(function (library) {
  94. return library.id;
  95. }) : [];
  96. },
  97. getBundleIds: function getBundleIds() {
  98. var libType = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : VIPRLibraries.LIB_TYPE_ALL;
  99. var ids = [];
  100. if (libType === VIPRLibraries.LIB_TYPE_ALL || libType === VIPRLibraries.LIB_TYPE_SYSTEM) {
  101. ids.push.apply(ids, this.getSystemLibIds());
  102. }
  103. if (libType === VIPRLibraries.LIB_TYPE_ALL || libType === VIPRLibraries.LIB_TYPE_CUSTOM) {
  104. ids.push.apply(ids, this.getCustomLibIds());
  105. }
  106. return ids;
  107. },
  108. _setLibraries: function _setLibraries() {
  109. var _this2 = this;
  110. var libraries = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  111. if (!this._customLibs) {
  112. this._customLibs = {};
  113. }
  114. if (!this._systemLibs) {
  115. this._systemLibs = {};
  116. }
  117. libraries.forEach(function (library) {
  118. if (library.isSchematic || library.isCustomVis || CUSTOM_PREVIEWS.indexOf(library.id) !== -1) {
  119. _this2._customLibs[library.id] = library;
  120. _this2.libraries.custom.push(library);
  121. } else {
  122. _this2._systemLibs[library.id] = library;
  123. _this2.libraries.system.push(library);
  124. }
  125. });
  126. },
  127. _getLibsBundleToCreate: function _getLibsBundleToCreate() {
  128. var libraries = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  129. var refresh = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  130. if (!refresh) {
  131. return libraries;
  132. }
  133. var libsToCreate = [];
  134. libraries.forEach(function (library) {
  135. if (library.isSchematic || library.isCustomVis || CUSTOM_PREVIEWS.indexOf(library.id) !== -1) {
  136. libsToCreate.push(library);
  137. }
  138. });
  139. return libsToCreate;
  140. },
  141. _createLibs: function _createLibs() {
  142. var _this3 = this;
  143. var configuredLibraries = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  144. var refresh = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  145. if (refresh) {
  146. configuredLibraries.forEach(function (library) {
  147. _this3._updateLibrary(library);
  148. });
  149. } else {
  150. _.extend(this, vida.VIPR.create({ bundles: configuredLibraries }));
  151. }
  152. },
  153. _updateLibrary: function _updateLibrary(library) {
  154. var bundle = library || {};
  155. var isSchematic = bundle.isSchematic;
  156. if (bundle.id && !isSchematic) {
  157. this.unregister(bundle.id);
  158. }
  159. if (bundle.id && (isSchematic && !this.isRegistered(bundle.id) || !isSchematic)) {
  160. this.register(bundle.id, bundle.location, bundle.config, bundle.fileName, bundle);
  161. }
  162. },
  163. /**
  164. * @param {String} viprDefinitionId - Vis bundle id
  165. * @param {Object} requiredConfig - contains a list of required configuration tokens/properties
  166. * @returns {String} a definition id for a library that should be removed as the user/server doesnt have the required configuration(e.g. without mapbox tokens, the user will not be able to use maps)
  167. * Makes a rest call searching for specific configuration tokens on a user's server and sets the corresponding default properties
  168. */
  169. _fetchAndApplyRequiredConfigurationDefaults: function _fetchAndApplyRequiredConfigurationDefaults(_ref) {
  170. var _this4 = this;
  171. var viprDefinitionId = _ref.viprDefinitionId,
  172. requiredConfig = _ref.requiredConfig;
  173. return Promise.reduce(requiredConfig, function (values, key) {
  174. return _this4.dashboardApi.getGlassCoreSvc('.Config').getConfigValue(key).then(function (value) {
  175. values[key] = value;
  176. return values;
  177. });
  178. }, {}).then(function (response) {
  179. if (Object.values(response).indexOf('default') === -1) {
  180. VIPRUtils.applyRequiredConfiguration(viprDefinitionId, requiredConfig, response);
  181. return null;
  182. }
  183. return viprDefinitionId;
  184. }).catch(function () {
  185. return viprDefinitionId;
  186. });
  187. },
  188. /*
  189. * Check each library to see if there is a required configuration. If so, then call fetchAndApplyRequiredConfigurationDefaults
  190. * which will gather up any viprdefinitionids that do not have the corresponding configuration tokens--in this case, the id will
  191. * be removed from the libraries so that the user cannot use that type of visualization.
  192. */
  193. _setRequiredConfigurationDefaults: function _setRequiredConfigurationDefaults(libraries) {
  194. var _this5 = this;
  195. var libraryRequiredConfigs = libraries.map(function (lib) {
  196. return { viprDefinitionId: lib.id, requiredConfig: VIPRUtils.getRequiredConfig(lib.id) };
  197. }).filter(function (libraryRequiredConfig) {
  198. return !!libraryRequiredConfig.requiredConfig;
  199. });
  200. return Promise.map(libraryRequiredConfigs, function (config) {
  201. return _this5._fetchAndApplyRequiredConfigurationDefaults(config);
  202. }).filter(function (definition) {
  203. return !!definition;
  204. }).then(function (definitionsToBeRemoved) {
  205. return libraries.filter(function (lib) {
  206. return !_.contains(definitionsToBeRemoved, lib.id);
  207. });
  208. });
  209. },
  210. createWidget: function createWidget(nodeId, hostAPI) {
  211. return new vida.SmallMultipleGrid({ vipr: this, hostAPI: hostAPI }, nodeId);
  212. },
  213. unloadBundle: function unloadBundle(id) {
  214. this.unload(id);
  215. },
  216. loadBundle: function loadBundle(id) {
  217. var _this6 = this;
  218. return this.load(id).then(function (bundle) {
  219. return vida.SmallMultipleGrid.extendVizBundle(bundle);
  220. }).catch(function (error) {
  221. var logger = _this6.dashboardApi.getGlassCoreSvc('.Logger');
  222. if (id !== 'visualizationPreview') {
  223. logger.error(error);
  224. }
  225. });
  226. }
  227. });
  228. // create a singleton instance
  229. return new VIPRProxy();
  230. });
  231. //# sourceMappingURL=VIPR.js.map