Skip to content

Commit

Permalink
refactor(module:timeline): refactor timeline (NG-ZORRO#2515)
Browse files Browse the repository at this point in the history
close NG-ZORRO#2379

* support alternate positions, alternate and right alternate
* support reversing
  • Loading branch information
Wendell authored and vthinkxie committed Nov 30, 2018
1 parent fc9ea8a commit 5ef9575
Show file tree
Hide file tree
Showing 15 changed files with 252 additions and 106 deletions.
11 changes: 11 additions & 0 deletions components/core/dom/reverse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export function reverseChildNodes(parent: HTMLElement): void {
const children = parent.childNodes;
let length = children.length;
if (length) {
const nodes: Node[] = [];
children.forEach((node, i) => nodes[ i ] = node);
while (length--) {
parent.appendChild(nodes[ length ]);
}
}
}
15 changes: 15 additions & 0 deletions components/timeline/demo/alternate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
order: 3
title:
zh-CN: 交替展现
en-US: Alternate
---

## zh-CN

内容在时间轴两侧交替出现。

## en-US

Alternate timeline.

20 changes: 20 additions & 0 deletions components/timeline/demo/alternate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Component } from '@angular/core';

@Component({
selector: 'nz-demo-timeline-alternate',
template: `
<nz-timeline nzMode="alternate">
<nz-timeline-item>Create a services site 2015-09-01</nz-timeline-item>
<nz-timeline-item nzColor="green">Solve initial network problems 2015-09-01</nz-timeline-item>
<nz-timeline-item [nzDot]="dotTemplate">Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo.</nz-timeline-item>
<nz-timeline-item nzColor="red">Network problems being solved 2015-09-01</nz-timeline-item>
<nz-timeline-item>Create a services site 2015-09-01</nz-timeline-item>
<nz-timeline-item [nzDot]="dotTemplate">Technical testing 2015-09-01</nz-timeline-item>
</nz-timeline>
<ng-template #dotTemplate>
<i nz-icon type="clock-circle-o" style="font-size: 16px;"></i>
</ng-template>
`
})
export class NzDemoTimelineAlternateComponent {
}
6 changes: 2 additions & 4 deletions components/timeline/demo/pending.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
---
order: 2
title:
zh-CN: 最后一个
zh-CN: 最后一个及排序
en-US: Last node
---

## zh-CN

在最后位置添加一个幽灵节点,表示时间轴未完成,还在记录过程中。可以指定 `[nzPending]="true"` 或者 `[nzPending]="一个 TemplateRef"`

一般用于按时间正序排列的模式。
当任务状态正在发生,还在记录过程中,可用幽灵节点来表示当前的时间节点,当 `nzPending` 为真值时展示幽灵节点,如果 `nzPending` 是 Template 可用于定制该节点内容,同时 `nzPendingDot` 将可以用于定制其轴点。`nzReverse` 属性用于控制节点排序,为 `false` 时按正序排列,为 `true` 时按倒序排列。

## en-US

Expand Down
9 changes: 7 additions & 2 deletions components/timeline/demo/pending.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@ import { Component } from '@angular/core';
@Component({
selector: 'nz-demo-timeline-pending',
template: `
<nz-timeline [nzPending]="'Recording...'">
<nz-timeline [nzPending]="'Recording...'" [nzReverse]="reverse">
<nz-timeline-item>Create a services site 2015-09-01</nz-timeline-item>
<nz-timeline-item>Solve initial network problems 2015-09-01</nz-timeline-item>
<nz-timeline-item>Technical testing 2015-09-01</nz-timeline-item>
<nz-timeline-item>Network problems being solved 2015-09-01</nz-timeline-item>
</nz-timeline>
<button nz-button style="margin-top: 16px" nzType="primary" (click)="toggleReverse()">Toggle Reverse</button>
`
})

export class NzDemoTimelinePendingComponent {
reverse = false;

toggleReverse(): void {
this.reverse = !this.reverse;
}
}
15 changes: 15 additions & 0 deletions components/timeline/demo/right.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
order: 5
title:
zh-CN: 右侧时间轴点
en-US: Right alternate
---

## zh-CN

时间轴可以在内容的右边。

## en-US

Right alternate timeline.

18 changes: 18 additions & 0 deletions components/timeline/demo/right.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Component } from '@angular/core';

@Component({
selector: 'nz-demo-timeline-right',
template: `
<nz-timeline nzMode="right">
<nz-timeline-item>Create a services site 2015-09-01</nz-timeline-item>
<nz-timeline-item>Solve initial network problems 2015-09-01</nz-timeline-item>
<nz-timeline-item [nzDot]="dotTemplate" nzColor="red">Technical testing 2015-09-01</nz-timeline-item>
<nz-timeline-item>Network problems being solved 2015-09-01</nz-timeline-item>
</nz-timeline>
<ng-template #dotTemplate>
<i nz-icon type="clock-circle-o" style="font-size: 16px;"></i>
</ng-template>
`
})
export class NzDemoTimelineRightComponent {
}
3 changes: 3 additions & 0 deletions components/timeline/doc/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ Timeline
| Property | Description | Type | Default |
| -------- | ----------- | ---- | ------- |
| `[nzPending]` | Set the last ghost node's existence or its content | boolean|string|`TemplateRef<void>` | `false` |
| `[nzPendingDot]` | Set the dot of the last ghost node when pending is true | string|`TemplateRef<void>` | `<i nz-icon type="loading"></i>` |
| `[nzReverse]` | Reverse nodes or not | boolean | false |
| `[nzMode]` | By sending `alternate` the timeline will distribute the nodes to the left and right | left|alternate|right | - |

### nz-timeline-item

Expand Down
3 changes: 3 additions & 0 deletions components/timeline/doc/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ title: Timeline
| 参数 | 说明 | 类型 | 默认值 |
| --- | --- | --- | --- |
| `[nzPending]` | 指定最后一个幽灵节点是否存在或内容 | boolean|string|`TemplateRef<void>` | false |
| `[nzPendingDot]` | 当最后一个幽灵节点存在時,指定其时间图点 | string|`TemplateRef<void>` | `<i nz-icon type="loading"></i>` |
| `[nzReverse]` | 节点排序 | boolean | false |
| `[nzMode]` | 可以改变时间轴和内容的相对位置 | left|alternate|right | - |

### nz-timeline-item

Expand Down
18 changes: 11 additions & 7 deletions components/timeline/nz-timeline-item.component.html
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
<li class="ant-timeline-item" #liTemplate>
<li
class="ant-timeline-item"
[class.ant-timeline-item-right]="position === 'right'"
[class.ant-timeline-item-left]="position === 'left'"
[class.ant-timeline-item-last]="isLast"
#liTemplate>
<div class="ant-timeline-item-tail"></div>
<div
class="ant-timeline-item-head"
[class.ant-timeline-item-head-custom]="nzDot"
[ngClass]="classMap">
<ng-container *ngIf="isDotString; else dotTemplate">{{ nzDot }}</ng-container>
<ng-template #dotTemplate>
<ng-template [ngTemplateOutlet]="nzDot"></ng-template>
</ng-template>
[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-custom]="!!nzDot">
<ng-container *nzStringTemplateOutlet="nzDot">{{ nzDot }}</ng-container>
</div>
<div class="ant-timeline-item-content">
<ng-content></ng-content>
Expand Down
80 changes: 28 additions & 52 deletions components/timeline/nz-timeline-item.component.ts
Original file line number Diff line number Diff line change
@@ -1,81 +1,57 @@
import {
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
ElementRef,
Input,
Input, OnChanges,
OnInit,
Renderer2,
SimpleChanges,
TemplateRef,
ViewChild
ViewChild,
ViewEncapsulation
} from '@angular/core';

import { NzTimelineMode } from './nz-timeline.component';

@Component({
selector : 'nz-timeline-item',
changeDetection : ChangeDetectionStrategy.OnPush,
encapsulation : ViewEncapsulation.None,
preserveWhitespaces: false,
selector : 'nz-timeline-item, [nz-timeline-item]',
templateUrl : './nz-timeline-item.component.html'
})
export class NzTimelineItemComponent implements OnInit {
private _dot: string | TemplateRef<void>;
private _color: string = 'blue';
private _isLast = false;
export class NzTimelineItemComponent implements OnInit, OnChanges {
@ViewChild('liTemplate') liTemplate: ElementRef;
isDotString: boolean;
classMap;

set isLast(value: boolean) {
this._isLast = value;
if (this.isLast) {
this.renderer.addClass(this.liTemplate.nativeElement, 'ant-timeline-item-last');
} else {
this.renderer.removeClass(this.liTemplate.nativeElement, 'ant-timeline-item-last');
}
}
@Input() nzColor: string = 'blue';
@Input() nzDot: string | TemplateRef<void>;

get isLast(): boolean {
return this._isLast;
}
isLast = false;
position: NzTimelineMode | undefined;

@Input()
set nzDot(value: string | TemplateRef<void>) {
this.isDotString = !(value instanceof TemplateRef);
this._dot = value;
}
constructor(private renderer: Renderer2, private cdr: ChangeDetectorRef) {}

get nzDot(): string | TemplateRef<void> {
return this._dot;
ngOnInit(): void {
this.tryUpdateCustomColor();
}

@Input()
set nzColor(color: string) {
this._color = color;
this.updateClassMap();
ngOnChanges(changes: SimpleChanges): void {
if (changes.nzColor) {
this.tryUpdateCustomColor();
}
}

get nzColor(): string {
return this._color;
detectChanges(): void {
this.cdr.detectChanges();
}

updateClassMap(): void {
// Support custom color
private tryUpdateCustomColor(): void {
const defaultColors = [ 'blue', 'red', 'green' ];
const circle = this.liTemplate.nativeElement.querySelector('.ant-timeline-item-head');
if (defaultColors.indexOf(this._color) === -1) {
this.renderer.setStyle(circle, 'border-color', this._color);
if (defaultColors.indexOf(this.nzColor) === -1) {
this.renderer.setStyle(circle, 'border-color', this.nzColor);
} else {
this.renderer.removeStyle(circle, 'border-color');
}

this.classMap = {
[ 'ant-timeline-item-head-green' ]: this.nzColor === 'green',
[ 'ant-timeline-item-head-red' ] : this.nzColor === 'red',
[ 'ant-timeline-item-head-blue' ] : this.nzColor === 'blue'
};
}

constructor(private renderer: Renderer2) {
}

ngOnInit(): void {
this.updateClassMap();
}

}
23 changes: 16 additions & 7 deletions components/timeline/nz-timeline.component.html
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
<ul class="ant-timeline" [class.ant-timeline-pending]="nzPending">
<ul
class="ant-timeline"
[class.ant-timeline-right]="nzMode === 'right'"
[class.ant-timeline-alternate]="nzMode === 'alternate'"
[class.ant-timeline-pending]="!!nzPending"
[class.ant-timeline-reverse]="nzReverse"
#timeline>
<!-- User inserted timeline dots. -->
<ng-content></ng-content>
<!-- Pending dot. -->
<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">
<i nz-icon type="loading"></i>
<ng-container *nzStringTemplateOutlet="nzPendingDot">
{{ nzPendingDot }}<i *ngIf="!nzPendingDot" nz-icon type="loading"></i>
</ng-container>
</div>
<div class="ant-timeline-item-content">
<ng-container *ngIf="isPendingString; else pendingTemplate">{{ isPendingBoolean ? '' : nzPending }}</ng-container>
<ng-template #pendingTemplate>
<ng-template [ngTemplateOutlet]="nzPending"></ng-template>
</ng-template>
<ng-container *nzStringTemplateOutlet="nzPending">
{{ isPendingBoolean ? '' : nzPending }}
</ng-container>
</div>
</li>
</ul>
</ul>
Loading

0 comments on commit 5ef9575

Please sign in to comment.