UNPKG

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