-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
marks.component.ts
124 lines (104 loc) · 3.43 KB
/
marks.component.ts
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
119
120
121
122
123
124
/**
* 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
*/
import { NgForOf, NgStyle } from '@angular/common';
import { ChangeDetectionStrategy, Component, Input, OnChanges, SimpleChanges, ViewEncapsulation } from '@angular/core';
import { BooleanInput, NgStyleInterface } from 'ng-zorro-antd/core/types';
import { InputBoolean } from 'ng-zorro-antd/core/util';
import { NzDisplayedMark, NzExtendedMark, NzMark, NzMarkObj } from './typings';
@Component({
changeDetection: ChangeDetectionStrategy.OnPush,
encapsulation: ViewEncapsulation.None,
preserveWhitespaces: false,
selector: 'nz-slider-marks',
exportAs: 'nzSliderMarks',
template: `
<span
class="ant-slider-mark-text"
*ngFor="let attr of marks; trackBy: trackById"
[class.ant-slider-mark-active]="attr.active"
[ngStyle]="attr.style!"
[innerHTML]="attr.label"
></span>
`,
imports: [NgStyle, NgForOf],
standalone: true,
host: {
class: 'ant-slider-mark'
}
})
export class NzSliderMarksComponent implements OnChanges {
static ngAcceptInputType_vertical: BooleanInput;
static ngAcceptInputType_included: BooleanInput;
@Input() lowerBound: number | null = null;
@Input() upperBound: number | null = null;
@Input() marksArray: NzExtendedMark[] = [];
@Input() min!: number;
@Input() max!: number;
@Input() @InputBoolean() vertical = false;
@Input() @InputBoolean() included = false;
@Input() reverse!: boolean;
marks: NzDisplayedMark[] = [];
ngOnChanges(changes: SimpleChanges): void {
const { marksArray, lowerBound, upperBound, reverse } = changes;
if (marksArray || reverse) {
this.buildMarks();
}
if (marksArray || lowerBound || upperBound || reverse) {
this.togglePointActive();
}
}
trackById(_index: number, mark: NzDisplayedMark): number {
return mark.value;
}
private buildMarks(): void {
const range = this.max - this.min;
this.marks = this.marksArray.map(mark => {
const { value, offset, config } = mark;
const style = this.getMarkStyles(value, range, config);
const label = isConfigObject(config) ? config.label : config;
return {
label,
offset,
style,
value,
config,
active: false
};
});
}
private getMarkStyles(value: number, range: number, config: NzMark): NgStyleInterface {
let style;
const markValue = this.reverse ? this.max + this.min - value : value;
if (this.vertical) {
style = {
marginBottom: '-50%',
bottom: `${((markValue - this.min) / range) * 100}%`
};
} else {
style = {
transform: `translate3d(-50%, 0, 0)`,
left: `${((markValue - this.min) / range) * 100}%`
};
}
if (isConfigObject(config) && config.style) {
style = { ...style, ...config.style };
}
return style;
}
private togglePointActive(): void {
if (this.marks && this.lowerBound !== null && this.upperBound !== null) {
this.marks.forEach(mark => {
const value = mark.value;
const isActive =
(!this.included && value === this.upperBound) ||
(this.included && value <= this.upperBound! && value >= this.lowerBound!);
mark.active = isActive;
});
}
}
}
function isConfigObject(config: NzMark): config is NzMarkObj {
return typeof config !== 'string';
}