[d565449] | 1 | /**
|
---|
| 2 | * A collection of shims that provide minimal functionality of the ES6 collections.
|
---|
| 3 | *
|
---|
| 4 | * These implementations are not meant to be used outside of the ResizeObserver
|
---|
| 5 | * modules as they cover only a limited range of use cases.
|
---|
| 6 | */
|
---|
| 7 | /* eslint-disable require-jsdoc, valid-jsdoc */
|
---|
| 8 | var MapShim = (function () {
|
---|
| 9 | if (typeof Map !== 'undefined') {
|
---|
| 10 | return Map;
|
---|
| 11 | }
|
---|
| 12 | /**
|
---|
| 13 | * Returns index in provided array that matches the specified key.
|
---|
| 14 | *
|
---|
| 15 | * @param {Array<Array>} arr
|
---|
| 16 | * @param {*} key
|
---|
| 17 | * @returns {number}
|
---|
| 18 | */
|
---|
| 19 | function getIndex(arr, key) {
|
---|
| 20 | var result = -1;
|
---|
| 21 | arr.some(function (entry, index) {
|
---|
| 22 | if (entry[0] === key) {
|
---|
| 23 | result = index;
|
---|
| 24 | return true;
|
---|
| 25 | }
|
---|
| 26 | return false;
|
---|
| 27 | });
|
---|
| 28 | return result;
|
---|
| 29 | }
|
---|
| 30 | return /** @class */ (function () {
|
---|
| 31 | function class_1() {
|
---|
| 32 | this.__entries__ = [];
|
---|
| 33 | }
|
---|
| 34 | Object.defineProperty(class_1.prototype, "size", {
|
---|
| 35 | /**
|
---|
| 36 | * @returns {boolean}
|
---|
| 37 | */
|
---|
| 38 | get: function () {
|
---|
| 39 | return this.__entries__.length;
|
---|
| 40 | },
|
---|
| 41 | enumerable: true,
|
---|
| 42 | configurable: true
|
---|
| 43 | });
|
---|
| 44 | /**
|
---|
| 45 | * @param {*} key
|
---|
| 46 | * @returns {*}
|
---|
| 47 | */
|
---|
| 48 | class_1.prototype.get = function (key) {
|
---|
| 49 | var index = getIndex(this.__entries__, key);
|
---|
| 50 | var entry = this.__entries__[index];
|
---|
| 51 | return entry && entry[1];
|
---|
| 52 | };
|
---|
| 53 | /**
|
---|
| 54 | * @param {*} key
|
---|
| 55 | * @param {*} value
|
---|
| 56 | * @returns {void}
|
---|
| 57 | */
|
---|
| 58 | class_1.prototype.set = function (key, value) {
|
---|
| 59 | var index = getIndex(this.__entries__, key);
|
---|
| 60 | if (~index) {
|
---|
| 61 | this.__entries__[index][1] = value;
|
---|
| 62 | }
|
---|
| 63 | else {
|
---|
| 64 | this.__entries__.push([key, value]);
|
---|
| 65 | }
|
---|
| 66 | };
|
---|
| 67 | /**
|
---|
| 68 | * @param {*} key
|
---|
| 69 | * @returns {void}
|
---|
| 70 | */
|
---|
| 71 | class_1.prototype.delete = function (key) {
|
---|
| 72 | var entries = this.__entries__;
|
---|
| 73 | var index = getIndex(entries, key);
|
---|
| 74 | if (~index) {
|
---|
| 75 | entries.splice(index, 1);
|
---|
| 76 | }
|
---|
| 77 | };
|
---|
| 78 | /**
|
---|
| 79 | * @param {*} key
|
---|
| 80 | * @returns {void}
|
---|
| 81 | */
|
---|
| 82 | class_1.prototype.has = function (key) {
|
---|
| 83 | return !!~getIndex(this.__entries__, key);
|
---|
| 84 | };
|
---|
| 85 | /**
|
---|
| 86 | * @returns {void}
|
---|
| 87 | */
|
---|
| 88 | class_1.prototype.clear = function () {
|
---|
| 89 | this.__entries__.splice(0);
|
---|
| 90 | };
|
---|
| 91 | /**
|
---|
| 92 | * @param {Function} callback
|
---|
| 93 | * @param {*} [ctx=null]
|
---|
| 94 | * @returns {void}
|
---|
| 95 | */
|
---|
| 96 | class_1.prototype.forEach = function (callback, ctx) {
|
---|
| 97 | if (ctx === void 0) { ctx = null; }
|
---|
| 98 | for (var _i = 0, _a = this.__entries__; _i < _a.length; _i++) {
|
---|
| 99 | var entry = _a[_i];
|
---|
| 100 | callback.call(ctx, entry[1], entry[0]);
|
---|
| 101 | }
|
---|
| 102 | };
|
---|
| 103 | return class_1;
|
---|
| 104 | }());
|
---|
| 105 | })();
|
---|
| 106 |
|
---|
| 107 | /**
|
---|
| 108 | * Detects whether window and document objects are available in current environment.
|
---|
| 109 | */
|
---|
| 110 | var isBrowser = typeof window !== 'undefined' && typeof document !== 'undefined' && window.document === document;
|
---|
| 111 |
|
---|
| 112 | // Returns global object of a current environment.
|
---|
| 113 | var global$1 = (function () {
|
---|
| 114 | if (typeof global !== 'undefined' && global.Math === Math) {
|
---|
| 115 | return global;
|
---|
| 116 | }
|
---|
| 117 | if (typeof self !== 'undefined' && self.Math === Math) {
|
---|
| 118 | return self;
|
---|
| 119 | }
|
---|
| 120 | if (typeof window !== 'undefined' && window.Math === Math) {
|
---|
| 121 | return window;
|
---|
| 122 | }
|
---|
| 123 | // eslint-disable-next-line no-new-func
|
---|
| 124 | return Function('return this')();
|
---|
| 125 | })();
|
---|
| 126 |
|
---|
| 127 | /**
|
---|
| 128 | * A shim for the requestAnimationFrame which falls back to the setTimeout if
|
---|
| 129 | * first one is not supported.
|
---|
| 130 | *
|
---|
| 131 | * @returns {number} Requests' identifier.
|
---|
| 132 | */
|
---|
| 133 | var requestAnimationFrame$1 = (function () {
|
---|
| 134 | if (typeof requestAnimationFrame === 'function') {
|
---|
| 135 | // It's required to use a bounded function because IE sometimes throws
|
---|
| 136 | // an "Invalid calling object" error if rAF is invoked without the global
|
---|
| 137 | // object on the left hand side.
|
---|
| 138 | return requestAnimationFrame.bind(global$1);
|
---|
| 139 | }
|
---|
| 140 | return function (callback) { return setTimeout(function () { return callback(Date.now()); }, 1000 / 60); };
|
---|
| 141 | })();
|
---|
| 142 |
|
---|
| 143 | // Defines minimum timeout before adding a trailing call.
|
---|
| 144 | var trailingTimeout = 2;
|
---|
| 145 | /**
|
---|
| 146 | * Creates a wrapper function which ensures that provided callback will be
|
---|
| 147 | * invoked only once during the specified delay period.
|
---|
| 148 | *
|
---|
| 149 | * @param {Function} callback - Function to be invoked after the delay period.
|
---|
| 150 | * @param {number} delay - Delay after which to invoke callback.
|
---|
| 151 | * @returns {Function}
|
---|
| 152 | */
|
---|
| 153 | function throttle (callback, delay) {
|
---|
| 154 | var leadingCall = false, trailingCall = false, lastCallTime = 0;
|
---|
| 155 | /**
|
---|
| 156 | * Invokes the original callback function and schedules new invocation if
|
---|
| 157 | * the "proxy" was called during current request.
|
---|
| 158 | *
|
---|
| 159 | * @returns {void}
|
---|
| 160 | */
|
---|
| 161 | function resolvePending() {
|
---|
| 162 | if (leadingCall) {
|
---|
| 163 | leadingCall = false;
|
---|
| 164 | callback();
|
---|
| 165 | }
|
---|
| 166 | if (trailingCall) {
|
---|
| 167 | proxy();
|
---|
| 168 | }
|
---|
| 169 | }
|
---|
| 170 | /**
|
---|
| 171 | * Callback invoked after the specified delay. It will further postpone
|
---|
| 172 | * invocation of the original function delegating it to the
|
---|
| 173 | * requestAnimationFrame.
|
---|
| 174 | *
|
---|
| 175 | * @returns {void}
|
---|
| 176 | */
|
---|
| 177 | function timeoutCallback() {
|
---|
| 178 | requestAnimationFrame$1(resolvePending);
|
---|
| 179 | }
|
---|
| 180 | /**
|
---|
| 181 | * Schedules invocation of the original function.
|
---|
| 182 | *
|
---|
| 183 | * @returns {void}
|
---|
| 184 | */
|
---|
| 185 | function proxy() {
|
---|
| 186 | var timeStamp = Date.now();
|
---|
| 187 | if (leadingCall) {
|
---|
| 188 | // Reject immediately following calls.
|
---|
| 189 | if (timeStamp - lastCallTime < trailingTimeout) {
|
---|
| 190 | return;
|
---|
| 191 | }
|
---|
| 192 | // Schedule new call to be in invoked when the pending one is resolved.
|
---|
| 193 | // This is important for "transitions" which never actually start
|
---|
| 194 | // immediately so there is a chance that we might miss one if change
|
---|
| 195 | // happens amids the pending invocation.
|
---|
| 196 | trailingCall = true;
|
---|
| 197 | }
|
---|
| 198 | else {
|
---|
| 199 | leadingCall = true;
|
---|
| 200 | trailingCall = false;
|
---|
| 201 | setTimeout(timeoutCallback, delay);
|
---|
| 202 | }
|
---|
| 203 | lastCallTime = timeStamp;
|
---|
| 204 | }
|
---|
| 205 | return proxy;
|
---|
| 206 | }
|
---|
| 207 |
|
---|
| 208 | // Minimum delay before invoking the update of observers.
|
---|
| 209 | var REFRESH_DELAY = 20;
|
---|
| 210 | // A list of substrings of CSS properties used to find transition events that
|
---|
| 211 | // might affect dimensions of observed elements.
|
---|
| 212 | var transitionKeys = ['top', 'right', 'bottom', 'left', 'width', 'height', 'size', 'weight'];
|
---|
| 213 | // Check if MutationObserver is available.
|
---|
| 214 | var mutationObserverSupported = typeof MutationObserver !== 'undefined';
|
---|
| 215 | /**
|
---|
| 216 | * Singleton controller class which handles updates of ResizeObserver instances.
|
---|
| 217 | */
|
---|
| 218 | var ResizeObserverController = /** @class */ (function () {
|
---|
| 219 | /**
|
---|
| 220 | * Creates a new instance of ResizeObserverController.
|
---|
| 221 | *
|
---|
| 222 | * @private
|
---|
| 223 | */
|
---|
| 224 | function ResizeObserverController() {
|
---|
| 225 | /**
|
---|
| 226 | * Indicates whether DOM listeners have been added.
|
---|
| 227 | *
|
---|
| 228 | * @private {boolean}
|
---|
| 229 | */
|
---|
| 230 | this.connected_ = false;
|
---|
| 231 | /**
|
---|
| 232 | * Tells that controller has subscribed for Mutation Events.
|
---|
| 233 | *
|
---|
| 234 | * @private {boolean}
|
---|
| 235 | */
|
---|
| 236 | this.mutationEventsAdded_ = false;
|
---|
| 237 | /**
|
---|
| 238 | * Keeps reference to the instance of MutationObserver.
|
---|
| 239 | *
|
---|
| 240 | * @private {MutationObserver}
|
---|
| 241 | */
|
---|
| 242 | this.mutationsObserver_ = null;
|
---|
| 243 | /**
|
---|
| 244 | * A list of connected observers.
|
---|
| 245 | *
|
---|
| 246 | * @private {Array<ResizeObserverSPI>}
|
---|
| 247 | */
|
---|
| 248 | this.observers_ = [];
|
---|
| 249 | this.onTransitionEnd_ = this.onTransitionEnd_.bind(this);
|
---|
| 250 | this.refresh = throttle(this.refresh.bind(this), REFRESH_DELAY);
|
---|
| 251 | }
|
---|
| 252 | /**
|
---|
| 253 | * Adds observer to observers list.
|
---|
| 254 | *
|
---|
| 255 | * @param {ResizeObserverSPI} observer - Observer to be added.
|
---|
| 256 | * @returns {void}
|
---|
| 257 | */
|
---|
| 258 | ResizeObserverController.prototype.addObserver = function (observer) {
|
---|
| 259 | if (!~this.observers_.indexOf(observer)) {
|
---|
| 260 | this.observers_.push(observer);
|
---|
| 261 | }
|
---|
| 262 | // Add listeners if they haven't been added yet.
|
---|
| 263 | if (!this.connected_) {
|
---|
| 264 | this.connect_();
|
---|
| 265 | }
|
---|
| 266 | };
|
---|
| 267 | /**
|
---|
| 268 | * Removes observer from observers list.
|
---|
| 269 | *
|
---|
| 270 | * @param {ResizeObserverSPI} observer - Observer to be removed.
|
---|
| 271 | * @returns {void}
|
---|
| 272 | */
|
---|
| 273 | ResizeObserverController.prototype.removeObserver = function (observer) {
|
---|
| 274 | var observers = this.observers_;
|
---|
| 275 | var index = observers.indexOf(observer);
|
---|
| 276 | // Remove observer if it's present in registry.
|
---|
| 277 | if (~index) {
|
---|
| 278 | observers.splice(index, 1);
|
---|
| 279 | }
|
---|
| 280 | // Remove listeners if controller has no connected observers.
|
---|
| 281 | if (!observers.length && this.connected_) {
|
---|
| 282 | this.disconnect_();
|
---|
| 283 | }
|
---|
| 284 | };
|
---|
| 285 | /**
|
---|
| 286 | * Invokes the update of observers. It will continue running updates insofar
|
---|
| 287 | * it detects changes.
|
---|
| 288 | *
|
---|
| 289 | * @returns {void}
|
---|
| 290 | */
|
---|
| 291 | ResizeObserverController.prototype.refresh = function () {
|
---|
| 292 | var changesDetected = this.updateObservers_();
|
---|
| 293 | // Continue running updates if changes have been detected as there might
|
---|
| 294 | // be future ones caused by CSS transitions.
|
---|
| 295 | if (changesDetected) {
|
---|
| 296 | this.refresh();
|
---|
| 297 | }
|
---|
| 298 | };
|
---|
| 299 | /**
|
---|
| 300 | * Updates every observer from observers list and notifies them of queued
|
---|
| 301 | * entries.
|
---|
| 302 | *
|
---|
| 303 | * @private
|
---|
| 304 | * @returns {boolean} Returns "true" if any observer has detected changes in
|
---|
| 305 | * dimensions of it's elements.
|
---|
| 306 | */
|
---|
| 307 | ResizeObserverController.prototype.updateObservers_ = function () {
|
---|
| 308 | // Collect observers that have active observations.
|
---|
| 309 | var activeObservers = this.observers_.filter(function (observer) {
|
---|
| 310 | return observer.gatherActive(), observer.hasActive();
|
---|
| 311 | });
|
---|
| 312 | // Deliver notifications in a separate cycle in order to avoid any
|
---|
| 313 | // collisions between observers, e.g. when multiple instances of
|
---|
| 314 | // ResizeObserver are tracking the same element and the callback of one
|
---|
| 315 | // of them changes content dimensions of the observed target. Sometimes
|
---|
| 316 | // this may result in notifications being blocked for the rest of observers.
|
---|
| 317 | activeObservers.forEach(function (observer) { return observer.broadcastActive(); });
|
---|
| 318 | return activeObservers.length > 0;
|
---|
| 319 | };
|
---|
| 320 | /**
|
---|
| 321 | * Initializes DOM listeners.
|
---|
| 322 | *
|
---|
| 323 | * @private
|
---|
| 324 | * @returns {void}
|
---|
| 325 | */
|
---|
| 326 | ResizeObserverController.prototype.connect_ = function () {
|
---|
| 327 | // Do nothing if running in a non-browser environment or if listeners
|
---|
| 328 | // have been already added.
|
---|
| 329 | if (!isBrowser || this.connected_) {
|
---|
| 330 | return;
|
---|
| 331 | }
|
---|
| 332 | // Subscription to the "Transitionend" event is used as a workaround for
|
---|
| 333 | // delayed transitions. This way it's possible to capture at least the
|
---|
| 334 | // final state of an element.
|
---|
| 335 | document.addEventListener('transitionend', this.onTransitionEnd_);
|
---|
| 336 | window.addEventListener('resize', this.refresh);
|
---|
| 337 | if (mutationObserverSupported) {
|
---|
| 338 | this.mutationsObserver_ = new MutationObserver(this.refresh);
|
---|
| 339 | this.mutationsObserver_.observe(document, {
|
---|
| 340 | attributes: true,
|
---|
| 341 | childList: true,
|
---|
| 342 | characterData: true,
|
---|
| 343 | subtree: true
|
---|
| 344 | });
|
---|
| 345 | }
|
---|
| 346 | else {
|
---|
| 347 | document.addEventListener('DOMSubtreeModified', this.refresh);
|
---|
| 348 | this.mutationEventsAdded_ = true;
|
---|
| 349 | }
|
---|
| 350 | this.connected_ = true;
|
---|
| 351 | };
|
---|
| 352 | /**
|
---|
| 353 | * Removes DOM listeners.
|
---|
| 354 | *
|
---|
| 355 | * @private
|
---|
| 356 | * @returns {void}
|
---|
| 357 | */
|
---|
| 358 | ResizeObserverController.prototype.disconnect_ = function () {
|
---|
| 359 | // Do nothing if running in a non-browser environment or if listeners
|
---|
| 360 | // have been already removed.
|
---|
| 361 | if (!isBrowser || !this.connected_) {
|
---|
| 362 | return;
|
---|
| 363 | }
|
---|
| 364 | document.removeEventListener('transitionend', this.onTransitionEnd_);
|
---|
| 365 | window.removeEventListener('resize', this.refresh);
|
---|
| 366 | if (this.mutationsObserver_) {
|
---|
| 367 | this.mutationsObserver_.disconnect();
|
---|
| 368 | }
|
---|
| 369 | if (this.mutationEventsAdded_) {
|
---|
| 370 | document.removeEventListener('DOMSubtreeModified', this.refresh);
|
---|
| 371 | }
|
---|
| 372 | this.mutationsObserver_ = null;
|
---|
| 373 | this.mutationEventsAdded_ = false;
|
---|
| 374 | this.connected_ = false;
|
---|
| 375 | };
|
---|
| 376 | /**
|
---|
| 377 | * "Transitionend" event handler.
|
---|
| 378 | *
|
---|
| 379 | * @private
|
---|
| 380 | * @param {TransitionEvent} event
|
---|
| 381 | * @returns {void}
|
---|
| 382 | */
|
---|
| 383 | ResizeObserverController.prototype.onTransitionEnd_ = function (_a) {
|
---|
| 384 | var _b = _a.propertyName, propertyName = _b === void 0 ? '' : _b;
|
---|
| 385 | // Detect whether transition may affect dimensions of an element.
|
---|
| 386 | var isReflowProperty = transitionKeys.some(function (key) {
|
---|
| 387 | return !!~propertyName.indexOf(key);
|
---|
| 388 | });
|
---|
| 389 | if (isReflowProperty) {
|
---|
| 390 | this.refresh();
|
---|
| 391 | }
|
---|
| 392 | };
|
---|
| 393 | /**
|
---|
| 394 | * Returns instance of the ResizeObserverController.
|
---|
| 395 | *
|
---|
| 396 | * @returns {ResizeObserverController}
|
---|
| 397 | */
|
---|
| 398 | ResizeObserverController.getInstance = function () {
|
---|
| 399 | if (!this.instance_) {
|
---|
| 400 | this.instance_ = new ResizeObserverController();
|
---|
| 401 | }
|
---|
| 402 | return this.instance_;
|
---|
| 403 | };
|
---|
| 404 | /**
|
---|
| 405 | * Holds reference to the controller's instance.
|
---|
| 406 | *
|
---|
| 407 | * @private {ResizeObserverController}
|
---|
| 408 | */
|
---|
| 409 | ResizeObserverController.instance_ = null;
|
---|
| 410 | return ResizeObserverController;
|
---|
| 411 | }());
|
---|
| 412 |
|
---|
| 413 | /**
|
---|
| 414 | * Defines non-writable/enumerable properties of the provided target object.
|
---|
| 415 | *
|
---|
| 416 | * @param {Object} target - Object for which to define properties.
|
---|
| 417 | * @param {Object} props - Properties to be defined.
|
---|
| 418 | * @returns {Object} Target object.
|
---|
| 419 | */
|
---|
| 420 | var defineConfigurable = (function (target, props) {
|
---|
| 421 | for (var _i = 0, _a = Object.keys(props); _i < _a.length; _i++) {
|
---|
| 422 | var key = _a[_i];
|
---|
| 423 | Object.defineProperty(target, key, {
|
---|
| 424 | value: props[key],
|
---|
| 425 | enumerable: false,
|
---|
| 426 | writable: false,
|
---|
| 427 | configurable: true
|
---|
| 428 | });
|
---|
| 429 | }
|
---|
| 430 | return target;
|
---|
| 431 | });
|
---|
| 432 |
|
---|
| 433 | /**
|
---|
| 434 | * Returns the global object associated with provided element.
|
---|
| 435 | *
|
---|
| 436 | * @param {Object} target
|
---|
| 437 | * @returns {Object}
|
---|
| 438 | */
|
---|
| 439 | var getWindowOf = (function (target) {
|
---|
| 440 | // Assume that the element is an instance of Node, which means that it
|
---|
| 441 | // has the "ownerDocument" property from which we can retrieve a
|
---|
| 442 | // corresponding global object.
|
---|
| 443 | var ownerGlobal = target && target.ownerDocument && target.ownerDocument.defaultView;
|
---|
| 444 | // Return the local global object if it's not possible extract one from
|
---|
| 445 | // provided element.
|
---|
| 446 | return ownerGlobal || global$1;
|
---|
| 447 | });
|
---|
| 448 |
|
---|
| 449 | // Placeholder of an empty content rectangle.
|
---|
| 450 | var emptyRect = createRectInit(0, 0, 0, 0);
|
---|
| 451 | /**
|
---|
| 452 | * Converts provided string to a number.
|
---|
| 453 | *
|
---|
| 454 | * @param {number|string} value
|
---|
| 455 | * @returns {number}
|
---|
| 456 | */
|
---|
| 457 | function toFloat(value) {
|
---|
| 458 | return parseFloat(value) || 0;
|
---|
| 459 | }
|
---|
| 460 | /**
|
---|
| 461 | * Extracts borders size from provided styles.
|
---|
| 462 | *
|
---|
| 463 | * @param {CSSStyleDeclaration} styles
|
---|
| 464 | * @param {...string} positions - Borders positions (top, right, ...)
|
---|
| 465 | * @returns {number}
|
---|
| 466 | */
|
---|
| 467 | function getBordersSize(styles) {
|
---|
| 468 | var positions = [];
|
---|
| 469 | for (var _i = 1; _i < arguments.length; _i++) {
|
---|
| 470 | positions[_i - 1] = arguments[_i];
|
---|
| 471 | }
|
---|
| 472 | return positions.reduce(function (size, position) {
|
---|
| 473 | var value = styles['border-' + position + '-width'];
|
---|
| 474 | return size + toFloat(value);
|
---|
| 475 | }, 0);
|
---|
| 476 | }
|
---|
| 477 | /**
|
---|
| 478 | * Extracts paddings sizes from provided styles.
|
---|
| 479 | *
|
---|
| 480 | * @param {CSSStyleDeclaration} styles
|
---|
| 481 | * @returns {Object} Paddings box.
|
---|
| 482 | */
|
---|
| 483 | function getPaddings(styles) {
|
---|
| 484 | var positions = ['top', 'right', 'bottom', 'left'];
|
---|
| 485 | var paddings = {};
|
---|
| 486 | for (var _i = 0, positions_1 = positions; _i < positions_1.length; _i++) {
|
---|
| 487 | var position = positions_1[_i];
|
---|
| 488 | var value = styles['padding-' + position];
|
---|
| 489 | paddings[position] = toFloat(value);
|
---|
| 490 | }
|
---|
| 491 | return paddings;
|
---|
| 492 | }
|
---|
| 493 | /**
|
---|
| 494 | * Calculates content rectangle of provided SVG element.
|
---|
| 495 | *
|
---|
| 496 | * @param {SVGGraphicsElement} target - Element content rectangle of which needs
|
---|
| 497 | * to be calculated.
|
---|
| 498 | * @returns {DOMRectInit}
|
---|
| 499 | */
|
---|
| 500 | function getSVGContentRect(target) {
|
---|
| 501 | var bbox = target.getBBox();
|
---|
| 502 | return createRectInit(0, 0, bbox.width, bbox.height);
|
---|
| 503 | }
|
---|
| 504 | /**
|
---|
| 505 | * Calculates content rectangle of provided HTMLElement.
|
---|
| 506 | *
|
---|
| 507 | * @param {HTMLElement} target - Element for which to calculate the content rectangle.
|
---|
| 508 | * @returns {DOMRectInit}
|
---|
| 509 | */
|
---|
| 510 | function getHTMLElementContentRect(target) {
|
---|
| 511 | // Client width & height properties can't be
|
---|
| 512 | // used exclusively as they provide rounded values.
|
---|
| 513 | var clientWidth = target.clientWidth, clientHeight = target.clientHeight;
|
---|
| 514 | // By this condition we can catch all non-replaced inline, hidden and
|
---|
| 515 | // detached elements. Though elements with width & height properties less
|
---|
| 516 | // than 0.5 will be discarded as well.
|
---|
| 517 | //
|
---|
| 518 | // Without it we would need to implement separate methods for each of
|
---|
| 519 | // those cases and it's not possible to perform a precise and performance
|
---|
| 520 | // effective test for hidden elements. E.g. even jQuery's ':visible' filter
|
---|
| 521 | // gives wrong results for elements with width & height less than 0.5.
|
---|
| 522 | if (!clientWidth && !clientHeight) {
|
---|
| 523 | return emptyRect;
|
---|
| 524 | }
|
---|
| 525 | var styles = getWindowOf(target).getComputedStyle(target);
|
---|
| 526 | var paddings = getPaddings(styles);
|
---|
| 527 | var horizPad = paddings.left + paddings.right;
|
---|
| 528 | var vertPad = paddings.top + paddings.bottom;
|
---|
| 529 | // Computed styles of width & height are being used because they are the
|
---|
| 530 | // only dimensions available to JS that contain non-rounded values. It could
|
---|
| 531 | // be possible to utilize the getBoundingClientRect if only it's data wasn't
|
---|
| 532 | // affected by CSS transformations let alone paddings, borders and scroll bars.
|
---|
| 533 | var width = toFloat(styles.width), height = toFloat(styles.height);
|
---|
| 534 | // Width & height include paddings and borders when the 'border-box' box
|
---|
| 535 | // model is applied (except for IE).
|
---|
| 536 | if (styles.boxSizing === 'border-box') {
|
---|
| 537 | // Following conditions are required to handle Internet Explorer which
|
---|
| 538 | // doesn't include paddings and borders to computed CSS dimensions.
|
---|
| 539 | //
|
---|
| 540 | // We can say that if CSS dimensions + paddings are equal to the "client"
|
---|
| 541 | // properties then it's either IE, and thus we don't need to subtract
|
---|
| 542 | // anything, or an element merely doesn't have paddings/borders styles.
|
---|
| 543 | if (Math.round(width + horizPad) !== clientWidth) {
|
---|
| 544 | width -= getBordersSize(styles, 'left', 'right') + horizPad;
|
---|
| 545 | }
|
---|
| 546 | if (Math.round(height + vertPad) !== clientHeight) {
|
---|
| 547 | height -= getBordersSize(styles, 'top', 'bottom') + vertPad;
|
---|
| 548 | }
|
---|
| 549 | }
|
---|
| 550 | // Following steps can't be applied to the document's root element as its
|
---|
| 551 | // client[Width/Height] properties represent viewport area of the window.
|
---|
| 552 | // Besides, it's as well not necessary as the <html> itself neither has
|
---|
| 553 | // rendered scroll bars nor it can be clipped.
|
---|
| 554 | if (!isDocumentElement(target)) {
|
---|
| 555 | // In some browsers (only in Firefox, actually) CSS width & height
|
---|
| 556 | // include scroll bars size which can be removed at this step as scroll
|
---|
| 557 | // bars are the only difference between rounded dimensions + paddings
|
---|
| 558 | // and "client" properties, though that is not always true in Chrome.
|
---|
| 559 | var vertScrollbar = Math.round(width + horizPad) - clientWidth;
|
---|
| 560 | var horizScrollbar = Math.round(height + vertPad) - clientHeight;
|
---|
| 561 | // Chrome has a rather weird rounding of "client" properties.
|
---|
| 562 | // E.g. for an element with content width of 314.2px it sometimes gives
|
---|
| 563 | // the client width of 315px and for the width of 314.7px it may give
|
---|
| 564 | // 314px. And it doesn't happen all the time. So just ignore this delta
|
---|
| 565 | // as a non-relevant.
|
---|
| 566 | if (Math.abs(vertScrollbar) !== 1) {
|
---|
| 567 | width -= vertScrollbar;
|
---|
| 568 | }
|
---|
| 569 | if (Math.abs(horizScrollbar) !== 1) {
|
---|
| 570 | height -= horizScrollbar;
|
---|
| 571 | }
|
---|
| 572 | }
|
---|
| 573 | return createRectInit(paddings.left, paddings.top, width, height);
|
---|
| 574 | }
|
---|
| 575 | /**
|
---|
| 576 | * Checks whether provided element is an instance of the SVGGraphicsElement.
|
---|
| 577 | *
|
---|
| 578 | * @param {Element} target - Element to be checked.
|
---|
| 579 | * @returns {boolean}
|
---|
| 580 | */
|
---|
| 581 | var isSVGGraphicsElement = (function () {
|
---|
| 582 | // Some browsers, namely IE and Edge, don't have the SVGGraphicsElement
|
---|
| 583 | // interface.
|
---|
| 584 | if (typeof SVGGraphicsElement !== 'undefined') {
|
---|
| 585 | return function (target) { return target instanceof getWindowOf(target).SVGGraphicsElement; };
|
---|
| 586 | }
|
---|
| 587 | // If it's so, then check that element is at least an instance of the
|
---|
| 588 | // SVGElement and that it has the "getBBox" method.
|
---|
| 589 | // eslint-disable-next-line no-extra-parens
|
---|
| 590 | return function (target) { return (target instanceof getWindowOf(target).SVGElement &&
|
---|
| 591 | typeof target.getBBox === 'function'); };
|
---|
| 592 | })();
|
---|
| 593 | /**
|
---|
| 594 | * Checks whether provided element is a document element (<html>).
|
---|
| 595 | *
|
---|
| 596 | * @param {Element} target - Element to be checked.
|
---|
| 597 | * @returns {boolean}
|
---|
| 598 | */
|
---|
| 599 | function isDocumentElement(target) {
|
---|
| 600 | return target === getWindowOf(target).document.documentElement;
|
---|
| 601 | }
|
---|
| 602 | /**
|
---|
| 603 | * Calculates an appropriate content rectangle for provided html or svg element.
|
---|
| 604 | *
|
---|
| 605 | * @param {Element} target - Element content rectangle of which needs to be calculated.
|
---|
| 606 | * @returns {DOMRectInit}
|
---|
| 607 | */
|
---|
| 608 | function getContentRect(target) {
|
---|
| 609 | if (!isBrowser) {
|
---|
| 610 | return emptyRect;
|
---|
| 611 | }
|
---|
| 612 | if (isSVGGraphicsElement(target)) {
|
---|
| 613 | return getSVGContentRect(target);
|
---|
| 614 | }
|
---|
| 615 | return getHTMLElementContentRect(target);
|
---|
| 616 | }
|
---|
| 617 | /**
|
---|
| 618 | * Creates rectangle with an interface of the DOMRectReadOnly.
|
---|
| 619 | * Spec: https://drafts.fxtf.org/geometry/#domrectreadonly
|
---|
| 620 | *
|
---|
| 621 | * @param {DOMRectInit} rectInit - Object with rectangle's x/y coordinates and dimensions.
|
---|
| 622 | * @returns {DOMRectReadOnly}
|
---|
| 623 | */
|
---|
| 624 | function createReadOnlyRect(_a) {
|
---|
| 625 | var x = _a.x, y = _a.y, width = _a.width, height = _a.height;
|
---|
| 626 | // If DOMRectReadOnly is available use it as a prototype for the rectangle.
|
---|
| 627 | var Constr = typeof DOMRectReadOnly !== 'undefined' ? DOMRectReadOnly : Object;
|
---|
| 628 | var rect = Object.create(Constr.prototype);
|
---|
| 629 | // Rectangle's properties are not writable and non-enumerable.
|
---|
| 630 | defineConfigurable(rect, {
|
---|
| 631 | x: x, y: y, width: width, height: height,
|
---|
| 632 | top: y,
|
---|
| 633 | right: x + width,
|
---|
| 634 | bottom: height + y,
|
---|
| 635 | left: x
|
---|
| 636 | });
|
---|
| 637 | return rect;
|
---|
| 638 | }
|
---|
| 639 | /**
|
---|
| 640 | * Creates DOMRectInit object based on the provided dimensions and the x/y coordinates.
|
---|
| 641 | * Spec: https://drafts.fxtf.org/geometry/#dictdef-domrectinit
|
---|
| 642 | *
|
---|
| 643 | * @param {number} x - X coordinate.
|
---|
| 644 | * @param {number} y - Y coordinate.
|
---|
| 645 | * @param {number} width - Rectangle's width.
|
---|
| 646 | * @param {number} height - Rectangle's height.
|
---|
| 647 | * @returns {DOMRectInit}
|
---|
| 648 | */
|
---|
| 649 | function createRectInit(x, y, width, height) {
|
---|
| 650 | return { x: x, y: y, width: width, height: height };
|
---|
| 651 | }
|
---|
| 652 |
|
---|
| 653 | /**
|
---|
| 654 | * Class that is responsible for computations of the content rectangle of
|
---|
| 655 | * provided DOM element and for keeping track of it's changes.
|
---|
| 656 | */
|
---|
| 657 | var ResizeObservation = /** @class */ (function () {
|
---|
| 658 | /**
|
---|
| 659 | * Creates an instance of ResizeObservation.
|
---|
| 660 | *
|
---|
| 661 | * @param {Element} target - Element to be observed.
|
---|
| 662 | */
|
---|
| 663 | function ResizeObservation(target) {
|
---|
| 664 | /**
|
---|
| 665 | * Broadcasted width of content rectangle.
|
---|
| 666 | *
|
---|
| 667 | * @type {number}
|
---|
| 668 | */
|
---|
| 669 | this.broadcastWidth = 0;
|
---|
| 670 | /**
|
---|
| 671 | * Broadcasted height of content rectangle.
|
---|
| 672 | *
|
---|
| 673 | * @type {number}
|
---|
| 674 | */
|
---|
| 675 | this.broadcastHeight = 0;
|
---|
| 676 | /**
|
---|
| 677 | * Reference to the last observed content rectangle.
|
---|
| 678 | *
|
---|
| 679 | * @private {DOMRectInit}
|
---|
| 680 | */
|
---|
| 681 | this.contentRect_ = createRectInit(0, 0, 0, 0);
|
---|
| 682 | this.target = target;
|
---|
| 683 | }
|
---|
| 684 | /**
|
---|
| 685 | * Updates content rectangle and tells whether it's width or height properties
|
---|
| 686 | * have changed since the last broadcast.
|
---|
| 687 | *
|
---|
| 688 | * @returns {boolean}
|
---|
| 689 | */
|
---|
| 690 | ResizeObservation.prototype.isActive = function () {
|
---|
| 691 | var rect = getContentRect(this.target);
|
---|
| 692 | this.contentRect_ = rect;
|
---|
| 693 | return (rect.width !== this.broadcastWidth ||
|
---|
| 694 | rect.height !== this.broadcastHeight);
|
---|
| 695 | };
|
---|
| 696 | /**
|
---|
| 697 | * Updates 'broadcastWidth' and 'broadcastHeight' properties with a data
|
---|
| 698 | * from the corresponding properties of the last observed content rectangle.
|
---|
| 699 | *
|
---|
| 700 | * @returns {DOMRectInit} Last observed content rectangle.
|
---|
| 701 | */
|
---|
| 702 | ResizeObservation.prototype.broadcastRect = function () {
|
---|
| 703 | var rect = this.contentRect_;
|
---|
| 704 | this.broadcastWidth = rect.width;
|
---|
| 705 | this.broadcastHeight = rect.height;
|
---|
| 706 | return rect;
|
---|
| 707 | };
|
---|
| 708 | return ResizeObservation;
|
---|
| 709 | }());
|
---|
| 710 |
|
---|
| 711 | var ResizeObserverEntry = /** @class */ (function () {
|
---|
| 712 | /**
|
---|
| 713 | * Creates an instance of ResizeObserverEntry.
|
---|
| 714 | *
|
---|
| 715 | * @param {Element} target - Element that is being observed.
|
---|
| 716 | * @param {DOMRectInit} rectInit - Data of the element's content rectangle.
|
---|
| 717 | */
|
---|
| 718 | function ResizeObserverEntry(target, rectInit) {
|
---|
| 719 | var contentRect = createReadOnlyRect(rectInit);
|
---|
| 720 | // According to the specification following properties are not writable
|
---|
| 721 | // and are also not enumerable in the native implementation.
|
---|
| 722 | //
|
---|
| 723 | // Property accessors are not being used as they'd require to define a
|
---|
| 724 | // private WeakMap storage which may cause memory leaks in browsers that
|
---|
| 725 | // don't support this type of collections.
|
---|
| 726 | defineConfigurable(this, { target: target, contentRect: contentRect });
|
---|
| 727 | }
|
---|
| 728 | return ResizeObserverEntry;
|
---|
| 729 | }());
|
---|
| 730 |
|
---|
| 731 | var ResizeObserverSPI = /** @class */ (function () {
|
---|
| 732 | /**
|
---|
| 733 | * Creates a new instance of ResizeObserver.
|
---|
| 734 | *
|
---|
| 735 | * @param {ResizeObserverCallback} callback - Callback function that is invoked
|
---|
| 736 | * when one of the observed elements changes it's content dimensions.
|
---|
| 737 | * @param {ResizeObserverController} controller - Controller instance which
|
---|
| 738 | * is responsible for the updates of observer.
|
---|
| 739 | * @param {ResizeObserver} callbackCtx - Reference to the public
|
---|
| 740 | * ResizeObserver instance which will be passed to callback function.
|
---|
| 741 | */
|
---|
| 742 | function ResizeObserverSPI(callback, controller, callbackCtx) {
|
---|
| 743 | /**
|
---|
| 744 | * Collection of resize observations that have detected changes in dimensions
|
---|
| 745 | * of elements.
|
---|
| 746 | *
|
---|
| 747 | * @private {Array<ResizeObservation>}
|
---|
| 748 | */
|
---|
| 749 | this.activeObservations_ = [];
|
---|
| 750 | /**
|
---|
| 751 | * Registry of the ResizeObservation instances.
|
---|
| 752 | *
|
---|
| 753 | * @private {Map<Element, ResizeObservation>}
|
---|
| 754 | */
|
---|
| 755 | this.observations_ = new MapShim();
|
---|
| 756 | if (typeof callback !== 'function') {
|
---|
| 757 | throw new TypeError('The callback provided as parameter 1 is not a function.');
|
---|
| 758 | }
|
---|
| 759 | this.callback_ = callback;
|
---|
| 760 | this.controller_ = controller;
|
---|
| 761 | this.callbackCtx_ = callbackCtx;
|
---|
| 762 | }
|
---|
| 763 | /**
|
---|
| 764 | * Starts observing provided element.
|
---|
| 765 | *
|
---|
| 766 | * @param {Element} target - Element to be observed.
|
---|
| 767 | * @returns {void}
|
---|
| 768 | */
|
---|
| 769 | ResizeObserverSPI.prototype.observe = function (target) {
|
---|
| 770 | if (!arguments.length) {
|
---|
| 771 | throw new TypeError('1 argument required, but only 0 present.');
|
---|
| 772 | }
|
---|
| 773 | // Do nothing if current environment doesn't have the Element interface.
|
---|
| 774 | if (typeof Element === 'undefined' || !(Element instanceof Object)) {
|
---|
| 775 | return;
|
---|
| 776 | }
|
---|
| 777 | if (!(target instanceof getWindowOf(target).Element)) {
|
---|
| 778 | throw new TypeError('parameter 1 is not of type "Element".');
|
---|
| 779 | }
|
---|
| 780 | var observations = this.observations_;
|
---|
| 781 | // Do nothing if element is already being observed.
|
---|
| 782 | if (observations.has(target)) {
|
---|
| 783 | return;
|
---|
| 784 | }
|
---|
| 785 | observations.set(target, new ResizeObservation(target));
|
---|
| 786 | this.controller_.addObserver(this);
|
---|
| 787 | // Force the update of observations.
|
---|
| 788 | this.controller_.refresh();
|
---|
| 789 | };
|
---|
| 790 | /**
|
---|
| 791 | * Stops observing provided element.
|
---|
| 792 | *
|
---|
| 793 | * @param {Element} target - Element to stop observing.
|
---|
| 794 | * @returns {void}
|
---|
| 795 | */
|
---|
| 796 | ResizeObserverSPI.prototype.unobserve = function (target) {
|
---|
| 797 | if (!arguments.length) {
|
---|
| 798 | throw new TypeError('1 argument required, but only 0 present.');
|
---|
| 799 | }
|
---|
| 800 | // Do nothing if current environment doesn't have the Element interface.
|
---|
| 801 | if (typeof Element === 'undefined' || !(Element instanceof Object)) {
|
---|
| 802 | return;
|
---|
| 803 | }
|
---|
| 804 | if (!(target instanceof getWindowOf(target).Element)) {
|
---|
| 805 | throw new TypeError('parameter 1 is not of type "Element".');
|
---|
| 806 | }
|
---|
| 807 | var observations = this.observations_;
|
---|
| 808 | // Do nothing if element is not being observed.
|
---|
| 809 | if (!observations.has(target)) {
|
---|
| 810 | return;
|
---|
| 811 | }
|
---|
| 812 | observations.delete(target);
|
---|
| 813 | if (!observations.size) {
|
---|
| 814 | this.controller_.removeObserver(this);
|
---|
| 815 | }
|
---|
| 816 | };
|
---|
| 817 | /**
|
---|
| 818 | * Stops observing all elements.
|
---|
| 819 | *
|
---|
| 820 | * @returns {void}
|
---|
| 821 | */
|
---|
| 822 | ResizeObserverSPI.prototype.disconnect = function () {
|
---|
| 823 | this.clearActive();
|
---|
| 824 | this.observations_.clear();
|
---|
| 825 | this.controller_.removeObserver(this);
|
---|
| 826 | };
|
---|
| 827 | /**
|
---|
| 828 | * Collects observation instances the associated element of which has changed
|
---|
| 829 | * it's content rectangle.
|
---|
| 830 | *
|
---|
| 831 | * @returns {void}
|
---|
| 832 | */
|
---|
| 833 | ResizeObserverSPI.prototype.gatherActive = function () {
|
---|
| 834 | var _this = this;
|
---|
| 835 | this.clearActive();
|
---|
| 836 | this.observations_.forEach(function (observation) {
|
---|
| 837 | if (observation.isActive()) {
|
---|
| 838 | _this.activeObservations_.push(observation);
|
---|
| 839 | }
|
---|
| 840 | });
|
---|
| 841 | };
|
---|
| 842 | /**
|
---|
| 843 | * Invokes initial callback function with a list of ResizeObserverEntry
|
---|
| 844 | * instances collected from active resize observations.
|
---|
| 845 | *
|
---|
| 846 | * @returns {void}
|
---|
| 847 | */
|
---|
| 848 | ResizeObserverSPI.prototype.broadcastActive = function () {
|
---|
| 849 | // Do nothing if observer doesn't have active observations.
|
---|
| 850 | if (!this.hasActive()) {
|
---|
| 851 | return;
|
---|
| 852 | }
|
---|
| 853 | var ctx = this.callbackCtx_;
|
---|
| 854 | // Create ResizeObserverEntry instance for every active observation.
|
---|
| 855 | var entries = this.activeObservations_.map(function (observation) {
|
---|
| 856 | return new ResizeObserverEntry(observation.target, observation.broadcastRect());
|
---|
| 857 | });
|
---|
| 858 | this.callback_.call(ctx, entries, ctx);
|
---|
| 859 | this.clearActive();
|
---|
| 860 | };
|
---|
| 861 | /**
|
---|
| 862 | * Clears the collection of active observations.
|
---|
| 863 | *
|
---|
| 864 | * @returns {void}
|
---|
| 865 | */
|
---|
| 866 | ResizeObserverSPI.prototype.clearActive = function () {
|
---|
| 867 | this.activeObservations_.splice(0);
|
---|
| 868 | };
|
---|
| 869 | /**
|
---|
| 870 | * Tells whether observer has active observations.
|
---|
| 871 | *
|
---|
| 872 | * @returns {boolean}
|
---|
| 873 | */
|
---|
| 874 | ResizeObserverSPI.prototype.hasActive = function () {
|
---|
| 875 | return this.activeObservations_.length > 0;
|
---|
| 876 | };
|
---|
| 877 | return ResizeObserverSPI;
|
---|
| 878 | }());
|
---|
| 879 |
|
---|
| 880 | // Registry of internal observers. If WeakMap is not available use current shim
|
---|
| 881 | // for the Map collection as it has all required methods and because WeakMap
|
---|
| 882 | // can't be fully polyfilled anyway.
|
---|
| 883 | var observers = typeof WeakMap !== 'undefined' ? new WeakMap() : new MapShim();
|
---|
| 884 | /**
|
---|
| 885 | * ResizeObserver API. Encapsulates the ResizeObserver SPI implementation
|
---|
| 886 | * exposing only those methods and properties that are defined in the spec.
|
---|
| 887 | */
|
---|
| 888 | var ResizeObserver = /** @class */ (function () {
|
---|
| 889 | /**
|
---|
| 890 | * Creates a new instance of ResizeObserver.
|
---|
| 891 | *
|
---|
| 892 | * @param {ResizeObserverCallback} callback - Callback that is invoked when
|
---|
| 893 | * dimensions of the observed elements change.
|
---|
| 894 | */
|
---|
| 895 | function ResizeObserver(callback) {
|
---|
| 896 | if (!(this instanceof ResizeObserver)) {
|
---|
| 897 | throw new TypeError('Cannot call a class as a function.');
|
---|
| 898 | }
|
---|
| 899 | if (!arguments.length) {
|
---|
| 900 | throw new TypeError('1 argument required, but only 0 present.');
|
---|
| 901 | }
|
---|
| 902 | var controller = ResizeObserverController.getInstance();
|
---|
| 903 | var observer = new ResizeObserverSPI(callback, controller, this);
|
---|
| 904 | observers.set(this, observer);
|
---|
| 905 | }
|
---|
| 906 | return ResizeObserver;
|
---|
| 907 | }());
|
---|
| 908 | // Expose public methods of ResizeObserver.
|
---|
| 909 | [
|
---|
| 910 | 'observe',
|
---|
| 911 | 'unobserve',
|
---|
| 912 | 'disconnect'
|
---|
| 913 | ].forEach(function (method) {
|
---|
| 914 | ResizeObserver.prototype[method] = function () {
|
---|
| 915 | var _a;
|
---|
| 916 | return (_a = observers.get(this))[method].apply(_a, arguments);
|
---|
| 917 | };
|
---|
| 918 | });
|
---|
| 919 |
|
---|
| 920 | var index = (function () {
|
---|
| 921 | // Export existing implementation if available.
|
---|
| 922 | if (typeof global$1.ResizeObserver !== 'undefined') {
|
---|
| 923 | return global$1.ResizeObserver;
|
---|
| 924 | }
|
---|
| 925 | return ResizeObserver;
|
---|
| 926 | })();
|
---|
| 927 |
|
---|
| 928 | export default index;
|
---|