LocalProvider.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. 'use strict';
  2. var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
  3. /**
  4. * Licensed Materials - Property of IBM
  5. * IBM Cognos Products: Dashboard
  6. * (C) Copyright IBM Corp. 2017, 2019
  7. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  8. */
  9. define(['underscore', './BaseProvider', '../../../DynamicFileLoader'], function (_, BaseProvider, DynamicFileLoader) {
  10. var ID_prefix = 'com.ibm.bi.dashboard.';
  11. var ICON_REG_EXP = /{{ICON:.*}}/;
  12. var LocalProvider = BaseProvider.extend({
  13. definitionsPromise: null,
  14. init: function init(options) {
  15. LocalProvider.inherited('init', this, arguments);
  16. this.logger = options.dashboardApi.getGlassCoreSvc('.Logger');
  17. this.localDefinitions = [];
  18. },
  19. clear: function clear() {
  20. this.definitionsPromise = null;
  21. },
  22. /**
  23. * this helper function will traverse the entire definition and replace {{ICON:....}} with the right icon id.
  24. * e.g. for "{{ICON:restore_16.default.id}}", it is equivalent as:
  25. * const module = require('dashboard-analytics/lib/@waca/dashboard-common/dist/lib/@ba-ui-toolkit/ba-graphics/dist/icons-js/restore_16');
  26. * const iconId = module['default']['id']
  27. */
  28. _fetchIconId: function _fetchIconId(definitionObj) {
  29. var _this = this;
  30. var proms = [];
  31. var _loop = function _loop(key) {
  32. if (_typeof(definitionObj[key]) == 'object' && definitionObj[key] !== null) {
  33. proms.push.apply(proms, _this._fetchIconId(definitionObj[key]));
  34. } else if (typeof definitionObj[key] === 'string' && ICON_REG_EXP.test(definitionObj[key])) {
  35. var iconPath = definitionObj[key].slice(7, -2).split('.');
  36. var filename = iconPath[0];
  37. var paths = iconPath.slice(1);
  38. var url = 'dashboard-analytics/lib/@waca/dashboard-common/dist/lib/@ba-ui-toolkit/ba-graphics/dist/icons-js/' + filename;
  39. proms.push(DynamicFileLoader.load([url]).then(function (modules) {
  40. var m = modules[0];
  41. paths.forEach(function (n) {
  42. m = m[n];
  43. });
  44. definitionObj[key] = m;
  45. }));
  46. }
  47. };
  48. for (var key in definitionObj) {
  49. _loop(key);
  50. }
  51. return proms;
  52. },
  53. _preprocessDefinitions: function _preprocessDefinitions(definitionList) {
  54. var _this2 = this;
  55. var proms = [];
  56. definitionList.forEach(function (definition) {
  57. // fix any fully qualified ids to local ids
  58. definition.id = definition.id.replace(ID_prefix, '');
  59. // replace {{ICON:****}} with the correct id
  60. proms.push.apply(proms, _this2._fetchIconId(definition));
  61. });
  62. return Promise.all(proms).then(function () {
  63. return definitionList;
  64. });
  65. },
  66. getDefinitions: function getDefinitions() {
  67. var _this3 = this;
  68. if (!this.definitionsPromise) {
  69. this.definitionsPromise = new Promise(function (resolve) {
  70. _this3.dashboardApi.findGlassCollection(ID_prefix + 'live-visualizations').then(function (data) {
  71. return _this3._preprocessDefinitions(data);
  72. }).then(function (definitionList) {
  73. var _localDefinitions;
  74. _this3.setDefinitions(definitionList);
  75. _this3.massageDefinitions(definitionList);
  76. _this3.freezeDefinitions(definitionList);
  77. definitionList = definitionList || [];
  78. (_localDefinitions = _this3.localDefinitions).push.apply(_localDefinitions, definitionList);
  79. resolve(_this3.localDefinitions);
  80. }).catch(function (e) {
  81. _this3.logger.error('Error: unable to query local definitions', e);
  82. resolve([]);
  83. });
  84. });
  85. }
  86. return this.definitionsPromise;
  87. },
  88. massageDefinitions: function massageDefinitions() {
  89. var definitions = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  90. definitions.forEach(function (definition) {
  91. var datasets = definition.datasets;
  92. if (datasets) {
  93. datasets.forEach(function (dataset) {
  94. dataset.getId = function () {
  95. return dataset.name;
  96. };
  97. });
  98. }
  99. });
  100. },
  101. refreshProvider: function refreshProvider() {
  102. return Promise.resolve(this.localDefinitions);
  103. },
  104. refresh: function refresh() {
  105. throw new Error('refresh is not supported for local provider');
  106. }
  107. });
  108. return LocalProvider;
  109. });
  110. //# sourceMappingURL=LocalProvider.js.map