Brak opisu
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MessageBanner.js 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // Copyright (c) Microsoft. All rights reserved. Licensed under the MIT license. See LICENSE in the project root for license information.
  2. /**
  3. * MessageBanner component
  4. *
  5. * A component to display error messages
  6. *
  7. */
  8. /**
  9. * @namespace fabric
  10. */
  11. var fabric = fabric || {};
  12. /**
  13. *
  14. * @param {HTMLElement} container - the target container for an instance of MessageBanner
  15. * @constructor
  16. */
  17. fabric.MessageBanner = function (container) {
  18. this.container = container;
  19. this.init();
  20. };
  21. fabric.MessageBanner.prototype = (function () {
  22. var _clipper;
  23. var _bufferSize;
  24. var _textContainerMaxWidth = 700;
  25. var _clientWidth;
  26. var _textWidth;
  27. var _initTextWidth;
  28. var _chevronButton;
  29. var _errorBanner;
  30. var _actionButton;
  31. var _closeButton;
  32. var _bufferElementsWidth = 88;
  33. var _bufferElementsWidthSmall = 35;
  34. var SMALL_BREAK_POINT = 480;
  35. /**
  36. * sets styles on resize
  37. */
  38. var _onResize = function () {
  39. _clientWidth = _errorBanner.offsetWidth;
  40. if (window.innerWidth >= SMALL_BREAK_POINT) {
  41. _resizeRegular();
  42. } else {
  43. _resizeSmall();
  44. }
  45. };
  46. /**
  47. * resize above 480 pixel breakpoint
  48. */
  49. var _resizeRegular = function () {
  50. if (_clientWidth - _bufferSize > _initTextWidth && _initTextWidth < _textContainerMaxWidth) {
  51. _textWidth = "auto";
  52. _chevronButton.className = "ms-MessageBanner-expand";
  53. _collapse();
  54. } else {
  55. _textWidth = Math.min(_clientWidth - _bufferSize, _textContainerMaxWidth) + "px";
  56. if (_chevronButton.className.indexOf("is-visible") === -1) {
  57. _chevronButton.className += " is-visible";
  58. }
  59. }
  60. _clipper.style.width = _textWidth;
  61. };
  62. /**
  63. * resize below 480 pixel breakpoint
  64. */
  65. var _resizeSmall = function () {
  66. if (_clientWidth - (_bufferElementsWidthSmall + _closeButton.offsetWidth) > _initTextWidth) {
  67. _textWidth = "auto";
  68. _collapse();
  69. } else {
  70. _textWidth = _clientWidth - (_bufferElementsWidthSmall + _closeButton.offsetWidth) + "px";
  71. }
  72. _clipper.style.width = _textWidth;
  73. };
  74. /**
  75. * caches elements and values of the component
  76. * @param {Object} context - the context
  77. */
  78. var _cacheDOM = function (context) {
  79. _errorBanner = context.container;
  80. _clipper = context.container.querySelector('.ms-MessageBanner-clipper');
  81. _chevronButton = context.container.querySelector('.ms-MessageBanner-expand');
  82. _actionButton = context.container.querySelector('.ms-MessageBanner-action');
  83. _bufferSize = _actionButton.offsetWidth + _bufferElementsWidth;
  84. _closeButton = context.container.querySelector('.ms-MessageBanner-close');
  85. };
  86. /**
  87. * expands component to show full error message
  88. */
  89. var _expand = function () {
  90. var icon = _chevronButton.querySelector('.ms-Icon');
  91. _errorBanner.className += " is-expanded";
  92. icon.className = "ms-Icon ms-Icon--chevronsUp";
  93. };
  94. /**
  95. * collapses component to only show truncated message
  96. */
  97. var _collapse = function () {
  98. var icon = _chevronButton.querySelector('.ms-Icon');
  99. _errorBanner.className = "ms-MessageBanner";
  100. icon.className = "ms-Icon ms-Icon--chevronsDown";
  101. };
  102. var _toggleExpansion = function () {
  103. if (_errorBanner.className.indexOf("is-expanded") > -1) {
  104. _collapse();
  105. } else {
  106. _expand();
  107. }
  108. };
  109. /**
  110. * hides banner when close button is clicked
  111. */
  112. var _hideBanner = function () {
  113. if (_errorBanner.className.indexOf("hide") === -1) {
  114. _errorBanner.className += " hide";
  115. setTimeout(function () {
  116. _errorBanner.className = "ms-MessageBanner is-hidden";
  117. }, 500);
  118. }
  119. };
  120. /**
  121. * shows banner if the banner is hidden
  122. */
  123. var _showBanner = function () {
  124. _errorBanner.className = "ms-MessageBanner";
  125. };
  126. /**
  127. * sets handlers for resize and button click events
  128. */
  129. var _setListeners = function () {
  130. window.addEventListener('resize', _onResize, false);
  131. _chevronButton.addEventListener("click", _toggleExpansion, false);
  132. _closeButton.addEventListener("click", _hideBanner, false);
  133. };
  134. /**
  135. * initializes component
  136. */
  137. var init = function () {
  138. _cacheDOM(this);
  139. _setListeners();
  140. _clientWidth = _errorBanner.offsetWidth;
  141. _initTextWidth = _clipper.offsetWidth;
  142. _onResize(null);
  143. };
  144. return {
  145. init: init,
  146. showBanner: _showBanner,
  147. hideBanner: _hideBanner,
  148. toggleExpansion: _toggleExpansion
  149. };
  150. }());