"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);
};
var core_1 = require("@angular/core");
var typescript_angular_utilities_1 = require("typescript-angular-utilities");
var __guid = typescript_angular_utilities_1.services.guid;
var document_provider_1 = require("../../services/document/document.provider");
var OffClickDirective = (function () {
    function OffClickDirective(guidService, document) {
        var _this = this;
        this.offClick = new core_1.EventEmitter();
        this._active = true;
        this.listener = function ($event) {
            if ($event.rlEventIdentifier !== _this.identifier) {
                _this.offClick.emit($event);
            }
        };
        this.identifier = guidService.random();
        this.document = document;
    }
    Object.defineProperty(OffClickDirective.prototype, "active", {
        set: function (value) {
            if (value) {
                this.addListener();
            }
            else {
                this.removeListener();
            }
            this._active = value;
        },
        enumerable: true,
        configurable: true
    });
    OffClickDirective.prototype.ngOnInit = function () {
        var _this = this;
        if (this._active) {
            setTimeout(function () {
                _this.addListener();
            });
        }
    };
    OffClickDirective.prototype.addListener = function () {
        this.document.addEventListener('click', this.listener);
    };
    OffClickDirective.prototype.removeListener = function () {
        this.document.removeEventListener('click', this.listener);
    };
    OffClickDirective.prototype.ngOnDestroy = function () {
        this.removeListener();
    };
    OffClickDirective.prototype.onClick = function ($event) {
        $event.rlEventIdentifier = this.identifier;
    };
    return OffClickDirective;
}());
__decorate([
    core_1.Output('offClick'),
    __metadata("design:type", core_1.EventEmitter)
], OffClickDirective.prototype, "offClick", void 0);
__decorate([
    core_1.Input('offClickActive'),
    __metadata("design:type", Boolean),
    __metadata("design:paramtypes", [Boolean])
], OffClickDirective.prototype, "active", null);
OffClickDirective = __decorate([
    core_1.Directive({
        selector: '[offClick]',
        host: {
            '(click)': 'onClick($event)',
        },
    }),
    __metadata("design:paramtypes", [__guid.GuidService, document_provider_1.DocumentWrapper])
], OffClickDirective);
exports.OffClickDirective = OffClickDirective;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoib2ZmQ2xpY2suanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyJvZmZDbGljay50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7Ozs7O0FBQUEsc0NBQStHO0FBRS9HLDZFQUF3RDtBQUN4RCxJQUFPLE1BQU0sR0FBRyx1Q0FBUSxDQUFDLElBQUksQ0FBQztBQUU5QiwrRUFBNEU7QUFZNUUsSUFBYSxpQkFBaUI7SUF1QjdCLDJCQUFZLFdBQStCLEVBQ3ZDLFFBQXlCO1FBRDdCLGlCQUlDO1FBMUJtQixhQUFRLEdBQXNCLElBQUksbUJBQVksRUFBRSxDQUFDO1FBRTdELFlBQU8sR0FBWSxJQUFJLENBQUM7UUFXaEMsYUFBUSxHQUFtQyxVQUFDLE1BQXNCO1lBQ2pFLEVBQUUsQ0FBQyxDQUFDLE1BQU0sQ0FBQyxpQkFBaUIsS0FBSyxLQUFJLENBQUMsVUFBVSxDQUFDLENBQUMsQ0FBQztnQkFDbEQsS0FBSSxDQUFDLFFBQVEsQ0FBQyxJQUFJLENBQUMsTUFBTSxDQUFDLENBQUM7WUFDNUIsQ0FBQztRQUNGLENBQUMsQ0FBQztRQU9ELElBQUksQ0FBQyxVQUFVLEdBQUcsV0FBVyxDQUFDLE1BQU0sRUFBRSxDQUFDO1FBQ3ZDLElBQUksQ0FBQyxRQUFRLEdBQVEsUUFBUSxDQUFDO0lBQy9CLENBQUM7SUF2QndCLHNCQUFJLHFDQUFNO2FBQVYsVUFBVyxLQUFjO1lBR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