-
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathcarousel-item.ts
More file actions
88 lines (76 loc) · 2.35 KB
/
carousel-item.ts
File metadata and controls
88 lines (76 loc) · 2.35 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
import { Component, ElementRef, forwardRef, Inject, Input, OnInit } from '@angular/core'
import { SafeStyle, DomSanitizer } from '@angular/platform-browser'
import { ElCarousel } from './carousel'
import { fadeAnimation } from '../shared/animation'
import { removeNgTag } from '../shared/utils'
@Component({
selector: 'el-carousel-item',
animations: [fadeAnimation],
template: `
<div class="el-carousel__item"
[class.is-active]="isActive"
[class.el-carousel__item--card]="root.type === 'card'"
[class.is-animating]="isAnimating"
[style]="styles">
<!--<div class="el-carousel__mask" *ngIf="root.type === 'card'"-->
<!--[@fadeAnimation]="isActive()">-->
<!--</div>-->
<ng-content></ng-content>
</div>
`,
})
export class ElCarouselItem implements OnInit {
// parent component will set index
@Input() index: number
@Input() label: string = ''
// oninit set
width: number
preTranslate: number
isAnimating: boolean
isActive: boolean = false
styles: SafeStyle
constructor(
@Inject(forwardRef(() => ElCarousel)) public root: ElCarousel,
private sanitizer: DomSanitizer,
private el: ElementRef,
) {
}
updateActive(): void {
const isActive: boolean = this.root.model === this.index
if (this.isActive !== isActive) {
this.isActive = isActive
}
}
updateStyles(): void {
const map: any = {
'1': 0 - this.width,
'-1': this.width,
'2': this.width,
'-2': 0 - this.width,
'0': 0,
}
const offset: number = this.root.model - this.index
const translate = map[offset]
const styles: string = `transform: translateX(${translate}px);`
// change direction disable animation
const changeDirection: boolean = (this.preTranslate < 0 && translate > 0)
|| (this.preTranslate > 0 && translate < 0)
this.isAnimating = !changeDirection
this.styles = this.sanitizer.bypassSecurityTrustStyle(styles)
// save current value
this.preTranslate = translate
}
update(): void {
this.updateStyles()
this.updateActive()
}
ngOnInit(): void {
// collect items
this.root.items.push(this.label)
this.width = this.el.nativeElement.children[0].offsetWidth
removeNgTag(this.el.nativeElement)
// manually update
this.root.subscriber.push(() => this.update())
this.update()
}
}