Skip to content

Commit

Permalink
fix(module:carousel): support dynamic change of nz-carousel-content (#60
Browse files Browse the repository at this point in the history
)

closes #56
  • Loading branch information
vthinkxie committed Aug 18, 2017
1 parent 530ae97 commit 44865c2
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 23 deletions.
12 changes: 10 additions & 2 deletions src/components/carousel/nz-carousel-content.directive.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import {
Directive,
HostBinding
HostBinding,
ElementRef
} from '@angular/core';

@Directive({
selector: '[nz-carousel-content]',
})
Expand All @@ -10,7 +12,9 @@ export class NzCarouselContentDirective {
isActive = false;
left = null;
top = null;
fadeMode = false;
fadeMode = false
nativeElement: HTMLElement;


@HostBinding('class.slick-slide') _nzSlickSlide = true;

Expand Down Expand Up @@ -48,4 +52,8 @@ export class NzCarouselContentDirective {
}
}

constructor(private _el: ElementRef) {
this.nativeElement = this._el.nativeElement;
}

}
28 changes: 24 additions & 4 deletions src/components/carousel/nz-carousel.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,14 @@ export class NzCarouselComponent implements AfterViewInit, OnDestroy {
activeIndex = 0;
transform = 'translate3d(0px, 0px, 0px)';
interval;
@ContentChildren(NzCarouselContentDirective) slideContents;
slideContents;

@ContentChildren(NzCarouselContentDirective)
set _slideContents(value) {
this.slideContents = value;
this.renderContent();
}

@ViewChild('slickList') slickList: ElementRef;
@ViewChild('slickTrack') slickTrack: ElementRef;
@Input() nzAutoPlay = false;
Expand Down Expand Up @@ -67,8 +74,14 @@ export class NzCarouselComponent implements AfterViewInit, OnDestroy {
}

ngAfterViewInit() {
this.renderContent();
}

renderContent() {
setTimeout(_ => {
this.slideContents.first.isActive = true;
if (this.slideContents.first) {
this.slideContents.first.isActive = true;
}
this.slideContents.forEach((content, i) => {
content.width = this.hostElement.nativeElement.offsetWidth;
if (this.nzEffect === 'fade') {
Expand All @@ -83,14 +96,21 @@ export class NzCarouselComponent implements AfterViewInit, OnDestroy {
if (this.nzAutoPlay) {
this.createInterval();
}
this._renderer.setStyle(this.slickList.nativeElement, 'height', `${this.hostElement.nativeElement.offsetHeight}px`);

if (this.nzVertical) {
this._renderer.removeStyle(this.slickList.nativeElement, 'height');
if (this.slideContents.first) {
this._renderer.setStyle(this.slickList.nativeElement, 'height', `${this.slideContents.first.nativeElement.offsetHeight}px`);
}
this._renderer.removeStyle(this.slickTrack.nativeElement, 'height');
this._renderer.setStyle(this.slickTrack.nativeElement, 'height', `${this.slideContents.length * this.hostElement.nativeElement.offsetHeight}px`);
} else {
this._renderer.removeStyle(this.slickList.nativeElement, 'height');
this._renderer.setStyle(this.slickList.nativeElement, 'height', `${this.hostElement.nativeElement.offsetHeight}px`);
this._renderer.removeStyle(this.slickTrack.nativeElement, 'width');
this._renderer.setStyle(this.slickTrack.nativeElement, 'width', `${this.slideContents.length * this.hostElement.nativeElement.offsetWidth}px`);
}
})

}

createInterval() {
Expand Down
11 changes: 7 additions & 4 deletions src/showcase/nz-demo-carousel/nz-demo-carousel-auto.component.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { Component, OnInit } from '@angular/core';

@Component({
selector: 'nz-demo-carousel-auto',
template: `
<nz-carousel [nzAutoPlay]="true">
<div nz-carousel-content><h3>1</h3></div>
<div nz-carousel-content><h3>2</h3></div>
<div nz-carousel-content><h3>3</h3></div>
<div nz-carousel-content><h3>4</h3></div>
<div nz-carousel-content *ngFor="let index of array"><h3>{{index}}</h3></div>
</nz-carousel>`,
styles : [
`:host ::ng-deep .ant-carousel .slick-slide {
Expand All @@ -17,17 +15,22 @@ import { Component, OnInit } from '@angular/core';
color: #fff;
overflow: hidden;
}
h3 {
color: #fff;
}
`
]
})
export class NzDemoCarouselAutoComponent implements OnInit {
array = [ 1 ];

constructor() {
}

ngOnInit() {
setTimeout(_ => {
this.array = [ 1, 2, 3, 4 ];
}, 500)
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { Component, OnInit } from '@angular/core';

@Component({
selector: 'nz-demo-carousel-basic',
template: `
<nz-carousel>
<div nz-carousel-content><h3>1</h3></div>
<div nz-carousel-content><h3>2</h3></div>
<div nz-carousel-content><h3>3</h3></div>
<div nz-carousel-content><h3>4</h3></div>
<div nz-carousel-content *ngFor="let index of array"><h3>{{index}}</h3></div>
</nz-carousel>`,
styles : [
`:host ::ng-deep .ant-carousel .slick-slide {
Expand All @@ -17,17 +15,22 @@ import { Component, OnInit } from '@angular/core';
color: #fff;
overflow: hidden;
}
h3 {
color: #fff;
}
`
]
})
export class NzDemoCarouselBasicComponent implements OnInit {
array = [ 1, 2, 3 ];

constructor() {
}

ngOnInit() {
setTimeout(_ => {
this.array = [ 1, 2, 3, 4 ];
}, 500)
}
}
10 changes: 5 additions & 5 deletions src/showcase/nz-demo-carousel/nz-demo-carousel-fade.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ import { Component, OnInit } from '@angular/core';
selector: 'nz-demo-carousel-fade',
template: `
<nz-carousel [nzEffect]="'fade'">
<div nz-carousel-content><h3>1</h3></div>
<div nz-carousel-content><h3>2</h3></div>
<div nz-carousel-content><h3>3</h3></div>
<div nz-carousel-content><h3>4</h3></div>
<div nz-carousel-content *ngFor="let index of array"><h3>{{index}}</h3></div>
</nz-carousel>`,
styles : [
`:host ::ng-deep .ant-carousel .slick-slide {
Expand All @@ -24,10 +21,13 @@ import { Component, OnInit } from '@angular/core';
]
})
export class NzDemoCarouselFadeComponent implements OnInit {

array = [];
constructor() {
}

ngOnInit() {
setTimeout(_ => {
this.array = [ 1, 2, 3, 4 ];
}, 500)
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { Component, OnInit } from '@angular/core';

@Component({
selector: 'nz-demo-carousel-vertical',
template: `
<nz-carousel [nzVertical]="true">
<div nz-carousel-content><h3>1</h3></div>
<div nz-carousel-content><h3>2</h3></div>
<div nz-carousel-content><h3>3</h3></div>
<div nz-carousel-content><h3>4</h3></div>
<div nz-carousel-content *ngFor="let index of array"><h3>{{index}}</h3></div>
</nz-carousel>`,
styles : [
`:host ::ng-deep .ant-carousel .slick-slide {
Expand All @@ -17,17 +15,22 @@ import { Component, OnInit } from '@angular/core';
color: #fff;
overflow: hidden;
}
h3 {
color: #fff;
}
`
]
})
export class NzDemoCarouselVerticalComponent implements OnInit {
array = [ 1, 2, 3 ]; // try dynamic change the array

constructor() {
}

ngOnInit() {
setTimeout(() => {
this.array = [ 1, 2, 3, 4 ];
}, 500)
}
}

0 comments on commit 44865c2

Please sign in to comment.