SinglePageDashboardsUpgradeHelper.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. 'use strict';
  2. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  3. /*
  4. *+------------------------------------------------------------------------+
  5. *| Licensed Materials - Property of IBM
  6. *| IBM Cognos Products: BI Dashboard
  7. *| (C) Copyright IBM Corp. 2018
  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', '../../../lib/@waca/upgrades/UpgradeBase', '../../../lib/@waca/dashboard-common/dist/core/Model', '../../../app/nls/StringResources', './ContainerPageIdUpgradeHelper'], function (_, Class, UpgradeBase, Model, StringResources, ContainerPageIdUpgradeHelper) {
  14. var SinglePageDashboardsUpgradeHelper = function () {
  15. function SinglePageDashboardsUpgradeHelper() {
  16. _classCallCheck(this, SinglePageDashboardsUpgradeHelper);
  17. }
  18. /**
  19. * Perform upgrade
  20. *
  21. * @param {object} spec - spec to perform upgrade on
  22. *
  23. * @return {Promise} Promise to be resolved when upgrade performed
  24. */
  25. SinglePageDashboardsUpgradeHelper.prototype.upgrade = function upgrade(spec) {
  26. this._containerPageIdUpgrader = new ContainerPageIdUpgradeHelper();
  27. if (!spec) {
  28. return Promise.resolve(spec);
  29. }
  30. if (!this._isSinglePageDashboard(spec)) {
  31. return Promise.resolve(spec);
  32. }
  33. this.upgradeSinglePageDashboard(spec);
  34. return Promise.resolve(spec);
  35. };
  36. /**
  37. * Check whether the spec is a single page dashboard
  38. *
  39. * @param {object} spec - dashboard spec
  40. *
  41. * @return {boolean}
  42. */
  43. SinglePageDashboardsUpgradeHelper.prototype._isSinglePageDashboard = function _isSinglePageDashboard(spec) {
  44. if (!spec || !spec.layout) {
  45. return false;
  46. }
  47. var type = spec.layout.type;
  48. if (type === 'absolute' || type === 'container') {
  49. var template = spec.layout.templateName;
  50. var isInfographic = template.lastIndexOf('Infographics', 0) === 0;
  51. return !isInfographic; //If we don't have an infographic, then we have a single page dashboard.
  52. }
  53. return false;
  54. };
  55. /**
  56. * Upgrade a single page dashboard spec to a tabbed dashboard with a single tab that is hidden
  57. *
  58. * @param {object} spec - dashboard spec
  59. */
  60. SinglePageDashboardsUpgradeHelper.prototype.upgradeSinglePageDashboard = function upgradeSinglePageDashboard(spec) {
  61. var tabLayout = {
  62. id: new Model().id,
  63. type: 'tab',
  64. items: [{
  65. title: StringResources.get('defaultTabTitle', {
  66. index: 1
  67. })
  68. }],
  69. style: {
  70. height: '100%'
  71. },
  72. hideTab: true
  73. };
  74. //Move the page properties into the new first tab.
  75. var layout = spec.layout;
  76. var firstTab = tabLayout.items[0];
  77. firstTab.id = layout.id;
  78. firstTab.type = layout.type;
  79. firstTab.items = layout.items;
  80. firstTab.templateName = layout.templateName;
  81. delete layout.templateName;
  82. if (layout.style) {
  83. firstTab.style = layout.style;
  84. }
  85. if (layout.css && layout.css.indexOf('gridCapable') !== -1) {
  86. //Clean up an unneeded css class which will cause problems if present on the tab layout.
  87. var newCss = layout.css.replace('gridCapable', '');
  88. if (newCss) {
  89. layout.css = newCss;
  90. } else {
  91. delete layout.css;
  92. }
  93. }
  94. //Merge in the new tab layout into the spec.
  95. _.extend(layout, tabLayout);
  96. if (this._isRelativeSinglePage(spec)) {
  97. var currentId = spec.layout.items[0].items[0].id;
  98. var newId = spec.layout.items[0].id;
  99. this._upgradeContainerPageIds(spec, currentId, newId);
  100. }
  101. };
  102. /**
  103. * check if the single page is a relative: check if the only item is of type container
  104. * @param {Object} spec
  105. * @return true if the single page is a relative one
  106. */
  107. SinglePageDashboardsUpgradeHelper.prototype._isRelativeSinglePage = function _isRelativeSinglePage(spec) {
  108. return spec.layout.items[0].type === 'container';
  109. };
  110. SinglePageDashboardsUpgradeHelper.prototype._upgradeContainerPageIds = function _upgradeContainerPageIds(spec, currentId, newId) {
  111. var containerPageIdMap = {};
  112. containerPageIdMap[currentId] = newId;
  113. this._containerPageIdUpgrader.upgrade(spec, containerPageIdMap);
  114. };
  115. return SinglePageDashboardsUpgradeHelper;
  116. }();
  117. return SinglePageDashboardsUpgradeHelper;
  118. });
  119. //# sourceMappingURL=SinglePageDashboardsUpgradeHelper.js.map