source: imaps-frontend/node_modules/resize-observer-polyfill/dist/ResizeObserver.js

main
Last change on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

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