ng-zorro-antd
Version:
An enterprise-class UI components based on Ant Design and Angular
619 lines (611 loc) • 18.1 kB
JavaScript
import { Platform, PlatformModule } from '@angular/cdk/platform';
import { EventEmitter, Component, ViewEncapsulation, ChangeDetectionStrategy, Inject, ChangeDetectorRef, NgZone, Renderer2, ViewChild, Input, Output, TemplateRef, ElementRef, ContentChild, NgModule } from '@angular/core';
import { __decorate, __metadata } from 'tslib';
import { DOCUMENT, CommonModule } from '@angular/common';
import { NzConfigService, WithConfig } from 'ng-zorro-antd/core/config';
import { warnDeprecation } from 'ng-zorro-antd/core/logger';
import { NzScrollService, SCROLL_SERVICE_PROVIDER } from 'ng-zorro-antd/core/services';
import { InputBoolean, InputNumber } from 'ng-zorro-antd/core/util';
import { Subject, fromEvent } from 'rxjs';
import { throttleTime, takeUntil } from 'rxjs/operators';
import { NzAffixModule } from 'ng-zorro-antd/affix';
/**
* @fileoverview added by tsickle
* Generated from: util.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/**
* @license
* Copyright Alibaba.com All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
*/
/**
* @param {?} element
* @param {?} container
* @return {?}
*/
function getOffsetTop(element, container) {
if (!element || !element.getClientRects().length) {
return 0;
}
/** @type {?} */
const rect = element.getBoundingClientRect();
if (rect.width || rect.height) {
if (container === window) {
/** @type {?} */
const documentElement = (/** @type {?} */ ((/** @type {?} */ (element.ownerDocument)).documentElement));
return rect.top - documentElement.clientTop;
}
return rect.top - ((/** @type {?} */ (container))).getBoundingClientRect().top;
}
return rect.top;
}
/**
* @fileoverview added by tsickle
* Generated from: anchor.component.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/**
* @record
*/
function Section() { }
if (false) {
/** @type {?} */
Section.prototype.comp;
/** @type {?} */
Section.prototype.top;
}
/** @type {?} */
const NZ_CONFIG_COMPONENT_NAME = 'anchor';
/** @type {?} */
const sharpMatcherRegx = /#([^#]+)$/;
class NzAnchorComponent {
/**
* @param {?} doc
* @param {?} nzConfigService
* @param {?} scrollSrv
* @param {?} cdr
* @param {?} platform
* @param {?} zone
* @param {?} renderer
*/
constructor(doc, nzConfigService, scrollSrv, cdr, platform, zone, renderer) {
this.doc = doc;
this.nzConfigService = nzConfigService;
this.scrollSrv = scrollSrv;
this.cdr = cdr;
this.platform = platform;
this.zone = zone;
this.renderer = renderer;
this.nzAffix = true;
this.nzClick = new EventEmitter();
this.nzScroll = new EventEmitter();
this.visible = false;
this.wrapperStyle = { 'max-height': '100vh' };
this.links = [];
this.animating = false;
this.destroy$ = new Subject();
this.handleScrollTimeoutID = -1;
}
/**
* @param {?} link
* @return {?}
*/
registerLink(link) {
this.links.push(link);
}
/**
* @param {?} link
* @return {?}
*/
unregisterLink(link) {
this.links.splice(this.links.indexOf(link), 1);
}
/**
* @private
* @return {?}
*/
getContainer() {
return this.container || window;
}
/**
* @return {?}
*/
ngAfterViewInit() {
this.registerScrollEvent();
}
/**
* @return {?}
*/
ngOnDestroy() {
clearTimeout(this.handleScrollTimeoutID);
this.destroy$.next();
this.destroy$.complete();
}
/**
* @private
* @return {?}
*/
registerScrollEvent() {
if (!this.platform.isBrowser) {
return;
}
this.destroy$.next();
this.zone.runOutsideAngular((/**
* @return {?}
*/
() => {
fromEvent(this.getContainer(), 'scroll')
.pipe(throttleTime(50), takeUntil(this.destroy$))
.subscribe((/**
* @return {?}
*/
() => this.handleScroll()));
}));
// Browser would maintain the scrolling position when refreshing.
// So we have to delay calculation in avoid of getting a incorrect result.
this.handleScrollTimeoutID = setTimeout((/**
* @return {?}
*/
() => this.handleScroll()));
}
/**
* @return {?}
*/
handleScroll() {
if (typeof document === 'undefined' || this.animating) {
return;
}
/** @type {?} */
const sections = [];
/** @type {?} */
const scope = (this.nzOffsetTop || 0) + this.nzBounds;
this.links.forEach((/**
* @param {?} comp
* @return {?}
*/
comp => {
/** @type {?} */
const sharpLinkMatch = sharpMatcherRegx.exec(comp.nzHref.toString());
if (!sharpLinkMatch) {
return;
}
/** @type {?} */
const target = this.doc.getElementById(sharpLinkMatch[1]);
if (target) {
/** @type {?} */
const top = getOffsetTop(target, this.getContainer());
if (top < scope) {
sections.push({
top,
comp
});
}
}
}));
this.visible = !!sections.length;
if (!this.visible) {
this.clearActive();
this.cdr.detectChanges();
}
else {
/** @type {?} */
const maxSection = sections.reduce((/**
* @param {?} prev
* @param {?} curr
* @return {?}
*/
(prev, curr) => (curr.top > prev.top ? curr : prev)));
this.handleActive(maxSection.comp);
}
this.setVisible();
}
/**
* @private
* @return {?}
*/
clearActive() {
this.links.forEach((/**
* @param {?} i
* @return {?}
*/
i => {
i.unsetActive();
}));
}
/**
* @private
* @param {?} comp
* @return {?}
*/
handleActive(comp) {
this.clearActive();
comp.setActive();
/** @type {?} */
const linkNode = comp.getLinkTitleElement();
this.ink.nativeElement.style.top = `${linkNode.offsetTop + linkNode.clientHeight / 2 - 4.5}px`;
this.visible = true;
this.setVisible();
this.nzScroll.emit(comp);
}
/**
* @private
* @return {?}
*/
setVisible() {
/** @type {?} */
const visible = this.visible;
/** @type {?} */
const visibleClassname = 'visible';
if (this.ink) {
if (visible) {
this.renderer.addClass(this.ink.nativeElement, visibleClassname);
}
else {
this.renderer.removeClass(this.ink.nativeElement, visibleClassname);
}
}
}
/**
* @param {?} linkComp
* @return {?}
*/
handleScrollTo(linkComp) {
/** @type {?} */
const el = this.doc.querySelector(linkComp.nzHref);
if (!el) {
return;
}
this.animating = true;
/** @type {?} */
const containerScrollTop = this.scrollSrv.getScroll(this.getContainer());
/** @type {?} */
const elOffsetTop = getOffsetTop(el, this.getContainer());
/** @type {?} */
const targetScrollTop = containerScrollTop + elOffsetTop - (this.nzOffsetTop || 0);
this.scrollSrv.scrollTo(this.getContainer(), targetScrollTop, undefined, (/**
* @return {?}
*/
() => {
this.animating = false;
this.handleActive(linkComp);
}));
this.nzClick.emit(linkComp.nzHref);
}
/**
* @param {?} changes
* @return {?}
*/
ngOnChanges(changes) {
const { nzOffsetTop, nzTarget, nzContainer } = changes;
if (nzOffsetTop) {
this.wrapperStyle = {
'max-height': `calc(100vh - ${this.nzOffsetTop}px)`
};
}
if (nzContainer || nzTarget) {
/** @type {?} */
const container = this.nzContainer || this.nzTarget;
this.container = typeof container === 'string' ? this.doc.querySelector(container) : container;
this.registerScrollEvent();
if (nzTarget) {
warnDeprecation(`'nzTarget' of 'nz-anchor' is deprecated and will be removed in 10.0.0.Please use 'nzContainer' instead.`);
}
}
}
}
NzAnchorComponent.decorators = [
{ type: Component, args: [{
selector: 'nz-anchor',
exportAs: 'nzAnchor',
preserveWhitespaces: false,
template: `
<nz-affix *ngIf="nzAffix; else content" [nzOffsetTop]="nzOffsetTop" [nzTarget]="container">
<ng-template [ngTemplateOutlet]="content"></ng-template>
</nz-affix>
<ng-template #content>
<div class="ant-anchor-wrapper" [ngStyle]="wrapperStyle">
<div class="ant-anchor" [ngClass]="{ fixed: !nzAffix && !nzShowInkInFixed }">
<div class="ant-anchor-ink">
<div class="ant-anchor-ink-ball" #ink></div>
</div>
<ng-content></ng-content>
</div>
</div>
</ng-template>
`,
encapsulation: ViewEncapsulation.None,
changeDetection: ChangeDetectionStrategy.OnPush
}] }
];
/** @nocollapse */
NzAnchorComponent.ctorParameters = () => [
{ type: undefined, decorators: [{ type: Inject, args: [DOCUMENT,] }] },
{ type: NzConfigService },
{ type: NzScrollService },
{ type: ChangeDetectorRef },
{ type: Platform },
{ type: NgZone },
{ type: Renderer2 }
];
NzAnchorComponent.propDecorators = {
ink: [{ type: ViewChild, args: ['ink', { static: false },] }],
nzAffix: [{ type: Input }],
nzShowInkInFixed: [{ type: Input }],
nzBounds: [{ type: Input }],
nzOffsetTop: [{ type: Input }],
nzContainer: [{ type: Input }],
nzTarget: [{ type: Input }],
nzClick: [{ type: Output }],
nzScroll: [{ type: Output }]
};
__decorate([
InputBoolean(),
__metadata("design:type", Object)
], NzAnchorComponent.prototype, "nzAffix", void 0);
__decorate([
WithConfig(NZ_CONFIG_COMPONENT_NAME, false),
InputBoolean(),
__metadata("design:type", Boolean)
], NzAnchorComponent.prototype, "nzShowInkInFixed", void 0);
__decorate([
WithConfig(NZ_CONFIG_COMPONENT_NAME, 5),
InputNumber(),
__metadata("design:type", Number)
], NzAnchorComponent.prototype, "nzBounds", void 0);
__decorate([
InputNumber(),
WithConfig(NZ_CONFIG_COMPONENT_NAME),
__metadata("design:type", Number)
], NzAnchorComponent.prototype, "nzOffsetTop", void 0);
if (false) {
/** @type {?} */
NzAnchorComponent.ngAcceptInputType_nzAffix;
/** @type {?} */
NzAnchorComponent.ngAcceptInputType_nzShowInkInFixed;
/** @type {?} */
NzAnchorComponent.ngAcceptInputType_nzBounds;
/** @type {?} */
NzAnchorComponent.ngAcceptInputType_nzOffsetTop;
/**
* @type {?}
* @private
*/
NzAnchorComponent.prototype.ink;
/** @type {?} */
NzAnchorComponent.prototype.nzAffix;
/** @type {?} */
NzAnchorComponent.prototype.nzShowInkInFixed;
/** @type {?} */
NzAnchorComponent.prototype.nzBounds;
/** @type {?} */
NzAnchorComponent.prototype.nzOffsetTop;
/** @type {?} */
NzAnchorComponent.prototype.nzContainer;
/** @type {?} */
NzAnchorComponent.prototype.nzTarget;
/** @type {?} */
NzAnchorComponent.prototype.nzClick;
/** @type {?} */
NzAnchorComponent.prototype.nzScroll;
/** @type {?} */
NzAnchorComponent.prototype.visible;
/** @type {?} */
NzAnchorComponent.prototype.wrapperStyle;
/** @type {?} */
NzAnchorComponent.prototype.container;
/**
* @type {?}
* @private
*/
NzAnchorComponent.prototype.links;
/**
* @type {?}
* @private
*/
NzAnchorComponent.prototype.animating;
/**
* @type {?}
* @private
*/
NzAnchorComponent.prototype.destroy$;
/**
* @type {?}
* @private
*/
NzAnchorComponent.prototype.handleScrollTimeoutID;
/**
* @type {?}
* @private
*/
NzAnchorComponent.prototype.doc;
/** @type {?} */
NzAnchorComponent.prototype.nzConfigService;
/**
* @type {?}
* @private
*/
NzAnchorComponent.prototype.scrollSrv;
/**
* @type {?}
* @private
*/
NzAnchorComponent.prototype.cdr;
/**
* @type {?}
* @private
*/
NzAnchorComponent.prototype.platform;
/**
* @type {?}
* @private
*/
NzAnchorComponent.prototype.zone;
/**
* @type {?}
* @private
*/
NzAnchorComponent.prototype.renderer;
}
/**
* @fileoverview added by tsickle
* Generated from: anchor-link.component.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
class NzAnchorLinkComponent {
/**
* @param {?} elementRef
* @param {?} anchorComp
* @param {?} platform
* @param {?} renderer
*/
constructor(elementRef, anchorComp, platform, renderer) {
this.elementRef = elementRef;
this.anchorComp = anchorComp;
this.platform = platform;
this.renderer = renderer;
this.nzHref = '#';
this.titleStr = '';
this.renderer.addClass(elementRef.nativeElement, 'ant-anchor-link');
}
/**
* @param {?} value
* @return {?}
*/
set nzTitle(value) {
if (value instanceof TemplateRef) {
this.titleStr = null;
this.titleTpl = value;
}
else {
this.titleStr = value;
}
}
/**
* @return {?}
*/
ngOnInit() {
this.anchorComp.registerLink(this);
}
/**
* @return {?}
*/
getLinkTitleElement() {
return this.linkTitle.nativeElement;
}
/**
* @return {?}
*/
setActive() {
this.renderer.addClass(this.elementRef.nativeElement, 'ant-anchor-link-active');
}
/**
* @return {?}
*/
unsetActive() {
this.renderer.removeClass(this.elementRef.nativeElement, 'ant-anchor-link-active');
}
/**
* @param {?} e
* @return {?}
*/
goToClick(e) {
e.preventDefault();
e.stopPropagation();
if (this.platform.isBrowser) {
this.anchorComp.handleScrollTo(this);
}
}
/**
* @return {?}
*/
ngOnDestroy() {
this.anchorComp.unregisterLink(this);
}
}
NzAnchorLinkComponent.decorators = [
{ type: Component, args: [{
selector: 'nz-link',
exportAs: 'nzLink',
preserveWhitespaces: false,
template: `
<a #linkTitle (click)="goToClick($event)" href="{{ nzHref }}" class="ant-anchor-link-title" title="{{ titleStr }}">
<span *ngIf="titleStr; else titleTpl || nzTemplate">{{ titleStr }}</span>
</a>
<ng-content></ng-content>
`,
encapsulation: ViewEncapsulation.None,
changeDetection: ChangeDetectionStrategy.OnPush
}] }
];
/** @nocollapse */
NzAnchorLinkComponent.ctorParameters = () => [
{ type: ElementRef },
{ type: NzAnchorComponent },
{ type: Platform },
{ type: Renderer2 }
];
NzAnchorLinkComponent.propDecorators = {
nzHref: [{ type: Input }],
nzTitle: [{ type: Input }],
nzTemplate: [{ type: ContentChild, args: ['nzTemplate', { static: false },] }],
linkTitle: [{ type: ViewChild, args: ['linkTitle',] }]
};
if (false) {
/** @type {?} */
NzAnchorLinkComponent.prototype.nzHref;
/** @type {?} */
NzAnchorLinkComponent.prototype.titleStr;
/** @type {?} */
NzAnchorLinkComponent.prototype.titleTpl;
/** @type {?} */
NzAnchorLinkComponent.prototype.nzTemplate;
/** @type {?} */
NzAnchorLinkComponent.prototype.linkTitle;
/** @type {?} */
NzAnchorLinkComponent.prototype.elementRef;
/**
* @type {?}
* @private
*/
NzAnchorLinkComponent.prototype.anchorComp;
/**
* @type {?}
* @private
*/
NzAnchorLinkComponent.prototype.platform;
/**
* @type {?}
* @private
*/
NzAnchorLinkComponent.prototype.renderer;
}
/**
* @fileoverview added by tsickle
* Generated from: anchor.module.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
class NzAnchorModule {
}
NzAnchorModule.decorators = [
{ type: NgModule, args: [{
declarations: [NzAnchorComponent, NzAnchorLinkComponent],
exports: [NzAnchorComponent, NzAnchorLinkComponent],
imports: [CommonModule, NzAffixModule, PlatformModule],
providers: [SCROLL_SERVICE_PROVIDER]
},] }
];
/**
* @fileoverview added by tsickle
* Generated from: public-api.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/**
* @fileoverview added by tsickle
* Generated from: ng-zorro-antd-anchor.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
export { NzAnchorComponent, NzAnchorLinkComponent, NzAnchorModule };
//# sourceMappingURL=ng-zorro-antd-anchor.js.map