07a86f56783763e4e597b8938ba7ca0bc7a45fe2a32f879c1bfe80e54a21bb69a8e869fc76eb3e60b4c1459b83c03a48765c0574a8766f0f6e17256454a8c4 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import {createRectInit, getContentRect} from './utils/geometry.js';
  2. /**
  3. * Class that is responsible for computations of the content rectangle of
  4. * provided DOM element and for keeping track of it's changes.
  5. */
  6. export default class ResizeObservation {
  7. /**
  8. * Reference to the observed element.
  9. *
  10. * @type {Element}
  11. */
  12. target;
  13. /**
  14. * Broadcasted width of content rectangle.
  15. *
  16. * @type {number}
  17. */
  18. broadcastWidth = 0;
  19. /**
  20. * Broadcasted height of content rectangle.
  21. *
  22. * @type {number}
  23. */
  24. broadcastHeight = 0;
  25. /**
  26. * Reference to the last observed content rectangle.
  27. *
  28. * @private {DOMRectInit}
  29. */
  30. contentRect_ = createRectInit(0, 0, 0, 0);
  31. /**
  32. * Creates an instance of ResizeObservation.
  33. *
  34. * @param {Element} target - Element to be observed.
  35. */
  36. constructor(target) {
  37. this.target = target;
  38. }
  39. /**
  40. * Updates content rectangle and tells whether it's width or height properties
  41. * have changed since the last broadcast.
  42. *
  43. * @returns {boolean}
  44. */
  45. isActive() {
  46. const rect = getContentRect(this.target);
  47. this.contentRect_ = rect;
  48. return (
  49. rect.width !== this.broadcastWidth ||
  50. rect.height !== this.broadcastHeight
  51. );
  52. }
  53. /**
  54. * Updates 'broadcastWidth' and 'broadcastHeight' properties with a data
  55. * from the corresponding properties of the last observed content rectangle.
  56. *
  57. * @returns {DOMRectInit} Last observed content rectangle.
  58. */
  59. broadcastRect() {
  60. const rect = this.contentRect_;
  61. this.broadcastWidth = rect.width;
  62. this.broadcastHeight = rect.height;
  63. return rect;
  64. }
  65. }