]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/app/search/search.component.ts
Merge branch 'develop' into pr/1285
[github/Chocobozzz/PeerTube.git] / client / src / app / search / search.component.ts
index 4c540ca72b2b467d57affba42ad2a36ece9e592a..c4a4b1fdebf7a472916c81bc22f1c86e10e8cd27 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, ServerService } 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,10 +39,10 @@ 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
+    private authService: AuthService,
+    private serverService: ServerService
   ) { }
 
   ngOnInit () {
@@ -50,15 +50,12 @@ export class SearchComponent implements OnInit, OnDestroy {
       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 +68,7 @@ export class SearchComponent implements OnInit, OnDestroy {
         this.search()
       },
 
-      err => this.notificationsService.error('Error', err.text)
+      err => this.notifier.error(err.text)
     )
   }
 
@@ -79,6 +76,10 @@ export class SearchComponent implements OnInit, OnDestroy {
     if (this.subActivatedRoute) this.subActivatedRoute.unsubscribe()
   }
 
+  isVideoBlur (video: Video) {
+    return video.isVideoNSFWForUser(this.authService.getUser(), this.serverService.getConfig())
+  }
+
   isVideoChannel (d: VideoChannel | Video): d is VideoChannel {
     return d instanceof VideoChannel
   }
@@ -103,18 +104,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 +135,10 @@ export class SearchComponent implements OnInit, OnDestroy {
     this.updateUrlFromAdvancedSearch()
   }
 
+  numberOfFilters () {
+    return this.advancedSearch.size()
+  }
+
   private resetPagination () {
     this.pagination.currentPage = 1
     this.pagination.totalItems = null
@@ -142,13 +148,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 })
     })
   }
 }