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

atinc / ngx-tethys / #55

30 Jul 2025 07:08AM UTC coverage: 9.866% (-80.4%) from 90.297%
#55

push

why520crazy
feat(empty): add setMessage for update display text #TINFR-2616

92 of 6794 branches covered (1.35%)

Branch coverage included in aggregate %.

2014 of 14552 relevant lines covered (13.84%)

6.15 hits per line

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

2.7
/src/nav/nav.component.ts
1
import {
2
    AfterContentChecked,
3
    AfterContentInit,
4
    AfterViewInit,
5
    ChangeDetectionStrategy,
6
    ChangeDetectorRef,
7
    Component,
8
    computed,
9
    contentChild,
10
    ContentChildren,
11
    contentChildren,
12
    DestroyRef,
13
    effect,
14
    ElementRef,
15
    inject,
16
    input,
17
    NgZone,
18
    OnChanges,
19
    OnDestroy,
1✔
20
    OnInit,
21
    QueryList,
22
    signal,
23
    Signal,
24
    SimpleChanges,
25
    TemplateRef,
26
    viewChild,
27
    WritableSignal
28
} from '@angular/core';
29
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
30
import { useHostRenderer } from '@tethys/cdk/dom';
31
import { ThyPopover, ThyPopoverConfig } from 'ngx-tethys/popover';
1✔
32
import { merge, Observable, of } from 'rxjs';
33
import { startWith, take, tap } from 'rxjs/operators';
34

35
import { NgClass, NgTemplateOutlet } from '@angular/common';
36
import { RouterLinkActive } from '@angular/router';
1✔
37
import { ThyPlacement } from 'ngx-tethys/core';
38
import { ThyDropdownMenuComponent, ThyDropdownMenuItemActiveDirective, ThyDropdownMenuItemDirective } from 'ngx-tethys/dropdown';
39
import { injectLocale, ThyNavLocale } from 'ngx-tethys/i18n';
40
import { ThyIcon } from 'ngx-tethys/icon';
41
import { coerceBooleanProperty } from 'ngx-tethys/util';
42
import { ThyNavInkBarDirective } from './nav-ink-bar.directive';
1✔
43
import { ThyNavItemDirective } from './nav-item.directive';
44
import { BypassSecurityTrustHtmlPipe } from './nav.pipe';
×
45

×
46
export type ThyNavType = 'pulled' | 'tabs' | 'pills' | 'lite' | 'card' | 'primary' | 'secondary' | 'thirdly' | 'secondary-divider';
47
export type ThyNavSize = 'lg' | 'md' | 'sm';
48
export type ThyNavHorizontal = '' | 'start' | 'center' | 'end';
×
49

50
const navTypeClassesMap = {
51
    pulled: ['thy-nav-pulled'],
×
52
    tabs: ['thy-nav-tabs'],
×
53
    pills: ['thy-nav-pills'],
54
    lite: ['thy-nav-lite'],
55
    card: ['thy-nav-card'],
×
56
    //如下类型已经废弃
×
57
    primary: ['thy-nav-primary'],
×
58
    secondary: ['thy-nav-secondary'],
59
    thirdly: ['thy-nav-thirdly'],
×
60
    'secondary-divider': ['thy-nav-secondary-divider']
×
61
};
62

×
63
const navSizeClassesMap = {
64
    lg: 'thy-nav-lg',
65
    md: 'thy-nav-md',
×
66
    sm: 'thy-nav-sm'
×
67
};
×
68

×
69
const tabItemRight = 20;
×
70

×
71
/**
×
72
 * 导航组件
73
 * @name thy-nav
74
 * @order 10
75
 */
76
@Component({
77
    selector: 'thy-nav',
×
78
    templateUrl: './nav.component.html',
×
79
    host: {
×
80
        '[class.thy-nav]': 'true',
×
81
        '[class.thy-nav--vertical]': 'thyVertical()',
×
82
        '[class.thy-nav--fill]': 'thyFill()'
×
83
    },
×
84
    changeDetection: ChangeDetectionStrategy.OnPush,
×
85
    imports: [
×
86
        NgClass,
×
87
        NgTemplateOutlet,
×
88
        ThyNavItemDirective,
×
89
        ThyIcon,
×
90
        ThyNavInkBarDirective,
×
91
        ThyDropdownMenuComponent,
×
92
        ThyDropdownMenuItemDirective,
×
93
        ThyDropdownMenuItemActiveDirective,
×
94
        BypassSecurityTrustHtmlPipe
×
95
    ]
×
96
})
×
97
export class ThyNav implements OnInit, AfterViewInit, AfterContentInit, AfterContentChecked, OnChanges, OnDestroy {
×
98
    private elementRef = inject(ElementRef);
×
99
    private ngZone = inject(NgZone);
×
100
    private changeDetectorRef = inject(ChangeDetectorRef);
×
101
    private popover = inject(ThyPopover);
102

×
103
    private readonly destroyRef = inject(DestroyRef);
×
104

×
105
    public initialized = false;
×
106
    public wrapperOffset: { height: number; width: number; left: number; top: number } = {
×
107
        height: 0,
108
        width: 0,
109
        left: 0,
110
        top: 0
×
111
    };
×
112

113
    public hiddenItems: ThyNavItemDirective[] = [];
114

115
    public moreActive: boolean;
×
116

×
117
    readonly showMore: WritableSignal<boolean> = signal(false);
×
118

×
119
    private moreBtnOffset: { height: number; width: number } = { height: 0, width: 0 };
×
120

×
121
    private hostRenderer = useHostRenderer();
122

123
    private innerLinks: QueryList<ThyNavItemDirective>;
×
124

×
125
    locale: Signal<ThyNavLocale> = injectLocale('nav');
×
126

×
127
    /**
128
     * 导航类型
×
129
     * @type pulled | tabs | pills | lite | primary | secondary | thirdly | secondary-divider
130
     * @default pulled
×
131
     */
×
132
    readonly thyType = input<ThyNavType>();
133

×
134
    /**
×
135
     * 导航大小
×
136
     * @type lg | md | sm
×
137
     * @default md
×
138
     */
139
    readonly thySize = input<ThyNavSize>('md');
×
140

×
141
    /**
142
     * 水平排列
143
     * @type '' | 'start' | 'center' | 'end'
144
     * @default false
×
145
     */
146
    readonly thyHorizontal = input<ThyNavHorizontal>('');
147

148
    /**
149
     * 是否垂直排列
150
     * @default false
×
151
     */
×
152
    readonly thyVertical = input(false, { transform: coerceBooleanProperty });
×
153

154
    /**
155
     * 是否是填充模式
156
     */
157
    readonly thyFill = input(false, { transform: coerceBooleanProperty });
×
158

×
159
    /**
×
160
     * 是否响应式,自动计算宽度存放 thyNavItem,并添加更多弹框
×
161
     * @default false
162
     */
×
163
    readonly thyResponsive = input(undefined, { transform: coerceBooleanProperty });
×
164

165
    /**
166
     * 支持暂停自适应计算
167
     */
×
168
    thyPauseReCalculate = input<boolean>(false);
×
169

×
170
    /**
×
171
     * 更多操作的菜单点击内部是否可关闭
×
172
     * @deprecated please use thyPopoverOptions
173
     */
174
    readonly thyInsideClosable = input(true, { transform: coerceBooleanProperty });
175

×
176
    /**
×
177
     * 更多菜单弹出框的参数,底层使用 Popover 组件
×
178
     * @type ThyPopoverConfig
×
179
     */
×
180
    thyPopoverOptions = input<ThyPopoverConfig<unknown>>(null);
181

182
    /**
×
183
     * 右侧额外区域模板
184
     * @type TemplateRef
185
     */
186
    readonly thyExtra = input<TemplateRef<unknown>>();
187

×
188
    /**
189
     * @private
190
     */
×
191
    @ContentChildren(ThyNavItemDirective, { descendants: true })
×
192
    set links(value) {
193
        this.innerLinks = value;
×
194
        this.prevActiveIndex = NaN;
×
195
    }
×
196
    get links(): QueryList<ThyNavItemDirective> {
197
        return this.innerLinks;
198
    }
199

200
    /**
×
201
     * @private
×
202
     */
203
    readonly routers = contentChildren(RouterLinkActive, { descendants: true });
×
204

205
    /**
206
     * 响应式模式下更多操作模板
×
207
     * @type TemplateRef
×
208
     */
×
209
    readonly moreOperation = contentChild<TemplateRef<unknown>>('more');
×
210

×
211
    /**
×
212
     * 响应式模式下更多弹框模板
213
     * @type TemplateRef
×
214
     */
×
215
    readonly morePopover = contentChild<TemplateRef<unknown>>('morePopover');
×
216

×
217
    /**
218
     * 右侧额外区域模板,支持 thyExtra 传参和 <ng-template #extra></ng-template> 模板
×
219
     * @name extra
×
220
     * @type TemplateRef
×
221
     */
222
    readonly extra = contentChild<TemplateRef<unknown>>('extra');
×
223

×
224
    readonly defaultMoreOperation = viewChild<ElementRef<HTMLAnchorElement>>('moreOperationContainer');
225

226
    readonly inkBar = viewChild.required(ThyNavInkBarDirective);
×
227

×
228
    readonly horizontal = computed(() => {
×
229
        const horizontalValue = this.thyHorizontal() as string;
×
230
        return horizontalValue === 'right' ? 'end' : horizontalValue;
×
231
    });
×
232

×
233
    get showInkBar(): boolean {
×
234
        const showTypes: ThyNavType[] = ['pulled', 'tabs'];
×
235
        return showTypes.includes(this.type());
236
    }
237

×
238
    private updateClasses() {
239
        let classNames: string[] = [];
×
240
        if (navTypeClassesMap[this.type()]) {
241
            classNames = [...navTypeClassesMap[this.type()]];
242
        }
×
243
        if (navSizeClassesMap[this.thySize()]) {
×
244
            classNames.push(navSizeClassesMap[this.thySize()]);
245
        }
246
        this.hostRenderer.updateClass(classNames);
×
247
    }
248

249
    private curActiveIndex: number;
×
250

×
251
    private prevActiveIndex: number = NaN;
×
252

×
253
    private navSubscription: { unsubscribe: () => void } | null = null;
×
254

×
255
    readonly type = computed(() => this.thyType() || 'pulled');
×
256

×
257
    constructor() {
×
258
        effect(() => {
259
            this.updateClasses();
260
        });
×
261
    }
262

×
263
    ngOnInit() {
264
        if (!this.thyResponsive()) {
265
            this.initialized = true;
×
266
        }
×
267
    }
268

269
    ngAfterViewInit() {
×
270
        if (this.thyResponsive()) {
271
            this.setMoreBtnOffset();
272
            this.ngZone.onStable.pipe(take(1)).subscribe(() => {
×
273
                this.setMoreBtnOffset();
×
274
                this.links.toArray().forEach(link => link.setOffset());
×
275
                this.setHiddenItems();
×
276
            });
×
277
        }
278

279
        this.ngZone.runOutsideAngular(() => {
280
            this.links.changes.pipe(startWith(this.links), takeUntilDestroyed(this.destroyRef)).subscribe(() => {
×
281
                if (this.navSubscription) {
282
                    this.navSubscription.unsubscribe();
283
                }
284

285
                this.navSubscription = merge(
286
                    this.createResizeObserver(this.elementRef.nativeElement),
287
                    ...this.links.map(item => this.createResizeObserver(item.elementRef.nativeElement).pipe(tap(() => item.setOffset()))),
288
                    ...(this.routers() || []).map(router => router?.isActiveChange)
×
289
                )
290
                    .pipe(
291
                        takeUntilDestroyed(this.destroyRef),
×
292
                        tap(() => {
293
                            if (this.thyPauseReCalculate()) {
294
                                return;
×
295
                            }
×
296

×
297
                            if (this.thyResponsive()) {
298
                                this.setMoreBtnOffset();
×
299
                                this.resetSizes();
×
300
                                this.setHiddenItems();
×
301
                                this.calculateMoreIsActive();
×
302
                            }
×
303

304
                            if (this.type() === 'card') {
×
305
                                this.setNavItemDivider();
×
306
                            }
×
307
                        })
308
                    )
309
                    .subscribe(() => {
310
                        this.alignInkBarToSelectedTab();
×
311
                    });
×
312
            });
×
313
        });
314
    }
315

316
    ngAfterContentInit(): void {
×
317
        if (this.thyResponsive()) {
×
318
            this.ngZone.onStable.pipe(take(1)).subscribe(() => {
319
                this.resetSizes();
320
            });
1✔
321
        }
1✔
322
    }
323

324
    ngAfterContentChecked() {
325
        this.calculateMoreIsActive();
326

327
        this.curActiveIndex = this.links && this.links.length ? this.links.toArray().findIndex(item => item.linkIsActive()) : -1;
328
        if (this.curActiveIndex < 0) {
329
            this.inkBar().hide();
330
        } else if (this.curActiveIndex !== this.prevActiveIndex) {
331
            this.alignInkBarToSelectedTab();
332
        }
333
    }
334

335
    private setMoreBtnOffset() {
336
        const defaultMoreOperation = this.defaultMoreOperation();
337
        const computedStyle = window.getComputedStyle(defaultMoreOperation?.nativeElement);
338
        this.moreBtnOffset = {
339
            height: defaultMoreOperation?.nativeElement?.offsetHeight + parseFloat(computedStyle?.marginBottom) || 0,
340
            width: defaultMoreOperation?.nativeElement?.offsetWidth + parseFloat(computedStyle?.marginRight) || 0
341
        };
1✔
342
    }
343

344
    private setNavItemDivider() {
345
        const tabs = this.links.toArray();
346
        const activeIndex = tabs.findIndex(item => item.linkIsActive());
347

348
        for (let i = 0; i < tabs.length; i++) {
349
            if ((i !== activeIndex && i !== activeIndex - 1 && i !== tabs.length - 1) || (i === activeIndex - 1 && this.moreActive)) {
350
                tabs[i].addClass('has-right-divider');
351
            } else {
352
                tabs[i].removeClass('has-right-divider');
353
            }
354
        }
355
    }
356

357
    createResizeObserver(element: HTMLElement) {
358
        return typeof ResizeObserver === 'undefined'
359
            ? of(null)
360
            : new Observable(observer => {
361
                  const resize = new ResizeObserver(entries => {
362
                      observer.next(entries);
363
                  });
364
                  resize.observe(element);
365
                  return () => {
366
                      resize.disconnect();
367
                  };
368
              });
369
    }
370

371
    private calculateMoreIsActive() {
372
        this.moreActive = this.hiddenItems.some(item => {
373
            return item.linkIsActive();
374
        });
375
        this.changeDetectorRef.detectChanges();
376
    }
377

378
    private setHiddenItems() {
379
        this.moreActive = false;
380
        const tabs = this.links.toArray();
381
        if (!tabs.length) {
382
            this.hiddenItems = [];
383
            this.showMore.set(false);
384
            return;
385
        }
386

387
        const endIndex = this.thyVertical() ? this.getShowItemsEndIndexWhenVertical(tabs) : this.getShowItemsEndIndexWhenHorizontal(tabs);
388

389
        const showItems = tabs.slice(0, endIndex + 1);
390
        (showItems || []).forEach(item => {
391
            item.setNavLinkHidden(false);
392
        });
393

394
        this.hiddenItems = endIndex === tabs.length - 1 ? [] : tabs.slice(endIndex + 1);
395
        (this.hiddenItems || []).forEach(item => {
396
            item.setNavLinkHidden(true);
397
        });
398

399
        this.showMore.set(this.hiddenItems.length > 0);
400
        this.initialized = true;
401
    }
402

403
    private getShowItemsEndIndexWhenHorizontal(tabs: ThyNavItemDirective[]) {
404
        const tabsLength = tabs.length;
405
        let endIndex = tabsLength;
406
        let totalWidth = 0;
407

408
        for (let i = 0; i < tabsLength; i += 1) {
409
            const _totalWidth = i === tabsLength - 1 ? totalWidth + tabs[i].offset.width : totalWidth + tabs[i].offset.width + tabItemRight;
410
            if (_totalWidth > this.wrapperOffset.width) {
411
                let moreOperationWidth = this.moreBtnOffset.width;
412
                if (totalWidth + moreOperationWidth <= this.wrapperOffset.width) {
413
                    endIndex = i - 1;
414
                } else {
415
                    endIndex = i - 2;
416
                }
417
                break;
418
            } else {
419
                totalWidth = _totalWidth;
420
                endIndex = i;
421
            }
422
        }
423
        return endIndex;
424
    }
425

426
    private getShowItemsEndIndexWhenVertical(tabs: ThyNavItemDirective[]) {
427
        const tabsLength = tabs.length;
428
        let endIndex = tabsLength;
429
        let totalHeight = 0;
430
        for (let i = 0; i < tabsLength; i += 1) {
431
            const _totalHeight = totalHeight + tabs[i].offset.height;
432
            if (_totalHeight > this.wrapperOffset.height) {
433
                let moreOperationHeight = this.moreBtnOffset.height;
434
                if (totalHeight + moreOperationHeight <= this.wrapperOffset.height) {
435
                    endIndex = i - 1;
436
                } else {
437
                    endIndex = i - 2;
438
                }
439
                break;
440
            } else {
441
                totalHeight = _totalHeight;
442
                endIndex = i;
443
            }
444
        }
445
        return endIndex;
446
    }
447

448
    private resetSizes() {
449
        this.wrapperOffset = {
450
            height: this.elementRef.nativeElement.offsetHeight || 0,
451
            width: this.elementRef.nativeElement.offsetWidth || 0,
452
            left: this.elementRef.nativeElement.offsetLeft || 0,
453
            top: this.elementRef.nativeElement.offsetTop || 0
454
        };
455
    }
456

457
    openMoreMenu(event: Event, template: TemplateRef<any>) {
458
        this.popover.open(
459
            template,
460
            Object.assign(
461
                {
462
                    origin: event.currentTarget as HTMLElement,
463
                    hasBackdrop: true,
464
                    backdropClosable: true,
465
                    insideClosable: true,
466
                    placement: 'bottom' as ThyPlacement,
467
                    panelClass: 'thy-nav-list-popover',
468
                    originActiveClass: 'thy-nav-origin-active'
469
                },
470
                this.thyPopoverOptions() ? this.thyPopoverOptions() : {}
471
            )
472
        );
473
    }
474

475
    navItemClick(item: ThyNavItemDirective) {
476
        item.elementRef.nativeElement.click();
477
    }
478

479
    private alignInkBarToSelectedTab(): void {
480
        if (!this.showInkBar) {
481
            this.inkBar().hide();
482
            return;
483
        }
484
        const tabs = this.links?.toArray() ?? [];
485
        const selectedItem = tabs.find(item => item.linkIsActive());
486
        let selectedItemElement: HTMLElement = selectedItem && selectedItem.elementRef.nativeElement;
487

488
        if (selectedItem && this.moreActive) {
489
            selectedItemElement = this.defaultMoreOperation().nativeElement;
490
        }
491
        if (selectedItemElement) {
492
            this.prevActiveIndex = this.curActiveIndex;
493
            this.inkBar().alignToElement(selectedItemElement);
494
        }
495
    }
496

497
    ngOnChanges(changes: SimpleChanges): void {
498
        const { thyVertical, thyType } = changes;
499

500
        if (thyType?.currentValue !== thyType?.previousValue || thyVertical?.currentValue !== thyVertical?.previousValue) {
501
            this.alignInkBarToSelectedTab();
502
        }
503
    }
504

505
    ngOnDestroy() {
506
        if (this.navSubscription) {
507
            this.navSubscription.unsubscribe();
508
        }
509
    }
510
}
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

© 2026 Coveralls, Inc