'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 Cognos Products: BI Cloud (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(['jquery', 'underscore', './Absolute', './ScalingAbsolute', '../TemplateHelper', '../../../util/PxPercentUtil', '../../../../app/util/DeepClone'], function ($, _, Absolute, ScalingAbsolute, TemplateHelper, PxPercentUtil) { /* * The generic page class is an authoring view manager that can switch between relative (scalingAbsolute) * and absolute (Absolute) layouts. * It isn't a view as it delegates that work off to one of the 2 authoring layout views. * There are only a few api's on the view that are utilized directly on the object [destroy() and _addWidget]. */ return function () { function GenericPage(options) { _classCallCheck(this, GenericPage); // Cache the options, so that we can switch views at will this._options = options; var templateHelperOptions = { model: options.model, layoutController: options.layoutController }; this._options.templateHelper = new TemplateHelper(templateHelperOptions); var layoutPositioning = options.model.getValueFromSelfOrParent('layoutPositioning'); this._switchLayoutView(layoutPositioning); this._setPageLayoutStyling(layoutPositioning); this._delegateView.model.on('change:layoutPositioning', this._onChangeLayoutPositioning, this); } /** * Called when the view is destroyed */ GenericPage.prototype.destroy = function destroy() { this._delegateView.model.off('change:layoutPositioning', this._onChangeLayoutPositioning, this); this._delegateView.destroy(); this._options.templateHelper.destroy(); this._options.templateHelper = null; }; GenericPage.prototype._switchLayoutView = function _switchLayoutView(layoutPositioning) { // Cleanup existing authoring view if present if (this._delegateView) { this._delegateView.destroy(); } if (layoutPositioning === 'absolute') { this._delegateView = new Absolute(this._options); } else { this._delegateView = new ScalingAbsolute(this._options); } // Set the author view reference to this generic page, so we can clean up after. this._delegateView.consumeView.authorViewManager = this; }; GenericPage.prototype._onChangeLayoutPositioning = function _onChangeLayoutPositioning(payload) { this._switchLayoutView(payload.value); this._setPageLayoutStyling(payload.value); // In the case of undo/redo we have already updated the models if (payload.sender !== 'UndoRedoController') { this._convertLayoutPositioning(payload); } }; // Convert widget/group style values (px->percent or percent->pixel) GenericPage.prototype._convertLayoutPositioning = function _convertLayoutPositioning(payload) { // Need to know the page size to use for the conversions // If the current layout is absolute, then we need to use the current absolute page size to generate the correct // widget percentages for the destination page. // If the current layout is relative, then we need to use the destination page size (absolute) to generate the correct // pixel values in the destination page. var pageSize = this._delegateView.getPhysicalPageSize('absolute'); var items = this._delegateView.model.findDescendantsWithType(['widget', 'group']); var childItemsToUpdate = items.map(function (item) { var styleCopy = _.deepClone(item.style); if (payload.value === 'relative') { PxPercentUtil.changePixelPropertiesToPercent(styleCopy, pageSize); } else { PxPercentUtil.changePercentPropertiesToPixel(styleCopy, pageSize); } return { id: item.id, style: styleCopy }; }); var transactionApi = this._delegateView.dashboardApi.getFeature('Transaction'); var transactionToken = transactionApi.startTransactionById(payload.data.undoRedoTransactionId); var validate = payload.eventName !== 'change:layoutPositioning'; // Add a hint to the data that the operation is caused by updating the style to percent // This is to avoid doing an extra call to 'updateStyleToPercent' as a result of a layout style change if (payload.value === 'absolute') { _.extend(payload.data, { hint: 'updateStyleToPercent' }); } this._delegateView.updateModel(childItemsToUpdate, transactionToken, validate); transactionApi.endTransaction(transactionToken); }; // Switch the styles for the different layout types GenericPage.prototype._setPageLayoutStyling = function _setPageLayoutStyling(layoutPositioning) { var isRelative = layoutPositioning === 'relative'; this._delegateView.$el.toggleClass('pagescalingAbsolute', isRelative); this._delegateView.$el.toggleClass('pageabsolute', !isRelative); }; return GenericPage; }(); }); //# sourceMappingURL=GenericPage.js.map