displace.es6.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /**
  2. * @file
  3. * Manages elements that can offset the size of the viewport.
  4. *
  5. * Measures and reports viewport offset dimensions from elements like the
  6. * toolbar that can potentially displace the positioning of other elements.
  7. */
  8. /**
  9. * @typedef {object} Drupal~displaceOffset
  10. *
  11. * @prop {number} top
  12. * @prop {number} left
  13. * @prop {number} right
  14. * @prop {number} bottom
  15. */
  16. /**
  17. * Triggers when layout of the page changes.
  18. *
  19. * This is used to position fixed element on the page during page resize and
  20. * Toolbar toggling.
  21. *
  22. * @event drupalViewportOffsetChange
  23. */
  24. (function ($, Drupal, debounce) {
  25. /**
  26. * @name Drupal.displace.offsets
  27. *
  28. * @type {Drupal~displaceOffset}
  29. */
  30. let offsets = {
  31. top: 0,
  32. right: 0,
  33. bottom: 0,
  34. left: 0,
  35. };
  36. /**
  37. * Registers a resize handler on the window.
  38. *
  39. * @type {Drupal~behavior}
  40. */
  41. Drupal.behaviors.drupalDisplace = {
  42. attach() {
  43. // Mark this behavior as processed on the first pass.
  44. if (this.displaceProcessed) {
  45. return;
  46. }
  47. this.displaceProcessed = true;
  48. $(window).on('resize.drupalDisplace', debounce(displace, 200));
  49. },
  50. };
  51. /**
  52. * Informs listeners of the current offset dimensions.
  53. *
  54. * @function Drupal.displace
  55. *
  56. * @prop {Drupal~displaceOffset} offsets
  57. *
  58. * @param {bool} [broadcast]
  59. * When true or undefined, causes the recalculated offsets values to be
  60. * broadcast to listeners.
  61. *
  62. * @return {Drupal~displaceOffset}
  63. * An object whose keys are the for sides an element -- top, right, bottom
  64. * and left. The value of each key is the viewport displacement distance for
  65. * that edge.
  66. *
  67. * @fires event:drupalViewportOffsetChange
  68. */
  69. function displace(broadcast) {
  70. offsets = Drupal.displace.offsets = calculateOffsets();
  71. if (typeof broadcast === 'undefined' || broadcast) {
  72. $(document).trigger('drupalViewportOffsetChange', offsets);
  73. }
  74. return offsets;
  75. }
  76. /**
  77. * Determines the viewport offsets.
  78. *
  79. * @return {Drupal~displaceOffset}
  80. * An object whose keys are the for sides an element -- top, right, bottom
  81. * and left. The value of each key is the viewport displacement distance for
  82. * that edge.
  83. */
  84. function calculateOffsets() {
  85. return {
  86. top: calculateOffset('top'),
  87. right: calculateOffset('right'),
  88. bottom: calculateOffset('bottom'),
  89. left: calculateOffset('left'),
  90. };
  91. }
  92. /**
  93. * Gets a specific edge's offset.
  94. *
  95. * Any element with the attribute data-offset-{edge} e.g. data-offset-top will
  96. * be considered in the viewport offset calculations. If the attribute has a
  97. * numeric value, that value will be used. If no value is provided, one will
  98. * be calculated using the element's dimensions and placement.
  99. *
  100. * @function Drupal.displace.calculateOffset
  101. *
  102. * @param {string} edge
  103. * The name of the edge to calculate. Can be 'top', 'right',
  104. * 'bottom' or 'left'.
  105. *
  106. * @return {number}
  107. * The viewport displacement distance for the requested edge.
  108. */
  109. function calculateOffset(edge) {
  110. let edgeOffset = 0;
  111. const displacingElements = document.querySelectorAll(`[data-offset-${edge}]`);
  112. const n = displacingElements.length;
  113. for (let i = 0; i < n; i++) {
  114. const el = displacingElements[i];
  115. // If the element is not visible, do consider its dimensions.
  116. if (el.style.display === 'none') {
  117. continue;
  118. }
  119. // If the offset data attribute contains a displacing value, use it.
  120. let displacement = parseInt(el.getAttribute(`data-offset-${edge}`), 10);
  121. // If the element's offset data attribute exits
  122. // but is not a valid number then get the displacement
  123. // dimensions directly from the element.
  124. if (isNaN(displacement)) {
  125. displacement = getRawOffset(el, edge);
  126. }
  127. // If the displacement value is larger than the current value for this
  128. // edge, use the displacement value.
  129. edgeOffset = Math.max(edgeOffset, displacement);
  130. }
  131. return edgeOffset;
  132. }
  133. /**
  134. * Calculates displacement for element based on its dimensions and placement.
  135. *
  136. * @param {HTMLElement} el
  137. * The jQuery element whose dimensions and placement will be measured.
  138. *
  139. * @param {string} edge
  140. * The name of the edge of the viewport that the element is associated
  141. * with.
  142. *
  143. * @return {number}
  144. * The viewport displacement distance for the requested edge.
  145. */
  146. function getRawOffset(el, edge) {
  147. const $el = $(el);
  148. const documentElement = document.documentElement;
  149. let displacement = 0;
  150. const horizontal = (edge === 'left' || edge === 'right');
  151. // Get the offset of the element itself.
  152. let placement = $el.offset()[horizontal ? 'left' : 'top'];
  153. // Subtract scroll distance from placement to get the distance
  154. // to the edge of the viewport.
  155. placement -= window[`scroll${horizontal ? 'X' : 'Y'}`] || document.documentElement[`scroll${horizontal ? 'Left' : 'Top'}`] || 0;
  156. // Find the displacement value according to the edge.
  157. switch (edge) {
  158. // Left and top elements displace as a sum of their own offset value
  159. // plus their size.
  160. case 'top':
  161. // Total displacement is the sum of the elements placement and size.
  162. displacement = placement + $el.outerHeight();
  163. break;
  164. case 'left':
  165. // Total displacement is the sum of the elements placement and size.
  166. displacement = placement + $el.outerWidth();
  167. break;
  168. // Right and bottom elements displace according to their left and
  169. // top offset. Their size isn't important.
  170. case 'bottom':
  171. displacement = documentElement.clientHeight - placement;
  172. break;
  173. case 'right':
  174. displacement = documentElement.clientWidth - placement;
  175. break;
  176. default:
  177. displacement = 0;
  178. }
  179. return displacement;
  180. }
  181. /**
  182. * Assign the displace function to a property of the Drupal global object.
  183. *
  184. * @ignore
  185. */
  186. Drupal.displace = displace;
  187. $.extend(Drupal.displace, {
  188. /**
  189. * Expose offsets to other scripts to avoid having to recalculate offsets.
  190. *
  191. * @ignore
  192. */
  193. offsets,
  194. /**
  195. * Expose method to compute a single edge offsets.
  196. *
  197. * @ignore
  198. */
  199. calculateOffset,
  200. });
  201. }(jQuery, Drupal, Drupal.debounce));