GenericPage.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. 'use strict';
  2. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  3. /**
  4. * Licensed Materials - Property of IBM
  5. * IBM Cognos Products: BI Cloud (C) Copyright IBM Corp. 2018, 2019
  6. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  7. */
  8. define(['jquery', 'underscore', './Absolute', './ScalingAbsolute', '../TemplateHelper', '../../../util/PxPercentUtil', '../../../../app/util/DeepClone'], function ($, _, Absolute, ScalingAbsolute, TemplateHelper, PxPercentUtil) {
  9. /*
  10. * The generic page class is an authoring view manager that can switch between relative (scalingAbsolute)
  11. * and absolute (Absolute) layouts.
  12. * It isn't a view as it delegates that work off to one of the 2 authoring layout views.
  13. * There are only a few api's on the view that are utilized directly on the object [destroy() and _addWidget].
  14. */
  15. return function () {
  16. function GenericPage(options) {
  17. _classCallCheck(this, GenericPage);
  18. // Cache the options, so that we can switch views at will
  19. this._options = options;
  20. var templateHelperOptions = {
  21. model: options.model,
  22. layoutController: options.layoutController
  23. };
  24. this._options.templateHelper = new TemplateHelper(templateHelperOptions);
  25. var layoutPositioning = options.model.getValueFromSelfOrParent('layoutPositioning');
  26. this._switchLayoutView(layoutPositioning);
  27. this._setPageLayoutStyling(layoutPositioning);
  28. this._delegateView.model.on('change:layoutPositioning', this._onChangeLayoutPositioning, this);
  29. }
  30. /**
  31. * Called when the view is destroyed
  32. */
  33. GenericPage.prototype.destroy = function destroy() {
  34. this._delegateView.model.off('change:layoutPositioning', this._onChangeLayoutPositioning, this);
  35. this._delegateView.destroy();
  36. this._options.templateHelper.destroy();
  37. this._options.templateHelper = null;
  38. };
  39. GenericPage.prototype._switchLayoutView = function _switchLayoutView(layoutPositioning) {
  40. // Cleanup existing authoring view if present
  41. if (this._delegateView) {
  42. this._delegateView.destroy();
  43. }
  44. if (layoutPositioning === 'absolute') {
  45. this._delegateView = new Absolute(this._options);
  46. } else {
  47. this._delegateView = new ScalingAbsolute(this._options);
  48. }
  49. // Set the author view reference to this generic page, so we can clean up after.
  50. this._delegateView.consumeView.authorViewManager = this;
  51. };
  52. GenericPage.prototype._onChangeLayoutPositioning = function _onChangeLayoutPositioning(payload) {
  53. this._switchLayoutView(payload.value);
  54. this._setPageLayoutStyling(payload.value);
  55. // In the case of undo/redo we have already updated the models
  56. if (payload.sender !== 'UndoRedoController') {
  57. this._convertLayoutPositioning(payload);
  58. }
  59. };
  60. // Convert widget/group style values (px->percent or percent->pixel)
  61. GenericPage.prototype._convertLayoutPositioning = function _convertLayoutPositioning(payload) {
  62. // Need to know the page size to use for the conversions
  63. // If the current layout is absolute, then we need to use the current absolute page size to generate the correct
  64. // widget percentages for the destination page.
  65. // If the current layout is relative, then we need to use the destination page size (absolute) to generate the correct
  66. // pixel values in the destination page.
  67. var pageSize = this._delegateView.getPhysicalPageSize('absolute');
  68. var items = this._delegateView.model.findDescendantsWithType(['widget', 'group']);
  69. var childItemsToUpdate = items.map(function (item) {
  70. var styleCopy = _.deepClone(item.style);
  71. if (payload.value === 'relative') {
  72. PxPercentUtil.changePixelPropertiesToPercent(styleCopy, pageSize);
  73. } else {
  74. PxPercentUtil.changePercentPropertiesToPixel(styleCopy, pageSize);
  75. }
  76. return {
  77. id: item.id,
  78. style: styleCopy
  79. };
  80. });
  81. var transactionApi = this._delegateView.dashboardApi.getFeature('Transaction');
  82. var transactionToken = transactionApi.startTransactionById(payload.data.undoRedoTransactionId);
  83. var validate = payload.eventName !== 'change:layoutPositioning';
  84. // Add a hint to the data that the operation is caused by updating the style to percent
  85. // This is to avoid doing an extra call to 'updateStyleToPercent' as a result of a layout style change
  86. if (payload.value === 'absolute') {
  87. _.extend(payload.data, { hint: 'updateStyleToPercent' });
  88. }
  89. this._delegateView.updateModel(childItemsToUpdate, transactionToken, validate);
  90. transactionApi.endTransaction(transactionToken);
  91. };
  92. // Switch the styles for the different layout types
  93. GenericPage.prototype._setPageLayoutStyling = function _setPageLayoutStyling(layoutPositioning) {
  94. var isRelative = layoutPositioning === 'relative';
  95. this._delegateView.$el.toggleClass('pagescalingAbsolute', isRelative);
  96. this._delegateView.$el.toggleClass('pageabsolute', !isRelative);
  97. };
  98. return GenericPage;
  99. }();
  100. });
  101. //# sourceMappingURL=GenericPage.js.map