ng-zorro-antd
Version:
An enterprise-class UI components based on Ant Design and Angular
266 lines (257 loc) • 9.87 kB
JavaScript
import { Injectable, Component, ChangeDetectionStrategy, ViewEncapsulation, ChangeDetectorRef, ViewChild, Input, Optional, ContentChildren, NgModule } from '@angular/core';
import { ReplaySubject, Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';
import { Directionality, BidiModule } from '@angular/cdk/bidi';
import { PlatformModule } from '@angular/cdk/platform';
import { CommonModule } from '@angular/common';
import { NzOutletModule } from 'ng-zorro-antd/core/outlet';
import { NzIconModule } from 'ng-zorro-antd/icon';
/**
* 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
*/
class TimelineService {
constructor() {
this.check$ = new ReplaySubject(1);
}
markForCheck() {
this.check$.next();
}
}
TimelineService.decorators = [
{ type: Injectable }
];
/**
* 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
*/
const TimelineModes = ['left', 'alternate', 'right', 'custom'];
const TimelinePositions = ['left', 'right'];
const TimelineTimeDefaultColors = ['red', 'blue', 'green', 'grey', 'gray'];
/**
* 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
*/
function isDefaultColor(color) {
return TimelineTimeDefaultColors.findIndex(i => i === color) !== -1;
}
class NzTimelineItemComponent {
constructor(cdr, timelineService) {
this.cdr = cdr;
this.timelineService = timelineService;
this.nzColor = 'blue';
this.isLast = false;
this.borderColor = null;
}
ngOnChanges(changes) {
this.timelineService.markForCheck();
if (changes.nzColor) {
this.updateCustomColor();
}
}
detectChanges() {
this.cdr.detectChanges();
}
updateCustomColor() {
this.borderColor = isDefaultColor(this.nzColor) ? null : this.nzColor;
}
}
NzTimelineItemComponent.decorators = [
{ type: Component, args: [{
changeDetection: ChangeDetectionStrategy.OnPush,
encapsulation: ViewEncapsulation.None,
preserveWhitespaces: false,
selector: 'nz-timeline-item, [nz-timeline-item]',
exportAs: 'nzTimelineItem',
template: `
<ng-template #template>
<li
class="ant-timeline-item"
[class.ant-timeline-item-right]="(nzPosition || position) === 'right'"
[class.ant-timeline-item-left]="(nzPosition || position) === 'left'"
[class.ant-timeline-item-last]="isLast"
>
<div class="ant-timeline-item-tail"></div>
<div
class="ant-timeline-item-head"
[class.ant-timeline-item-head-red]="nzColor === 'red'"
[class.ant-timeline-item-head-blue]="nzColor === 'blue'"
[class.ant-timeline-item-head-green]="nzColor === 'green'"
[class.ant-timeline-item-head-gray]="nzColor === 'gray'"
[class.ant-timeline-item-head-custom]="!!nzDot"
[style.border-color]="borderColor"
>
<ng-container *nzStringTemplateOutlet="nzDot">{{ nzDot }}</ng-container>
</div>
<div class="ant-timeline-item-content">
<ng-content></ng-content>
</div>
</li>
</ng-template>
`
},] }
];
NzTimelineItemComponent.ctorParameters = () => [
{ type: ChangeDetectorRef },
{ type: TimelineService }
];
NzTimelineItemComponent.propDecorators = {
template: [{ type: ViewChild, args: ['template', { static: false },] }],
nzPosition: [{ type: Input }],
nzColor: [{ type: Input }],
nzDot: [{ type: Input }]
};
/**
* 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
*/
class NzTimelineComponent {
constructor(cdr, timelineService, directionality) {
this.cdr = cdr;
this.timelineService = timelineService;
this.directionality = directionality;
this.nzMode = 'left';
this.nzReverse = false;
this.isPendingBoolean = false;
this.timelineItems = [];
this.dir = 'ltr';
this.destroy$ = new Subject();
}
ngOnChanges(changes) {
const { nzMode, nzReverse, nzPending } = changes;
if (simpleChangeActivated(nzMode) || simpleChangeActivated(nzReverse)) {
this.updateChildren();
}
if (nzPending) {
this.isPendingBoolean = nzPending.currentValue === true;
}
}
ngOnInit() {
var _a;
this.timelineService.check$.pipe(takeUntil(this.destroy$)).subscribe(() => {
this.cdr.markForCheck();
});
(_a = this.directionality.change) === null || _a === void 0 ? void 0 : _a.pipe(takeUntil(this.destroy$)).subscribe((direction) => {
this.dir = direction;
this.cdr.detectChanges();
});
this.dir = this.directionality.value;
}
ngAfterContentInit() {
this.updateChildren();
this.listOfItems.changes.pipe(takeUntil(this.destroy$)).subscribe(() => {
this.updateChildren();
});
}
ngOnDestroy() {
this.destroy$.next();
this.destroy$.complete();
}
updateChildren() {
if (this.listOfItems && this.listOfItems.length) {
const length = this.listOfItems.length;
this.listOfItems.forEach((item, index) => {
item.isLast = !this.nzReverse ? index === length - 1 : index === 0;
item.position = getInferredTimelineItemPosition(index, this.nzMode);
item.detectChanges();
});
this.timelineItems = this.nzReverse ? this.listOfItems.toArray().reverse() : this.listOfItems.toArray();
}
this.cdr.markForCheck();
}
}
NzTimelineComponent.decorators = [
{ type: Component, args: [{
changeDetection: ChangeDetectionStrategy.OnPush,
encapsulation: ViewEncapsulation.None,
preserveWhitespaces: false,
selector: 'nz-timeline',
providers: [TimelineService],
exportAs: 'nzTimeline',
template: `
<ul
class="ant-timeline"
[class.ant-timeline-right]="nzMode === 'right'"
[class.ant-timeline-alternate]="nzMode === 'alternate' || nzMode === 'custom'"
[class.ant-timeline-pending]="!!nzPending"
[class.ant-timeline-reverse]="nzReverse"
[class.ant-timeline-rtl]="dir === 'rtl'"
>
<!-- pending dot (reversed) -->
<ng-container *ngIf="nzReverse" [ngTemplateOutlet]="pendingTemplate"></ng-container>
<!-- timeline items -->
<ng-container *ngFor="let item of timelineItems">
<ng-template [ngTemplateOutlet]="item.template"></ng-template>
</ng-container>
<ng-container *ngIf="!nzReverse" [ngTemplateOutlet]="pendingTemplate"></ng-container>
<!-- pending dot -->
</ul>
<ng-template #pendingTemplate>
<li *ngIf="nzPending" class="ant-timeline-item ant-timeline-item-pending">
<div class="ant-timeline-item-tail"></div>
<div class="ant-timeline-item-head ant-timeline-item-head-custom ant-timeline-item-head-blue">
<ng-container *nzStringTemplateOutlet="nzPendingDot">
{{ nzPendingDot }}
<i *ngIf="!nzPendingDot" nz-icon nzType="loading"></i>
</ng-container>
</div>
<div class="ant-timeline-item-content">
<ng-container *nzStringTemplateOutlet="nzPending">
{{ isPendingBoolean ? '' : nzPending }}
</ng-container>
</div>
</li>
</ng-template>
<!-- Grasp items -->
<ng-content></ng-content>
`
},] }
];
NzTimelineComponent.ctorParameters = () => [
{ type: ChangeDetectorRef },
{ type: TimelineService },
{ type: Directionality, decorators: [{ type: Optional }] }
];
NzTimelineComponent.propDecorators = {
listOfItems: [{ type: ContentChildren, args: [NzTimelineItemComponent,] }],
nzMode: [{ type: Input }],
nzPending: [{ type: Input }],
nzPendingDot: [{ type: Input }],
nzReverse: [{ type: Input }]
};
function simpleChangeActivated(simpleChange) {
return !!(simpleChange && (simpleChange.previousValue !== simpleChange.currentValue || simpleChange.isFirstChange()));
}
function getInferredTimelineItemPosition(index, mode) {
return mode === 'custom'
? undefined
: mode === 'left'
? 'left'
: mode === 'right'
? 'right'
: mode === 'alternate' && index % 2 === 0
? 'left'
: 'right';
}
/**
* 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
*/
class NzTimelineModule {
}
NzTimelineModule.decorators = [
{ type: NgModule, args: [{
declarations: [NzTimelineItemComponent, NzTimelineComponent],
exports: [NzTimelineItemComponent, NzTimelineComponent],
imports: [BidiModule, CommonModule, PlatformModule, NzIconModule, NzOutletModule]
},] }
];
/**
* 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
*/
/**
* Generated bundle index. Do not edit.
*/
export { NzTimelineComponent, NzTimelineItemComponent, NzTimelineModule, TimelineService };
//# sourceMappingURL=ng-zorro-antd-timeline.js.map