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);
};
import { autoinject, bindable, customAttribute } from 'aurelia-framework';
export var UIInfiniteScrollCustomAttribute = (function () {
    function UIInfiniteScrollCustomAttribute(element) {
        var _this = this;
        this.element = element;
        this.isTicking = false;
        this.scrollBuffer = 0;
        this.isActive = true;
        this.loadOnBottom = true;
        this.onScrollChange = function () {
            if (!_this.isActive) {
                return false;
            }
            if (!_this.isTicking) {
                window.requestAnimationFrame(function () {
                    _this.checkScrollPosition();
                    _this.isTicking = false;
                });
            }
            _this.isTicking = true;
        };
        this.element = element;
    }
    UIInfiniteScrollCustomAttribute_1 = UIInfiniteScrollCustomAttribute;
    UIInfiniteScrollCustomAttribute.prototype.attached = function () {
        this.element.addEventListener(UIInfiniteScrollCustomAttribute_1.ScrollEventName, this.onScrollChange);
        this.onScrollChange();
    };
    UIInfiniteScrollCustomAttribute.prototype.detached = function () {
        this.element.removeEventListener(UIInfiniteScrollCustomAttribute_1.ScrollEventName, this.onScrollChange);
    };
    UIInfiniteScrollCustomAttribute.prototype.callbackChanged = function (newCallback) {
        this.callback = newCallback;
    };
    UIInfiniteScrollCustomAttribute.prototype.scrollBufferChanged = function (buffer) {
        this.scrollBuffer = +buffer;
    };
    UIInfiniteScrollCustomAttribute.prototype.checkScrollPosition = function () {
        if (this.callback) {
            var windowScrollPosition = this.element.offsetHeight + this.element.scrollTop;
            var isPageScrolledToElementBottom = (windowScrollPosition + this.scrollBuffer) >= (this.element.scrollHeight - 10);
            var isElementScrolledtoTop = this.element.scrollTop == 0;
            if (this.loadOnBottom === true && isPageScrolledToElementBottom) {
                this.callback();
            }
            if (this.loadOnBottom === false && isElementScrolledtoTop) {
                this.callback();
            }
        }
    };
    var UIInfiniteScrollCustomAttribute_1;
    UIInfiniteScrollCustomAttribute.ScrollEventName = 'scroll';
    __decorate([
        bindable,
        __metadata("design:type", Function)
    ], UIInfiniteScrollCustomAttribute.prototype, "callback", void 0);
    __decorate([
        bindable,
        __metadata("design:type", Number)
    ], UIInfiniteScrollCustomAttribute.prototype, "scrollBuffer", void 0);
    __decorate([
        bindable,
        __metadata("design:type", Boolean)
    ], UIInfiniteScrollCustomAttribute.prototype, "isActive", void 0);
    __decorate([
        bindable,
        __metadata("design:type", Boolean)
    ], UIInfiniteScrollCustomAttribute.prototype, "loadOnBottom", void 0);
    UIInfiniteScrollCustomAttribute = UIInfiniteScrollCustomAttribute_1 = __decorate([
        autoinject(),
        customAttribute('ui-infinite-scroll'),
        __metadata("design:paramtypes", [Element])
    ], UIInfiniteScrollCustomAttribute);
    return UIInfiniteScrollCustomAttribute;
}());

//# sourceMappingURL=ui-infinite-scroll.js.map
