• 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

86.46
/src/empty/empty.component.ts
1
import { ThyTranslate } from 'ngx-tethys/core';
2
import { useHostRenderer } from '@tethys/cdk/dom';
3
import {
4
    AfterViewInit,
5
    Component,
6
    ContentChild,
7
    ElementRef,
8
    OnChanges,
9
    Input,
10
    NgZone,
11
    OnInit,
12
    TemplateRef,
13
    SimpleChanges,
1✔
14
    inject,
15
    Signal
16
} from '@angular/core';
17

18
import { ThyEmptyConfig } from './empty.config';
1✔
19
import { PRESET_SVG } from './svgs';
20
import { DomSanitizer } from '@angular/platform-browser';
21
import { SafeAny } from 'ngx-tethys/types';
22
import { ThyIcon } from 'ngx-tethys/icon';
23
import { NgClass, NgTemplateOutlet } from '@angular/common';
24
import { coerceBooleanProperty } from 'ngx-tethys/util';
25
import { injectLocale, ThyEmptyLocale } from 'ngx-tethys/i18n';
26

27
const sizeClassMap = {
28
    lg: ['thy-empty-state', 'thy-empty-state--lg'],
29
    md: ['thy-empty-state'],
30
    sm: ['thy-empty-state', 'thy-empty-state--sm']
31
};
32

33
const sizeMap = {
34
    lg: {
35
        height: 168, // 空提示的高度
36
        offsetTop: 30, // 空提示图标和大小之间的空白距离,需要除去,否则会不居中
37
        defaultMarginTop: 120 // 不自动计算默认的 top 距离
38
    },
39
    md: {
40
        height: 118,
1✔
41
        offsetTop: 20,
42
        defaultMarginTop: 10
33✔
43
    },
33✔
44
    sm: {
33✔
45
        height: 78,
33✔
46
        offsetTop: 10,
33✔
47
        defaultMarginTop: 10
33✔
48
    }
33✔
49
};
33✔
50

33✔
51
/** https://html.spec.whatwg.org/multipage/embedded-content.html#attr-img-loading */
52
export type ThyEmptyImageLoading = 'eager' | 'lazy';
53

36✔
54
/** https://wicg.github.io/priority-hints/#idl-index */
36✔
55
export type ThyEmptyImageFetchPriority = 'high' | 'low' | 'auto';
3✔
56

57
/**
58
 * 空页面组件
59
 * @name thy-empty
104✔
60
 * @order 10
90✔
61
 */
62
@Component({
14✔
63
    selector: 'thy-empty',
2✔
64
    templateUrl: './empty.component.html',
65
    imports: [ThyIcon, NgClass, NgTemplateOutlet]
12✔
66
})
2✔
67
export class ThyEmpty implements OnInit, AfterViewInit, OnChanges {
68
    private thyTranslate = inject(ThyTranslate);
69
    private thyEmptyConfig = inject(ThyEmptyConfig);
70
    private elementRef = inject(ElementRef);
10✔
71
    private ngZone = inject(NgZone);
2✔
72
    private sanitizer = inject(DomSanitizer);
73
    private locale: Signal<ThyEmptyLocale> = injectLocale('empty');
74

75
    /**
8!
UNCOV
76
     * 显示文本提示信息。同时传入 thyMessage,thyTranslationKey,thyEntityName,thyEntityNameTranslateKey 时优先级最高
×
77
     * @default 暂无数据
78
     */
79
    @Input() thyMessage: string;
8✔
80

81
    /**
82
     * 已废弃。显示文本提示信息多语言 Key。同时传入 thyTranslationKey,thyEntityName,thyEntityNameTranslateKey 时优先级最高
83
     * @deprecated
33✔
84
     */
33✔
85
    @Input() thyTranslationKey: string;
33✔
86

87
    /**
11!
88
     * 已废弃。显示文本提示信息多语言 Key 的 Values。传入 thyTranslationKey 后,传入这个才会生效
89
     * @deprecated
11✔
90
     */
91
    @Input() thyTranslationValues: any;
11!
UNCOV
92

×
93
    /**
94
     * 已废弃。显示默认提示信息,替换默认提示信息的目标对象,比如:没有 {thyEntityName}。同时传入 thyEntityName,thyEntityNameTranslateKey 时优先级较高
11✔
95
     * @deprecated
96
     */
11!
97
    @Input() thyEntityName: string;
11✔
98

99
    /**
100
     * 已废弃。thyEntityName 的多语言 Key。thyMessage,thyTranslationKey,thyEntityName 均未传入时才会生效
101
     * @deprecated
22!
UNCOV
102
     */
×
103
    @Input() thyEntityNameTranslateKey: string;
104

105
    /**
22✔
106
     * 提示图标名
107
     */
108
    @Input() thyIconName: string;
33!
UNCOV
109

×
110
    /**
111
     * 大小
112
     * @type sm | md | lg
113
     * @default md
33✔
114
     */
33✔
115
    @Input()
33✔
116
    set thySize(value: string) {
117
        this.size = value;
118
        if (this._initialized) {
36✔
119
            this.updateClass();
36!
120
        }
36✔
121
    }
122

123
    /**
124
     * 距上距离
33✔
125
     */
33✔
126
    @Input() thyMarginTop: number | string;
33✔
127

128
    /**
129
     * 是否自动根据父容器计算高度,垂直居中
130
     * @default false
131
     */
42!
UNCOV
132
    @Input({ transform: coerceBooleanProperty }) thyTopAuto: boolean;
×
133

134
    /**
135
     * 自动计算高度垂直居中(即 thyTopAuto 为 true)时,支持传入自定义父容器
136
     */
33✔
137
    @Input() thyContainer: ElementRef;
33✔
138

33✔
139
    /**
140
     * 提示图片链接
1✔
141
     */
142
    @Input() thyImageUrl: string;
143

144
    @Input() thyImageLoading?: ThyEmptyImageLoading;
145

146
    @Input() thyImageFetchPriority?: ThyEmptyImageFetchPriority;
147

148
    /**
149
     * 显示文本描述
150
     */
151
    @Input() thyDescription: string;
152

153
    private size: string = 'md';
154

155
    private _initialized = false;
156

157
    private hostRenderer = useHostRenderer();
158

1✔
159
    presetSvg: SafeAny;
160

161
    /**
162
     * 除提示图片,文本外的其他信息传入模板
163
     * @type TemplateRef
164
     */
165
    @ContentChild('extra') extraTemplateRef: TemplateRef<any>;
166

167
    get displayText() {
168
        if (this.thyMessage) {
169
            return this.thyMessage;
170
        } else if (this.thyTranslationKey) {
171
            return this.thyTranslate.instant(this.thyTranslationKey, this.thyTranslationValues);
172
        } else if (this.thyEntityName) {
173
            return this.thyTranslate.instant(this.thyEmptyConfig.noResultWithTargetTranslateKey, {
174
                target: this.thyEntityName
175
            });
176
        } else if (this.thyEntityNameTranslateKey) {
177
            return this.thyTranslate.instant(this.thyEmptyConfig.noResultWithTargetTranslateKey, {
178
                target: this.thyTranslate.instant(this.thyEntityNameTranslateKey)
179
            });
180
        } else if (this.thyTranslate.instant(this.thyEmptyConfig.noResultTranslateKey) !== 'common.tips.NO_RESULT') {
181
            return this.thyTranslate.instant(this.thyEmptyConfig.noResultTranslateKey);
182
        } else {
183
            return this.locale().noDataText;
184
        }
185
    }
186

187
    private _calculatePosition() {
188
        const sizeOptions = sizeMap[this.thySize || 'md'];
189
        let marginTop = null;
190
        if (this.thyTopAuto) {
191
            // 选择参考父容器居中
192
            const containerElement = this.thyContainer ? this.thyContainer.nativeElement : this.elementRef.nativeElement.parentElement;
193
            // containerElement.height;
194
            let emptyStateHeight = this.elementRef.nativeElement.offsetHeight;
195
            // 高度没有自动计算出来使用默认值
196
            if (emptyStateHeight <= 10) {
197
                emptyStateHeight = sizeOptions.height;
198
            }
199
            marginTop = (containerElement.offsetHeight - emptyStateHeight) / 2 - sizeOptions.offsetTop;
200
            // marginTop = (containerElement.offsetHeight - emptyStateHeight) / 2;
201
            if (marginTop < 0) {
202
                marginTop = 0; // sizeOptions.defaultMarginTop;
203
            }
204
        } else {
205
            if (this.thyMarginTop) {
206
                marginTop = this.thyMarginTop;
207
            } else {
208
                marginTop = 0; // sizeOptions.defaultMarginTop;
209
            }
210
        }
211
        if (marginTop) {
212
            this.hostRenderer.setStyle('marginTop', marginTop + 'px');
213
        }
214
    }
215

216
    ngOnInit() {
217
        this.updateClass();
218
        this._initialized = true;
219
        this.setPresetSvg(this.thyIconName);
220
    }
221

222
    updateClass() {
223
        const classList = sizeClassMap[this.size] || sizeClassMap['md'];
224
        if (classList) {
225
            this.hostRenderer.updateClass(classList);
226
        }
227
    }
228

229
    ngAfterViewInit() {
230
        this.ngZone.runOutsideAngular(() => {
231
            setTimeout(() => {
232
                this._calculatePosition();
233
            }, 50);
234
        });
235
    }
236

237
    ngOnChanges(changes: SimpleChanges): void {
238
        if (changes.thyIconName && changes.thyIconName.currentValue && !changes.thyIconName.firstChange) {
239
            this.setPresetSvg(changes.thyIconName.currentValue);
240
        }
241
    }
242

243
    setPresetSvg(icon: string) {
244
        this.presetSvg = '';
245
        let presetSvg = icon ? PRESET_SVG[icon] : PRESET_SVG.default;
246

247
        this.presetSvg = presetSvg ? this.sanitizer.bypassSecurityTrustHtml(presetSvg) : '';
248
    }
249
}
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