[d565449] | 1 | import {Map} from './shims/es6-collections.js';
|
---|
| 2 | import ResizeObservation from './ResizeObservation.js';
|
---|
| 3 | import ResizeObserverEntry from './ResizeObserverEntry.js';
|
---|
| 4 | import getWindowOf from './utils/getWindowOf.js';
|
---|
| 5 |
|
---|
| 6 | export default class ResizeObserverSPI {
|
---|
| 7 | /**
|
---|
| 8 | * Collection of resize observations that have detected changes in dimensions
|
---|
| 9 | * of elements.
|
---|
| 10 | *
|
---|
| 11 | * @private {Array<ResizeObservation>}
|
---|
| 12 | */
|
---|
| 13 | activeObservations_ = [];
|
---|
| 14 |
|
---|
| 15 | /**
|
---|
| 16 | * Reference to the callback function.
|
---|
| 17 | *
|
---|
| 18 | * @private {ResizeObserverCallback}
|
---|
| 19 | */
|
---|
| 20 | callback_;
|
---|
| 21 |
|
---|
| 22 | /**
|
---|
| 23 | * Public ResizeObserver instance which will be passed to the callback
|
---|
| 24 | * function and used as a value of it's "this" binding.
|
---|
| 25 | *
|
---|
| 26 | * @private {ResizeObserver}
|
---|
| 27 | */
|
---|
| 28 | callbackCtx_;
|
---|
| 29 |
|
---|
| 30 | /**
|
---|
| 31 | * Reference to the associated ResizeObserverController.
|
---|
| 32 | *
|
---|
| 33 | * @private {ResizeObserverController}
|
---|
| 34 | */
|
---|
| 35 | controller_;
|
---|
| 36 |
|
---|
| 37 | /**
|
---|
| 38 | * Registry of the ResizeObservation instances.
|
---|
| 39 | *
|
---|
| 40 | * @private {Map<Element, ResizeObservation>}
|
---|
| 41 | */
|
---|
| 42 | observations_ = new Map();
|
---|
| 43 |
|
---|
| 44 | /**
|
---|
| 45 | * Creates a new instance of ResizeObserver.
|
---|
| 46 | *
|
---|
| 47 | * @param {ResizeObserverCallback} callback - Callback function that is invoked
|
---|
| 48 | * when one of the observed elements changes it's content dimensions.
|
---|
| 49 | * @param {ResizeObserverController} controller - Controller instance which
|
---|
| 50 | * is responsible for the updates of observer.
|
---|
| 51 | * @param {ResizeObserver} callbackCtx - Reference to the public
|
---|
| 52 | * ResizeObserver instance which will be passed to callback function.
|
---|
| 53 | */
|
---|
| 54 | constructor(callback, controller, callbackCtx) {
|
---|
| 55 | if (typeof callback !== 'function') {
|
---|
| 56 | throw new TypeError('The callback provided as parameter 1 is not a function.');
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | this.callback_ = callback;
|
---|
| 60 | this.controller_ = controller;
|
---|
| 61 | this.callbackCtx_ = callbackCtx;
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | /**
|
---|
| 65 | * Starts observing provided element.
|
---|
| 66 | *
|
---|
| 67 | * @param {Element} target - Element to be observed.
|
---|
| 68 | * @returns {void}
|
---|
| 69 | */
|
---|
| 70 | observe(target) {
|
---|
| 71 | if (!arguments.length) {
|
---|
| 72 | throw new TypeError('1 argument required, but only 0 present.');
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | // Do nothing if current environment doesn't have the Element interface.
|
---|
| 76 | if (typeof Element === 'undefined' || !(Element instanceof Object)) {
|
---|
| 77 | return;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | if (!(target instanceof getWindowOf(target).Element)) {
|
---|
| 81 | throw new TypeError('parameter 1 is not of type "Element".');
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | const observations = this.observations_;
|
---|
| 85 |
|
---|
| 86 | // Do nothing if element is already being observed.
|
---|
| 87 | if (observations.has(target)) {
|
---|
| 88 | return;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | observations.set(target, new ResizeObservation(target));
|
---|
| 92 |
|
---|
| 93 | this.controller_.addObserver(this);
|
---|
| 94 |
|
---|
| 95 | // Force the update of observations.
|
---|
| 96 | this.controller_.refresh();
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | /**
|
---|
| 100 | * Stops observing provided element.
|
---|
| 101 | *
|
---|
| 102 | * @param {Element} target - Element to stop observing.
|
---|
| 103 | * @returns {void}
|
---|
| 104 | */
|
---|
| 105 | unobserve(target) {
|
---|
| 106 | if (!arguments.length) {
|
---|
| 107 | throw new TypeError('1 argument required, but only 0 present.');
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | // Do nothing if current environment doesn't have the Element interface.
|
---|
| 111 | if (typeof Element === 'undefined' || !(Element instanceof Object)) {
|
---|
| 112 | return;
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | if (!(target instanceof getWindowOf(target).Element)) {
|
---|
| 116 | throw new TypeError('parameter 1 is not of type "Element".');
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | const observations = this.observations_;
|
---|
| 120 |
|
---|
| 121 | // Do nothing if element is not being observed.
|
---|
| 122 | if (!observations.has(target)) {
|
---|
| 123 | return;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | observations.delete(target);
|
---|
| 127 |
|
---|
| 128 | if (!observations.size) {
|
---|
| 129 | this.controller_.removeObserver(this);
|
---|
| 130 | }
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | /**
|
---|
| 134 | * Stops observing all elements.
|
---|
| 135 | *
|
---|
| 136 | * @returns {void}
|
---|
| 137 | */
|
---|
| 138 | disconnect() {
|
---|
| 139 | this.clearActive();
|
---|
| 140 | this.observations_.clear();
|
---|
| 141 | this.controller_.removeObserver(this);
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | /**
|
---|
| 145 | * Collects observation instances the associated element of which has changed
|
---|
| 146 | * it's content rectangle.
|
---|
| 147 | *
|
---|
| 148 | * @returns {void}
|
---|
| 149 | */
|
---|
| 150 | gatherActive() {
|
---|
| 151 | this.clearActive();
|
---|
| 152 |
|
---|
| 153 | this.observations_.forEach(observation => {
|
---|
| 154 | if (observation.isActive()) {
|
---|
| 155 | this.activeObservations_.push(observation);
|
---|
| 156 | }
|
---|
| 157 | });
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | /**
|
---|
| 161 | * Invokes initial callback function with a list of ResizeObserverEntry
|
---|
| 162 | * instances collected from active resize observations.
|
---|
| 163 | *
|
---|
| 164 | * @returns {void}
|
---|
| 165 | */
|
---|
| 166 | broadcastActive() {
|
---|
| 167 | // Do nothing if observer doesn't have active observations.
|
---|
| 168 | if (!this.hasActive()) {
|
---|
| 169 | return;
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | const ctx = this.callbackCtx_;
|
---|
| 173 |
|
---|
| 174 | // Create ResizeObserverEntry instance for every active observation.
|
---|
| 175 | const entries = this.activeObservations_.map(observation => {
|
---|
| 176 | return new ResizeObserverEntry(
|
---|
| 177 | observation.target,
|
---|
| 178 | observation.broadcastRect()
|
---|
| 179 | );
|
---|
| 180 | });
|
---|
| 181 |
|
---|
| 182 | this.callback_.call(ctx, entries, ctx);
|
---|
| 183 | this.clearActive();
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | /**
|
---|
| 187 | * Clears the collection of active observations.
|
---|
| 188 | *
|
---|
| 189 | * @returns {void}
|
---|
| 190 | */
|
---|
| 191 | clearActive() {
|
---|
| 192 | this.activeObservations_.splice(0);
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 | /**
|
---|
| 196 | * Tells whether observer has active observations.
|
---|
| 197 | *
|
---|
| 198 | * @returns {boolean}
|
---|
| 199 | */
|
---|
| 200 | hasActive() {
|
---|
| 201 | return this.activeObservations_.length > 0;
|
---|
| 202 | }
|
---|
| 203 | }
|
---|