text: "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 core_1 = require("@angular/core");
  var common_1 = require("@angular/common");
  var domhandler_1 = require("../dom/domhandler");
  var Tooltip = /** @class */ (function () {
      function Tooltip(el, domHandler, renderer) {
          this.el = el;
          this.domHandler = domHandler;
          this.renderer = renderer;
          this.tooltipPosition = 'right';
          this.tooltipEvent = 'hover';
          this.appendTo = 'body';
          this.tooltipZIndex = 'auto';
          this.escape = true;
      }
      Tooltip.prototype.onMouseEnter = function (e) {
          if (this.tooltipEvent === 'hover') {
              if (this.hideTimeout) {
                  clearTimeout(this.hideTimeout);
                  this.destroy();
              }
              this.activate();
          }
      };
      Tooltip.prototype.onMouseLeave = function (e) {
          if (this.tooltipEvent === 'hover') {
              this.deactivate(true);
          }
      };
      Tooltip.prototype.onFocus = function (e) {
          if (this.tooltipEvent === 'focus') {
              this.activate();
          }
      };
      Tooltip.prototype.onBlur = function (e) {
          if (this.tooltipEvent === 'focus') {
              this.deactivate(true);
          }
      };
      Tooltip.prototype.onClick = function (e) {
          if (this.tooltipEvent === 'hover') {
              this.deactivate(true);
          }
      };
      Tooltip.prototype.activate = function () {
          var _this = this;
          this.active = true;
          if (this.hideTimeout) {
              clearTimeout(this.hideTimeout);
          }
          if (this.showDelay)
              this.showTimeout = setTimeout(function () { _this.show(); }, this.showDelay);
          else
              this.show();
          if (this.life) {
              this.lifeTimeout = setTimeout(function () { _this.deactivate(false); }, this.life);
          }
      };
      Tooltip.prototype.deactivate = function (useDelay) {
          var _this = this;
          this.active = false;
          if (this.showTimeout) {
              clearTimeout(this.showTimeout);
          }
          if (this.lifeTimeout) {
              clearTimeout(this.lifeTimeout);
          }
          if (this.hideDelay && useDelay)
              this.hideTimeout = setTimeout(function () { _this.hide(); }, this.hideDelay);
          else
              this.hide();
      };
      Object.defineProperty(Tooltip.prototype, "text", {
          get: function () {
              return this._text;
          },
          set: function (text) {
              this._text = text;
              if (this.active) {
                  if (this._text) {
                      if (this.container && this.container.offsetParent)
                          this.updateText();
                      else
                          this.show();
                  }
                  else {
                      this.hide();
                  }
              }
          },
          enumerable: true,
          configurable: true
      });
      Tooltip.prototype.create = function () {
          this.container = document.createElement('div');
          var tooltipArrow = document.createElement('div');
          tooltipArrow.className = 'ui-tooltip-arrow';
          this.container.appendChild(tooltipArrow);
          this.tooltipText = document.createElement('div');
          this.tooltipText.className = 'ui-tooltip-text ui-shadow ui-corner-all';
          this.updateText();
          if (this.positionStyle) {
              this.container.style.position = this.positionStyle;
          }
          this.container.appendChild(this.tooltipText);
          if (this.appendTo === 'body')
              document.body.appendChild(this.container);
          else if (this.appendTo === 'target')
              this.domHandler.appendChild(this.container, this.el.nativeElement);
          else
              this.domHandler.appendChild(this.container, this.appendTo);
          this.container.style.display = 'inline-block';
      };
      Tooltip.prototype.show = function () {
          if (!this.text || this.disabled) {
              return;
          }
          this.create();
          this.align();
          if (this.tooltipStyleClass) {
              this.container.className = this.container.className + ' ' + this.tooltipStyleClass;
          }
          this.domHandler.fadeIn(this.container, 250);
          if (this.tooltipZIndex === 'auto')
              this.container.style.zIndex = ++domhandler_1.DomHandler.zindex;
          else
              this.container.style.zIndex = this.tooltipZIndex;
          this.bindDocumentResizeListener();
      };
      Tooltip.prototype.hide = function () {
          this.destroy();
      };
      Tooltip.prototype.updateText = function () {
          if (this.escape) {
              this.tooltipText.innerHTML = '';
              this.tooltipText.appendChild(document.createTextNode(this._text));
          }
          else {
              this.tooltipText.innerHTML = this._text;
          }
      };
      Tooltip.prototype.align = function () {
          var position = this.tooltipPosition;
          switch (position) {
              case 'top':
                  this.alignTop();
                  if (this.isOutOfBounds()) {
                      this.alignBottom();
                  }
                  break;
              case 'bottom':
                  this.alignBottom();
                  if (this.isOutOfBounds()) {
                      this.alignTop();
                  }
                  break;
              case 'left':
                  this.alignLeft();
                  if (this.isOutOfBounds()) {
                      this.alignRight();
                      if (this.isOutOfBounds()) {
                          this.alignTop();
                          if (this.isOutOfBounds()) {
                              this.alignBottom();
                          }
                      }
                  }
                  break;
              case 'right':
                  this.alignRight();
                  if (this.isOutOfBounds()) {
                      this.alignLeft();
                      if (this.isOutOfBounds()) {
                          this.alignTop();
                          if (this.isOutOfBounds()) {
                              this.alignBottom();
                          }
                      }
                  }
                  break;
          }
      };
      Tooltip.prototype.getHostOffset = function () {
          var offset = this.el.nativeElement.getBoundingClientRect();
          var targetLeft = offset.left + this.domHandler.getWindowScrollLeft();
          var targetTop = offset.top + this.domHandler.getWindowScrollTop();
          return { left: targetLeft, top: targetTop };
      };
      Tooltip.prototype.alignRight = function () {
          this.preAlign();
          this.container.className = 'ui-tooltip ui-widget ui-tooltip-right';
          var hostOffset = this.getHostOffset();
          var left = hostOffset.left + this.domHandler.getOuterWidth(this.el.nativeElement);
          var top = hostOffset.top + (this.domHandler.getOuterHeight(this.el.nativeElement) - this.domHandler.getOuterHeight(this.container)) / 2;
          this.container.style.left = left + 'px';
          this.container.style.top = top + 'px';
      };
      Tooltip.prototype.alignLeft = function () {
          this.preAlign();
          this.container.className = 'ui-tooltip ui-widget ui-tooltip-left';
          var hostOffset = this.getHostOffset();
          var left = hostOffset.left - this.domHandler.getOuterWidth(this.container);
          var top = hostOffset.top + (this.domHandler.getOuterHeight(this.el.nativeElement) - this.domHandler.getOuterHeight(this.container)) / 2;
          this.container.style.left = left + 'px';
          this.container.style.top = top + 'px';
      };
      Tooltip.prototype.alignTop = function () {
          this.preAlign();
          this.container.className = 'ui-tooltip ui-widget ui-tooltip-top';
          var hostOffset = this.getHostOffset();
          var left = hostOffset.left + (this.domHandler.getOuterWidth(this.el.nativeElement) - this.domHandler.getOuterWidth(this.container)) / 2;
          var top = hostOffset.top - this.domHandler.getOuterHeight(this.container);
          this.container.style.left = left + 'px';
          this.container.style.top = top + 'px';
      };
      Tooltip.prototype.alignBottom = function () {
          this.preAlign();
          this.container.className = 'ui-tooltip ui-widget ui-tooltip-bottom';
          var hostOffset = this.getHostOffset();
          var left = hostOffset.left + (this.domHandler.getOuterWidth(this.el.nativeElement) - this.domHandler.getOuterWidth(this.container)) / 2;
          var top = hostOffset.top + this.domHandler.getOuterHeight(this.el.nativeElement);
          this.container.style.left = left + 'px';
          this.container.style.top = top + 'px';
      };
      Tooltip.prototype.preAlign = function () {
          this.container.style.left = -999 + 'px';
          this.container.style.top = -999 + 'px';
      };
      Tooltip.prototype.isOutOfBounds = function () {
          var offset = this.container.getBoundingClientRect();
          var targetTop = offset.top;
          var targetLeft = offset.left;
          var width = this.domHandler.getOuterWidth(this.container);
          var height = this.domHandler.getOuterHeight(this.container);
          var viewport = this.domHandler.getViewport();
          return (targetLeft + width > viewport.width) || (targetLeft < 0) || (targetTop < 0) || (targetTop + height > viewport.height);
      };
      Tooltip.prototype.bindDocumentResizeListener = function () {
          var _this = this;
          this.documentResizeListener = this.renderer.listen('window', 'resize', function (event) {
              _this.hide();
          });
      };
      Tooltip.prototype.unbindDocumentResizeListener = function () {
          if (this.documentResizeListener) {
              this.documentResizeListener();
              this.documentResizeListener = null;
          }
      };
      Tooltip.prototype.destroy = function () {
          this.unbindDocumentResizeListener();
          if (this.container && this.container.parentElement) {
              if (this.appendTo === 'body')
                  document.body.removeChild(this.container);
              else if (this.appendTo === 'target')
                  this.el.nativeElement.removeChild(this.container);
              else
                  this.domHandler.removeChild(this.container, this.appendTo);
          }
          this.container = null;
      };
      Tooltip.prototype.ngOnDestroy = function () {
          this.destroy();
      };
      __decorate([
          core_1.Input(),
          __metadata("design:type", String)
      ], Tooltip.prototype, "tooltipPosition", void 0);
      __decorate([
          core_1.Input(),
          __metadata("design:type", String)
      ], Tooltip.prototype, "tooltipEvent", void 0);
      __decorate([
          core_1.Input(),
          __metadata("design:type", Object)
      ], Tooltip.prototype, "appendTo", void 0);
      __decorate([
          core_1.Input(),
          __metadata("design:type", String)
      ], Tooltip.prototype, "positionStyle", void 0);
      __decorate([
          core_1.Input(),
          __metadata("design:type", String)
      ], Tooltip.prototype, "tooltipStyleClass", void 0);
      __decorate([
          core_1.Input(),
          __metadata("design:type", String)
      ], Tooltip.prototype, "tooltipZIndex", void 0);
      __decorate([
          core_1.Input("tooltipDisabled"),
          __metadata("design:type", Boolean)
      ], Tooltip.prototype, "disabled", void 0);
      __decorate([
          core_1.Input(),
          __metadata("design:type", Boolean)
      ], Tooltip.prototype, "escape", void 0);
      __decorate([
          core_1.Input(),
          __metadata("design:type", Number)
      ], Tooltip.prototype, "showDelay", void 0);
      __decorate([
          core_1.Input(),
          __metadata("design:type", Number)
      ], Tooltip.prototype, "hideDelay", void 0);
      __decorate([
          core_1.Input(),
          __metadata("design:type", Number)
      ], Tooltip.prototype, "life", void 0);
      __decorate([
          core_1.HostListener('mouseenter', ['$event']),
          __metadata("design:type", Function),
          __metadata("design:paramtypes", [Event]),
          __metadata("design:returntype", void 0)
      ], Tooltip.prototype, "onMouseEnter", null);
      __decorate([
          core_1.HostListener('mouseleave', ['$event']),
          __metadata("design:type", Function),
          __metadata("design:paramtypes", [Event]),
          __metadata("design:returntype", void 0)
      ], Tooltip.prototype, "onMouseLeave", null);
      __decorate([
          core_1.HostListener('focus', ['$event']),
          __metadata("design:type", Function),
          __metadata("design:paramtypes", [Event]),
          __metadata("design:returntype", void 0)
      ], Tooltip.prototype, "onFocus", null);
      __decorate([
          core_1.HostListener('blur', ['$event']),
          __metadata("design:type", Function),
          __metadata("design:paramtypes", [Event]),
          __metadata("design:returntype", void 0)
      ], Tooltip.prototype, "onBlur", null);
      __decorate([
          core_1.HostListener('click', ['$event']),
          __metadata("design:type", Function),
          __metadata("design:paramtypes", [Event]),
          __metadata("design:returntype", void 0)
      ], Tooltip.prototype, "onClick", null);
      __decorate([
          core_1.Input('pTooltip'),
          __metadata("design:type", String),
          __metadata("design:paramtypes", [String])
      ], Tooltip.prototype, "text", null);
      Tooltip = __decorate([
          core_1.Directive({
              selector: '[pTooltip]',
              providers: [domhandler_1.DomHandler]
          }),
          __metadata("design:paramtypes", [core_1.ElementRef, domhandler_1.DomHandler, core_1.Renderer2])
      ], Tooltip);
      return Tooltip;
  }());
  exports.Tooltip = Tooltip;
  var TooltipModule = /** @class */ (function () {
      function TooltipModule() {
      }
      TooltipModule = __decorate([
          core_1.NgModule({
              imports: [common_1.CommonModule],
              exports: [Tooltip],
              declarations: [Tooltip]
          })
      ], TooltipModule);
      return TooltipModule;
  }());
  exports.TooltipModule = TooltipModule;
  //# sourceMappingURL=tooltip.js.map