Skip to content

Commit

Permalink
fix(template): use navigateByUrl when click search result item path i…
Browse files Browse the repository at this point in the history
…s inernal route #OSP-253 #350
  • Loading branch information
why520crazy committed Apr 24, 2022
1 parent a2954e7 commit 152e6f7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
10 changes: 5 additions & 5 deletions packages/template/src/services/search.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ import { DOCUMENT } from '@angular/common';
import { fromEvent, Subject } from 'rxjs';
import { debounceTime, distinctUntilChanged, map, takeUntil } from 'rxjs/operators';

interface PageInfo {
export interface SearchPageInfo {
title: string;
id: string;
path: string;
parent?: PageInfo;
parent?: SearchPageInfo;
}

@Injectable({ providedIn: 'root' })
export class SearchService {
private allPages: PageInfo[] = [];
private allPages: SearchPageInfo[] = [];

private destroyed$ = new Subject();

public result: PageInfo[] = [];
public result: SearchPageInfo[] = [];

public get hasAlgolia() {
return !!(this.global.config.algolia && this.global.config.algolia.apiKey && this.global.config.algolia.indexName);
Expand Down Expand Up @@ -126,7 +126,7 @@ export class SearchService {
}
}

public trackByFn(index: number, item: PageInfo) {
public trackByFn(index: number, item: SearchPageInfo) {
return item.id || index;
}
}
11 changes: 8 additions & 3 deletions packages/template/src/shared/search/search.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@
[class.is-searching]="hasSearchText && isFocus"
[class.result-empty]="searchService.result?.length === 0"
>
<a class="search-result" *ngFor="let result of searchService.result; trackBy: searchService.trackByFn" [href]="result.path">
<ng-container *ngIf="result.parent"> {{ result.parent?.title }} &gt; </ng-container>
<span [innerHtml]="searchText | highlight: result.title"></span>
<a
class="search-result"
*ngFor="let item of searchService.result; trackBy: searchService.trackByFn"
[href]="item.path"
(click)="toRoute($event, item)"
>
<ng-container *ngIf="item.parent"> {{ item.parent?.title }} &gt; </ng-container>
<span [innerHtml]="searchText | highlight: item.title"></span>
</a>
<ng-container *ngIf="hasSearchText && searchService.result.length === 0">
<dg-icon class="empty-icon" iconName="empty"></dg-icon>
Expand Down
12 changes: 10 additions & 2 deletions packages/template/src/shared/search/search.component.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Component, OnInit, AfterViewInit } from '@angular/core';
import { SearchService } from '../../services/search.service';
import { Router } from '@angular/router';
import { SearchPageInfo, SearchService } from '../../services/search.service';

@Component({
selector: 'dg-search',
Expand All @@ -12,7 +13,7 @@ export class SearchComponent implements OnInit, AfterViewInit {

public hasSearchText: boolean;

constructor(public searchService: SearchService) {}
constructor(public searchService: SearchService, private router: Router) {}

ngOnInit(): void {}

Expand All @@ -39,4 +40,11 @@ export class SearchComponent implements OnInit, AfterViewInit {
this.hasSearchText = !!this.searchText?.trim();
}, 200);
}

toRoute($event: Event, item: SearchPageInfo) {
if (!item.path.startsWith('http')) {
$event.preventDefault();
this.router.navigateByUrl(item.path);
}
}
}

0 comments on commit 152e6f7

Please sign in to comment.