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. Drupal.behaviors.drupalDisplace = {
  15. attach: function attach() {
  16. if (this.displaceProcessed) {
  17. return;
  18. }
  19. this.displaceProcessed = true;
  20. $(window).on('resize.drupalDisplace', debounce(displace, 200));
  21. }
  22. };
  23. function displace(broadcast) {
  24. offsets = calculateOffsets();
  25. Drupal.displace.offsets = offsets;
  26. if (typeof broadcast === 'undefined' || broadcast) {
  27. $(document).trigger('drupalViewportOffsetChange', offsets);
  28. }
  29. return offsets;
  30. }
  31. function calculateOffsets() {
  32. return {
  33. top: calculateOffset('top'),
  34. right: calculateOffset('right'),
  35. bottom: calculateOffset('bottom'),
  36. left: calculateOffset('left')
  37. };
  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 getRawOffset(el, edge) {
  57. var $el = $(el);
  58. var documentElement = document.documentElement;
  59. var displacement = 0;
  60. var horizontal = edge === 'left' || edge === 'right';
  61. var placement = $el.offset()[horizontal ? 'left' : 'top'];
  62. placement -= window['scroll' + (horizontal ? 'X' : 'Y')] || document.documentElement['scroll' + (horizontal ? 'Left' : 'Top')] || 0;
  63. switch (edge) {
  64. case 'top':
  65. displacement = placement + $el.outerHeight();
  66. break;
  67. case 'left':
  68. displacement = placement + $el.outerWidth();
  69. break;
  70. case 'bottom':
  71. displacement = documentElement.clientHeight - placement;
  72. break;
  73. case 'right':
  74. displacement = documentElement.clientWidth - placement;
  75. break;
  76. default:
  77. displacement = 0;
  78. }
  79. return displacement;
  80. }
  81. Drupal.displace = displace;
  82. $.extend(Drupal.displace, {
  83. offsets: offsets,
  84. calculateOffset: calculateOffset
  85. });
  86. })(jQuery, Drupal, Drupal.debounce);