X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=web%2Fcore%2Fmisc%2Fdisplace.es6.js;fp=web%2Fcore%2Fmisc%2Fdisplace.es6.js;h=e3909c9d88c531d7560fcb3854bb73c116ad2c49;hp=6ac09f4e6aae63778a7e40ef991f0d7fd4e21c4d;hb=0bf8d09d2542548982e81a441b1f16e75873a04f;hpb=74df008bdbb3a11eeea356744f39b802369bda3c diff --git a/web/core/misc/displace.es6.js b/web/core/misc/displace.es6.js index 6ac09f4e6..e3909c9d8 100644 --- a/web/core/misc/displace.es6.js +++ b/web/core/misc/displace.es6.js @@ -24,7 +24,7 @@ * @event drupalViewportOffsetChange */ -(function ($, Drupal, debounce) { +(function($, Drupal, debounce) { /** * @name Drupal.displace.offsets * @@ -38,64 +38,59 @@ }; /** - * Registers a resize handler on the window. - * - * @type {Drupal~behavior} - */ - Drupal.behaviors.drupalDisplace = { - attach() { - // Mark this behavior as processed on the first pass. - if (this.displaceProcessed) { - return; - } - this.displaceProcessed = true; - - $(window).on('resize.drupalDisplace', debounce(displace, 200)); - }, - }; - - /** - * Informs listeners of the current offset dimensions. - * - * @function Drupal.displace - * - * @prop {Drupal~displaceOffset} offsets + * Calculates displacement for element based on its dimensions and placement. * - * @param {bool} [broadcast] - * When true or undefined, causes the recalculated offsets values to be - * broadcast to listeners. + * @param {HTMLElement} el + * The jQuery element whose dimensions and placement will be measured. * - * @return {Drupal~displaceOffset} - * An object whose keys are the for sides an element -- top, right, bottom - * and left. The value of each key is the viewport displacement distance for - * that edge. + * @param {string} edge + * The name of the edge of the viewport that the element is associated + * with. * - * @fires event:drupalViewportOffsetChange + * @return {number} + * The viewport displacement distance for the requested edge. */ - function displace(broadcast) { - offsets = calculateOffsets(); - Drupal.displace.offsets = offsets; - if (typeof broadcast === 'undefined' || broadcast) { - $(document).trigger('drupalViewportOffsetChange', offsets); - } - return offsets; - } + function getRawOffset(el, edge) { + const $el = $(el); + const documentElement = document.documentElement; + let displacement = 0; + const horizontal = edge === 'left' || edge === 'right'; + // Get the offset of the element itself. + let placement = $el.offset()[horizontal ? 'left' : 'top']; + // Subtract scroll distance from placement to get the distance + // to the edge of the viewport. + placement -= + window[`scroll${horizontal ? 'X' : 'Y'}`] || + document.documentElement[`scroll${horizontal ? 'Left' : 'Top'}`] || + 0; + // Find the displacement value according to the edge. + switch (edge) { + // Left and top elements displace as a sum of their own offset value + // plus their size. + case 'top': + // Total displacement is the sum of the elements placement and size. + displacement = placement + $el.outerHeight(); + break; - /** - * Determines the viewport offsets. - * - * @return {Drupal~displaceOffset} - * An object whose keys are the for sides an element -- top, right, bottom - * and left. The value of each key is the viewport displacement distance for - * that edge. - */ - function calculateOffsets() { - return { - top: calculateOffset('top'), - right: calculateOffset('right'), - bottom: calculateOffset('bottom'), - left: calculateOffset('left'), - }; + case 'left': + // Total displacement is the sum of the elements placement and size. + displacement = placement + $el.outerWidth(); + break; + + // Right and bottom elements displace according to their left and + // top offset. Their size isn't important. + case 'bottom': + displacement = documentElement.clientHeight - placement; + break; + + case 'right': + displacement = documentElement.clientWidth - placement; + break; + + default: + displacement = 0; + } + return displacement; } /** @@ -117,7 +112,9 @@ */ function calculateOffset(edge) { let edgeOffset = 0; - const displacingElements = document.querySelectorAll(`[data-offset-${edge}]`); + const displacingElements = document.querySelectorAll( + `[data-offset-${edge}]`, + ); const n = displacingElements.length; for (let i = 0; i < n; i++) { const el = displacingElements[i]; @@ -130,6 +127,7 @@ // If the element's offset data attribute exits // but is not a valid number then get the displacement // dimensions directly from the element. + // eslint-disable-next-line no-restricted-globals if (isNaN(displacement)) { displacement = getRawOffset(el, edge); } @@ -142,58 +140,66 @@ } /** - * Calculates displacement for element based on its dimensions and placement. + * Determines the viewport offsets. * - * @param {HTMLElement} el - * The jQuery element whose dimensions and placement will be measured. + * @return {Drupal~displaceOffset} + * An object whose keys are the for sides an element -- top, right, bottom + * and left. The value of each key is the viewport displacement distance for + * that edge. + */ + function calculateOffsets() { + return { + top: calculateOffset('top'), + right: calculateOffset('right'), + bottom: calculateOffset('bottom'), + left: calculateOffset('left'), + }; + } + + /** + * Informs listeners of the current offset dimensions. * - * @param {string} edge - * The name of the edge of the viewport that the element is associated - * with. + * @function Drupal.displace * - * @return {number} - * The viewport displacement distance for the requested edge. + * @prop {Drupal~displaceOffset} offsets + * + * @param {bool} [broadcast] + * When true or undefined, causes the recalculated offsets values to be + * broadcast to listeners. + * + * @return {Drupal~displaceOffset} + * An object whose keys are the for sides an element -- top, right, bottom + * and left. The value of each key is the viewport displacement distance for + * that edge. + * + * @fires event:drupalViewportOffsetChange */ - function getRawOffset(el, edge) { - const $el = $(el); - const documentElement = document.documentElement; - let displacement = 0; - const horizontal = (edge === 'left' || edge === 'right'); - // Get the offset of the element itself. - let placement = $el.offset()[horizontal ? 'left' : 'top']; - // Subtract scroll distance from placement to get the distance - // to the edge of the viewport. - placement -= window[`scroll${horizontal ? 'X' : 'Y'}`] || document.documentElement[`scroll${horizontal ? 'Left' : 'Top'}`] || 0; - // Find the displacement value according to the edge. - switch (edge) { - // Left and top elements displace as a sum of their own offset value - // plus their size. - case 'top': - // Total displacement is the sum of the elements placement and size. - displacement = placement + $el.outerHeight(); - break; - - case 'left': - // Total displacement is the sum of the elements placement and size. - displacement = placement + $el.outerWidth(); - break; - - // Right and bottom elements displace according to their left and - // top offset. Their size isn't important. - case 'bottom': - displacement = documentElement.clientHeight - placement; - break; - - case 'right': - displacement = documentElement.clientWidth - placement; - break; - - default: - displacement = 0; + function displace(broadcast) { + offsets = calculateOffsets(); + Drupal.displace.offsets = offsets; + if (typeof broadcast === 'undefined' || broadcast) { + $(document).trigger('drupalViewportOffsetChange', offsets); } - return displacement; + return offsets; } + /** + * Registers a resize handler on the window. + * + * @type {Drupal~behavior} + */ + Drupal.behaviors.drupalDisplace = { + attach() { + // Mark this behavior as processed on the first pass. + if (this.displaceProcessed) { + return; + } + this.displaceProcessed = true; + + $(window).on('resize.drupalDisplace', debounce(displace, 200)); + }, + }; + /** * Assign the displace function to a property of the Drupal global object. * @@ -201,7 +207,6 @@ */ Drupal.displace = displace; $.extend(Drupal.displace, { - /** * Expose offsets to other scripts to avoid having to recalculate offsets. * @@ -216,4 +221,4 @@ */ calculateOffset, }); -}(jQuery, Drupal, Drupal.debounce)); +})(jQuery, Drupal, Drupal.debounce);