'use strict'; function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } 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; } 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; } /* *+------------------------------------------------------------------------+ *| Licensed Materials - Property of IBM *| IBM Cognos Products: BI Dashboard *| (C) Copyright IBM Corp. 2018, 2019 *| *| US Government Users Restricted Rights - Use, duplication or disclosure *| restricted by GSA ADP Schedule Contract with IBM Corp. *+------------------------------------------------------------------------+ */ define(['../../../lib/@waca/upgrades/UpgradeBase', '../../../lib/@waca/core-client/js/core-client/utils/UniqueId', './ContainerPageIdUpgradeHelper', 'underscore'], function (UpgradeBase, UniqueId, ContainerPageIdUpgradeHelper, _) { var ABSOLUTE_TYPE = 'absolute'; var SCALINGABSOLUTE_TYPE = 'scalingAbsolute'; var CONTAINER_TYPE = 'container'; var WIDGET_TYPE = 'widget'; var GROUP_TYPE = 'group'; var INFOGRAPHIC_TEMPLATE_TYPE = 'Infographic'; var REGULAR_TEMPLATE_TYPE = 'Template'; /** * Upgrades the absolute layout to be in a parent container similar to relative layouts for the page size funtionality. * Upgrades the layout to contain a pageSize based off widget positioning/page types. **/ var LayoutPageSizeUpgrade = function (_UpgradeBase) { _inherits(LayoutPageSizeUpgrade, _UpgradeBase); function LayoutPageSizeUpgrade() { _classCallCheck(this, LayoutPageSizeUpgrade); var _this = _possibleConstructorReturn(this, _UpgradeBase.call(this)); _this.VERSION = 1011; _this._containerPageIdUpgrader = new ContainerPageIdUpgradeHelper(); return _this; } /** * Perform upgrade * * @param {object} spec - spec to perform upgrade on * * @return {Promise} Promise to be resolved when upgrade performed */ LayoutPageSizeUpgrade.prototype.up = function up(spec) { var _this2 = this; return Promise.resolve().then(function () { _this2._init(); if (spec && spec.layout) { _this2._upgradeLayout(spec.layout); _this2._upgradePageSize(spec.layout); _this2._containerPageIdUpgrader.upgrade(spec, _this2._containerPageIdMap); } return spec; }); }; // Downgrades are not available in CA LayoutPageSizeUpgrade.prototype.down = function down(spec) { return Promise.resolve(spec); }; /** * Set the container id upgrade function * If supplied, a custom container upgrade function will be called to upgrade the spec * @param {object} containerPageIdUpgrade - container id upgrade object which implements the following interface: * upgrade(spec, idMap) * Currently only used for unit testing purposes */ LayoutPageSizeUpgrade.prototype.setContainerIdUpgrader = function setContainerIdUpgrader(containerPageIdUpgrade) { if (containerPageIdUpgrade) { this._containerPageIdUpgrader = containerPageIdUpgrade; } }; LayoutPageSizeUpgrade.prototype._init = function _init() { this._hasRelativeLayouts = false; this._hasAbsoluteLayouts = false; this._relativeTemplateType = undefined; // Default minimum page size during upgrade this._boundingPageSize = { width: 1280, height: 720 }; this._containerPageIdMap = {}; }; LayoutPageSizeUpgrade.prototype._upgradeLayout = function _upgradeLayout(layout) { if (layout.items) { // Recursively upgrade child layouts for (var _iterator = layout.items, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) { var _ref; if (_isArray) { if (_i >= _iterator.length) break; _ref = _iterator[_i++]; } else { _i = _iterator.next(); if (_i.done) break; _ref = _i.value; } var item = _ref; this._upgradeLayout(item); } } this._determinePageSizeFromLayout(layout); this._determineRelativeTemplateType(layout); this._determineLayoutTypes(layout); this._upgradeAbsoluteContainer(layout); }; LayoutPageSizeUpgrade.prototype._determinePageSizeFromLayout = function _determinePageSizeFromLayout(layout) { if (layout.type === ABSOLUTE_TYPE) { // Determine bounds from widgets & groups for (var _iterator2 = layout.items, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) { var _ref2; if (_isArray2) { if (_i2 >= _iterator2.length) break; _ref2 = _iterator2[_i2++]; } else { _i2 = _iterator2.next(); if (_i2.done) break; _ref2 = _i2.value; } var item = _ref2; if (item.type === WIDGET_TYPE || item.type === GROUP_TYPE) { var rawStyle = this._getRawStyle(item.style, 'px'); if (rawStyle) { var right = rawStyle.left + rawStyle.width; if (right > this._boundingPageSize.width) { this._boundingPageSize.width = right; } var bottom = rawStyle.top + rawStyle.height; if (bottom > this._boundingPageSize.height) { this._boundingPageSize.height = bottom; } } } } } }; LayoutPageSizeUpgrade.prototype._determineRelativeTemplateType = function _determineRelativeTemplateType(layout) { if (layout.type === CONTAINER_TYPE) { var templateName = layout.templateName; if (templateName) { if (templateName.lastIndexOf('Infographics', 0) === 0) { this._relativeTemplateType = INFOGRAPHIC_TEMPLATE_TYPE; } else if (templateName.lastIndexOf('Template', 0) === 0) { this._relativeTemplateType = REGULAR_TEMPLATE_TYPE; } } } }; LayoutPageSizeUpgrade.prototype._determineLayoutTypes = function _determineLayoutTypes(layout) { if (layout.type === ABSOLUTE_TYPE) { this._hasAbsoluteLayouts = true; } else if (layout.type === SCALINGABSOLUTE_TYPE) { this._hasRelativeLayouts = true; } }; // Absolute layout will now be under a parent container, similar to relative layouts LayoutPageSizeUpgrade.prototype._upgradeAbsoluteContainer = function _upgradeAbsoluteContainer(layout) { if (layout.type === ABSOLUTE_TYPE) { var copyLayout = _.deepClone(layout); // Start clean on new container for (var prop in layout) { delete layout[prop]; } layout.id = UniqueId.get('model'); layout.items = [copyLayout]; layout.title = copyLayout.title; layout.type = CONTAINER_TYPE; layout.templateName = copyLayout.templateName; delete copyLayout.title; delete copyLayout.templateName; this._containerPageIdMap[copyLayout.id] = layout.id; } }; LayoutPageSizeUpgrade.prototype._getRawStyle = function _getRawStyle(style, units) { // Validate style units (can have bad dashboards with a mix of relative/absolute coordinates) if (style.left.indexOf(units) !== -1 && style.top.indexOf(units) !== -1 && style.width.indexOf(units) !== -1 && style.height.indexOf(units) !== -1) { return { left: parseInt(style.left, 10), top: parseInt(style.top, 10), width: parseInt(style.width, 10), height: parseInt(style.height, 10) }; } return undefined; }; LayoutPageSizeUpgrade.prototype._upgradePageSize = function _upgradePageSize(layout) { if (this._hasAbsoluteLayouts && !this._hasRelativeLayouts) { layout.pageSize = this._boundingPageSize; } else if (this._hasRelativeLayouts && !this._hasAbsoluteLayouts) { layout.pageSize = this._getDefaultPageSizeForTemplate(); } else if (this._hasRelativeLayouts && this._hasAbsoluteLayouts) { // A combination of absolute and relative // 1.) Calculate minimum bounding rect of absolute items // 2a.) Increase height to match relative aspect ratio (template or infographics) // 2b.) or if height needs to be smaller, then increase width to match relative aspect ratio. layout.pageSize = this._boundingPageSize; var absoluteRatio = this._boundingPageSize.height / this._boundingPageSize.width; var relativePageSize = this._getDefaultPageSizeForTemplate(); var relativeRatio = relativePageSize.height / relativePageSize.width; if (absoluteRatio < relativeRatio) { // Adjust the height to match the aspect ratio layout.pageSize = { width: this._boundingPageSize.width, height: Math.round(this._boundingPageSize.width * relativeRatio) }; } else { // Adjust the width to match the aspect ratio layout.pageSize = { width: Math.round(this._boundingPageSize.height / relativeRatio), height: this._boundingPageSize.height }; } } }; LayoutPageSizeUpgrade.prototype._getDefaultPageSizeForTemplate = function _getDefaultPageSizeForTemplate() { // Page aspect ratio for height: (1:0.55 tabbed, 1:2 infographic) if (this._relativeTemplateType === INFOGRAPHIC_TEMPLATE_TYPE) { return { width: 512, height: 1024 }; } else { return { width: 1280, height: 704 }; } }; return LayoutPageSizeUpgrade; }(UpgradeBase); return new LayoutPageSizeUpgrade(); }); //# sourceMappingURL=LayoutPageSizeUpgrade.js.map