displace.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 = Drupal.displace.offsets = calculateOffsets();
  25. if (typeof broadcast === 'undefined' || broadcast) {
  26. $(document).trigger('drupalViewportOffsetChange', offsets);
  27. }
  28. return offsets;
  29. }
  30. function calculateOffsets() {
  31. return {
  32. top: calculateOffset('top'),
  33. right: calculateOffset('right'),
  34. bottom: calculateOffset('bottom'),
  35. left: calculateOffset('left')
  36. };
  37. }
  38. function calculateOffset(edge) {
  39. var edgeOffset = 0;
  40. var displacingElements = document.querySelectorAll('[data-offset-' + edge + ']');
  41. var n = displacingElements.length;
  42. for (var i = 0; i < n; i++) {
  43. var el = displacingElements[i];
  44. if (el.style.display === 'none') {
  45. continue;
  46. }
  47. var displacement = parseInt(el.getAttribute('data-offset-' + edge), 10);
  48. if (isNaN(displacement)) {
  49. displacement = getRawOffset(el, edge);
  50. }
  51. edgeOffset = Math.max(edgeOffset, displacement);
  52. }
  53. return edgeOffset;
  54. }
  55. function getRawOffset(el, edge) {
  56. var $el = $(el);
  57. var documentElement = document.documentElement;
  58. var displacement = 0;
  59. var horizontal = edge === 'left' || edge === 'right';
  60. var placement = $el.offset()[horizontal ? 'left' : 'top'];
  61. placement -= window['scroll' + (horizontal ? 'X' : 'Y')] || document.documentElement['scroll' + (horizontal ? 'Left' : 'Top')] || 0;
  62. switch (edge) {
  63. case 'top':
  64. displacement = placement + $el.outerHeight();
  65. break;
  66. case 'left':
  67. displacement = placement + $el.outerWidth();
  68. break;
  69. case 'bottom':
  70. displacement = documentElement.clientHeight - placement;
  71. break;
  72. case 'right':
  73. displacement = documentElement.clientWidth - placement;
  74. break;
  75. default:
  76. displacement = 0;
  77. }
  78. return displacement;
  79. }
  80. Drupal.displace = displace;
  81. $.extend(Drupal.displace, {
  82. offsets: offsets,
  83. calculateOffset: calculateOffset
  84. });
  85. })(jQuery, Drupal, Drupal.debounce);