BoardModelFactory.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. define(['underscore', './BoardModel', '../../lib/@waca/core-client/js/core-client/utils/ClassFactory'], function (_, BoardModel, ClassFactory) {
  9. var BoardModelFactory = function () {
  10. function BoardModelFactory(options) {
  11. _classCallCheck(this, BoardModelFactory);
  12. this.glassContext = options.glassContext;
  13. this.dashboardAPI = options.dashboardAPI;
  14. this.boardId = options.boardId;
  15. this.objectUrl = options.objectUrl;
  16. this.boardSpec = options.boardSpec;
  17. this.widgetRegistry = options.widgetRegistry;
  18. this.extensions = options.extensions;
  19. this.stringResources = options.stringResources;
  20. this.eventRouter = options.eventRouter;
  21. this.logger = options.logger;
  22. this.createLayoutModel = options.createLayoutModel;
  23. }
  24. BoardModelFactory.prototype.createBoardModel = function createBoardModel() {
  25. var _this = this;
  26. return this._getBoardSpec(this.boardId, this.objectUrl).then(function (boardSpec) {
  27. return _this._upgradeSpec(boardSpec);
  28. }).then(function (specInfo) {
  29. //If a dashboard is opened, set the saved instance with the spec that comes from cm.
  30. if (_this._onLoadSpec) {
  31. _this.dashboardAPI.getCurrentContentView().setSavedInstance(_this._onLoadSpec);
  32. }
  33. return specInfo;
  34. }).then(this._createBoardModel.bind(this));
  35. };
  36. BoardModelFactory.prototype._createBoardModel = function _createBoardModel(specInfo) {
  37. var meta = specInfo.spec && specInfo.spec._meta;
  38. var id = meta && meta.bundleID || this.boardId;
  39. // ensure that the boardModel name matches the one in _meta that can be updated by rename on welcome
  40. // TODO: store the name in one place only
  41. var name = meta && meta.name;
  42. var boardModel = new BoardModel(specInfo.spec, {
  43. id: id,
  44. name: name,
  45. eventRouter: this.eventRouter,
  46. widgetRegistry: this.widgetRegistry,
  47. boardModelExtensions: this.extensions.getBoardModelExtensions(),
  48. layoutExtensions: this.extensions.getLayoutModelExtensions(),
  49. dashboardApi: this.dashboardAPI,
  50. logger: this.logger,
  51. createLayoutModel: this.createLayoutModel,
  52. isUpgraded: specInfo.upgraded
  53. });
  54. var boardModelInfo = {
  55. boardModel: boardModel,
  56. boardSpec: specInfo.spec,
  57. boardId: this.boardId
  58. };
  59. return boardModelInfo;
  60. };
  61. BoardModelFactory.prototype._getBoardSpec = function _getBoardSpec(boardId, boardUrl) {
  62. var _this2 = this;
  63. if (!this.boardId && !this.objectUrl) {
  64. var moreButton = this.dashboardAPI.findGlassPlugin('com.ibm.bi.glass.common.operations');
  65. if (moreButton) {
  66. moreButton.disable();
  67. }
  68. }
  69. if (this.boardSpec) {
  70. return Promise.resolve(this.boardSpec);
  71. }
  72. //Fetch board spec via content service if it's not set
  73. return this.glassContext.getSvc('.DashboardContent').then(function (contentService) {
  74. return contentService.getDashboard(boardId, boardUrl);
  75. }).then(function (dashboardInfo) {
  76. return _this2._resolveBoardSpec(dashboardInfo);
  77. });
  78. };
  79. /**
  80. * Update the boardSpec from the responseData
  81. * @private
  82. * @param {Object} dashboardInfo
  83. * @example
  84. {
  85. boardSpec: {xxx},
  86. boardId: 'i12345678',
  87. permissions: ['execute','read','setPolicy','traverse', 'write'],
  88. searchPath: 'CAMID("LDAP:u:uid=hmiller,ou=people")/folder[@name=\'My Folders\']/exploration[@name=\'New dashboard1\']',
  89. name: 'New dashboard1'
  90. }
  91. * @returns Pormise<Object> Resolved with boardSpec
  92. */
  93. BoardModelFactory.prototype._resolveBoardSpec = function _resolveBoardSpec(_ref) {
  94. var boardId = _ref.boardId,
  95. name = _ref.name,
  96. searchPath = _ref.searchPath,
  97. permissions = _ref.permissions,
  98. boardSpec = _ref.boardSpec;
  99. boardSpec._meta = {
  100. bundleID: boardId
  101. };
  102. this.dashboardAPI.setPermissions(permissions);
  103. // Gemini expects the title to be in the spec. Get the default so that we can benefit from multilingual names,
  104. boardSpec.name = name;
  105. boardSpec.searchPath = searchPath;
  106. this.boardSpec = boardSpec;
  107. this._onLoadSpec = JSON.stringify(boardSpec);
  108. // in the case where we loaded the object from a url we don't have the ID. so we set it here
  109. this.boardId = boardId;
  110. return boardSpec;
  111. };
  112. BoardModelFactory.prototype._upgradeSpec = function _upgradeSpec(boardSpec) {
  113. var _this3 = this;
  114. return this.glassContext.getSvc('.UpgradeService').then(function (upgradeService) {
  115. boardSpec.version = boardSpec.version || upgradeService.getLatestDashboardSpecVersion();
  116. if (upgradeService.needsUpgrade(boardSpec, null)) {
  117. return _this3._doUpgrade(boardSpec, upgradeService).then(function (newSpec) {
  118. return {
  119. spec: newSpec,
  120. upgraded: true
  121. };
  122. });
  123. } else {
  124. return {
  125. spec: boardSpec,
  126. upgraded: false
  127. };
  128. }
  129. }).catch(function (err) {
  130. if (err.obj) {
  131. // recover
  132. return {
  133. spec: err.obj,
  134. upgraded: true
  135. };
  136. }
  137. // re-throw
  138. throw err;
  139. });
  140. };
  141. BoardModelFactory.prototype._doUpgrade = function _doUpgrade(boardSpec, upgradeService) {
  142. var _this4 = this;
  143. var PROGRESS_FAST = 10,
  144. PROGRESS_SLOW = 100;
  145. var toast = null;
  146. var completeMessage = this.dashboardAPI.canAuthor() ? this.stringResources.get('toastUpgradeDone') : this.stringResources.get('upgradeComplete');
  147. var upgradeProgress = function upgradeProgress(data) {
  148. toast.setComplete(data.completed / data.total * 100, {
  149. isComplete: data.done,
  150. completeMsg: data.done ? completeMessage : null,
  151. duration: data.done ? PROGRESS_FAST : PROGRESS_SLOW
  152. });
  153. };
  154. return Promise.all([ClassFactory.loadModule('dashboard-core/js/lib/@waca/core-client/js/core-client/ui/ProgressToast'), this.glassContext.getCoreSvc('.Config').getProductVersion()]).then(function (_ref2) {
  155. var ProgressToast = _ref2[0],
  156. productVersion = _ref2[1];
  157. toast = new ProgressToast({
  158. noCancelBtn: true,
  159. noHideBtn: true
  160. });
  161. var upgradeMessage = _this4.stringResources.get('upgradingDashboard', {
  162. name: boardSpec.name,
  163. version: productVersion
  164. });
  165. toast.show(upgradeMessage);
  166. //If a previous upgrade failed (e.g. datasource not found) and dashboard spec now has widget models under layout, moving the internal
  167. //widget model from the layout to the root, so that the upgrade steps can work again when we relink
  168. boardSpec = _this4._restoreWidgetModelsForUpgrade(boardSpec, upgradeService.getLatestDashboardSpecVersion());
  169. return upgradeService.upgrade(boardSpec, null /*requestedVersion -> null means it's Infinity*/, {
  170. ajaxSvc: _this4.glassContext.getCoreSvc('.Ajax'),
  171. dashboardApi: _this4.dashboardAPI,
  172. services: _this4.services,
  173. logger: _this4.logger,
  174. hasWrite: _this4.dashboardAPI.canAuthor(),
  175. progress: upgradeProgress,
  176. noToast: true,
  177. originalSpecVersion: boardSpec.version
  178. });
  179. }).catch(function (err) {
  180. if (toast) {
  181. setTimeout(function () {
  182. toast.showButton('hide');
  183. toast.fail();
  184. }, 1000);
  185. }
  186. _this4.logger.error(err);
  187. throw err;
  188. });
  189. };
  190. BoardModelFactory.prototype._findWidgetInLayouts = function _findWidgetInLayouts(layout, widgetLayouts) {
  191. if (layout && layout.type === 'widget' && layout.features && layout.features.Models_internal) {
  192. widgetLayouts.push(layout);
  193. }
  194. if (layout && layout.items && layout.items.length > 0) {
  195. for (var i = 0; i < layout.items.length; i++) {
  196. var item = layout.items[i];
  197. this._findWidgetInLayouts(item, widgetLayouts);
  198. }
  199. }
  200. };
  201. BoardModelFactory.prototype._restoreWidgetModelsForUpgrade = function _restoreWidgetModelsForUpgrade(boardSpec, latestVersion) {
  202. var widgetLayouts = [];
  203. if (boardSpec.version < latestVersion) {
  204. if (boardSpec.layout) {
  205. this._findWidgetInLayouts(boardSpec.layout, widgetLayouts);
  206. for (var i = 0; i < widgetLayouts.length; i++) {
  207. var layout = widgetLayouts[i];
  208. var widget = layout.features.Models_internal;
  209. if (!boardSpec.widgets) {
  210. boardSpec.widgets = {};
  211. }
  212. boardSpec.widgets[layout.id] = widget;
  213. delete layout.features.Models_internal;
  214. }
  215. }
  216. }
  217. return boardSpec;
  218. };
  219. return BoardModelFactory;
  220. }();
  221. return BoardModelFactory;
  222. });
  223. //# sourceMappingURL=BoardModelFactory.js.map