utils.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import addEventListener from '../vc-util/Dom/addEventListener';
  2. import supportsPassive from '../_util/supportsPassive';
  3. export function getTargetRect(target) {
  4. return target !== window ? target.getBoundingClientRect() : {
  5. top: 0,
  6. bottom: window.innerHeight
  7. };
  8. }
  9. export function getFixedTop(placeholderReact, targetRect, offsetTop) {
  10. if (offsetTop !== undefined && targetRect.top > placeholderReact.top - offsetTop) {
  11. return "".concat(offsetTop + targetRect.top, "px");
  12. }
  13. return undefined;
  14. }
  15. export function getFixedBottom(placeholderReact, targetRect, offsetBottom) {
  16. if (offsetBottom !== undefined && targetRect.bottom < placeholderReact.bottom + offsetBottom) {
  17. var targetBottomOffset = window.innerHeight - targetRect.bottom;
  18. return "".concat(offsetBottom + targetBottomOffset, "px");
  19. }
  20. return undefined;
  21. }
  22. // ======================== Observer ========================
  23. var TRIGGER_EVENTS = ['resize', 'scroll', 'touchstart', 'touchmove', 'touchend', 'pageshow', 'load'];
  24. var observerEntities = [];
  25. export function getObserverEntities() {
  26. // Only used in test env. Can be removed if refactor.
  27. return observerEntities;
  28. }
  29. export function addObserveTarget(target, affix) {
  30. if (!target) return;
  31. var entity = observerEntities.find(function (item) {
  32. return item.target === target;
  33. });
  34. if (entity) {
  35. entity.affixList.push(affix);
  36. } else {
  37. entity = {
  38. target: target,
  39. affixList: [affix],
  40. eventHandlers: {}
  41. };
  42. observerEntities.push(entity);
  43. // Add listener
  44. TRIGGER_EVENTS.forEach(function (eventName) {
  45. entity.eventHandlers[eventName] = addEventListener(target, eventName, function () {
  46. entity.affixList.forEach(function (targetAffix) {
  47. var lazyUpdatePosition = targetAffix.exposed.lazyUpdatePosition;
  48. lazyUpdatePosition();
  49. }, (eventName === 'touchstart' || eventName === 'touchmove') && supportsPassive ? {
  50. passive: true
  51. } : false);
  52. });
  53. });
  54. }
  55. }
  56. export function removeObserveTarget(affix) {
  57. var observerEntity = observerEntities.find(function (oriObserverEntity) {
  58. var hasAffix = oriObserverEntity.affixList.some(function (item) {
  59. return item === affix;
  60. });
  61. if (hasAffix) {
  62. oriObserverEntity.affixList = oriObserverEntity.affixList.filter(function (item) {
  63. return item !== affix;
  64. });
  65. }
  66. return hasAffix;
  67. });
  68. if (observerEntity && observerEntity.affixList.length === 0) {
  69. observerEntities = observerEntities.filter(function (item) {
  70. return item !== observerEntity;
  71. });
  72. // Remove listener
  73. TRIGGER_EVENTS.forEach(function (eventName) {
  74. var handler = observerEntity.eventHandlers[eventName];
  75. if (handler && handler.remove) {
  76. handler.remove();
  77. }
  78. });
  79. }
  80. }