/src/sap.ui.unified/src/sap/ui/unified/ContentSwitcher.js

https://gitlab.com/crsr/openui5 · JavaScript · 182 lines · 73 code · 43 blank · 66 comment · 8 complexity · a737df4c5087fcd8cf5c599a36f030e9 MD5 · raw file

  1. /*!
  2. * ${copyright}
  3. */
  4. // Provides control sap.ui.unified.ContentSwitcher.
  5. sap.ui.define(['jquery.sap.global', 'sap/ui/core/Control', './library'],
  6. function(jQuery, Control, library) {
  7. "use strict";
  8. /**
  9. * Constructor for a new ContentSwitcher.
  10. *
  11. * @param {string} [sId] id for the new control, generated automatically if no id is given
  12. * @param {object} [mSettings] initial settings for the new control
  13. *
  14. * @class
  15. * Switches between two control areas and animates it via CSS transitions
  16. * @extends sap.ui.core.Control
  17. *
  18. * @author SAP SE
  19. * @version ${version}
  20. *
  21. * @constructor
  22. * @public
  23. * @since 1.16.0
  24. * @experimental Since version 1.16.0.
  25. * API is not yet finished and might change completely
  26. * @alias sap.ui.unified.ContentSwitcher
  27. * @ui5-metamodel This control/element also will be described in the UI5 (legacy) designtime metamodel
  28. */
  29. var ContentSwitcher = Control.extend("sap.ui.unified.ContentSwitcher", /** @lends sap.ui.unified.ContentSwitcher.prototype */ { metadata : {
  30. deprecated : true,
  31. library : "sap.ui.unified",
  32. properties : {
  33. /**
  34. * Set the used animation when changing content. This just sets a CSS-class named "sapUiUnifiedACSwitcherAnimation" + this value on the root element of the control. The animation has to be implemented in CSS. This also enables applications to implement their own animations via CSS by reacting to the parent class.
  35. * See the types sap.ui.unified.ContentSwitcherAnimation for default implementations.
  36. */
  37. animation : {type : "string", group : "Appearance", defaultValue : 'None'},
  38. /**
  39. * The number of the currently active content (1 or 2).
  40. */
  41. activeContent : {type : "int", group : "Behavior", defaultValue : 1}
  42. },
  43. aggregations : {
  44. /**
  45. * The controls that should be shown in the first content
  46. */
  47. content1 : {type : "sap.ui.core.Control", multiple : true, singularName : "content1"},
  48. /**
  49. * The controls that should be shown in the second content
  50. */
  51. content2 : {type : "sap.ui.core.Control", multiple : true, singularName : "content2"}
  52. }
  53. }});
  54. (function(window) {
  55. ////////////////////////////////////////// Public Methods //////////////////////////////////////////
  56. /**
  57. * This file defines behavior for the control,
  58. */
  59. ContentSwitcher.prototype.init = function(){
  60. };
  61. /**
  62. * Changes the currently active content to the other one. If content 1 is active, content 2 will
  63. * be activated and the other way around.
  64. *
  65. * @public
  66. */
  67. ContentSwitcher.prototype.switchContent = function() {
  68. this.setActiveContent(this.getActiveContent() == 1 ? 2 : 1);
  69. return this;
  70. };
  71. ////////////////////////////////////////// onEvent Methods /////////////////////////////////////////
  72. ContentSwitcher.prototype.onAfterRendering = function() {
  73. this._$Contents = [
  74. this.$("content1"),
  75. this.$("content2")
  76. ];
  77. };
  78. ////////////////////////////////////////// Private Methods /////////////////////////////////////////
  79. /**
  80. * Make the content-area with the given number appear/visible. This just sets the CSS-class
  81. * sapUiUnifiedCSwitcherVisible
  82. */
  83. ContentSwitcher.prototype._showActiveContent = function(iNumber) {
  84. this._$Contents[0].toggleClass("sapUiUfdCSwitcherVisible", iNumber === 1);
  85. this._$Contents[1].toggleClass("sapUiUfdCSwitcherVisible", iNumber === 2);
  86. };
  87. ///////////////////////////////////////// Hidden Functions /////////////////////////////////////////
  88. //////////////////////////////////////// Overridden Methods ////////////////////////////////////////
  89. ///////////////////////////////// Property "activeContent" /////////////////////////////////
  90. ContentSwitcher.prototype.setActiveContent = function(iNumber) {
  91. iNumber = parseInt(iNumber, 10);
  92. if (isNaN(iNumber) || iNumber < 1) {
  93. iNumber = 1;
  94. jQuery.sap.log.warning(
  95. "setActiveContent argument must be either 1 or 2. Active content set to 1."
  96. );
  97. } else if (iNumber > 2) {
  98. iNumber = 2;
  99. jQuery.sap.log.warning(
  100. "setActiveContent argument must be either 1 or 2. Active content set to 2."
  101. );
  102. }
  103. this.setProperty("activeContent", iNumber, /* supressInvalidate: */ true);
  104. this._showActiveContent(iNumber);
  105. return this;
  106. };
  107. /////////////////////////////////// Property "animation" ///////////////////////////////////
  108. ContentSwitcher.prototype.setAnimation = function(sAnimation, bSuppressInvalidate){
  109. if (typeof (sAnimation) !== "string") {
  110. sAnimation = sap.ui.unified.ContentSwitcherAnimation.None;
  111. jQuery.sap.log.warning(
  112. "setAnimation argument must be a string. Animation was set to \"" +
  113. sap.ui.unified.ContentSwitcherAnimation.None + "\"."
  114. );
  115. }
  116. // Remove all non-alphanumerical characters from the animation string
  117. sAnimation = sAnimation.replace(/[^a-zA-Z0-9]/g, "");
  118. var sCurrentAnimation = this.getProperty("animation");
  119. if (sAnimation === sCurrentAnimation) {
  120. // No change.
  121. return;
  122. }
  123. var $Dom = this.$();
  124. if ($Dom[0]) {
  125. // We are already rendered - so we have to change the class on the fly...
  126. $Dom.toggleClass("sapUiUfdCSwitcherAnimation" + sCurrentAnimation, false);
  127. $Dom.toggleClass("sapUiUfdCSwitcherAnimation" + sAnimation, true);
  128. }/* else {
  129. // The renderer will take care of it.
  130. }/**/
  131. this.setProperty("animation", sAnimation, bSuppressInvalidate);
  132. return this;
  133. };
  134. //////////////////////////////////////// Event "xxx" ///////////////////////////////////////
  135. ///////////////////////////////////// Aggregation "xxx" ////////////////////////////////////
  136. ///////////////////////////////////// Association "xxx" ////////////////////////////////////
  137. })(window);
  138. return ContentSwitcher;
  139. }, /* bExport= */ true);