]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/commitdiff
Simplify code for search-typeahead 2448/head
authorRigel Kent <sendmemail@rigelk.eu>
Wed, 12 Feb 2020 15:20:49 +0000 (16:20 +0100)
committerRigel Kent <sendmemail@rigelk.eu>
Thu, 13 Feb 2020 15:48:46 +0000 (16:48 +0100)
client/src/app/header/search-typeahead.component.html
client/src/app/header/search-typeahead.component.ts
client/src/app/header/suggestion.component.html
client/src/app/shared/angular/highlight.pipe.ts
client/src/app/shared/shared.module.ts

index 428246585f536c0e91ff46d86d5487eefca74b8a..0c9df618e4cf3c361b231457f75c49758afb64d2 100644 (file)
     </div>
 
     <!-- search instructions, when search input is empty -->
-    <div *ngIf="showInstructions" id="typeahead-instructions" class="overflow-hidden">
+    <div *ngIf="areInstructionsDisplayed" id="typeahead-instructions" class="overflow-hidden">
       <div class="d-flex justify-content-between">
         <label class="small-title" i18n>Advanced search</label>
         <div class="advanced-search-status c-help">
-          <span [ngClass]="anyURI ? 'text-success' : 'text-muted'" i18n-title title="Determines whether you can resolve any distant content, or if this instance only allows doing so for instances it follows.">
-            <span *ngIf="anyURI" class="mr-1" i18n>any instance</span>
-            <span *ngIf="!anyURI" class="mr-1" i18n>only followed instances</span>
-            <i [ngClass]="anyURI ? 'glyphicon glyphicon-ok-sign' : 'glyphicon glyphicon-exclamation-sign'"></i>
+          <span [ngClass]="canSearchAnyURI ? 'text-success' : 'text-muted'" i18n-title title="Determines whether you can resolve any distant content, or if this instance only allows doing so for instances it follows.">
+            <span *ngIf="canSearchAnyURI" class="mr-1" i18n>any instance</span>
+            <span *ngIf="!canSearchAnyURI" class="mr-1" i18n>only followed instances</span>
+            <i [ngClass]="canSearchAnyURI ? 'glyphicon glyphicon-ok-sign' : 'glyphicon glyphicon-exclamation-sign'"></i>
           </span>
         </div>
       </div>
index c265f2c83d16777777621a8ba3205adffbed7471..210a1474cbfa5aca191ece43a9e825bac437e20b 100644 (file)
@@ -1,6 +1,5 @@
 import {
   Component,
-  AfterViewInit,
   OnInit,
   OnDestroy,
   QueryList,
@@ -14,7 +13,6 @@ import { ListKeyManager } from '@angular/cdk/a11y'
 import { UP_ARROW, DOWN_ARROW, ENTER } from '@angular/cdk/keycodes'
 import { SuggestionComponent, Result } from './suggestion.component'
 import { of } from 'rxjs'
-import { getParameterByName } from '@app/shared/misc/utils'
 import { ServerConfig } from '@shared/models'
 
 @Component({
@@ -22,7 +20,7 @@ import { ServerConfig } from '@shared/models'
   templateUrl: './search-typeahead.component.html',
   styleUrls: [ './search-typeahead.component.scss' ]
 })
-export class SearchTypeaheadComponent implements OnInit, OnDestroy, AfterViewInit {
+export class SearchTypeaheadComponent implements OnInit, OnDestroy {
   @ViewChild('searchVideo', { static: true }) searchInput: ElementRef<HTMLInputElement>
 
   hasChannel = false
@@ -35,7 +33,7 @@ export class SearchTypeaheadComponent implements OnInit, OnDestroy, AfterViewIni
   inThisChannelText: string
 
   keyboardEventsManager: ListKeyManager<SuggestionComponent>
-  results: any[] = []
+  results: Result[] = []
 
   constructor (
     private authService: AuthService,
@@ -45,6 +43,9 @@ export class SearchTypeaheadComponent implements OnInit, OnDestroy, AfterViewIni
   ) {}
 
   ngOnInit () {
+    const query = this.route.snapshot.queryParams
+    if (query['search']) this.search = query['search']
+
     this.serverService.getConfig()
       .subscribe(config => this.serverConfig = config)
   }
@@ -53,23 +54,19 @@ export class SearchTypeaheadComponent implements OnInit, OnDestroy, AfterViewIni
     if (this.keyboardEventsManager) this.keyboardEventsManager.change.unsubscribe()
   }
 
-  ngAfterViewInit () {
-    this.search = getParameterByName('search', window.location.href) || ''
-  }
-
   get activeResult () {
-    return this.keyboardEventsManager && this.keyboardEventsManager.activeItem && this.keyboardEventsManager.activeItem.result
+    return this.keyboardEventsManager?.activeItem?.result
   }
 
-  get showInstructions () {
+  get areInstructionsDisplayed () {
     return !this.search
   }
 
   get showHelp () {
-    return this.search && this.newSearch && this.activeResult && this.activeResult.type === 'search-global' || false
+    return this.search && this.newSearch && this.activeResult?.type === 'search-global'
   }
 
-  get anyURI () {
+  get canSearchAnyURI () {
     if (!this.serverConfig) return false
     return this.authService.isLoggedIn()
       ? this.serverConfig.search.remoteUri.users
@@ -131,28 +128,33 @@ export class SearchTypeaheadComponent implements OnInit, OnDestroy, AfterViewIni
 
   initKeyboardEventsManager (event: { items: QueryList<SuggestionComponent>, index?: number }) {
     if (this.keyboardEventsManager) this.keyboardEventsManager.change.unsubscribe()
+
     this.keyboardEventsManager = new ListKeyManager(event.items)
+
     if (event.index !== undefined) {
       this.keyboardEventsManager.setActiveItem(event.index)
     } else {
       this.keyboardEventsManager.setFirstItemActive()
     }
+
     this.keyboardEventsManager.change.subscribe(
       _ => this.setEventItems(event)
     )
   }
 
-  handleKeyUp (event: KeyboardEvent, indexSelected?: number) {
+  handleKeyUp (event: KeyboardEvent) {
     event.stopImmediatePropagation()
-    if (this.keyboardEventsManager) {
-      if (event.keyCode === DOWN_ARROW || event.keyCode === UP_ARROW) {
+    if (!this.keyboardEventsManager) return
+    
+    switch (event.key) {
+      case "ArrowDown":
+      case "ArrowUp":
         this.keyboardEventsManager.onKeydown(event)
-        return false
-      } else if (event.keyCode === ENTER) {
+        break
+      case "Enter":
         this.newSearch = false
         this.doSearch()
-        return false
-      }
+        break
     }
   }
 
index edde2023afc377965ecdc4b858d212b4ec95ce72..d7ae3450a9ece36c06b78c9880381eed27af122c 100644 (file)
@@ -1,4 +1,4 @@
-<a tabindex="-1" class="d-flex flex-auto flex-items-center p-2" [class.focus-visible]="active" [routerLink]="result.routerLink">
+<a tabindex="-1" class="d-flex flex-auto flex-items-center p-2" [class.focus-visible]="active">
   <div class="flex-shrink-0 mr-2 text-center">
     <my-global-icon *ngIf="result.type !== 'channel'" iconName="search"></my-global-icon>
     <my-global-icon *ngIf="result.type === 'channel'" iconName="folder"></my-global-icon>
index e219b3823a3e98b231ee40d688523bff4bfdb51f..fb604228024ef9eb2e8b813cf36f5f5d1dc46d21 100644 (file)
@@ -30,7 +30,7 @@ export class HighlightPipe implements PipeTransform {
           break
         }
         case 'Single-And-StartsWith-Match': {
-          regex = new RegExp("^" + stringToHighlight, caseFlag)
+          regex = new RegExp('^' + stringToHighlight, caseFlag)
           break
         }
         case 'Multi-Match': {
index 090a5b7f99e88fa33162366c49d12fd8f82a2fbe..30b3ba0c12761c804ccdb19aa55ba0f434b04f8f 100644 (file)
@@ -89,7 +89,7 @@ import { NumberFormatterPipe } from '@app/shared/angular/number-formatter.pipe'
 import { VideoDurationPipe } from '@app/shared/angular/video-duration-formatter.pipe'
 import { ObjectLengthPipe } from '@app/shared/angular/object-length.pipe'
 import { FromNowPipe } from '@app/shared/angular/from-now.pipe'
-import { HighlightPipe }from '@app/shared/angular/highlight.pipe'
+import { HighlightPipe } from '@app/shared/angular/highlight.pipe'
 import { PeerTubeTemplateDirective } from '@app/shared/angular/peertube-template.directive'
 import { VideoActionsDropdownComponent } from '@app/shared/video/video-actions-dropdown.component'
 import { VideoBlacklistComponent } from '@app/shared/video/modals/video-blacklist.component'