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