TooltipContent.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. "use strict";
  2. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  3. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  4. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  5. 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;
  6. return c > 3 && r && Object.defineProperty(target, key, r), r;
  7. };
  8. var __metadata = (this && this.__metadata) || function (k, v) {
  9. if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
  10. };
  11. var core_1 = require("@angular/core");
  12. var TooltipContent = (function () {
  13. // -------------------------------------------------------------------------
  14. // Constructor
  15. // -------------------------------------------------------------------------
  16. function TooltipContent(element, cdr) {
  17. this.element = element;
  18. this.cdr = cdr;
  19. this.placement = "bottom";
  20. this.animation = true;
  21. // -------------------------------------------------------------------------
  22. // Properties
  23. // -------------------------------------------------------------------------
  24. this.top = -100000;
  25. this.left = -100000;
  26. this.isIn = false;
  27. this.isFade = false;
  28. }
  29. // -------------------------------------------------------------------------
  30. // Lifecycle callbacks
  31. // -------------------------------------------------------------------------
  32. TooltipContent.prototype.ngAfterViewInit = function () {
  33. this.show();
  34. this.cdr.detectChanges();
  35. };
  36. // -------------------------------------------------------------------------
  37. // Public Methods
  38. // -------------------------------------------------------------------------
  39. TooltipContent.prototype.show = function () {
  40. if (!this.hostElement)
  41. return;
  42. var p = this.positionElements(this.hostElement, this.element.nativeElement.children[0], this.placement);
  43. this.top = p.top;
  44. this.left = p.left;
  45. this.isIn = true;
  46. if (this.animation)
  47. this.isFade = true;
  48. };
  49. TooltipContent.prototype.hide = function () {
  50. this.top = -100000;
  51. this.left = -100000;
  52. this.isIn = true;
  53. if (this.animation)
  54. this.isFade = false;
  55. };
  56. // -------------------------------------------------------------------------
  57. // Private Methods
  58. // -------------------------------------------------------------------------
  59. TooltipContent.prototype.positionElements = function (hostEl, targetEl, positionStr, appendToBody) {
  60. if (appendToBody === void 0) { appendToBody = false; }
  61. var positionStrParts = positionStr.split("-");
  62. var pos0 = positionStrParts[0];
  63. var pos1 = positionStrParts[1] || "center";
  64. var hostElPos = appendToBody ? this.offset(hostEl) : this.position(hostEl);
  65. var targetElWidth = targetEl.offsetWidth;
  66. var targetElHeight = targetEl.offsetHeight;
  67. var shiftWidth = {
  68. center: function () {
  69. return hostElPos.left + hostElPos.width / 2 - targetElWidth / 2;
  70. },
  71. left: function () {
  72. return hostElPos.left;
  73. },
  74. right: function () {
  75. return hostElPos.left + hostElPos.width;
  76. }
  77. };
  78. var shiftHeight = {
  79. center: function () {
  80. return hostElPos.top + hostElPos.height / 2 - targetElHeight / 2;
  81. },
  82. top: function () {
  83. return hostElPos.top;
  84. },
  85. bottom: function () {
  86. return hostElPos.top + hostElPos.height;
  87. }
  88. };
  89. var targetElPos;
  90. switch (pos0) {
  91. case "right":
  92. targetElPos = {
  93. top: shiftHeight[pos1](),
  94. left: shiftWidth[pos0]()
  95. };
  96. break;
  97. case "left":
  98. targetElPos = {
  99. top: shiftHeight[pos1](),
  100. left: hostElPos.left - targetElWidth
  101. };
  102. break;
  103. case "bottom":
  104. targetElPos = {
  105. top: shiftHeight[pos0](),
  106. left: shiftWidth[pos1]()
  107. };
  108. break;
  109. default:
  110. targetElPos = {
  111. top: hostElPos.top - targetElHeight,
  112. left: shiftWidth[pos1]()
  113. };
  114. break;
  115. }
  116. return targetElPos;
  117. };
  118. TooltipContent.prototype.position = function (nativeEl) {
  119. var offsetParentBCR = { top: 0, left: 0 };
  120. var elBCR = this.offset(nativeEl);
  121. var offsetParentEl = this.parentOffsetEl(nativeEl);
  122. if (offsetParentEl !== window.document) {
  123. offsetParentBCR = this.offset(offsetParentEl);
  124. offsetParentBCR.top += offsetParentEl.clientTop - offsetParentEl.scrollTop;
  125. offsetParentBCR.left += offsetParentEl.clientLeft - offsetParentEl.scrollLeft;
  126. }
  127. var boundingClientRect = nativeEl.getBoundingClientRect();
  128. return {
  129. width: boundingClientRect.width || nativeEl.offsetWidth,
  130. height: boundingClientRect.height || nativeEl.offsetHeight,
  131. top: elBCR.top - offsetParentBCR.top,
  132. left: elBCR.left - offsetParentBCR.left
  133. };
  134. };
  135. TooltipContent.prototype.offset = function (nativeEl) {
  136. var boundingClientRect = nativeEl.getBoundingClientRect();
  137. return {
  138. width: boundingClientRect.width || nativeEl.offsetWidth,
  139. height: boundingClientRect.height || nativeEl.offsetHeight,
  140. top: boundingClientRect.top + (window.pageYOffset || window.document.documentElement.scrollTop),
  141. left: boundingClientRect.left + (window.pageXOffset || window.document.documentElement.scrollLeft)
  142. };
  143. };
  144. TooltipContent.prototype.getStyle = function (nativeEl, cssProp) {
  145. if (nativeEl.currentStyle)
  146. return nativeEl.currentStyle[cssProp];
  147. if (window.getComputedStyle)
  148. return window.getComputedStyle(nativeEl)[cssProp];
  149. // finally try and get inline style
  150. return nativeEl.style[cssProp];
  151. };
  152. TooltipContent.prototype.isStaticPositioned = function (nativeEl) {
  153. return (this.getStyle(nativeEl, "position") || "static") === "static";
  154. };
  155. TooltipContent.prototype.parentOffsetEl = function (nativeEl) {
  156. var offsetParent = nativeEl.offsetParent || window.document;
  157. while (offsetParent && offsetParent !== window.document && this.isStaticPositioned(offsetParent)) {
  158. offsetParent = offsetParent.offsetParent;
  159. }
  160. return offsetParent || window.document;
  161. };
  162. return TooltipContent;
  163. }());
  164. __decorate([
  165. core_1.Input(),
  166. __metadata("design:type", HTMLElement)
  167. ], TooltipContent.prototype, "hostElement", void 0);
  168. __decorate([
  169. core_1.Input(),
  170. __metadata("design:type", String)
  171. ], TooltipContent.prototype, "content", void 0);
  172. __decorate([
  173. core_1.Input(),
  174. __metadata("design:type", String)
  175. ], TooltipContent.prototype, "placement", void 0);
  176. __decorate([
  177. core_1.Input(),
  178. __metadata("design:type", Boolean)
  179. ], TooltipContent.prototype, "animation", void 0);
  180. TooltipContent = __decorate([
  181. core_1.Component({
  182. selector: "tooltip-content",
  183. template: "\n<div class=\"tooltip {{ placement }}\"\n [style.top]=\"top + 'px'\"\n [style.left]=\"left + 'px'\"\n [class.in]=\"isIn\"\n [class.fade]=\"isFade\"\n role=\"tooltip\">\n <div class=\"tooltip-arrow\"></div> \n <div class=\"tooltip-inner\">\n <ng-content></ng-content>\n {{ content }}\n </div> \n</div>\n"
  184. }),
  185. __metadata("design:paramtypes", [core_1.ElementRef,
  186. core_1.ChangeDetectorRef])
  187. ], TooltipContent);
  188. exports.TooltipContent = TooltipContent;
  189. //# sourceMappingURL=TooltipContent.js.map