3 * Manages elements that can offset the size of the viewport.
5 * Measures and reports viewport offset dimensions from elements like the
6 * toolbar that can potentially displace the positioning of other elements.
10 * @typedef {object} Drupal~displaceOffset
14 * @prop {number} right
15 * @prop {number} bottom
19 * Triggers when layout of the page changes.
21 * This is used to position fixed element on the page during page resize and
24 * @event drupalViewportOffsetChange
27 (function ($, Drupal, debounce) {
29 * @name Drupal.displace.offsets
31 * @type {Drupal~displaceOffset}
41 * Registers a resize handler on the window.
43 * @type {Drupal~behavior}
45 Drupal.behaviors.drupalDisplace = {
47 // Mark this behavior as processed on the first pass.
48 if (this.displaceProcessed) {
51 this.displaceProcessed = true;
53 $(window).on('resize.drupalDisplace', debounce(displace, 200));
58 * Informs listeners of the current offset dimensions.
60 * @function Drupal.displace
62 * @prop {Drupal~displaceOffset} offsets
64 * @param {bool} [broadcast]
65 * When true or undefined, causes the recalculated offsets values to be
66 * broadcast to listeners.
68 * @return {Drupal~displaceOffset}
69 * An object whose keys are the for sides an element -- top, right, bottom
70 * and left. The value of each key is the viewport displacement distance for
73 * @fires event:drupalViewportOffsetChange
75 function displace(broadcast) {
76 offsets = Drupal.displace.offsets = calculateOffsets();
77 if (typeof broadcast === 'undefined' || broadcast) {
78 $(document).trigger('drupalViewportOffsetChange', offsets);
84 * Determines the viewport offsets.
86 * @return {Drupal~displaceOffset}
87 * An object whose keys are the for sides an element -- top, right, bottom
88 * and left. The value of each key is the viewport displacement distance for
91 function calculateOffsets() {
93 top: calculateOffset('top'),
94 right: calculateOffset('right'),
95 bottom: calculateOffset('bottom'),
96 left: calculateOffset('left'),
101 * Gets a specific edge's offset.
103 * Any element with the attribute data-offset-{edge} e.g. data-offset-top will
104 * be considered in the viewport offset calculations. If the attribute has a
105 * numeric value, that value will be used. If no value is provided, one will
106 * be calculated using the element's dimensions and placement.
108 * @function Drupal.displace.calculateOffset
110 * @param {string} edge
111 * The name of the edge to calculate. Can be 'top', 'right',
112 * 'bottom' or 'left'.
115 * The viewport displacement distance for the requested edge.
117 function calculateOffset(edge) {
119 const displacingElements = document.querySelectorAll(`[data-offset-${edge}]`);
120 const n = displacingElements.length;
121 for (let i = 0; i < n; i++) {
122 const el = displacingElements[i];
123 // If the element is not visible, do consider its dimensions.
124 if (el.style.display === 'none') {
127 // If the offset data attribute contains a displacing value, use it.
128 let displacement = parseInt(el.getAttribute(`data-offset-${edge}`), 10);
129 // If the element's offset data attribute exits
130 // but is not a valid number then get the displacement
131 // dimensions directly from the element.
132 if (isNaN(displacement)) {
133 displacement = getRawOffset(el, edge);
135 // If the displacement value is larger than the current value for this
136 // edge, use the displacement value.
137 edgeOffset = Math.max(edgeOffset, displacement);
144 * Calculates displacement for element based on its dimensions and placement.
146 * @param {HTMLElement} el
147 * The jQuery element whose dimensions and placement will be measured.
149 * @param {string} edge
150 * The name of the edge of the viewport that the element is associated
154 * The viewport displacement distance for the requested edge.
156 function getRawOffset(el, edge) {
158 const documentElement = document.documentElement;
159 let displacement = 0;
160 const horizontal = (edge === 'left' || edge === 'right');
161 // Get the offset of the element itself.
162 let placement = $el.offset()[horizontal ? 'left' : 'top'];
163 // Subtract scroll distance from placement to get the distance
164 // to the edge of the viewport.
165 placement -= window[`scroll${horizontal ? 'X' : 'Y'}`] || document.documentElement[`scroll${horizontal ? 'Left' : 'Top'}`] || 0;
166 // Find the displacement value according to the edge.
168 // Left and top elements displace as a sum of their own offset value
171 // Total displacement is the sum of the elements placement and size.
172 displacement = placement + $el.outerHeight();
176 // Total displacement is the sum of the elements placement and size.
177 displacement = placement + $el.outerWidth();
180 // Right and bottom elements displace according to their left and
181 // top offset. Their size isn't important.
183 displacement = documentElement.clientHeight - placement;
187 displacement = documentElement.clientWidth - placement;
197 * Assign the displace function to a property of the Drupal global object.
201 Drupal.displace = displace;
202 $.extend(Drupal.displace, {
205 * Expose offsets to other scripts to avoid having to recalculate offsets.
212 * Expose method to compute a single edge offsets.
218 }(jQuery, Drupal, Drupal.debounce));