-
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathcarousel.ts
More file actions
118 lines (107 loc) · 3.88 KB
/
carousel.ts
File metadata and controls
118 lines (107 loc) · 3.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import {
AfterViewInit, Component, ContentChildren, forwardRef,
OnChanges, OnDestroy, QueryList, SimpleChanges,
} from '@angular/core'
import { ElCarouselItem } from './carousel-item'
import { ElCarouselProps } from './carousel-props'
import { carouselBtnLeftAnimation, carouselBtnRightAnimation } from './animations'
@Component({
selector: 'el-carousel',
animations: [carouselBtnLeftAnimation, carouselBtnRightAnimation],
template: `
<div class="el-carousel"
#carousel
[class.el-carousel--card]="type === 'card'"
(mouseenter)="carousel.hover = true"
(mouseleave)="carousel.hover = false">
<div class="el-carousel__container" [ngStyle]="{height: height}">
<button class="el-carousel__arrow el-carousel__arrow--left"
#leftBtn
*ngIf="arrow !== 'never'"
[@carouselBtnLeftAnimation]="arrow === 'always' || carousel.hover"
(mouseenter)="btnActionHandle(model - 1,'hover')"
(click)="btnActionHandle(model - 1, 'click')">
<i class="el-icon-arrow-left"></i>
</button>
<button class="el-carousel__arrow el-carousel__arrow--right"
#rightBtn
*ngIf="arrow !== 'never'"
[@carouselBtnRightAnimation]="arrow === 'always' || carousel.hover"
(mouseenter)="btnActionHandle(model + 1, 'hover')"
(click)="btnActionHandle(model + 1, 'click')">
<i class="el-icon-arrow-right"></i>
</button>
<ng-content></ng-content>
</div>
<ul class="el-carousel__indicators" *ngIf="indicatorPosition !== 'none'"
[class.el-carousel__indicators--labels]="hasLabel"
[class.el-carousel__indicators--outside]="indicatorPosition === 'outside' || type === 'card'">
<li *ngFor="let item of items; let i = index"
class="el-carousel__indicator"
[class.is-active]="i === model"
(mouseenter)="indicatorActionHandle(i, 'hover')"
(click)="indicatorActionHandle(i, 'click')">
<button class="el-carousel__button">
<span *ngIf="hasLabel">{{item}}</span>
</button>
</li>
</ul>
</div>
`,
})
export class ElCarousel extends ElCarouselProps implements AfterViewInit, OnChanges, OnDestroy {
@ContentChildren(forwardRef(() => ElCarouselItem)) children: QueryList<ElCarouselItem>
subscriber: Function[] = []
items: any[] = []
hasLabel: boolean = false
timer: any
constructor(
) {
super()
}
btnActionHandle(nextValue: number, eventType: string): void {
if (this.trigger !== eventType) return
this.autoplay && this.resetInterval()
this.setActiveItem(nextValue)
}
indicatorActionHandle(nextValue: number, eventType: string): void {
if (this.indicatorTrigger !== eventType) return
this.autoplay && this.resetInterval()
this.setActiveItem(nextValue)
}
setActiveItem(index: number): void {
const len = this.children.length
const nextValue = index >= len ? 0 : index < 0 ? len - 1 : index
this.model = nextValue
this.subscriber.forEach(func => func())
this.modelChange.emit(this.model)
}
resetInterval(): void {
this.timer && clearInterval(this.timer)
this.timer = setInterval(() => {
this.setActiveItem(this.model + 1)
}, this.interval)
}
ngAfterViewInit(): void {
const timer = setTimeout(() => {
this.children.forEach((item, index) => {
item.index = index
item.updateActive()
item.updateStyles()
})
// all labels are validated
this.hasLabel = !this.items.some(item => !item)
// auto play
this.autoplay && this.resetInterval()
clearTimeout(timer)
}, 0)
}
ngOnChanges(changes: SimpleChanges): void {
// not include model
if (!changes || !changes.model) return
this.setActiveItem(changes.model.currentValue)
}
ngOnDestroy(): void {
this.timer && clearInterval(this.timer)
}
}