Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(module:anchor): url fragment change on click - nzReplace #8464

Open
wants to merge 36 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
5c11a42
fix(module:tabs): wrong cursor
ParsaArvanehPA Feb 1, 2024
aac33c4
Merge branch 'NG-ZORRO:master' into master
ParsaArvanehPA Feb 13, 2024
f7525f5
Merge branch 'NG-ZORRO:master' into master
ParsaArvanehPA Feb 19, 2024
8a974b9
Merge branch 'NG-ZORRO:master' into master
ParsaArvanehPA Feb 19, 2024
e5276bb
Merge branch 'NG-ZORRO:master' into master
ParsaArvanehPA Feb 20, 2024
0f989fe
Merge branch 'NG-ZORRO:master' into master
ParsaArvanehPA Feb 27, 2024
ec35a90
Merge branch 'NG-ZORRO:master' into master
ParsaArvanehPA Feb 28, 2024
6ec6fe1
Merge branch 'NG-ZORRO:master' into master
ParsaArvanehPA Mar 2, 2024
5d1e384
Merge branch 'NG-ZORRO:master' into master
ParsaArvanehPA Mar 24, 2024
d81339e
Merge branch 'NG-ZORRO:master' into master
ParsaArvanehPA Mar 24, 2024
a494c4f
feat(module:anchor): url fragment change on click - nzReplace
ParsaArvanehPA Mar 24, 2024
9f9acf9
feat(module:anchor): url fragment change on click - nzReplace
ParsaArvanehPA Mar 24, 2024
2284767
Merge branch 'NG-ZORRO:master' into master
ParsaArvanehPA Apr 6, 2024
0ac094d
Merge branch 'NG-ZORRO:master' into master
ParsaArvanehPA Apr 23, 2024
59d40a5
Merge branch 'NG-ZORRO:master' into master
ParsaArvanehPA May 12, 2024
35b916c
Merge branch 'master' into feature/anchor-navigation-without-location…
Laffery Jun 4, 2024
82afabb
Merge branch 'NG-ZORRO:master' into master
ParsaArvanehPA Jul 14, 2024
11b7fc8
Merge branch 'NG-ZORRO:master' into master
ParsaArvanehPA Jul 17, 2024
8705669
Merge branch 'NG-ZORRO:master' into master
ParsaArvanehPA Aug 4, 2024
0e963ac
Merge branch 'NG-ZORRO:master' into master
ParsaArvanehPA Aug 12, 2024
5df9b87
feat(module:anchor): url fragment change on click - nzReplace
ParsaArvanehPA Aug 12, 2024
672ab2d
Merge branch 'feature/anchor-navigation-without-location-change' of h…
ParsaArvanehPA Aug 12, 2024
500dcf9
feat(module:anchor): url fragment change on click - nzReplace
ParsaArvanehPA Aug 12, 2024
7b9723c
feat(module:anchor): url fragment change on click - nzReplace
ParsaArvanehPA Aug 13, 2024
04b1832
Update anchor-link.component.ts
Laffery Nov 23, 2024
b451362
Update anchor.component.ts
Laffery Nov 23, 2024
4c2a709
Update anchor.spec.ts
Laffery Nov 23, 2024
211dc34
Merge branch 'master' into feature/anchor-navigation-without-location…
Laffery Nov 23, 2024
fe67bf4
Update anchor.spec.ts
Laffery Nov 23, 2024
5de5899
Update anchor.spec.ts
Laffery Nov 23, 2024
0a75117
Update replace.ts
Laffery Nov 23, 2024
c4d289d
Merge branch 'master' into feature/anchor-navigation-without-location…
Laffery Nov 26, 2024
fbe2b98
Update anchor.component.ts
Laffery Nov 26, 2024
f9fb540
Update anchor-link.component.ts
Laffery Nov 26, 2024
561d0d9
Update anchor.spec.ts
Laffery Nov 26, 2024
49ecf10
Update anchor.spec.ts
Laffery Nov 26, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions components/anchor/anchor-link.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import { Platform } from '@angular/cdk/platform';
import { NgTemplateOutlet } from '@angular/common';
import {
booleanAttribute,
ChangeDetectionStrategy,
Component,
ContentChild,
Expand Down Expand Up @@ -57,6 +58,7 @@ import { NzAnchorComponent } from './anchor.component';
export class NzAnchorLinkComponent implements OnInit, OnDestroy {
@Input() nzHref = '#';
@Input() nzTarget?: string;
@Input({ transform: booleanAttribute }) nzReplace: boolean = false;

titleStr: string | null = '';
titleTpl?: TemplateRef<NzSafeAny>;
Expand Down
35 changes: 28 additions & 7 deletions components/anchor/anchor.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

import { normalizePassiveListenerOptions, Platform } from '@angular/cdk/platform';
import { DOCUMENT, NgClass, NgIf, NgStyle, NgTemplateOutlet } from '@angular/common';
import { DOCUMENT, NgStyle, NgTemplateOutlet } from '@angular/common';
import {
AfterViewInit,
booleanAttribute,
Expand All @@ -24,11 +24,12 @@
ViewChild,
ViewEncapsulation
} from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { Subject } from 'rxjs';
import { takeUntil, throttleTime } from 'rxjs/operators';

import { NzAffixModule } from 'ng-zorro-antd/affix';
import { NzConfigKey, NzConfigService, WithConfig } from 'ng-zorro-antd/core/config';
import { NzConfigKey, WithConfig } from 'ng-zorro-antd/core/config';
import { NzScrollService } from 'ng-zorro-antd/core/services';
import { NgStyleInterface, NzDirectionVHType } from 'ng-zorro-antd/core/types';
import { fromEventOutsideAngular, numberAttributeWithZeroFallback } from 'ng-zorro-antd/core/util';
Expand All @@ -52,7 +53,7 @@
exportAs: 'nzAnchor',
preserveWhitespaces: false,
standalone: true,
imports: [NgClass, NgIf, NgStyle, NgTemplateOutlet, NzAffixModule],
imports: [NgStyle, NgTemplateOutlet, NzAffixModule],
template: `
@if (nzAffix) {
<nz-affix [nzOffsetTop]="nzOffsetTop" [nzTarget]="container">
Expand All @@ -65,10 +66,10 @@
<ng-template #content>
<div
class="ant-anchor-wrapper"
[ngClass]="{ 'ant-anchor-wrapper-horizontal': nzDirection === 'horizontal' }"
[class.ant-anchor-wrapper-horizontal]="nzDirection === 'horizontal'"
[ngStyle]="wrapperStyle"
>
<div class="ant-anchor" [ngClass]="{ 'ant-anchor-fixed': !nzAffix && !nzShowInkInFixed }">
<div class="ant-anchor" [class.ant-anchor-fixed]="!nzAffix && !nzShowInkInFixed">
<div class="ant-anchor-ink">
<div class="ant-anchor-ink-ball" #ink></div>
</div>
Expand Down Expand Up @@ -106,6 +107,7 @@
@Input() nzContainer?: string | HTMLElement;
@Input() nzCurrentAnchor?: string;
@Input() nzDirection: NzDirectionVHType = 'vertical';
@Input({ transform: booleanAttribute }) nzReplace: boolean = false;

@Output() readonly nzClick = new EventEmitter<string>();
@Output() readonly nzChange = new EventEmitter<string>();
Expand All @@ -123,8 +125,11 @@
private handleScrollTimeoutID?: ReturnType<typeof setTimeout>;
private doc: Document = inject(DOCUMENT);

// router module is optional for some projects, so these should be injected as optional.
private router = inject(Router, { optional: true });
private activatedRoute = inject(ActivatedRoute, { optional: true });

constructor(
public nzConfigService: NzConfigService,
private scrollSrv: NzScrollService,
private cdr: ChangeDetectorRef,
private platform: Platform,
Expand Down Expand Up @@ -163,7 +168,7 @@
.pipe(throttleTime(50), takeUntil(this.destroy$))
.subscribe(() => this.handleScroll());
// Browser would maintain the scrolling position when refreshing.
// So we have to delay calculation in avoid of getting a incorrect result.
// So we have to delay calculation in avoid of getting an incorrect result.
this.handleScrollTimeoutID = setTimeout(() => this.handleScroll());
}

Expand Down Expand Up @@ -264,6 +269,7 @@
}
});
this.nzClick.emit(linkComp.nzHref);
this.updateUrlFragment(linkComp.nzHref, linkComp.nzReplace);
}

ngOnChanges(changes: SimpleChanges): void {
Expand All @@ -282,4 +288,19 @@
this.setActive();
}
}

updateUrlFragment(nzHref: string, replace?: boolean): void {
if (!this.router || !this.activatedRoute) {
return;

Check warning on line 294 in components/anchor/anchor.component.ts

View check run for this annotation

Codecov / codecov/patch

components/anchor/anchor.component.ts#L294

Added line #L294 was not covered by tests
}

let urlWithoutFragment = nzHref.split('#')[1] ?? nzHref;
this.router
.navigate([], {
relativeTo: this.activatedRoute,
fragment: urlWithoutFragment,
replaceUrl: replace ? replace : this.nzReplace
})
.then();
}
}
Loading
Loading