'use strict'; function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } /** * Licensed Materials - Property of IBM * IBM Business Analytics (C) Copyright IBM Corp. 2019, 2020 * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp. */ define(['underscore', './BoardModel', '../../lib/@waca/core-client/js/core-client/utils/ClassFactory'], function (_, BoardModel, ClassFactory) { var BoardModelFactory = function () { function BoardModelFactory(options) { _classCallCheck(this, BoardModelFactory); this.glassContext = options.glassContext; this.dashboardAPI = options.dashboardAPI; this.boardId = options.boardId; this.objectUrl = options.objectUrl; this.boardSpec = options.boardSpec; this.widgetRegistry = options.widgetRegistry; this.extensions = options.extensions; this.stringResources = options.stringResources; this.eventRouter = options.eventRouter; this.logger = options.logger; this.createLayoutModel = options.createLayoutModel; } BoardModelFactory.prototype.createBoardModel = function createBoardModel() { var _this = this; return this._getBoardSpec(this.boardId, this.objectUrl).then(function (boardSpec) { return _this._upgradeSpec(boardSpec); }).then(function (specInfo) { //If a dashboard is opened, set the saved instance with the spec that comes from cm. if (_this._onLoadSpec) { _this.dashboardAPI.getCurrentContentView().setSavedInstance(_this._onLoadSpec); } return specInfo; }).then(this._createBoardModel.bind(this)); }; BoardModelFactory.prototype._createBoardModel = function _createBoardModel(specInfo) { var meta = specInfo.spec && specInfo.spec._meta; var id = meta && meta.bundleID || this.boardId; // ensure that the boardModel name matches the one in _meta that can be updated by rename on welcome // TODO: store the name in one place only var name = meta && meta.name; var boardModel = new BoardModel(specInfo.spec, { id: id, name: name, eventRouter: this.eventRouter, widgetRegistry: this.widgetRegistry, boardModelExtensions: this.extensions.getBoardModelExtensions(), layoutExtensions: this.extensions.getLayoutModelExtensions(), dashboardApi: this.dashboardAPI, logger: this.logger, createLayoutModel: this.createLayoutModel, isUpgraded: specInfo.upgraded }); var boardModelInfo = { boardModel: boardModel, boardSpec: specInfo.spec, boardId: this.boardId }; return boardModelInfo; }; BoardModelFactory.prototype._getBoardSpec = function _getBoardSpec(boardId, boardUrl) { var _this2 = this; if (!this.boardId && !this.objectUrl) { var moreButton = this.dashboardAPI.findGlassPlugin('com.ibm.bi.glass.common.operations'); if (moreButton) { moreButton.disable(); } } if (this.boardSpec) { return Promise.resolve(this.boardSpec); } //Fetch board spec via content service if it's not set return this.glassContext.getSvc('.DashboardContent').then(function (contentService) { return contentService.getDashboard(boardId, boardUrl); }).then(function (dashboardInfo) { return _this2._resolveBoardSpec(dashboardInfo); }); }; /** * Update the boardSpec from the responseData * @private * @param {Object} dashboardInfo * @example { boardSpec: {xxx}, boardId: 'i12345678', permissions: ['execute','read','setPolicy','traverse', 'write'], searchPath: 'CAMID("LDAP:u:uid=hmiller,ou=people")/folder[@name=\'My Folders\']/exploration[@name=\'New dashboard1\']', name: 'New dashboard1' } * @returns Pormise Resolved with boardSpec */ BoardModelFactory.prototype._resolveBoardSpec = function _resolveBoardSpec(_ref) { var boardId = _ref.boardId, name = _ref.name, searchPath = _ref.searchPath, permissions = _ref.permissions, boardSpec = _ref.boardSpec; boardSpec._meta = { bundleID: boardId }; this.dashboardAPI.setPermissions(permissions); // Gemini expects the title to be in the spec. Get the default so that we can benefit from multilingual names, boardSpec.name = name; boardSpec.searchPath = searchPath; this.boardSpec = boardSpec; this._onLoadSpec = JSON.stringify(boardSpec); // in the case where we loaded the object from a url we don't have the ID. so we set it here this.boardId = boardId; return boardSpec; }; BoardModelFactory.prototype._upgradeSpec = function _upgradeSpec(boardSpec) { var _this3 = this; return this.glassContext.getSvc('.UpgradeService').then(function (upgradeService) { boardSpec.version = boardSpec.version || upgradeService.getLatestDashboardSpecVersion(); if (upgradeService.needsUpgrade(boardSpec, null)) { return _this3._doUpgrade(boardSpec, upgradeService).then(function (newSpec) { return { spec: newSpec, upgraded: true }; }); } else { return { spec: boardSpec, upgraded: false }; } }).catch(function (err) { if (err.obj) { // recover return { spec: err.obj, upgraded: true }; } // re-throw throw err; }); }; BoardModelFactory.prototype._doUpgrade = function _doUpgrade(boardSpec, upgradeService) { var _this4 = this; var PROGRESS_FAST = 10, PROGRESS_SLOW = 100; var toast = null; var completeMessage = this.dashboardAPI.canAuthor() ? this.stringResources.get('toastUpgradeDone') : this.stringResources.get('upgradeComplete'); var upgradeProgress = function upgradeProgress(data) { toast.setComplete(data.completed / data.total * 100, { isComplete: data.done, completeMsg: data.done ? completeMessage : null, duration: data.done ? PROGRESS_FAST : PROGRESS_SLOW }); }; 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) { var ProgressToast = _ref2[0], productVersion = _ref2[1]; toast = new ProgressToast({ noCancelBtn: true, noHideBtn: true }); var upgradeMessage = _this4.stringResources.get('upgradingDashboard', { name: boardSpec.name, version: productVersion }); toast.show(upgradeMessage); //If a previous upgrade failed (e.g. datasource not found) and dashboard spec now has widget models under layout, moving the internal //widget model from the layout to the root, so that the upgrade steps can work again when we relink boardSpec = _this4._restoreWidgetModelsForUpgrade(boardSpec, upgradeService.getLatestDashboardSpecVersion()); return upgradeService.upgrade(boardSpec, null /*requestedVersion -> null means it's Infinity*/, { ajaxSvc: _this4.glassContext.getCoreSvc('.Ajax'), dashboardApi: _this4.dashboardAPI, services: _this4.services, logger: _this4.logger, hasWrite: _this4.dashboardAPI.canAuthor(), progress: upgradeProgress, noToast: true, originalSpecVersion: boardSpec.version }); }).catch(function (err) { if (toast) { setTimeout(function () { toast.showButton('hide'); toast.fail(); }, 1000); } _this4.logger.error(err); throw err; }); }; BoardModelFactory.prototype._findWidgetInLayouts = function _findWidgetInLayouts(layout, widgetLayouts) { if (layout && layout.type === 'widget' && layout.features && layout.features.Models_internal) { widgetLayouts.push(layout); } if (layout && layout.items && layout.items.length > 0) { for (var i = 0; i < layout.items.length; i++) { var item = layout.items[i]; this._findWidgetInLayouts(item, widgetLayouts); } } }; BoardModelFactory.prototype._restoreWidgetModelsForUpgrade = function _restoreWidgetModelsForUpgrade(boardSpec, latestVersion) { var widgetLayouts = []; if (boardSpec.version < latestVersion) { if (boardSpec.layout) { this._findWidgetInLayouts(boardSpec.layout, widgetLayouts); for (var i = 0; i < widgetLayouts.length; i++) { var layout = widgetLayouts[i]; var widget = layout.features.Models_internal; if (!boardSpec.widgets) { boardSpec.widgets = {}; } boardSpec.widgets[layout.id] = widget; delete layout.features.Models_internal; } } } return boardSpec; }; return BoardModelFactory; }(); return BoardModelFactory; }); //# sourceMappingURL=BoardModelFactory.js.map