displace.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /**
  2. * DO NOT EDIT THIS FILE.
  3. * See the following change record for more information,
  4. * https://www.drupal.org/node/2815083
  5. * @preserve
  6. **/
  7. (function ($, Drupal, debounce) {
  8. var offsets = {
  9. top: 0,
  10. right: 0,
  11. bottom: 0,
  12. left: 0
  13. };
  14. function getRawOffset(el, edge) {
  15. var $el = $(el);
  16. var documentElement = document.documentElement;
  17. var displacement = 0;
  18. var horizontal = edge === 'left' || edge === 'right';
  19. var placement = $el.offset()[horizontal ? 'left' : 'top'];
  20. placement -= window['scroll' + (horizontal ? 'X' : 'Y')] || document.documentElement['scroll' + (horizontal ? 'Left' : 'Top')] || 0;
  21. switch (edge) {
  22. case 'top':
  23. displacement = placement + $el.outerHeight();
  24. break;
  25. case 'left':
  26. displacement = placement + $el.outerWidth();
  27. break;
  28. case 'bottom':
  29. displacement = documentElement.clientHeight - placement;
  30. break;
  31. case 'right':
  32. displacement = documentElement.clientWidth - placement;
  33. break;
  34. default:
  35. displacement = 0;
  36. }
  37. return displacement;
  38. }
  39. function calculateOffset(edge) {
  40. var edgeOffset = 0;
  41. var displacingElements = document.querySelectorAll('[data-offset-' + edge + ']');
  42. var n = displacingElements.length;
  43. for (var i = 0; i < n; i++) {
  44. var el = displacingElements[i];
  45. if (el.style.display === 'none') {
  46. continue;
  47. }
  48. var displacement = parseInt(el.getAttribute('data-offset-' + edge), 10);
  49. if (isNaN(displacement)) {
  50. displacement = getRawOffset(el, edge);
  51. }
  52. edgeOffset = Math.max(edgeOffset, displacement);
  53. }
  54. return edgeOffset;
  55. }
  56. function calculateOffsets() {
  57. return {
  58. top: calculateOffset('top'),
  59. right: calculateOffset('right'),
  60. bottom: calculateOffset('bottom'),
  61. left: calculateOffset('left')
  62. };
  63. }
  64. function displace(broadcast) {
  65. offsets = calculateOffsets();
  66. Drupal.displace.offsets = offsets;
  67. if (typeof broadcast === 'undefined' || broadcast) {
  68. $(document).trigger('drupalViewportOffsetChange', offsets);
  69. }
  70. return offsets;
  71. }
  72. Drupal.behaviors.drupalDisplace = {
  73. attach: function attach() {
  74. if (this.displaceProcessed) {
  75. return;
  76. }
  77. this.displaceProcessed = true;
  78. $(window).on('resize.drupalDisplace', debounce(displace, 200));
  79. }
  80. };
  81. Drupal.displace = displace;
  82. $.extend(Drupal.displace, {
  83. offsets: offsets,
  84. calculateOffset: calculateOffset
  85. });
  86. })(jQuery, Drupal, Drupal.debounce);