• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

atinc / ngx-tethys / 5fa9630c-19a1-4ee7-af3d-6a0c3535952a

08 Oct 2024 08:24AM UTC coverage: 90.438% (+0.007%) from 90.431%
5fa9630c-19a1-4ee7-af3d-6a0c3535952a

push

circleci

minlovehua
refactor: refactor all control-flow directives to new control-flow #TINFR-381

5511 of 6738 branches covered (81.79%)

Branch coverage included in aggregate %.

98 of 104 new or added lines in 58 files covered. (94.23%)

113 existing lines in 17 files now uncovered.

13253 of 14010 relevant lines covered (94.6%)

991.73 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.96
/src/time-picker/time-picker-panel.component.ts
1
import {
2
    ChangeDetectionStrategy,
3
    ChangeDetectorRef,
4
    Component,
5
    ElementRef,
6
    EventEmitter,
7
    forwardRef,
8
    Input,
9
    NgZone,
10
    OnDestroy,
11
    OnInit,
12
    Output,
13
    ViewChild
14
} from '@angular/core';
1✔
15
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
16
import { isValid } from 'date-fns';
25✔
17
import { reqAnimFrame } from 'ngx-tethys/core';
24✔
18
import { coerceBooleanProperty, TinyDate } from 'ngx-tethys/util';
24!
19
import { ThyButton } from 'ngx-tethys/button';
24✔
20
import { DecimalPipe } from '@angular/common';
24✔
21

22
/**
23
 * 时间选择面板组件
1✔
24
 * @name thy-time-picker-panel
1✔
25
 */
1✔
26
@Component({
27
    selector: 'thy-time-picker-panel',
75✔
28
    templateUrl: './time-picker-panel.component.html',
25✔
29
    changeDetection: ChangeDetectionStrategy.OnPush,
30
    providers: [
31
        {
23✔
32
            provide: NG_VALUE_ACCESSOR,
23✔
33
            multi: true,
23✔
34
            useExisting: forwardRef(() => ThyTimePanel)
23✔
35
        }
23✔
36
    ],
23✔
37
    host: {
23✔
38
        class: 'thy-time-picker-panel',
23✔
39
        '[class.thy-time-picker-panel-has-bottom-operation]': `thyShowOperations`,
23✔
40
        '[class.thy-time-picker-panel-columns-2]': `showColumnCount === 2`,
41
        '[class.thy-time-picker-panel-columns-3]': `showColumnCount === 3`
23✔
42
    },
23✔
43
    standalone: true,
23✔
44
    imports: [ThyButton, DecimalPipe]
23✔
45
})
23✔
46
export class ThyTimePanel implements OnInit, OnDestroy, ControlValueAccessor {
23✔
47
    @ViewChild('hourListElement', { static: false }) hourListRef: ElementRef<HTMLElement>;
23✔
48

23✔
49
    @ViewChild('minuteListElement', { static: false }) minuteListRef: ElementRef<HTMLElement>;
23✔
50

23✔
51
    @ViewChild('secondListElement', { static: false }) secondListRef: ElementRef<HTMLElement>;
23✔
52

23✔
53
    /**
54
     * 展示的日期格式,支持 'HH:mm:ss' | 'HH:mm' | 'mm:ss'
55
     * @type string
23✔
56
     * @default HH:mm:ss
23✔
57
     */
23✔
58
    @Input() set thyFormat(value: string) {
23✔
59
        if (value) {
60
            const formatSet = new Set(value);
61
            this.showHourColumn = formatSet.has('H') || formatSet.has('h');
62
            this.showMinuteColumn = formatSet.has('m');
23✔
63
            this.showSecondColumn = formatSet.has('s');
23✔
64
        } else {
23✔
65
            this.showHourColumn = true;
66
            this.showMinuteColumn = true;
67
            this.showSecondColumn = true;
4✔
68
        }
4✔
69
        this.showColumnCount = [this.showHourColumn, this.showMinuteColumn, this.showSecondColumn].filter(m => m).length;
4✔
70
        this.cdr.markForCheck();
4✔
71
    }
72

73
    /**
3✔
74
     * 小时间隔步长
3✔
75
     * @type number
3✔
76
     */
3✔
77
    @Input() thyHourStep: number = 1;
78

79
    /**
3✔
80
     * 分钟间隔步长
3✔
81
     * @type number
3✔
82
     */
3✔
83
    @Input() thyMinuteStep: number = 1;
84

85
    /**
1✔
86
     * 秒间隔步长
1✔
87
     * @type number
1✔
88
     */
1✔
89
    @Input() thySecondStep: number = 1;
90

91
    /**
2!
92
     * 展示选择此刻
2✔
93
     * @type boolean
94
     */
10!
95
    @Input({ transform: coerceBooleanProperty }) thyShowSelectNow = true;
121✔
96

121✔
97
    /**
98
     * 展示底部操作
99
     * @type boolean
60✔
100
     */
31✔
101
    @Input({ transform: coerceBooleanProperty }) thyShowOperations = true;
31✔
102

103
    /**
104
     * 选择时间触发的事件
29✔
105
     * @type EventEmitter<Date>
106
     */
60✔
107
    @Output() thyPickChange = new EventEmitter<Date>();
60✔
108

109
    /**
110
     * 关闭面板事件
23✔
111
     * @type EventEmitter<void>
112
     */
113
    @Output() thyClosePanel = new EventEmitter<void>();
23✔
114

115
    // margin-top + 1px border
116
    SCROLL_OFFSET_SPACING = 5;
52✔
117

52✔
118
    SCROLL_DEFAULT_DURATION = 120;
52✔
119

52✔
120
    prefixCls = 'thy-time-picker-panel';
121

180✔
122
    hourRange: ReadonlyArray<{ value: number; disabled: boolean }> = [];
69✔
123

3,052✔
124
    minuteRange: ReadonlyArray<{ value: number; disabled: boolean }> = [];
3,052✔
125

126
    secondRange: ReadonlyArray<{ value: number; disabled: boolean }> = [];
127

128
    showHourColumn = true;
129

130
    showMinuteColumn = true;
131

32✔
132
    showSecondColumn = true;
32✔
133

32✔
134
    showColumnCount: number = 3;
135

136
    value: Date;
23!
137

23✔
138
    hour: number;
139

23!
140
    minute: number;
23✔
141

142
    second: number;
23!
143

23✔
144
    initialScrollPosition: boolean;
145

23✔
146
    onValueChangeFn: (val: Date) => void = () => void 0;
147

×
148
    onTouchedFn: () => void = () => void 0;
265✔
149

114✔
150
    constructor(
114✔
151
        private cdr: ChangeDetectorRef,
152
        private ngZone: NgZone
151✔
153
    ) {}
151✔
154

151✔
155
    ngOnInit(): void {
151✔
156
        this.generateTimeRange();
144✔
157
        this.initialValue();
144!
UNCOV
158
        setTimeout(() => {
×
159
            this.initialScrollPosition = true;
160
        });
144✔
161
    }
162

163
    generateTimeRange() {
164
        this.hourRange = this.buildTimeRange(24, this.thyHourStep);
×
165
        this.minuteRange = this.buildTimeRange(60, this.thyMinuteStep);
60✔
166
        this.secondRange = this.buildTimeRange(60, this.thySecondStep);
153✔
167
    }
168

60✔
169
    pickHours(hours: { value: number; disabled: boolean }, index: number) {
291✔
170
        this.value.setHours(hours.value);
171
        this.hour = hours.value;
60✔
172
        this.thyPickChange.emit(this.value);
287✔
173
        this.scrollTo(this.hourListRef.nativeElement, index);
174
    }
175

176
    pickMinutes(minutes: { value: number; disabled: boolean }, index: number) {
177
        this.value.setMinutes(minutes.value);
23✔
178
        this.minute = minutes.value;
23✔
179
        this.thyPickChange.emit(this.value);
180
        this.scrollTo(this.minuteListRef.nativeElement, index);
181
    }
1✔
182

183
    pickSeconds(seconds: { value: number; disabled: boolean }, index: number) {
184
        this.value.setSeconds(seconds.value);
185
        this.second = seconds.value;
1✔
186
        this.thyPickChange.emit(this.value);
187
        this.scrollTo(this.secondListRef.nativeElement, index);
188
    }
189

190
    selectNow() {
191
        this.value = new Date();
192
        this.setHMSProperty();
193
        this.thyPickChange.emit(this.value);
194
        this.thyClosePanel.emit();
195
    }
196

197
    confirmPickTime() {
198
        this.onValueChangeFn(this.value || new Date());
199
        this.thyClosePanel.emit();
1✔
200
    }
201

202
    scrollTo(container: HTMLElement, index: number = 0, duration: number = this.SCROLL_DEFAULT_DURATION) {
203
        const offsetTop = (container.children[index] as HTMLElement).offsetTop - this.SCROLL_OFFSET_SPACING;
204
        this.runScrollAnimationFrame(container, offsetTop, duration);
205
    }
206

207
    writeValue(value: Date | number): void {
208
        if (value && isValid(value)) {
23✔
209
            this.value = new Date(value);
210
            this.setHMSProperty();
211
        } else {
212
            this.initialValue();
213
        }
214
        this.autoScroll(this.initialScrollPosition ? this.SCROLL_DEFAULT_DURATION : 0);
215

216
        this.cdr.markForCheck();
217
    }
218

219
    registerOnChange(fn: (value: Date) => void): void {
220
        this.onValueChangeFn = fn;
221
    }
222

223
    registerOnTouched(fn: () => void): void {
224
        this.onTouchedFn = fn;
225
    }
226

227
    private initialValue() {
228
        this.hour = 0;
229
        this.minute = 0;
230
        this.second = 0;
231
        this.value = new TinyDate().setHms(0, 0, 0).nativeDate;
232
    }
233

234
    private buildTimeRange(length: number, step: number = 1, start: number = 0, disables: number[] = []) {
235
        return new Array(Math.ceil(length / step)).fill(0).map((_, i) => {
236
            const value = (i + start) * step;
237
            return {
238
                value: value,
239
                disabled: disables.indexOf(value) > -1
240
            };
241
        });
242
    }
243

244
    private setHMSProperty() {
245
        this.hour = this.value.getHours();
246
        this.minute = this.value.getMinutes();
247
        this.second = this.value.getSeconds();
248
    }
249

250
    private resetScrollPosition() {
251
        if (this.hourListRef) {
252
            this.hourListRef.nativeElement.scrollTop = 0;
253
        }
254
        if (this.minuteListRef) {
255
            this.minuteListRef.nativeElement.scrollTop = 0;
256
        }
257
        if (this.secondListRef) {
258
            this.secondListRef.nativeElement.scrollTop = 0;
259
        }
260
        this.initialScrollPosition = false;
261
    }
262

263
    private runScrollAnimationFrame(container: HTMLElement, to: number, duration: number = this.SCROLL_DEFAULT_DURATION) {
264
        if (duration <= 0) {
265
            container.scrollTop = to;
266
            return;
267
        }
268
        const offset = to - container.scrollTop;
269
        const frame = (offset / duration) * 10;
270
        this.ngZone.runOutsideAngular(() => {
271
            reqAnimFrame(() => {
272
                container.scrollTop += frame;
273
                if (container.scrollTop === to) {
274
                    return;
275
                }
276
                this.runScrollAnimationFrame(container, to, duration - 10);
277
            });
278
        });
279
    }
280

281
    private autoScroll(duration: number = this.SCROLL_DEFAULT_DURATION) {
282
        if (this.hourListRef) {
283
            this.scrollTo(
284
                this.hourListRef.nativeElement,
285
                this.hourRange.findIndex(m => m.value === this.hour),
286
                duration
287
            );
288
        }
289
        if (this.minuteListRef) {
290
            this.scrollTo(
291
                this.minuteListRef.nativeElement,
292
                this.minuteRange.findIndex(m => m.value === this.minute),
293
                duration
294
            );
295
        }
296
        if (this.secondListRef) {
297
            this.scrollTo(
298
                this.secondListRef.nativeElement,
299
                this.secondRange.findIndex(m => m.value === this.second),
300
                duration
301
            );
302
        }
303
    }
304

305
    ngOnDestroy(): void {
306
        // 关闭面板时有 0.2s 的动画,所以延迟 200ms 再重置滚动位置
307
        setTimeout(() => {
308
            this.resetScrollPosition();
309
        }, 200);
310
    }
311
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc