]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/app/search/search.component.ts
WIP plugins: update plugin
[github/Chocobozzz/PeerTube.git] / client / src / app / search / search.component.ts
index 4c540ca72b2b467d57affba42ad2a36ece9e592a..a7ddbe1f8fb78437c3b8fd98fd67aaf9e08324e0 100644 (file)
@@ -1,7 +1,6 @@
 import { Component, OnDestroy, OnInit } from '@angular/core'
 import { ActivatedRoute, Router } from '@angular/router'
-import { AuthService, RedirectService } from '@app/core'
-import { NotificationsService } from 'angular2-notifications'
+import { AuthService, Notifier } from '@app/core'
 import { forkJoin, Subscription } from 'rxjs'
 import { SearchService } from '@app/search/search.service'
 import { ComponentPagination } from '@app/shared/rest/component-pagination.model'
@@ -30,7 +29,8 @@ export class SearchComponent implements OnInit, OnDestroy {
   currentSearch: string
 
   private subActivatedRoute: Subscription
-  private isInitialLoad = true
+  private isInitialLoad = false // set to false to show the search filters on first arrival
+  private firstSearch = true
 
   private channelsPerPage = 2
 
@@ -39,26 +39,26 @@ export class SearchComponent implements OnInit, OnDestroy {
     private route: ActivatedRoute,
     private router: Router,
     private metaService: MetaService,
-    private redirectService: RedirectService,
-    private notificationsService: NotificationsService,
+    private notifier: Notifier,
     private searchService: SearchService,
     private authService: AuthService
   ) { }
 
+  get user () {
+    return this.authService.getUser()
+  }
+
   ngOnInit () {
     this.subActivatedRoute = this.route.queryParams.subscribe(
       queryParams => {
         const querySearch = queryParams['search']
 
-        // New empty search
-        if (this.currentSearch && !querySearch) return this.redirectService.redirectToHomepage()
-
         // Search updated, reset filters
         if (this.currentSearch !== querySearch) {
           this.resetPagination()
           this.advancedSearch.reset()
 
-          this.currentSearch = querySearch
+          this.currentSearch = querySearch || undefined
           this.updateTitle()
         }
 
@@ -71,7 +71,7 @@ export class SearchComponent implements OnInit, OnDestroy {
         this.search()
       },
 
-      err => this.notificationsService.error('Error', err.text)
+      err => this.notifier.error(err.text)
     )
   }
 
@@ -103,18 +103,19 @@ export class SearchComponent implements OnInit, OnDestroy {
                              .concat(videosResult.videos)
           this.pagination.totalItems = videosResult.totalVideos + videoChannelsResult.total
 
-          // Focus on channels
-          if (this.channelsPerPage !== 10 && videosResult.videos.length < this.pagination.itemsPerPage) {
+          // Focus on channels if there are no enough videos
+          if (this.firstSearch === true && videosResult.videos.length < this.pagination.itemsPerPage) {
             this.resetPagination()
+            this.firstSearch = false
 
             this.channelsPerPage = 10
             this.search()
           }
+
+          this.firstSearch = false
         },
 
-        error => {
-          this.notificationsService.error(this.i18n('Error'), error.message)
-        }
+        err => this.notifier.error(err.message)
       )
 
   }
@@ -133,6 +134,14 @@ export class SearchComponent implements OnInit, OnDestroy {
     this.updateUrlFromAdvancedSearch()
   }
 
+  numberOfFilters () {
+    return this.advancedSearch.size()
+  }
+
+  removeVideoFromArray (video: Video) {
+    this.results = this.results.filter(r => !this.isVideo(r) || r.id !== video.id)
+  }
+
   private resetPagination () {
     this.pagination.currentPage = 1
     this.pagination.totalItems = null
@@ -142,13 +151,16 @@ export class SearchComponent implements OnInit, OnDestroy {
   }
 
   private updateTitle () {
-    this.metaService.setTitle(this.i18n('Search') + ' ' + this.currentSearch)
+    const suffix = this.currentSearch ? ' ' + this.currentSearch : ''
+    this.metaService.setTitle(this.i18n('Search') + suffix)
   }
 
   private updateUrlFromAdvancedSearch () {
+    const search = this.currentSearch || undefined
+
     this.router.navigate([], {
       relativeTo: this.route,
-      queryParams: Object.assign({}, this.advancedSearch.toUrlObject(), { search: this.currentSearch })
+      queryParams: Object.assign({}, this.advancedSearch.toUrlObject(), { search })
     })
   }
 }