GenericPage.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. 'use strict';
  2. /**
  3. * Licensed Materials - Property of IBM
  4. * IBM Cognos Products: BI Cloud (C) Copyright IBM Corp. 2018, 2019
  5. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  6. */
  7. define(['./LayoutBaseView', 'jquery'], function (BaseClass, $) {
  8. /*
  9. * The generic page class is a view which can represent a relative or absolute layout */
  10. var ASPECT_RATIO_STYLE_ID = 'dashboardRelativeAspectRatio';
  11. var GenericPage = BaseClass.extend({
  12. aspectRatio: 0,
  13. init: function init() /*options*/{
  14. GenericPage.inherited('init', this, arguments);
  15. this.layoutController.topLayoutModel.on('change:pageSize', this._onChangePageSize, this);
  16. this.layoutController.topLayoutModel.on('change:fitPage', this._onFitPage, this);
  17. this.layoutController.topLayoutModel.on('change:layoutPositioning', this._onChangeLayoutPositioning, this);
  18. this._setPageLayoutStyling();
  19. },
  20. /**
  21. * Called when the view is destroyed
  22. */
  23. destroy: function destroy() {
  24. this.layoutController.topLayoutModel.off('change:pageSize', this._onChangePageSize, this);
  25. this.layoutController.topLayoutModel.off('change:fitPage', this._onFitPage, this);
  26. this.layoutController.topLayoutModel.off('change:layoutPositioning', this._onChangeLayoutPositioning, this);
  27. // If this is the last generic page, clean up any page size styles
  28. var layoutModels = this.layoutController.topLayoutModel.findDescendantsWithType('genericPage');
  29. if (layoutModels.length === 0) {
  30. this._clearPageSize();
  31. }
  32. GenericPage.inherited('destroy', this, arguments);
  33. },
  34. /**
  35. * Invoked when a hidden layout is shown
  36. */
  37. onShow: function onShow() {
  38. GenericPage.inherited('onShow', this, arguments);
  39. // Update page size when shown, as the 1 aspect ratio style may be different from the previous shown view
  40. if (this.$el.is(':visible')) {
  41. this._setPageFitStyling();
  42. this._applyPageSize();
  43. this._setPageLayoutStyling();
  44. }
  45. },
  46. /**
  47. * Apply the current page size configuration to the view
  48. */
  49. _applyPageSize: function _applyPageSize() {
  50. var layoutPositioning = this._getLayoutPositioning();
  51. if (layoutPositioning === 'absolute') {
  52. var pageSize = this.model.getValueFromSelfOrParent('pageSize');
  53. this._setAbsolutePageSize(pageSize);
  54. } else if (layoutPositioning === 'relative') {
  55. var _pageSize = this.model.getValueFromSelfOrParent('pageSize');
  56. this._setRelativeAspectRatio(_pageSize);
  57. }
  58. },
  59. /**
  60. * Clear page size settings from the view
  61. */
  62. _clearPageSize: function _clearPageSize(layoutPos) {
  63. var layoutPositioning = layoutPos || this._getLayoutPositioning();
  64. if (layoutPositioning === 'absolute') {
  65. this.$el.css({ width: '', height: '' });
  66. } else if (layoutPositioning === 'relative') {
  67. this._removeDynamicStyles(ASPECT_RATIO_STYLE_ID);
  68. }
  69. },
  70. _getLayoutPositioning: function _getLayoutPositioning() {
  71. return this.model.getLayoutPositioning(false);
  72. },
  73. _onChangePageSize: function _onChangePageSize() {
  74. var payload = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  75. // Remove previous page size remnants from the consumeView
  76. if (this.$el.is(':visible')) {
  77. this._clearPageSize(payload.prevValue);
  78. this._applyPageSize();
  79. }
  80. },
  81. _onFitPage: function _onFitPage() {
  82. if (this.$el.is(':visible')) {
  83. this._setPageFitStyling();
  84. this._onChangePageSize();
  85. }
  86. this.onResize();
  87. },
  88. _onChangeLayoutPositioning: function _onChangeLayoutPositioning(payload) {
  89. this._clearPageSize(payload.prevValue);
  90. if (this.$el.is(':visible')) {
  91. this._setPageLayoutStyling();
  92. this._setPageFitStyling();
  93. // Remove previous page size remnants
  94. this._applyPageSize();
  95. }
  96. this.onResize();
  97. },
  98. _setPageFitStyling: function _setPageFitStyling() {
  99. var isRelative = this._getLayoutPositioning() === 'relative';
  100. var fitPage = isRelative && this.getFitPage();
  101. this.$el.toggleClass('fitToPage', fitPage);
  102. },
  103. _setPageLayoutStyling: function _setPageLayoutStyling() {
  104. var isRelative = this._getLayoutPositioning() === 'relative';
  105. this.$el.toggleClass('templateBox', isRelative);
  106. },
  107. _setAbsolutePageSize: function _setAbsolutePageSize(pageSize) {
  108. this.$el.width(pageSize.width);
  109. this.$el.height(pageSize.height);
  110. },
  111. _setRelativeAspectRatio: function _setRelativeAspectRatio(pageSize) {
  112. this.aspectRatio = pageSize.height / pageSize.width;
  113. var fitPage = this.getFitPage();
  114. if (fitPage) {
  115. this._addAspectRatioForFitToPage();
  116. } else {
  117. // Fit to Width
  118. this._createStyleNode('.aspectRatio_default:before { padding-top:' + this.aspectRatio * 100 + '%; }', ASPECT_RATIO_STYLE_ID);
  119. }
  120. },
  121. onResize: function onResize() {
  122. if (this.$el.is(':visible')) {
  123. var fitPage = this.getFitPage();
  124. if (fitPage) {
  125. this._addAspectRatioForFitToPage();
  126. }
  127. }
  128. GenericPage.inherited('onResize', this, arguments);
  129. },
  130. _addAspectRatioForFitToPage: function _addAspectRatioForFitToPage() {
  131. var size = { 'height': this.$el.parent().height(), 'width': this.$el.parent().width() };
  132. var style = '.page.pagecontainer .pagegenericPage { width: ' + size.width + 'px; height:' + this.aspectRatio * size.width + 'px; max-height: ' + size.height + 'px; max-width:' + 1 / this.aspectRatio * size.height + 'px;}';
  133. this._createStyleNode(style, ASPECT_RATIO_STYLE_ID);
  134. },
  135. _createStyleNode: function _createStyleNode(styles, stylesId) {
  136. var dynamicAspectRatioStyle = $('#' + stylesId).html();
  137. if (dynamicAspectRatioStyle !== styles) {
  138. this._removeDynamicStyles(stylesId);
  139. var styleNode = $('<style class=\'dashboardLayoutStyles\' type="text/css" id="' + stylesId + '"></style>');
  140. styleNode.html(styles);
  141. styleNode.appendTo('head');
  142. }
  143. },
  144. _removeDynamicStyles: function _removeDynamicStyles(stylesId) {
  145. $('#' + stylesId).remove();
  146. }
  147. });
  148. return GenericPage;
  149. });
  150. //# sourceMappingURL=GenericPage.js.map