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 = calculateOffsets();
77 Drupal.displace.offsets = offsets;
78 if (typeof broadcast === 'undefined' || broadcast) {
79 $(document).trigger('drupalViewportOffsetChange', offsets);
85 * Determines the viewport offsets.
87 * @return {Drupal~displaceOffset}
88 * An object whose keys are the for sides an element -- top, right, bottom
89 * and left. The value of each key is the viewport displacement distance for
92 function calculateOffsets() {
94 top: calculateOffset('top'),
95 right: calculateOffset('right'),
96 bottom: calculateOffset('bottom'),
97 left: calculateOffset('left'),
102 * Gets a specific edge's offset.
104 * Any element with the attribute data-offset-{edge} e.g. data-offset-top will
105 * be considered in the viewport offset calculations. If the attribute has a
106 * numeric value, that value will be used. If no value is provided, one will
107 * be calculated using the element's dimensions and placement.
109 * @function Drupal.displace.calculateOffset
111 * @param {string} edge
112 * The name of the edge to calculate. Can be 'top', 'right',
113 * 'bottom' or 'left'.
116 * The viewport displacement distance for the requested edge.
118 function calculateOffset(edge) {
120 const displacingElements = document.querySelectorAll(`[data-offset-${edge}]`);
121 const n = displacingElements.length;
122 for (let i = 0; i < n; i++) {
123 const el = displacingElements[i];
124 // If the element is not visible, do consider its dimensions.
125 if (el.style.display === 'none') {
128 // If the offset data attribute contains a displacing value, use it.
129 let displacement = parseInt(el.getAttribute(`data-offset-${edge}`), 10);
130 // If the element's offset data attribute exits
131 // but is not a valid number then get the displacement
132 // dimensions directly from the element.
133 if (isNaN(displacement)) {
134 displacement = getRawOffset(el, edge);
136 // If the displacement value is larger than the current value for this
137 // edge, use the displacement value.
138 edgeOffset = Math.max(edgeOffset, displacement);
145 * Calculates displacement for element based on its dimensions and placement.
147 * @param {HTMLElement} el
148 * The jQuery element whose dimensions and placement will be measured.
150 * @param {string} edge
151 * The name of the edge of the viewport that the element is associated
155 * The viewport displacement distance for the requested edge.
157 function getRawOffset(el, edge) {
159 const documentElement = document.documentElement;
160 let displacement = 0;
161 const horizontal = (edge === 'left' || edge === 'right');
162 // Get the offset of the element itself.
163 let placement = $el.offset()[horizontal ? 'left' : 'top'];
164 // Subtract scroll distance from placement to get the distance
165 // to the edge of the viewport.
166 placement -= window[`scroll${horizontal ? 'X' : 'Y'}`] || document.documentElement[`scroll${horizontal ? 'Left' : 'Top'}`] || 0;
167 // Find the displacement value according to the edge.
169 // Left and top elements displace as a sum of their own offset value
172 // Total displacement is the sum of the elements placement and size.
173 displacement = placement + $el.outerHeight();
177 // Total displacement is the sum of the elements placement and size.
178 displacement = placement + $el.outerWidth();
181 // Right and bottom elements displace according to their left and
182 // top offset. Their size isn't important.
184 displacement = documentElement.clientHeight - placement;
188 displacement = documentElement.clientWidth - placement;
198 * Assign the displace function to a property of the Drupal global object.
202 Drupal.displace = displace;
203 $.extend(Drupal.displace, {
206 * Expose offsets to other scripts to avoid having to recalculate offsets.
213 * Expose method to compute a single edge offsets.
219 }(jQuery, Drupal, Drupal.debounce));