Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
Size: Mime:
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
var css_element_queries_1 = require("css-element-queries");
var mobx_1 = require("mobx");
var uuid = require("uuid");
var RectServer = /** @class */ (function () {
    function RectServer() {
        var _this = this;
        this._windowEventElements = new Map();
        this._windowElementSensors = new Map();
        this.viewportScrollDepth = 0;
        this.viewportScrollY = 0;
        this.viewportWidth = 0;
        this.viewportHeight = 0;
        this.windowWidth = 0;
        this.windowHeight = 0;
        this.windowOffsetX = 0;
        this.isResizingAutomatically = false;
        this.isResizingManually = false;
        this.isResizingElement = false;
        this.elementRects = new Map();
        this.bindElement = function (element, id) {
            if (typeof id === 'undefined') {
                id = element.getAttribute('id') || uuid.v4();
            }
            if (_this._windowEventElements.has(id)) {
                if (_this._windowEventElements.get(id) === element) {
                    return id;
                }
                _this.unbindElement(id);
            }
            element.setAttribute('id', id);
            _this
                ._windowEventElements
                .set(id, element);
            _this
                ._windowElementSensors
                .set(id, new css_element_queries_1.ResizeSensor(element, function () {
                if (_this.isResizing) {
                    return;
                }
                _this.updateVisibleElementRects();
            }));
            _this.runWindowEvents();
            return id;
        };
        this.updateVisibleElementRects = function () {
            _this
                .getVisibleElementIds()
                .forEach(function (id) {
                var element = _this
                    ._windowEventElements
                    .get(id);
                if (!element) {
                    return;
                }
                _this.updateElementRect(element, id);
            });
        };
        this.getElement = function (id) {
            return _this._windowEventElements.get(id);
        };
        this.getElementRect = function (id) {
            return _this
                .elementRects
                .get(id) || null;
        };
        this.updateElementRects = function () {
            _this
                ._windowEventElements
                .forEach(_this.updateElementRect);
        };
        this.updateElementRect = function (element, id) {
            if (!_this._window.document.getElementById(id)) {
                _this.unbindElement(id);
            }
            else {
                var elementRect = element.getBoundingClientRect();
                _this
                    .elementRects
                    .set(id, elementRect);
            }
        };
        this.unbindElement = function (id) {
            if (!_this._windowEventElements.has(id)) {
                throw new Error('Trying to unbind element that already has been unbound: ' + id);
            }
            if (_this.elementRects.has(id)) {
                _this.elementRects.delete(id);
            }
            _this._windowEventElements.delete(id);
            if (_this._windowElementSensors.has(id)) {
                var sensor = _this._windowElementSensors.get(id);
                sensor.detach(function () {
                    _this._windowElementSensors.delete(id);
                });
            }
        };
        if (typeof window !== 'undefined') {
            this.bindToWindow(window);
        }
    }
    Object.defineProperty(RectServer.prototype, "isResizing", {
        get: function () {
            return this.isResizingAutomatically || this.isResizingManually;
        },
        enumerable: true,
        configurable: true
    });
    RectServer.prototype.bindToWindow = function (_window) {
        _window.addEventListener('resize', this.runWindowEvents.bind(this));
        _window.addEventListener('scroll', this.runWindowEvents.bind(this));
        this._window = _window;
        this.runWindowEvents();
        return this;
    };
    /* Cant use HTMLElement as param type in actions b/c problems running in node context
    https://github.com/tjoskar/ng-lazyload-image/issues/271 */
    RectServer.prototype.bindToViewport = function (_viewportElement) {
        _viewportElement.onscroll = this
            .runWindowEvents
            .bind(this);
        this._viewportElement = _viewportElement;
        this
            .elementRects
            .set('viewport', _viewportElement.getBoundingClientRect());
        this._viewportResizeSensor = new css_element_queries_1.ResizeSensor(_viewportElement, this.runWindowEvents.bind(this));
        this.runWindowEvents();
        return this;
    };
    RectServer.prototype.getVisibleElementIds = function () {
        var _this = this;
        var viewportRect = this.getViewportRect();
        var ids = [];
        this
            .elementRects
            .forEach(function (rect, id) {
            if (id === 'viewport') {
                return;
            }
            if (rect.top > viewportRect.top + _this.viewportHeight) {
                return;
            }
            if (viewportRect.top > rect.top + rect.height) {
                return;
            }
            ids.push(id);
        });
        return ids;
    };
    RectServer.prototype.getViewportRect = function () {
        var viewportRect = this
            .elementRects
            .get('viewport');
        if (viewportRect) {
            return viewportRect;
        }
        else {
            var width = 0;
            var height = 0;
            var top_1 = 0;
            if (typeof this._window !== 'undefined') {
                top_1 = this.windowOffsetX;
                width = this._window.innerWidth;
                height = this._window.innerHeight - top_1;
            }
            return {
                top: top_1,
                right: 0,
                bottom: 0,
                left: 0,
                width: width,
                height: height
            };
        }
    };
    RectServer.prototype.getElementBoundRect = function (id) {
        var elementRect = this.getElementRect(id);
        if (elementRect && typeof this._viewportElement !== 'undefined') {
            var viewportRect = this
                .elementRects
                .get('viewport');
            return this.transformElementRect(elementRect, viewportRect);
        }
        return elementRect;
    };
    RectServer.prototype.transformElementRect = function (rect, viewportRect) {
        if (rect.left >= viewportRect.left + this.viewportWidth) {
            return null;
        }
        if (rect.top >= viewportRect.top + this.viewportHeight) {
            return null;
        }
        if (viewportRect.left >= rect.left + rect.width) {
            return null;
        }
        if (viewportRect.top >= rect.top + rect.height) {
            return null;
        }
        var newRect = {
            left: rect.left,
            top: rect.top,
            width: rect.width,
            height: rect.height,
            right: rect.right,
            bottom: rect.bottom
        };
        if (viewportRect.left > newRect.left) {
            newRect.width = newRect.width - viewportRect.left + newRect.left;
            newRect.left = viewportRect.left;
        }
        if (viewportRect.top > newRect.top) {
            newRect.height = newRect.height - viewportRect.top + newRect.top;
            newRect.top = viewportRect.top;
        }
        if (newRect.left + newRect.width >= viewportRect.left + viewportRect.width) {
            newRect.width = viewportRect.left + viewportRect.width - newRect.left;
        }
        if (newRect.top + newRect.height >= viewportRect.top + viewportRect.height) {
            newRect.height = viewportRect.top + viewportRect.height - newRect.top;
        }
        return newRect;
    };
    RectServer.prototype.runWindowEvents = function () {
        if (typeof this._window === 'undefined') {
            return;
        }
        if (typeof this._viewportElement !== 'undefined') {
            this.viewportWidth = this._viewportElement.clientWidth;
            this.viewportHeight = this._viewportElement.clientHeight;
            this.viewportScrollY = this._viewportElement.scrollTop;
            this
                .elementRects
                .set('viewport', this._viewportElement.getBoundingClientRect());
        }
        else {
            this.viewportWidth = this._window.innerWidth;
            this.viewportHeight = this._window.innerHeight;
            this.viewportScrollY = this._window.scrollY;
            this
                .elementRects
                .delete('viewport');
        }
        this.windowWidth = this._window.innerWidth;
        this.windowHeight = this._window.innerHeight;
        this.updateElementRects();
        this.isResizingAutomatically = false;
        this
            ._window
            .dispatchEvent(new CustomEvent('windowEventsRun'));
    };
    __decorate([
        mobx_1.observable,
        __metadata("design:type", Number)
    ], RectServer.prototype, "viewportScrollDepth", void 0);
    __decorate([
        mobx_1.observable,
        __metadata("design:type", Number)
    ], RectServer.prototype, "viewportScrollY", void 0);
    __decorate([
        mobx_1.observable,
        __metadata("design:type", Number)
    ], RectServer.prototype, "viewportWidth", void 0);
    __decorate([
        mobx_1.observable,
        __metadata("design:type", Number)
    ], RectServer.prototype, "viewportHeight", void 0);
    __decorate([
        mobx_1.observable,
        __metadata("design:type", Number)
    ], RectServer.prototype, "windowWidth", void 0);
    __decorate([
        mobx_1.observable,
        __metadata("design:type", Number)
    ], RectServer.prototype, "windowHeight", void 0);
    __decorate([
        mobx_1.observable,
        __metadata("design:type", Number)
    ], RectServer.prototype, "windowOffsetX", void 0);
    __decorate([
        mobx_1.observable,
        __metadata("design:type", Object)
    ], RectServer.prototype, "isResizingAutomatically", void 0);
    __decorate([
        mobx_1.observable,
        __metadata("design:type", Object)
    ], RectServer.prototype, "isResizingManually", void 0);
    __decorate([
        mobx_1.observable,
        __metadata("design:type", Object)
    ], RectServer.prototype, "isResizingElement", void 0);
    __decorate([
        mobx_1.observable,
        __metadata("design:type", Object)
    ], RectServer.prototype, "elementRects", void 0);
    __decorate([
        mobx_1.computed,
        __metadata("design:type", Object),
        __metadata("design:paramtypes", [])
    ], RectServer.prototype, "isResizing", null);
    __decorate([
        mobx_1.action,
        __metadata("design:type", Object)
    ], RectServer.prototype, "bindElement", void 0);
    __decorate([
        mobx_1.action,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", [Object]),
        __metadata("design:returntype", void 0)
    ], RectServer.prototype, "bindToViewport", null);
    __decorate([
        mobx_1.action,
        __metadata("design:type", Object)
    ], RectServer.prototype, "updateVisibleElementRects", void 0);
    __decorate([
        mobx_1.action,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", []),
        __metadata("design:returntype", void 0)
    ], RectServer.prototype, "runWindowEvents", null);
    __decorate([
        mobx_1.action,
        __metadata("design:type", Object)
    ], RectServer.prototype, "updateElementRects", void 0);
    __decorate([
        mobx_1.action,
        __metadata("design:type", Object)
    ], RectServer.prototype, "updateElementRect", void 0);
    __decorate([
        mobx_1.action,
        __metadata("design:type", Object)
    ], RectServer.prototype, "unbindElement", void 0);
    return RectServer;
}());
exports.RectServer = RectServer;
//# sourceMappingURL=rectServer.js.map