UNPKG

ng-zorro-antd

Version:

An enterprise-class UI components based on Ant Design and Angular

577 lines (570 loc) 19.8 kB
import { __spread } from 'tslib'; import { BehaviorSubject, Subscription } from 'rxjs'; import { distinctUntilChanged } from 'rxjs/operators'; import { OverlayModule } from '@angular/cdk/overlay'; import { CommonModule } from '@angular/common'; import { ChangeDetectionStrategy, ChangeDetectorRef, Component, ContentChild, EventEmitter, Host, Input, Optional, Output, TemplateRef, ViewChild, ViewEncapsulation, ComponentFactoryResolver, Directive, ElementRef, Renderer2, ViewContainerRef, NgModule } from '@angular/core'; import { getPlacementName, isNotNil, toBoolean, zoomBigMotion, DEFAULT_TOOLTIP_POSITIONS, NzNoAnimationDirective, POSITION_MAP, NzAddOnModule, NzNoAnimationModule, NzOverlayModule } from 'ng-zorro-antd/core'; /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ var NzToolTipComponent = /** @class */ (function () { function NzToolTipComponent(cdr, noAnimation) { this.cdr = cdr; this.noAnimation = noAnimation; this._hasBackdrop = false; this._prefix = 'ant-tooltip-placement'; this._positions = __spread(DEFAULT_TOOLTIP_POSITIONS); this._classMap = {}; this._placement = 'top'; this._trigger = 'hover'; this.visibleSource = new BehaviorSubject(false); this.visible$ = this.visibleSource.asObservable(); this.nzOverlayClassName = ''; this.nzOverlayStyle = {}; this.nzMouseEnterDelay = 0.15; // second // second this.nzMouseLeaveDelay = 0.1; // second this.nzVisibleChange = new EventEmitter(); } Object.defineProperty(NzToolTipComponent.prototype, "nzVisible", { get: /** * @return {?} */ function () { return this.visibleSource.value; }, set: // second /** * @param {?} value * @return {?} */ function (value) { /** @type {?} */ var visible = toBoolean(value); if (this.visibleSource.value !== visible) { this.visibleSource.next(visible); this.nzVisibleChange.emit(visible); } }, enumerable: true, configurable: true }); Object.defineProperty(NzToolTipComponent.prototype, "nzTrigger", { get: /** * @return {?} */ function () { return this._trigger; }, set: /** * @param {?} value * @return {?} */ function (value) { this._trigger = value; this._hasBackdrop = this._trigger === 'click'; }, enumerable: true, configurable: true }); Object.defineProperty(NzToolTipComponent.prototype, "nzPlacement", { get: /** * @return {?} */ function () { return this._placement; }, set: /** * @param {?} value * @return {?} */ function (value) { if (value !== this._placement) { this._placement = value; this._positions = __spread([POSITION_MAP[this.nzPlacement]], this._positions); } }, enumerable: true, configurable: true }); /** * @return {?} */ NzToolTipComponent.prototype.ngOnChanges = /** * @return {?} */ function () { var _this = this; Promise.resolve().then((/** * @return {?} */ function () { _this.updatePosition(); })); }; // Manually force updating current overlay's position // Manually force updating current overlay's position /** * @return {?} */ NzToolTipComponent.prototype.updatePosition = // Manually force updating current overlay's position /** * @return {?} */ function () { if (this.overlay && this.overlay.overlayRef) { this.overlay.overlayRef.updatePosition(); } }; /** * @param {?} position * @return {?} */ NzToolTipComponent.prototype.onPositionChange = /** * @param {?} position * @return {?} */ function (position) { this.nzPlacement = (/** @type {?} */ (getPlacementName(position))); this.setClassMap(); this.cdr.detectChanges(); // TODO: performance? }; /** * @return {?} */ NzToolTipComponent.prototype.show = /** * @return {?} */ function () { if (!this.isContentEmpty()) { this.nzVisible = true; } }; /** * @return {?} */ NzToolTipComponent.prototype.hide = /** * @return {?} */ function () { this.nzVisible = false; }; /** * @param {?} e * @return {?} */ NzToolTipComponent.prototype._afterVisibilityAnimation = /** * @param {?} e * @return {?} */ function (e) { if (e.toState === 'false' && !this.nzVisible) { this.nzVisibleChange.emit(false); } if (e.toState === 'true' && this.nzVisible) { this.nzVisibleChange.emit(true); } }; /** * @return {?} */ NzToolTipComponent.prototype.setClassMap = /** * @return {?} */ function () { var _a; this._classMap = (_a = {}, _a[this.nzOverlayClassName] = true, _a[this._prefix + "-" + this._placement] = true, _a); }; /** * @param {?} origin * @return {?} */ NzToolTipComponent.prototype.setOverlayOrigin = /** * @param {?} origin * @return {?} */ function (origin) { this.overlayOrigin = origin; }; /** * @protected * @return {?} */ NzToolTipComponent.prototype.isContentEmpty = /** * @protected * @return {?} */ function () { return this.nzTitle instanceof TemplateRef ? false : this.nzTitle === '' || !isNotNil(this.nzTitle); }; NzToolTipComponent.decorators = [ { type: Component, args: [{ selector: 'nz-tooltip', exportAs: 'nzTooltipComponent', changeDetection: ChangeDetectionStrategy.OnPush, encapsulation: ViewEncapsulation.None, animations: [zoomBigMotion], template: "<ng-content></ng-content>\n<ng-template\n #overlay=\"cdkConnectedOverlay\"\n cdkConnectedOverlay\n nzConnectedOverlay\n [cdkConnectedOverlayOrigin]=\"overlayOrigin\"\n [cdkConnectedOverlayOpen]=\"visible$ | async\"\n [cdkConnectedOverlayHasBackdrop]=\"_hasBackdrop\"\n [cdkConnectedOverlayPositions]=\"_positions\"\n (backdropClick)=\"hide()\"\n (detach)=\"hide()\"\n (positionChange)=\"onPositionChange($event)\">\n <div\n class=\"ant-tooltip\"\n [ngClass]=\"_classMap\"\n [ngStyle]=\"nzOverlayStyle\"\n [nzNoAnimation]=\"noAnimation?.nzNoAnimation\"\n [@zoomBigMotion]=\"'active'\"\n (@zoomBigMotion.done)=\"_afterVisibilityAnimation($event)\">\n <div class=\"ant-tooltip-content\">\n <div class=\"ant-tooltip-arrow\"></div>\n <div class=\"ant-tooltip-inner\">\n <ng-container *nzStringTemplateOutlet=\"nzTitle\">{{ nzTitle }}</ng-container>\n </div>\n </div>\n </div>\n</ng-template>", preserveWhitespaces: false, styles: ["\n .ant-tooltip {\n position: relative;\n }\n "] }] } ]; /** @nocollapse */ NzToolTipComponent.ctorParameters = function () { return [ { type: ChangeDetectorRef }, { type: NzNoAnimationDirective, decorators: [{ type: Host }, { type: Optional }] } ]; }; NzToolTipComponent.propDecorators = { overlay: [{ type: ViewChild, args: ['overlay',] }], nzTitle: [{ type: Input }, { type: ContentChild, args: ['nzTemplate',] }], nzOverlayClassName: [{ type: Input }], nzOverlayStyle: [{ type: Input }], nzMouseEnterDelay: [{ type: Input }], nzMouseLeaveDelay: [{ type: Input }], nzVisible: [{ type: Input }], nzTrigger: [{ type: Input }], nzPlacement: [{ type: Input }], nzVisibleChange: [{ type: Output }] }; return NzToolTipComponent; }()); /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ var NzTooltipDirective = /** @class */ (function () { function NzTooltipDirective(elementRef, hostView, resolver, renderer, tooltip, noAnimation) { this.elementRef = elementRef; this.hostView = hostView; this.resolver = resolver; this.renderer = renderer; this.tooltip = tooltip; this.noAnimation = noAnimation; // [NOTE] Here hard coded, and nzTitle used only under NzTooltipDirective currently. this.isTooltipOpen = false; this.isDynamicTooltip = false; // Indicate whether current tooltip is dynamic created this.factory = this.resolver.resolveComponentFactory(NzToolTipComponent); /** * Names of properties that should be proxy to child component. */ this.needProxyProperties = [ 'nzTitle', 'nzContent', 'nzOverlayClassName', 'nzOverlayStyle', 'nzMouseEnterDelay', 'nzMouseLeaveDelay', 'nzVisible', 'nzTrigger', 'nzPlacement' ]; this.subs_ = new Subscription(); this.nzVisibleChange = new EventEmitter(); } Object.defineProperty(NzTooltipDirective.prototype, "setTitle", { set: /** * @param {?} title * @return {?} */ function (title) { this.nzTitle = title; }, enumerable: true, configurable: true }); /** * @param {?} changes * @return {?} */ NzTooltipDirective.prototype.ngOnChanges = /** * @param {?} changes * @return {?} */ function (changes) { this.updateProxies(changes); }; /** * @return {?} */ NzTooltipDirective.prototype.ngOnInit = /** * @return {?} */ function () { var _this = this; // Support faster tooltip mode: <a nz-tooltip="xxx"></a>. [NOTE] Used only under NzTooltipDirective currently. if (!this.tooltip) { /** @type {?} */ var tooltipComponent = this.hostView.createComponent(this.factory); this.tooltip = tooltipComponent.instance; this.tooltip.noAnimation = this.noAnimation; // Remove element when use directive https://github.com/NG-ZORRO/ng-zorro-antd/issues/1967 this.renderer.removeChild(this.renderer.parentNode(this.elementRef.nativeElement), tooltipComponent.location.nativeElement); this.isDynamicTooltip = true; this.needProxyProperties.forEach((/** * @param {?} property * @return {?} */ function (property) { return _this.updateCompValue(property, _this[property]); })); /** @type {?} */ var visible_ = this.tooltip.nzVisibleChange.pipe(distinctUntilChanged()).subscribe((/** * @param {?} data * @return {?} */ function (data) { _this.visible = data; _this.nzVisibleChange.emit(data); })); this.subs_.add(visible_); } this.tooltip.setOverlayOrigin(this); }; /** * @return {?} */ NzTooltipDirective.prototype.ngAfterViewInit = /** * @return {?} */ function () { var _this = this; if (this.tooltip.nzTrigger === 'hover') { /** @type {?} */ var overlayElement_1; this.renderer.listen(this.elementRef.nativeElement, 'mouseenter', (/** * @return {?} */ function () { return _this.delayEnterLeave(true, true, _this.tooltip.nzMouseEnterDelay); })); this.renderer.listen(this.elementRef.nativeElement, 'mouseleave', (/** * @return {?} */ function () { _this.delayEnterLeave(true, false, _this.tooltip.nzMouseLeaveDelay); if (_this.tooltip.overlay.overlayRef && !overlayElement_1) { // NOTE: we bind events under "mouseleave" due to the overlayRef is only created after the overlay was completely shown up overlayElement_1 = _this.tooltip.overlay.overlayRef.overlayElement; _this.renderer.listen(overlayElement_1, 'mouseenter', (/** * @return {?} */ function () { return _this.delayEnterLeave(false, true); })); _this.renderer.listen(overlayElement_1, 'mouseleave', (/** * @return {?} */ function () { return _this.delayEnterLeave(false, false); })); } })); } else if (this.tooltip.nzTrigger === 'focus') { this.renderer.listen(this.elementRef.nativeElement, 'focus', (/** * @return {?} */ function () { return _this.show(); })); this.renderer.listen(this.elementRef.nativeElement, 'blur', (/** * @return {?} */ function () { return _this.hide(); })); } else if (this.tooltip.nzTrigger === 'click') { this.renderer.listen(this.elementRef.nativeElement, 'click', (/** * @param {?} e * @return {?} */ function (e) { e.preventDefault(); _this.show(); })); } }; /** * @return {?} */ NzTooltipDirective.prototype.ngOnDestroy = /** * @return {?} */ function () { this.subs_.unsubscribe(); }; // tslint:disable-next-line:no-any // tslint:disable-next-line:no-any /** * @protected * @param {?} key * @param {?} value * @return {?} */ NzTooltipDirective.prototype.updateCompValue = // tslint:disable-next-line:no-any /** * @protected * @param {?} key * @param {?} value * @return {?} */ function (key, value) { if (this.isDynamicTooltip && isNotNil(value)) { this.tooltip[key] = value; } }; /** * @private * @return {?} */ NzTooltipDirective.prototype.show = /** * @private * @return {?} */ function () { this.tooltip.show(); this.isTooltipOpen = true; }; /** * @private * @return {?} */ NzTooltipDirective.prototype.hide = /** * @private * @return {?} */ function () { this.tooltip.hide(); this.isTooltipOpen = false; }; /** * @private * @param {?} isOrigin * @param {?} isEnter * @param {?=} delay * @return {?} */ NzTooltipDirective.prototype.delayEnterLeave = /** * @private * @param {?} isOrigin * @param {?} isEnter * @param {?=} delay * @return {?} */ function (isOrigin, isEnter, delay) { var _this = this; if (delay === void 0) { delay = -1; } if (this.delayTimer) { // Clear timer during the delay time clearTimeout(this.delayTimer); this.delayTimer = null; } else if (delay > 0) { this.delayTimer = setTimeout((/** * @return {?} */ function () { _this.delayTimer = null; isEnter ? _this.show() : _this.hide(); }), delay * 1000); } else { isEnter && isOrigin ? this.show() : this.hide(); // [Compatible] The "isOrigin" is used due to the tooltip will not hide immediately (may caused by the fade-out animation) } }; /** * Set inputs of child components when this component's inputs change. * @param changes */ /** * Set inputs of child components when this component's inputs change. * @private * @param {?} changes * @return {?} */ NzTooltipDirective.prototype.updateProxies = /** * Set inputs of child components when this component's inputs change. * @private * @param {?} changes * @return {?} */ function (changes) { var _this = this; if (this.tooltip) { Object.keys(changes).forEach((/** * @param {?} key * @return {?} */ function (key) { /** @type {?} */ var change = changes[key]; if (change) { _this.updateCompValue(key, change.currentValue); } })); if (changes.setTitle) { this.nzTitle = changes.setTitle.currentValue; this.updateCompValue('nzTitle', changes.setTitle.currentValue); } this.tooltip.cdr.markForCheck(); // Manually trigger change detection of component. } }; NzTooltipDirective.decorators = [ { type: Directive, args: [{ selector: '[nz-tooltip]', exportAs: 'nzTooltip', host: { '[class.ant-tooltip-open]': 'isTooltipOpen' } },] } ]; /** @nocollapse */ NzTooltipDirective.ctorParameters = function () { return [ { type: ElementRef }, { type: ViewContainerRef }, { type: ComponentFactoryResolver }, { type: Renderer2 }, { type: NzToolTipComponent, decorators: [{ type: Optional }] }, { type: NzNoAnimationDirective, decorators: [{ type: Host }, { type: Optional }] } ]; }; NzTooltipDirective.propDecorators = { nzVisibleChange: [{ type: Output }], nzTitle: [{ type: Input, args: ['nz-tooltip',] }], setTitle: [{ type: Input, args: ['nzTitle',] }], nzContent: [{ type: Input }], nzMouseEnterDelay: [{ type: Input }], nzMouseLeaveDelay: [{ type: Input }], nzOverlayClassName: [{ type: Input }], nzOverlayStyle: [{ type: Input }], nzTrigger: [{ type: Input }], nzVisible: [{ type: Input }], nzPlacement: [{ type: Input }] }; return NzTooltipDirective; }()); /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ var NzToolTipModule = /** @class */ (function () { function NzToolTipModule() { } NzToolTipModule.decorators = [ { type: NgModule, args: [{ declarations: [NzToolTipComponent, NzTooltipDirective], exports: [NzToolTipComponent, NzTooltipDirective], imports: [CommonModule, OverlayModule, NzAddOnModule, NzOverlayModule, NzNoAnimationModule], entryComponents: [NzToolTipComponent] },] } ]; return NzToolTipModule; }()); /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ export { NzToolTipComponent, NzTooltipDirective, NzToolTipModule }; //# sourceMappingURL=ng-zorro-antd-tooltip.js.map