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

atinc / ngx-tethys / 68ef226c-f83e-44c1-b8ed-e420a83c5d84

28 May 2025 10:31AM UTC coverage: 10.352% (-80.0%) from 90.316%
68ef226c-f83e-44c1-b8ed-e420a83c5d84

Pull #3460

circleci

pubuzhixing8
chore: xxx
Pull Request #3460: refactor(icon): migrate signal input #TINFR-1476

132 of 6823 branches covered (1.93%)

Branch coverage included in aggregate %.

10 of 14 new or added lines in 1 file covered. (71.43%)

11648 existing lines in 344 files now uncovered.

2078 of 14525 relevant lines covered (14.31%)

6.69 hits per line

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

4.92
/src/image/image.directive.ts
1
import {
2
    Directive,
3
    ElementRef,
4
    InjectFlags,
5
    Input,
6
    OnChanges,
7
    OnInit,
8
    SimpleChanges,
9
    Injector,
10
    OnDestroy,
11
    AfterViewInit,
1✔
12
    inject
UNCOV
13
} from '@angular/core';
×
UNCOV
14
import { IThyImageDirective, IThyImageGroupComponent, THY_IMAGE_GROUP_COMPONENT } from './image.token';
×
UNCOV
15
import { ThyImageMeta } from './image.class';
×
UNCOV
16
import { ThyImageService } from './image.service';
×
17
import { coerceBooleanProperty } from 'ngx-tethys/util';
18

UNCOV
19
/**
×
20
 * thyImage: 预览图片指令,只可绑定到 img 标签上
21
 * @name img[thyImage]
UNCOV
22
 * @order 10
×
23
 */
24
@Directive({
UNCOV
25
    selector: 'img[thyImage]',
×
UNCOV
26
    exportAs: 'thyImage',
×
27
    host: {
28
        '(click)': 'onPreview($event)',
29
        class: 'thy-image',
UNCOV
30
        '[class.thy-image-disabled]': 'thyDisablePreview'
×
31
    }
UNCOV
32
})
×
UNCOV
33
export class ThyImageDirective implements IThyImageDirective, OnInit, OnChanges, AfterViewInit, OnDestroy {
×
UNCOV
34
    private thyImageService = inject(ThyImageService);
×
UNCOV
35
    private injector = inject(Injector);
×
36
    private elementRef = inject(ElementRef);
UNCOV
37

×
38
    /**
39
     * 图片地址
40
     */
UNCOV
41
    @Input() thySrc: string;
×
UNCOV
42

×
UNCOV
43
    /**
×
UNCOV
44
     * 预览图片地址
×
UNCOV
45
     */
×
UNCOV
46
    @Input() thyPreviewSrc: string;
×
47

48
    /**
49
     * 图片原图地址
×
50
     */
51
    @Input() thyOriginSrc: string;
52

53
    /**
UNCOV
54
     * 图片附加信息,包含 { name: string, size?: string | number; }
×
UNCOV
55
     */
×
UNCOV
56
    @Input() thyImageMeta: ThyImageMeta;
×
57

58
    /**
59
     * 是否禁止预览
UNCOV
60
     * @default false
×
UNCOV
61
     */
×
62
    @Input({ transform: coerceBooleanProperty }) thyDisablePreview: boolean;
UNCOV
63

×
64
    /**
×
65
     * 是否自动计算图片资源大小
×
66
     */
×
67
    @Input({ transform: coerceBooleanProperty }) thyResolveSize = false;
68

69
    get previewable(): boolean {
70
        return !this.thyDisablePreview;
71
    }
72

×
73
    private parentGroup: IThyImageGroupComponent;
×
74

75
    ngOnInit(): void {
76
        this.getParentGroup();
77
    }
78

UNCOV
79
    ngAfterViewInit(): void {
×
80
        if (this.parentGroup) {
81
            this.addParentImage();
×
82
        }
83
    }
84

85
    getParentGroup() {
86
        while (true) {
87
            // 多层 thy-image-group 嵌套时,获取最外层 thy-image-group 下的所有图片
UNCOV
88
            const injector = this.parentGroup?.injector || this.injector;
×
89
            const parentGroup = injector.get(THY_IMAGE_GROUP_COMPONENT, null, InjectFlags.SkipSelf);
90
            if (!parentGroup) {
91
                break;
UNCOV
92
            }
×
UNCOV
93
            this.parentGroup = parentGroup;
×
UNCOV
94
        }
×
95
    }
96

97
    addParentImage() {
1✔
98
        setTimeout(() => {
99
            const parentElement: HTMLElement = this.parentGroup.element.nativeElement;
100
            const images = parentElement.querySelectorAll('img[thyImage]');
101
            const index = Array.prototype.indexOf.call(images, this.elementRef.nativeElement);
102
            if (index >= 0) {
103
                this.parentGroup.addImage(this, index);
104
            } else {
105
                this.parentGroup.addImage(this, this.parentGroup.images.length);
106
            }
1✔
107
        });
108
    }
109

110
    ngOnChanges(changes: SimpleChanges): void {
111
        const { thySrc } = changes;
112
        if (thySrc) {
113
            this.elementRef.nativeElement.src = thySrc.currentValue;
114
        }
115
    }
116

117
    onPreview(event: MouseEvent) {
118
        if (!this.previewable || event.button !== 0) {
119
            return;
120
        }
121
        if (this.parentGroup) {
122
            const previewAbleImages = this.parentGroup.images.filter(e => e.previewable);
123
            const previewImages = previewAbleImages.map(e => ({
124
                src: e.thyPreviewSrc || e.thySrc,
125
                ...e.thyImageMeta,
126
                origin: {
127
                    src: e.thyOriginSrc
128
                }
129
            }));
130
            const startIndex = previewAbleImages.findIndex(el => this === el);
131
            this.thyImageService.preview(previewImages, {
132
                startIndex,
133
                resolveSize: this.thyResolveSize
134
            });
135
        } else {
136
            const previewImages = [
137
                {
138
                    src: this.thyPreviewSrc || this.thySrc,
139
                    ...this.thyImageMeta,
140
                    origin: {
141
                        src: this.thyOriginSrc
142
                    }
143
                }
144
            ];
145
            this.thyImageService.preview(previewImages, { resolveSize: this.thyResolveSize });
146
        }
147
    }
148

149
    ngOnDestroy(): void {
150
        if (this.parentGroup) {
151
            const index = this.parentGroup.images.findIndex(item => item === this);
152
            this.parentGroup.removeImage(index);
153
        }
154
    }
155
}
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