tooltip.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. Object.defineProperty(exports, "__esModule", { value: true });
  12. var core_1 = require("@angular/core");
  13. var common_1 = require("@angular/common");
  14. var domhandler_1 = require("../dom/domhandler");
  15. var Tooltip = /** @class */ (function () {
  16. function Tooltip(el, zone) {
  17. this.el = el;
  18. this.zone = zone;
  19. this.tooltipPosition = 'right';
  20. this.tooltipEvent = 'hover';
  21. this.appendTo = 'body';
  22. this.tooltipZIndex = 'auto';
  23. this.escape = true;
  24. }
  25. Tooltip.prototype.ngAfterViewInit = function () {
  26. var _this = this;
  27. this.zone.runOutsideAngular(function () {
  28. if (_this.tooltipEvent === 'hover') {
  29. _this.mouseEnterListener = _this.onMouseEnter.bind(_this);
  30. _this.mouseLeaveListener = _this.onMouseLeave.bind(_this);
  31. _this.clickListener = _this.onClick.bind(_this);
  32. _this.el.nativeElement.addEventListener('mouseenter', _this.mouseEnterListener);
  33. _this.el.nativeElement.addEventListener('mouseleave', _this.mouseLeaveListener);
  34. _this.el.nativeElement.addEventListener('click', _this.clickListener);
  35. }
  36. else if (_this.tooltipEvent === 'focus') {
  37. _this.focusListener = _this.onFocus.bind(_this);
  38. _this.blurListener = _this.onBlur.bind(_this);
  39. _this.el.nativeElement.addEventListener('focus', _this.focusListener);
  40. _this.el.nativeElement.addEventListener('blur', _this.blurListener);
  41. }
  42. });
  43. };
  44. Tooltip.prototype.onMouseEnter = function (e) {
  45. if (!this.container && !this.showTimeout) {
  46. this.activate();
  47. }
  48. };
  49. Tooltip.prototype.onMouseLeave = function (e) {
  50. this.deactivate();
  51. };
  52. Tooltip.prototype.onFocus = function (e) {
  53. this.activate();
  54. };
  55. Tooltip.prototype.onBlur = function (e) {
  56. this.deactivate();
  57. };
  58. Tooltip.prototype.onClick = function (e) {
  59. this.deactivate();
  60. };
  61. Tooltip.prototype.activate = function () {
  62. var _this = this;
  63. this.active = true;
  64. this.clearHideTimeout();
  65. if (this.showDelay)
  66. this.showTimeout = setTimeout(function () { _this.show(); }, this.showDelay);
  67. else
  68. this.show();
  69. if (this.life) {
  70. var duration = this.showDelay ? this.life + this.showDelay : this.life;
  71. this.hideTimeout = setTimeout(function () { _this.hide(); }, duration);
  72. }
  73. };
  74. Tooltip.prototype.deactivate = function () {
  75. var _this = this;
  76. this.active = false;
  77. this.clearShowTimeout();
  78. if (this.hideDelay) {
  79. this.clearHideTimeout(); //life timeout
  80. this.hideTimeout = setTimeout(function () { _this.hide(); }, this.hideDelay);
  81. }
  82. else {
  83. this.hide();
  84. }
  85. };
  86. Object.defineProperty(Tooltip.prototype, "text", {
  87. get: function () {
  88. return this._text;
  89. },
  90. set: function (text) {
  91. this._text = text;
  92. if (this.active) {
  93. if (this._text) {
  94. if (this.container && this.container.offsetParent)
  95. this.updateText();
  96. else
  97. this.show();
  98. }
  99. else {
  100. this.hide();
  101. }
  102. }
  103. },
  104. enumerable: true,
  105. configurable: true
  106. });
  107. Tooltip.prototype.create = function () {
  108. this.container = document.createElement('div');
  109. var tooltipArrow = document.createElement('div');
  110. tooltipArrow.className = 'ui-tooltip-arrow';
  111. this.container.appendChild(tooltipArrow);
  112. this.tooltipText = document.createElement('div');
  113. this.tooltipText.className = 'ui-tooltip-text ui-shadow ui-corner-all';
  114. this.updateText();
  115. if (this.positionStyle) {
  116. this.container.style.position = this.positionStyle;
  117. }
  118. this.container.appendChild(this.tooltipText);
  119. if (this.appendTo === 'body')
  120. document.body.appendChild(this.container);
  121. else if (this.appendTo === 'target')
  122. domhandler_1.DomHandler.appendChild(this.container, this.el.nativeElement);
  123. else
  124. domhandler_1.DomHandler.appendChild(this.container, this.appendTo);
  125. this.container.style.display = 'inline-block';
  126. };
  127. Tooltip.prototype.show = function () {
  128. if (!this.text || this.disabled) {
  129. return;
  130. }
  131. this.create();
  132. this.align();
  133. domhandler_1.DomHandler.fadeIn(this.container, 250);
  134. if (this.tooltipZIndex === 'auto')
  135. this.container.style.zIndex = ++domhandler_1.DomHandler.zindex;
  136. else
  137. this.container.style.zIndex = this.tooltipZIndex;
  138. this.bindDocumentResizeListener();
  139. };
  140. Tooltip.prototype.hide = function () {
  141. this.remove();
  142. };
  143. Tooltip.prototype.updateText = function () {
  144. if (this.escape) {
  145. this.tooltipText.innerHTML = '';
  146. this.tooltipText.appendChild(document.createTextNode(this._text));
  147. }
  148. else {
  149. this.tooltipText.innerHTML = this._text;
  150. }
  151. };
  152. Tooltip.prototype.align = function () {
  153. var position = this.tooltipPosition;
  154. switch (position) {
  155. case 'top':
  156. this.alignTop();
  157. if (this.isOutOfBounds()) {
  158. this.alignBottom();
  159. if (this.isOutOfBounds()) {
  160. this.alignRight();
  161. if (this.isOutOfBounds()) {
  162. this.alignLeft();
  163. }
  164. }
  165. }
  166. break;
  167. case 'bottom':
  168. this.alignBottom();
  169. if (this.isOutOfBounds()) {
  170. this.alignTop();
  171. if (this.isOutOfBounds()) {
  172. this.alignRight();
  173. if (this.isOutOfBounds()) {
  174. this.alignLeft();
  175. }
  176. }
  177. }
  178. break;
  179. case 'left':
  180. this.alignLeft();
  181. if (this.isOutOfBounds()) {
  182. this.alignRight();
  183. if (this.isOutOfBounds()) {
  184. this.alignTop();
  185. if (this.isOutOfBounds()) {
  186. this.alignBottom();
  187. }
  188. }
  189. }
  190. break;
  191. case 'right':
  192. this.alignRight();
  193. if (this.isOutOfBounds()) {
  194. this.alignLeft();
  195. if (this.isOutOfBounds()) {
  196. this.alignTop();
  197. if (this.isOutOfBounds()) {
  198. this.alignBottom();
  199. }
  200. }
  201. }
  202. break;
  203. }
  204. };
  205. Tooltip.prototype.getHostOffset = function () {
  206. if (this.appendTo === 'body' || this.appendTo === 'target') {
  207. var offset = this.el.nativeElement.getBoundingClientRect();
  208. var targetLeft = offset.left + domhandler_1.DomHandler.getWindowScrollLeft();
  209. var targetTop = offset.top + domhandler_1.DomHandler.getWindowScrollTop();
  210. return { left: targetLeft, top: targetTop };
  211. }
  212. else {
  213. return { left: 0, top: 0 };
  214. }
  215. };
  216. Tooltip.prototype.alignRight = function () {
  217. this.preAlign('right');
  218. var hostOffset = this.getHostOffset();
  219. var left = hostOffset.left + domhandler_1.DomHandler.getOuterWidth(this.el.nativeElement);
  220. var top = hostOffset.top + (domhandler_1.DomHandler.getOuterHeight(this.el.nativeElement) - domhandler_1.DomHandler.getOuterHeight(this.container)) / 2;
  221. this.container.style.left = left + 'px';
  222. this.container.style.top = top + 'px';
  223. };
  224. Tooltip.prototype.alignLeft = function () {
  225. this.preAlign('left');
  226. var hostOffset = this.getHostOffset();
  227. var left = hostOffset.left - domhandler_1.DomHandler.getOuterWidth(this.container);
  228. var top = hostOffset.top + (domhandler_1.DomHandler.getOuterHeight(this.el.nativeElement) - domhandler_1.DomHandler.getOuterHeight(this.container)) / 2;
  229. this.container.style.left = left + 'px';
  230. this.container.style.top = top + 'px';
  231. };
  232. Tooltip.prototype.alignTop = function () {
  233. this.preAlign('top');
  234. var hostOffset = this.getHostOffset();
  235. var left = hostOffset.left + (domhandler_1.DomHandler.getOuterWidth(this.el.nativeElement) - domhandler_1.DomHandler.getOuterWidth(this.container)) / 2;
  236. var top = hostOffset.top - domhandler_1.DomHandler.getOuterHeight(this.container);
  237. this.container.style.left = left + 'px';
  238. this.container.style.top = top + 'px';
  239. };
  240. Tooltip.prototype.alignBottom = function () {
  241. this.preAlign('bottom');
  242. var hostOffset = this.getHostOffset();
  243. var left = hostOffset.left + (domhandler_1.DomHandler.getOuterWidth(this.el.nativeElement) - domhandler_1.DomHandler.getOuterWidth(this.container)) / 2;
  244. var top = hostOffset.top + domhandler_1.DomHandler.getOuterHeight(this.el.nativeElement);
  245. this.container.style.left = left + 'px';
  246. this.container.style.top = top + 'px';
  247. };
  248. Tooltip.prototype.preAlign = function (position) {
  249. this.container.style.left = -999 + 'px';
  250. this.container.style.top = -999 + 'px';
  251. var defaultClassName = 'ui-tooltip ui-widget ui-tooltip-' + position;
  252. this.container.className = this.tooltipStyleClass ? defaultClassName + ' ' + this.tooltipStyleClass : defaultClassName;
  253. };
  254. Tooltip.prototype.isOutOfBounds = function () {
  255. var offset = this.container.getBoundingClientRect();
  256. var targetTop = offset.top;
  257. var targetLeft = offset.left;
  258. var width = domhandler_1.DomHandler.getOuterWidth(this.container);
  259. var height = domhandler_1.DomHandler.getOuterHeight(this.container);
  260. var viewport = domhandler_1.DomHandler.getViewport();
  261. return (targetLeft + width > viewport.width) || (targetLeft < 0) || (targetTop < 0) || (targetTop + height > viewport.height);
  262. };
  263. Tooltip.prototype.onWindowResize = function (e) {
  264. this.hide();
  265. };
  266. Tooltip.prototype.bindDocumentResizeListener = function () {
  267. var _this = this;
  268. this.zone.runOutsideAngular(function () {
  269. _this.resizeListener = _this.onWindowResize.bind(_this);
  270. window.addEventListener('resize', _this.resizeListener);
  271. });
  272. };
  273. Tooltip.prototype.unbindDocumentResizeListener = function () {
  274. if (this.resizeListener) {
  275. window.removeEventListener('resize', this.resizeListener);
  276. this.resizeListener = null;
  277. }
  278. };
  279. Tooltip.prototype.unbindEvents = function () {
  280. if (this.tooltipEvent === 'hover') {
  281. this.el.nativeElement.removeEventListener('mouseenter', this.mouseEnterListener);
  282. this.el.nativeElement.removeEventListener('mouseleave', this.mouseLeaveListener);
  283. this.el.nativeElement.removeEventListener('click', this.clickListener);
  284. }
  285. else if (this.tooltipEvent === 'focus') {
  286. this.el.nativeElement.removeEventListener('focus', this.focusListener);
  287. this.el.nativeElement.removeEventListener('blur', this.blurListener);
  288. }
  289. this.unbindDocumentResizeListener();
  290. };
  291. Tooltip.prototype.remove = function () {
  292. if (this.container && this.container.parentElement) {
  293. if (this.appendTo === 'body')
  294. document.body.removeChild(this.container);
  295. else if (this.appendTo === 'target')
  296. this.el.nativeElement.removeChild(this.container);
  297. else
  298. domhandler_1.DomHandler.removeChild(this.container, this.appendTo);
  299. }
  300. this.unbindDocumentResizeListener();
  301. this.clearTimeouts();
  302. this.container = null;
  303. };
  304. Tooltip.prototype.clearShowTimeout = function () {
  305. if (this.showTimeout) {
  306. clearTimeout(this.showTimeout);
  307. this.showTimeout = null;
  308. }
  309. };
  310. Tooltip.prototype.clearHideTimeout = function () {
  311. if (this.hideTimeout) {
  312. clearTimeout(this.hideTimeout);
  313. this.hideTimeout = null;
  314. }
  315. };
  316. Tooltip.prototype.clearTimeouts = function () {
  317. this.clearShowTimeout();
  318. this.clearHideTimeout();
  319. };
  320. Tooltip.prototype.ngOnDestroy = function () {
  321. this.unbindEvents();
  322. this.remove();
  323. };
  324. __decorate([
  325. core_1.Input(),
  326. __metadata("design:type", String)
  327. ], Tooltip.prototype, "tooltipPosition", void 0);
  328. __decorate([
  329. core_1.Input(),
  330. __metadata("design:type", String)
  331. ], Tooltip.prototype, "tooltipEvent", void 0);
  332. __decorate([
  333. core_1.Input(),
  334. __metadata("design:type", Object)
  335. ], Tooltip.prototype, "appendTo", void 0);
  336. __decorate([
  337. core_1.Input(),
  338. __metadata("design:type", String)
  339. ], Tooltip.prototype, "positionStyle", void 0);
  340. __decorate([
  341. core_1.Input(),
  342. __metadata("design:type", String)
  343. ], Tooltip.prototype, "tooltipStyleClass", void 0);
  344. __decorate([
  345. core_1.Input(),
  346. __metadata("design:type", String)
  347. ], Tooltip.prototype, "tooltipZIndex", void 0);
  348. __decorate([
  349. core_1.Input("tooltipDisabled"),
  350. __metadata("design:type", Boolean)
  351. ], Tooltip.prototype, "disabled", void 0);
  352. __decorate([
  353. core_1.Input(),
  354. __metadata("design:type", Boolean)
  355. ], Tooltip.prototype, "escape", void 0);
  356. __decorate([
  357. core_1.Input(),
  358. __metadata("design:type", Number)
  359. ], Tooltip.prototype, "showDelay", void 0);
  360. __decorate([
  361. core_1.Input(),
  362. __metadata("design:type", Number)
  363. ], Tooltip.prototype, "hideDelay", void 0);
  364. __decorate([
  365. core_1.Input(),
  366. __metadata("design:type", Number)
  367. ], Tooltip.prototype, "life", void 0);
  368. __decorate([
  369. core_1.Input('pTooltip'),
  370. __metadata("design:type", String),
  371. __metadata("design:paramtypes", [String])
  372. ], Tooltip.prototype, "text", null);
  373. Tooltip = __decorate([
  374. core_1.Directive({
  375. selector: '[pTooltip]'
  376. }),
  377. __metadata("design:paramtypes", [core_1.ElementRef, core_1.NgZone])
  378. ], Tooltip);
  379. return Tooltip;
  380. }());
  381. exports.Tooltip = Tooltip;
  382. var TooltipModule = /** @class */ (function () {
  383. function TooltipModule() {
  384. }
  385. TooltipModule = __decorate([
  386. core_1.NgModule({
  387. imports: [common_1.CommonModule],
  388. exports: [Tooltip],
  389. declarations: [Tooltip]
  390. })
  391. ], TooltipModule);
  392. return TooltipModule;
  393. }());
  394. exports.TooltipModule = TooltipModule;
  395. //# sourceMappingURL=tooltip.js.map