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

atinc / ngx-tethys / edbc1d43-1648-411a-a6bc-f24c9aa3f654

27 Mar 2025 06:13AM UTC coverage: 90.236% (+0.06%) from 90.179%
edbc1d43-1648-411a-a6bc-f24c9aa3f654

push

circleci

web-flow
Merge pull request #3282 from atinc/v19.0.0-next

5598 of 6865 branches covered (81.54%)

Branch coverage included in aggregate %.

8 of 8 new or added lines in 7 files covered. (100.0%)

157 existing lines in 46 files now uncovered.

13357 of 14141 relevant lines covered (94.46%)

992.51 hits per line

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

93.68
/src/switch/switch.component.ts
1
import { NgClass } from '@angular/common';
2
import {
3
    ChangeDetectionStrategy,
4
    ChangeDetectorRef,
5
    Component,
6
    ElementRef,
7
    EventEmitter,
8
    forwardRef,
9
    Input,
10
    OnInit,
11
    Output,
12
    ViewChild,
13
    inject
1✔
14
} from '@angular/core';
15
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
8✔
16
import { TabIndexDisabledControlValueAccessorMixin } from 'ngx-tethys/core';
4✔
17
import { coerceBooleanProperty } from 'ngx-tethys/util';
18

8✔
19
/**
8✔
20
 * 开关组件
3✔
21
 * @name thy-switch
22
 * @order 10
23
 */
24
@Component({
6!
25
    selector: 'thy-switch',
×
26
    templateUrl: './switch.component.html',
27
    changeDetection: ChangeDetectionStrategy.OnPush,
6✔
28
    providers: [
6✔
29
        {
1✔
30
            provide: NG_VALUE_ACCESSOR,
31
            useExisting: forwardRef(() => ThySwitch),
6!
32
            multi: true
×
33
        }
34
    ],
35
    imports: [NgClass],
36
    host: {
7✔
37
        class: 'thy-switch',
7✔
38
        '[class.thy-switch-xs]': 'size === "xs"',
39
        '[class.thy-switch-sm]': 'size === "sm"'
40
    }
706✔
41
})
42
export class ThySwitch extends TabIndexDisabledControlValueAccessorMixin implements OnInit, ControlValueAccessor {
43
    cdr = inject(ChangeDetectorRef);
6✔
44

6✔
45
    public model: boolean;
1✔
46

47
    public type?: string = 'primary';
6✔
48

1✔
49
    public size?: string = '';
1✔
50

51
    public disabled?: boolean = false;
52

53
    public loading: boolean = false;
327✔
54

327✔
55
    public classNames: string[];
327✔
56

327✔
57
    public typeArray: string[] = ['primary', 'info', 'warning', 'danger'];
327✔
58

327✔
59
    public sizeArray: string[] = ['', 'sm', 'xs'];
327✔
60

327✔
61
    public loadingCircle: {
327✔
62
        viewBox?: string;
327✔
63
        cx?: number;
327✔
64
        cy?: number;
327✔
65
        r?: number;
327✔
66
        dasharray?: string;
327✔
67
    } = {};
327✔
68

69
    private initialized = false;
70

323✔
71
    private loadingInitialized = false;
323✔
72

73
    private isDisabledFirstChange = true;
74

646✔
75
    @ViewChild('switch', { static: true }) switchElementRef: ElementRef;
646✔
76

77
    /**
78
     * 类型,目前分为: 'primary' |'info' | 'warning' | 'danger'
79
     */
323✔
80
    @Input()
81
    set thyType(value: string) {
82
        if (!this.typeArray.includes(value)) {
323✔
83
            value = 'primary';
84
        }
85
        this.type = value;
328✔
86
        if (this.initialized) {
328✔
87
            this.setClassNames();
328✔
88
        }
89
    }
90

1✔
91
    /**
1✔
92
     * 大小
1✔
93
     * @type xs | sm | md
1✔
94
     * @default md
95
     */
96
    @Input()
663✔
97
    set thySize(value: string) {
663✔
98
        if (!this.sizeArray.includes(value)) {
4✔
99
            value = '';
100
        }
663✔
101
        this.size = value;
8✔
102
        if (this.initialized) {
8!
UNCOV
103
            this.setClassNames();
×
104
        }
105

106
        if (this.loadingInitialized) {
663✔
107
            this.setLoadingCircle();
108
        }
109
    }
1✔
110

111
    /**
112
     * 是否属于禁用状态
113
     */
114
    @Input({ transform: coerceBooleanProperty })
1✔
115
    override set thyDisabled(value: boolean) {
1✔
116
        this.disabled = value;
1✔
117
        this.setClassNames();
1✔
118
    }
119
    override get thyDisabled(): boolean {
120
        return this.disabled;
121
    }
122

123
    /**
124
     * 是否加载中
1✔
125
     */
126
    @Input({ transform: coerceBooleanProperty }) set thyLoading(value: boolean) {
1✔
127
        this.loading = value;
1✔
128
        if (this.initialized) {
129
            this.setClassNames();
130
        }
131

132
        if (this.loading && !this.loadingInitialized) {
133
            this.setLoadingCircle();
134
            this.loadingInitialized = true;
135
        }
136
    }
1✔
137

138
    /**
139
     * 数据变化的回调事件,即将被弃用,请使用 ngModelChange
140
     * @deprecated
141
     */
142
    @Output() thyChange: EventEmitter<Event> = new EventEmitter<Event>();
143

144
    constructor() {
327✔
145
        super();
146
    }
147

148
    ngOnInit() {
149
        this.setClassNames();
150
        this.initialized = true;
151
    }
152

153
    public onModelChange: Function = () => {};
154

155
    public onModelTouched: Function = () => {};
156

157
    writeValue(value: boolean) {
158
        this.model = value;
159
        this.cdr.markForCheck();
160
        // this.setClassNames();
161
    }
162

163
    registerOnChange(fn: Function): void {
164
        this.onModelChange = fn;
165
    }
166

167
    registerOnTouched(fn: Function): void {
168
        this.onModelTouched = fn;
169
    }
170

171
    setDisabledState(isDisabled: boolean): void {
172
        this.disabled = (this.isDisabledFirstChange && this.thyDisabled) || isDisabled;
173
        this.isDisabledFirstChange = false;
174
        this.setClassNames();
175
    }
176

177
    toggle(event: Event) {
178
        this.model = !this.model;
179
        this.onModelChange(this.model);
180
        this.onModelTouched();
181
        this.thyChange.emit(event);
182
    }
183

184
    setClassNames() {
185
        this.classNames = [`thy-switch-${this.type}`];
186
        if (this.size) {
187
            this.classNames.push(`thy-switch-${this.size}`);
188
        }
189
        if (this.disabled || this.loading) {
190
            this.classNames.push(`thy-switch-disabled`);
191
            if (this.model) {
192
                this.classNames.push(`thy-switch-disabled-true`);
193
            }
194
        }
195
        this.cdr.markForCheck();
196
    }
197

198
    setLoadingCircle() {
199
        const svgSize = {
200
            ['xs']: 12,
201
            ['sm']: 16,
202
            ['']: 20
203
        };
204

205
        const circleSize = svgSize[this.size];
206
        const centerPoint = circleSize / 2;
207
        const r = circleSize / 4;
208

209
        this.loadingCircle = {
210
            viewBox: `0 0 ${circleSize} ${circleSize}`,
211
            cx: centerPoint,
212
            cy: centerPoint,
213
            r: r,
214
            dasharray: `${2 * Math.PI * r * 0.75} ${2 * Math.PI * r * 0.25}`
215
        };
216
        this.cdr.markForCheck();
217
    }
218
}
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