LayoutPageSizeUpgrade.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. 'use strict';
  2. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  3. function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
  4. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
  5. /*
  6. *+------------------------------------------------------------------------+
  7. *| Licensed Materials - Property of IBM
  8. *| IBM Cognos Products: BI Dashboard
  9. *| (C) Copyright IBM Corp. 2018, 2019
  10. *|
  11. *| US Government Users Restricted Rights - Use, duplication or disclosure
  12. *| restricted by GSA ADP Schedule Contract with IBM Corp.
  13. *+------------------------------------------------------------------------+
  14. */
  15. define(['../../../lib/@waca/upgrades/UpgradeBase', '../../../lib/@waca/core-client/js/core-client/utils/UniqueId', './ContainerPageIdUpgradeHelper', 'underscore'], function (UpgradeBase, UniqueId, ContainerPageIdUpgradeHelper, _) {
  16. var ABSOLUTE_TYPE = 'absolute';
  17. var SCALINGABSOLUTE_TYPE = 'scalingAbsolute';
  18. var CONTAINER_TYPE = 'container';
  19. var WIDGET_TYPE = 'widget';
  20. var GROUP_TYPE = 'group';
  21. var INFOGRAPHIC_TEMPLATE_TYPE = 'Infographic';
  22. var REGULAR_TEMPLATE_TYPE = 'Template';
  23. /**
  24. * Upgrades the absolute layout to be in a parent container similar to relative layouts for the page size funtionality.
  25. * Upgrades the layout to contain a pageSize based off widget positioning/page types.
  26. **/
  27. var LayoutPageSizeUpgrade = function (_UpgradeBase) {
  28. _inherits(LayoutPageSizeUpgrade, _UpgradeBase);
  29. function LayoutPageSizeUpgrade() {
  30. _classCallCheck(this, LayoutPageSizeUpgrade);
  31. var _this = _possibleConstructorReturn(this, _UpgradeBase.call(this));
  32. _this.VERSION = 1011;
  33. _this._containerPageIdUpgrader = new ContainerPageIdUpgradeHelper();
  34. return _this;
  35. }
  36. /**
  37. * Perform upgrade
  38. *
  39. * @param {object} spec - spec to perform upgrade on
  40. *
  41. * @return {Promise} Promise to be resolved when upgrade performed
  42. */
  43. LayoutPageSizeUpgrade.prototype.up = function up(spec) {
  44. var _this2 = this;
  45. return Promise.resolve().then(function () {
  46. _this2._init();
  47. if (spec && spec.layout) {
  48. _this2._upgradeLayout(spec.layout);
  49. _this2._upgradePageSize(spec.layout);
  50. _this2._containerPageIdUpgrader.upgrade(spec, _this2._containerPageIdMap);
  51. }
  52. return spec;
  53. });
  54. };
  55. // Downgrades are not available in CA
  56. LayoutPageSizeUpgrade.prototype.down = function down(spec) {
  57. return Promise.resolve(spec);
  58. };
  59. /**
  60. * Set the container id upgrade function
  61. * If supplied, a custom container upgrade function will be called to upgrade the spec
  62. * @param {object} containerPageIdUpgrade - container id upgrade object which implements the following interface:
  63. * upgrade(spec, idMap)
  64. * Currently only used for unit testing purposes
  65. */
  66. LayoutPageSizeUpgrade.prototype.setContainerIdUpgrader = function setContainerIdUpgrader(containerPageIdUpgrade) {
  67. if (containerPageIdUpgrade) {
  68. this._containerPageIdUpgrader = containerPageIdUpgrade;
  69. }
  70. };
  71. LayoutPageSizeUpgrade.prototype._init = function _init() {
  72. this._hasRelativeLayouts = false;
  73. this._hasAbsoluteLayouts = false;
  74. this._relativeTemplateType = undefined;
  75. // Default minimum page size during upgrade
  76. this._boundingPageSize = { width: 1280, height: 720 };
  77. this._containerPageIdMap = {};
  78. };
  79. LayoutPageSizeUpgrade.prototype._upgradeLayout = function _upgradeLayout(layout) {
  80. if (layout.items) {
  81. // Recursively upgrade child layouts
  82. for (var _iterator = layout.items, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
  83. var _ref;
  84. if (_isArray) {
  85. if (_i >= _iterator.length) break;
  86. _ref = _iterator[_i++];
  87. } else {
  88. _i = _iterator.next();
  89. if (_i.done) break;
  90. _ref = _i.value;
  91. }
  92. var item = _ref;
  93. this._upgradeLayout(item);
  94. }
  95. }
  96. this._determinePageSizeFromLayout(layout);
  97. this._determineRelativeTemplateType(layout);
  98. this._determineLayoutTypes(layout);
  99. this._upgradeAbsoluteContainer(layout);
  100. };
  101. LayoutPageSizeUpgrade.prototype._determinePageSizeFromLayout = function _determinePageSizeFromLayout(layout) {
  102. if (layout.type === ABSOLUTE_TYPE) {
  103. // Determine bounds from widgets & groups
  104. for (var _iterator2 = layout.items, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) {
  105. var _ref2;
  106. if (_isArray2) {
  107. if (_i2 >= _iterator2.length) break;
  108. _ref2 = _iterator2[_i2++];
  109. } else {
  110. _i2 = _iterator2.next();
  111. if (_i2.done) break;
  112. _ref2 = _i2.value;
  113. }
  114. var item = _ref2;
  115. if (item.type === WIDGET_TYPE || item.type === GROUP_TYPE) {
  116. var rawStyle = this._getRawStyle(item.style, 'px');
  117. if (rawStyle) {
  118. var right = rawStyle.left + rawStyle.width;
  119. if (right > this._boundingPageSize.width) {
  120. this._boundingPageSize.width = right;
  121. }
  122. var bottom = rawStyle.top + rawStyle.height;
  123. if (bottom > this._boundingPageSize.height) {
  124. this._boundingPageSize.height = bottom;
  125. }
  126. }
  127. }
  128. }
  129. }
  130. };
  131. LayoutPageSizeUpgrade.prototype._determineRelativeTemplateType = function _determineRelativeTemplateType(layout) {
  132. if (layout.type === CONTAINER_TYPE) {
  133. var templateName = layout.templateName;
  134. if (templateName) {
  135. if (templateName.lastIndexOf('Infographics', 0) === 0) {
  136. this._relativeTemplateType = INFOGRAPHIC_TEMPLATE_TYPE;
  137. } else if (templateName.lastIndexOf('Template', 0) === 0) {
  138. this._relativeTemplateType = REGULAR_TEMPLATE_TYPE;
  139. }
  140. }
  141. }
  142. };
  143. LayoutPageSizeUpgrade.prototype._determineLayoutTypes = function _determineLayoutTypes(layout) {
  144. if (layout.type === ABSOLUTE_TYPE) {
  145. this._hasAbsoluteLayouts = true;
  146. } else if (layout.type === SCALINGABSOLUTE_TYPE) {
  147. this._hasRelativeLayouts = true;
  148. }
  149. };
  150. // Absolute layout will now be under a parent container, similar to relative layouts
  151. LayoutPageSizeUpgrade.prototype._upgradeAbsoluteContainer = function _upgradeAbsoluteContainer(layout) {
  152. if (layout.type === ABSOLUTE_TYPE) {
  153. var copyLayout = _.deepClone(layout);
  154. // Start clean on new container
  155. for (var prop in layout) {
  156. delete layout[prop];
  157. }
  158. layout.id = UniqueId.get('model');
  159. layout.items = [copyLayout];
  160. layout.title = copyLayout.title;
  161. layout.type = CONTAINER_TYPE;
  162. layout.templateName = copyLayout.templateName;
  163. delete copyLayout.title;
  164. delete copyLayout.templateName;
  165. this._containerPageIdMap[copyLayout.id] = layout.id;
  166. }
  167. };
  168. LayoutPageSizeUpgrade.prototype._getRawStyle = function _getRawStyle(style, units) {
  169. // Validate style units (can have bad dashboards with a mix of relative/absolute coordinates)
  170. if (style.left.indexOf(units) !== -1 && style.top.indexOf(units) !== -1 && style.width.indexOf(units) !== -1 && style.height.indexOf(units) !== -1) {
  171. return {
  172. left: parseInt(style.left, 10),
  173. top: parseInt(style.top, 10),
  174. width: parseInt(style.width, 10),
  175. height: parseInt(style.height, 10)
  176. };
  177. }
  178. return undefined;
  179. };
  180. LayoutPageSizeUpgrade.prototype._upgradePageSize = function _upgradePageSize(layout) {
  181. if (this._hasAbsoluteLayouts && !this._hasRelativeLayouts) {
  182. layout.pageSize = this._boundingPageSize;
  183. } else if (this._hasRelativeLayouts && !this._hasAbsoluteLayouts) {
  184. layout.pageSize = this._getDefaultPageSizeForTemplate();
  185. } else if (this._hasRelativeLayouts && this._hasAbsoluteLayouts) {
  186. // A combination of absolute and relative
  187. // 1.) Calculate minimum bounding rect of absolute items
  188. // 2a.) Increase height to match relative aspect ratio (template or infographics)
  189. // 2b.) or if height needs to be smaller, then increase width to match relative aspect ratio.
  190. layout.pageSize = this._boundingPageSize;
  191. var absoluteRatio = this._boundingPageSize.height / this._boundingPageSize.width;
  192. var relativePageSize = this._getDefaultPageSizeForTemplate();
  193. var relativeRatio = relativePageSize.height / relativePageSize.width;
  194. if (absoluteRatio < relativeRatio) {
  195. // Adjust the height to match the aspect ratio
  196. layout.pageSize = {
  197. width: this._boundingPageSize.width,
  198. height: Math.round(this._boundingPageSize.width * relativeRatio)
  199. };
  200. } else {
  201. // Adjust the width to match the aspect ratio
  202. layout.pageSize = {
  203. width: Math.round(this._boundingPageSize.height / relativeRatio),
  204. height: this._boundingPageSize.height
  205. };
  206. }
  207. }
  208. };
  209. LayoutPageSizeUpgrade.prototype._getDefaultPageSizeForTemplate = function _getDefaultPageSizeForTemplate() {
  210. // Page aspect ratio for height: (1:0.55 tabbed, 1:2 infographic)
  211. if (this._relativeTemplateType === INFOGRAPHIC_TEMPLATE_TYPE) {
  212. return { width: 512, height: 1024 };
  213. } else {
  214. return { width: 1280, height: 704 };
  215. }
  216. };
  217. return LayoutPageSizeUpgrade;
  218. }(UpgradeBase);
  219. return new LayoutPageSizeUpgrade();
  220. });
  221. //# sourceMappingURL=LayoutPageSizeUpgrade.js.map