mirror of
				https://github.com/paperless-ngx/paperless-ngx.git
				synced 2025-10-30 03:56:23 -05:00 
			
		
		
		
	Refactor query param handling to service
This commit is contained in:
		| @@ -22,6 +22,7 @@ import { | |||||||
|   RemoteVersionService, |   RemoteVersionService, | ||||||
|   AppRemoteVersion, |   AppRemoteVersion, | ||||||
| } from 'src/app/services/rest/remote-version.service' | } from 'src/app/services/rest/remote-version.service' | ||||||
|  | import { QueryParamsService } from 'src/app/services/query-params.service' | ||||||
|  |  | ||||||
| @Component({ | @Component({ | ||||||
|   selector: 'app-app-frame', |   selector: 'app-app-frame', | ||||||
| @@ -37,7 +38,8 @@ export class AppFrameComponent { | |||||||
|     public savedViewService: SavedViewService, |     public savedViewService: SavedViewService, | ||||||
|     private list: DocumentListViewService, |     private list: DocumentListViewService, | ||||||
|     private meta: Meta, |     private meta: Meta, | ||||||
|     private remoteVersionService: RemoteVersionService |     private remoteVersionService: RemoteVersionService, | ||||||
|  |     private queryParamsService: QueryParamsService | ||||||
|   ) { |   ) { | ||||||
|     this.remoteVersionService |     this.remoteVersionService | ||||||
|       .checkForUpdates() |       .checkForUpdates() | ||||||
| @@ -92,7 +94,7 @@ export class AppFrameComponent { | |||||||
|  |  | ||||||
|   search() { |   search() { | ||||||
|     this.closeMenu() |     this.closeMenu() | ||||||
|     this.list.quickFilter([ |     this.queryParamsService.loadFilterRules([ | ||||||
|       { |       { | ||||||
|         rule_type: FILTER_FULLTEXT_QUERY, |         rule_type: FILTER_FULLTEXT_QUERY, | ||||||
|         value: (this.searchField.value as string).trim(), |         value: (this.searchField.value as string).trim(), | ||||||
|   | |||||||
| @@ -3,11 +3,11 @@ import { Router } from '@angular/router' | |||||||
| import { Subscription } from 'rxjs' | import { Subscription } from 'rxjs' | ||||||
| import { PaperlessDocument } from 'src/app/data/paperless-document' | import { PaperlessDocument } from 'src/app/data/paperless-document' | ||||||
| import { PaperlessSavedView } from 'src/app/data/paperless-saved-view' | import { PaperlessSavedView } from 'src/app/data/paperless-saved-view' | ||||||
| import { DocumentListViewService } from 'src/app/services/document-list-view.service' |  | ||||||
| import { ConsumerStatusService } from 'src/app/services/consumer-status.service' | import { ConsumerStatusService } from 'src/app/services/consumer-status.service' | ||||||
| import { DocumentService } from 'src/app/services/rest/document.service' | import { DocumentService } from 'src/app/services/rest/document.service' | ||||||
| import { PaperlessTag } from 'src/app/data/paperless-tag' | import { PaperlessTag } from 'src/app/data/paperless-tag' | ||||||
| import { FILTER_HAS_TAGS_ALL } from 'src/app/data/filter-rule-type' | import { FILTER_HAS_TAGS_ALL } from 'src/app/data/filter-rule-type' | ||||||
|  | import { QueryParamsService } from 'src/app/services/query-params.service' | ||||||
|  |  | ||||||
| @Component({ | @Component({ | ||||||
|   selector: 'app-saved-view-widget', |   selector: 'app-saved-view-widget', | ||||||
| @@ -18,7 +18,7 @@ export class SavedViewWidgetComponent implements OnInit, OnDestroy { | |||||||
|   constructor( |   constructor( | ||||||
|     private documentService: DocumentService, |     private documentService: DocumentService, | ||||||
|     private router: Router, |     private router: Router, | ||||||
|     private list: DocumentListViewService, |     private queryParamsService: QueryParamsService, | ||||||
|     private consumerStatusService: ConsumerStatusService |     private consumerStatusService: ConsumerStatusService | ||||||
|   ) {} |   ) {} | ||||||
|  |  | ||||||
| @@ -67,7 +67,7 @@ export class SavedViewWidgetComponent implements OnInit, OnDestroy { | |||||||
|   } |   } | ||||||
|  |  | ||||||
|   clickTag(tag: PaperlessTag) { |   clickTag(tag: PaperlessTag) { | ||||||
|     this.list.quickFilter([ |     this.queryParamsService.loadFilterRules([ | ||||||
|       { rule_type: FILTER_HAS_TAGS_ALL, value: tag.id.toString() }, |       { rule_type: FILTER_HAS_TAGS_ALL, value: tag.id.toString() }, | ||||||
|     ]) |     ]) | ||||||
|   } |   } | ||||||
|   | |||||||
| @@ -35,6 +35,7 @@ import { | |||||||
| import { PaperlessDocumentSuggestions } from 'src/app/data/paperless-document-suggestions' | import { PaperlessDocumentSuggestions } from 'src/app/data/paperless-document-suggestions' | ||||||
| import { FILTER_FULLTEXT_MORELIKE } from 'src/app/data/filter-rule-type' | import { FILTER_FULLTEXT_MORELIKE } from 'src/app/data/filter-rule-type' | ||||||
| import { normalizeDateStr } from 'src/app/utils/date' | import { normalizeDateStr } from 'src/app/utils/date' | ||||||
|  | import { QueryParamsService } from 'src/app/services/query-params.service' | ||||||
|  |  | ||||||
| @Component({ | @Component({ | ||||||
|   selector: 'app-document-detail', |   selector: 'app-document-detail', | ||||||
| @@ -114,7 +115,8 @@ export class DocumentDetailComponent | |||||||
|     private documentListViewService: DocumentListViewService, |     private documentListViewService: DocumentListViewService, | ||||||
|     private documentTitlePipe: DocumentTitlePipe, |     private documentTitlePipe: DocumentTitlePipe, | ||||||
|     private toastService: ToastService, |     private toastService: ToastService, | ||||||
|     private settings: SettingsService |     private settings: SettingsService, | ||||||
|  |     private queryParamsService: QueryParamsService | ||||||
|   ) { |   ) { | ||||||
|     this.titleSubject |     this.titleSubject | ||||||
|       .pipe( |       .pipe( | ||||||
| @@ -446,7 +448,7 @@ export class DocumentDetailComponent | |||||||
|   } |   } | ||||||
|  |  | ||||||
|   moreLike() { |   moreLike() { | ||||||
|     this.documentListViewService.quickFilter([ |     this.queryParamsService.loadFilterRules([ | ||||||
|       { |       { | ||||||
|         rule_type: FILTER_FULLTEXT_MORELIKE, |         rule_type: FILTER_FULLTEXT_MORELIKE, | ||||||
|         value: this.documentId.toString(), |         value: this.documentId.toString(), | ||||||
|   | |||||||
| @@ -31,6 +31,7 @@ import { | |||||||
| } from 'src/app/directives/sortable.directive' | } from 'src/app/directives/sortable.directive' | ||||||
| import { ConsumerStatusService } from 'src/app/services/consumer-status.service' | import { ConsumerStatusService } from 'src/app/services/consumer-status.service' | ||||||
| import { DocumentListViewService } from 'src/app/services/document-list-view.service' | import { DocumentListViewService } from 'src/app/services/document-list-view.service' | ||||||
|  | import { QueryParamsService } from 'src/app/services/query-params.service' | ||||||
| import { | import { | ||||||
|   DocumentService, |   DocumentService, | ||||||
|   DOCUMENT_SORT_FIELDS, |   DOCUMENT_SORT_FIELDS, | ||||||
| @@ -55,7 +56,8 @@ export class DocumentListComponent implements OnInit, OnDestroy, AfterViewInit { | |||||||
|     private router: Router, |     private router: Router, | ||||||
|     private toastService: ToastService, |     private toastService: ToastService, | ||||||
|     private modalService: NgbModal, |     private modalService: NgbModal, | ||||||
|     private consumerStatusService: ConsumerStatusService |     private consumerStatusService: ConsumerStatusService, | ||||||
|  |     private queryParamsService: QueryParamsService | ||||||
|   ) {} |   ) {} | ||||||
|  |  | ||||||
|   @ViewChild('filterEditor') |   @ViewChild('filterEditor') | ||||||
| @@ -127,10 +129,6 @@ export class DocumentListComponent implements OnInit, OnDestroy, AfterViewInit { | |||||||
|         this.unmodifiedFilterRules = view.filter_rules |         this.unmodifiedFilterRules = view.filter_rules | ||||||
|       }) |       }) | ||||||
|  |  | ||||||
|     const allFilterRuleQueryParams: string[] = FILTER_RULE_TYPES.map( |  | ||||||
|       (rt) => rt.filtervar |  | ||||||
|     ) |  | ||||||
|  |  | ||||||
|     this.route.queryParamMap |     this.route.queryParamMap | ||||||
|       .pipe( |       .pipe( | ||||||
|         filter(() => !this.route.snapshot.paramMap.has('id')), // only when not on saved view |         filter(() => !this.route.snapshot.paramMap.has('id')), // only when not on saved view | ||||||
| @@ -140,30 +138,9 @@ export class DocumentListComponent implements OnInit, OnDestroy, AfterViewInit { | |||||||
|         if (queryParams.has('view')) { |         if (queryParams.has('view')) { | ||||||
|           this.loadViewConfig(parseInt(queryParams.get('view'))) |           this.loadViewConfig(parseInt(queryParams.get('view'))) | ||||||
|         } else { |         } else { | ||||||
|           // transform query params to filter rules |  | ||||||
|           let filterRulesFromQueryParams: FilterRule[] = [] |  | ||||||
|           allFilterRuleQueryParams |  | ||||||
|             .filter((frqp) => queryParams.has(frqp)) |  | ||||||
|             .forEach((filterQueryParamName) => { |  | ||||||
|               const filterQueryParamValues: string[] = queryParams |  | ||||||
|                 .get(filterQueryParamName) |  | ||||||
|                 .split(',') |  | ||||||
|  |  | ||||||
|               filterRulesFromQueryParams = filterRulesFromQueryParams.concat( |  | ||||||
|                 // map all values to filter rules |  | ||||||
|                 filterQueryParamValues.map((val) => { |  | ||||||
|                   return { |  | ||||||
|                     rule_type: FILTER_RULE_TYPES.find( |  | ||||||
|                       (rt) => rt.filtervar == filterQueryParamName |  | ||||||
|                     ).id, |  | ||||||
|                     value: val, |  | ||||||
|                   } |  | ||||||
|                 }) |  | ||||||
|               ) |  | ||||||
|             }) |  | ||||||
|  |  | ||||||
|           this.list.activateSavedView(null) |           this.list.activateSavedView(null) | ||||||
|           this.list.filterRules = filterRulesFromQueryParams |           this.queryParamsService.params = queryParams | ||||||
|  |           this.list.filterRules = this.queryParamsService.filterRules | ||||||
|           this.list.reload() |           this.list.reload() | ||||||
|           this.unmodifiedFilterRules = [] |           this.unmodifiedFilterRules = [] | ||||||
|         } |         } | ||||||
| @@ -175,8 +152,7 @@ export class DocumentListComponent implements OnInit, OnDestroy, AfterViewInit { | |||||||
|       .pipe(takeUntil(this.unsubscribeNotifier)) |       .pipe(takeUntil(this.unsubscribeNotifier)) | ||||||
|       .subscribe({ |       .subscribe({ | ||||||
|         next: (filterRules) => { |         next: (filterRules) => { | ||||||
|           const params = |           this.queryParamsService.filterRules = filterRules | ||||||
|             this.documentService.filterRulesToQueryParams(filterRules) |  | ||||||
|  |  | ||||||
|           // if we were on a saved view we navigate 'away' to /documents |           // if we were on a saved view we navigate 'away' to /documents | ||||||
|           let base = [] |           let base = [] | ||||||
| @@ -184,7 +160,7 @@ export class DocumentListComponent implements OnInit, OnDestroy, AfterViewInit { | |||||||
|  |  | ||||||
|           this.router.navigate(base, { |           this.router.navigate(base, { | ||||||
|             relativeTo: this.route, |             relativeTo: this.route, | ||||||
|             queryParams: params, |             queryParams: this.queryParamsService.params, | ||||||
|           }) |           }) | ||||||
|         }, |         }, | ||||||
|       }) |       }) | ||||||
| @@ -296,7 +272,7 @@ export class DocumentListComponent implements OnInit, OnDestroy, AfterViewInit { | |||||||
|   } |   } | ||||||
|  |  | ||||||
|   clickMoreLike(documentID: number) { |   clickMoreLike(documentID: number) { | ||||||
|     this.list.quickFilter([ |     this.queryParamsService.loadFilterRules([ | ||||||
|       { rule_type: FILTER_FULLTEXT_MORELIKE, value: documentID.toString() }, |       { rule_type: FILTER_FULLTEXT_MORELIKE, value: documentID.toString() }, | ||||||
|     ]) |     ]) | ||||||
|   } |   } | ||||||
|   | |||||||
| @@ -3,7 +3,7 @@ import { NgbModal } from '@ng-bootstrap/ng-bootstrap' | |||||||
| import { FILTER_CORRESPONDENT } from 'src/app/data/filter-rule-type' | import { FILTER_CORRESPONDENT } from 'src/app/data/filter-rule-type' | ||||||
| import { PaperlessCorrespondent } from 'src/app/data/paperless-correspondent' | import { PaperlessCorrespondent } from 'src/app/data/paperless-correspondent' | ||||||
| import { CustomDatePipe } from 'src/app/pipes/custom-date.pipe' | import { CustomDatePipe } from 'src/app/pipes/custom-date.pipe' | ||||||
| import { DocumentListViewService } from 'src/app/services/document-list-view.service' | import { QueryParamsService } from 'src/app/services/query-params.service' | ||||||
| import { CorrespondentService } from 'src/app/services/rest/correspondent.service' | import { CorrespondentService } from 'src/app/services/rest/correspondent.service' | ||||||
| import { ToastService } from 'src/app/services/toast.service' | import { ToastService } from 'src/app/services/toast.service' | ||||||
| import { CorrespondentEditDialogComponent } from '../../common/edit-dialog/correspondent-edit-dialog/correspondent-edit-dialog.component' | import { CorrespondentEditDialogComponent } from '../../common/edit-dialog/correspondent-edit-dialog/correspondent-edit-dialog.component' | ||||||
| @@ -20,7 +20,7 @@ export class CorrespondentListComponent extends ManagementListComponent<Paperles | |||||||
|     correspondentsService: CorrespondentService, |     correspondentsService: CorrespondentService, | ||||||
|     modalService: NgbModal, |     modalService: NgbModal, | ||||||
|     toastService: ToastService, |     toastService: ToastService, | ||||||
|     list: DocumentListViewService, |     queryParamsService: QueryParamsService, | ||||||
|     private datePipe: CustomDatePipe |     private datePipe: CustomDatePipe | ||||||
|   ) { |   ) { | ||||||
|     super( |     super( | ||||||
| @@ -28,7 +28,7 @@ export class CorrespondentListComponent extends ManagementListComponent<Paperles | |||||||
|       modalService, |       modalService, | ||||||
|       CorrespondentEditDialogComponent, |       CorrespondentEditDialogComponent, | ||||||
|       toastService, |       toastService, | ||||||
|       list, |       queryParamsService, | ||||||
|       FILTER_CORRESPONDENT, |       FILTER_CORRESPONDENT, | ||||||
|       $localize`correspondent`, |       $localize`correspondent`, | ||||||
|       [ |       [ | ||||||
|   | |||||||
| @@ -2,7 +2,7 @@ import { Component } from '@angular/core' | |||||||
| import { NgbModal } from '@ng-bootstrap/ng-bootstrap' | import { NgbModal } from '@ng-bootstrap/ng-bootstrap' | ||||||
| import { FILTER_DOCUMENT_TYPE } from 'src/app/data/filter-rule-type' | import { FILTER_DOCUMENT_TYPE } from 'src/app/data/filter-rule-type' | ||||||
| import { PaperlessDocumentType } from 'src/app/data/paperless-document-type' | import { PaperlessDocumentType } from 'src/app/data/paperless-document-type' | ||||||
| import { DocumentListViewService } from 'src/app/services/document-list-view.service' | import { QueryParamsService } from 'src/app/services/query-params.service' | ||||||
| import { DocumentTypeService } from 'src/app/services/rest/document-type.service' | import { DocumentTypeService } from 'src/app/services/rest/document-type.service' | ||||||
| import { ToastService } from 'src/app/services/toast.service' | import { ToastService } from 'src/app/services/toast.service' | ||||||
| import { DocumentTypeEditDialogComponent } from '../../common/edit-dialog/document-type-edit-dialog/document-type-edit-dialog.component' | import { DocumentTypeEditDialogComponent } from '../../common/edit-dialog/document-type-edit-dialog/document-type-edit-dialog.component' | ||||||
| @@ -18,14 +18,14 @@ export class DocumentTypeListComponent extends ManagementListComponent<Paperless | |||||||
|     documentTypeService: DocumentTypeService, |     documentTypeService: DocumentTypeService, | ||||||
|     modalService: NgbModal, |     modalService: NgbModal, | ||||||
|     toastService: ToastService, |     toastService: ToastService, | ||||||
|     list: DocumentListViewService |     queryParamsService: QueryParamsService | ||||||
|   ) { |   ) { | ||||||
|     super( |     super( | ||||||
|       documentTypeService, |       documentTypeService, | ||||||
|       modalService, |       modalService, | ||||||
|       DocumentTypeEditDialogComponent, |       DocumentTypeEditDialogComponent, | ||||||
|       toastService, |       toastService, | ||||||
|       list, |       queryParamsService, | ||||||
|       FILTER_DOCUMENT_TYPE, |       FILTER_DOCUMENT_TYPE, | ||||||
|       $localize`document type`, |       $localize`document type`, | ||||||
|       [] |       [] | ||||||
|   | |||||||
| @@ -19,6 +19,7 @@ import { | |||||||
|   SortEvent, |   SortEvent, | ||||||
| } from 'src/app/directives/sortable.directive' | } from 'src/app/directives/sortable.directive' | ||||||
| import { DocumentListViewService } from 'src/app/services/document-list-view.service' | import { DocumentListViewService } from 'src/app/services/document-list-view.service' | ||||||
|  | import { QueryParamsService } from 'src/app/services/query-params.service' | ||||||
| import { AbstractNameFilterService } from 'src/app/services/rest/abstract-name-filter-service' | import { AbstractNameFilterService } from 'src/app/services/rest/abstract-name-filter-service' | ||||||
| import { ToastService } from 'src/app/services/toast.service' | import { ToastService } from 'src/app/services/toast.service' | ||||||
| import { ConfirmDialogComponent } from '../../common/confirm-dialog/confirm-dialog.component' | import { ConfirmDialogComponent } from '../../common/confirm-dialog/confirm-dialog.component' | ||||||
| @@ -42,7 +43,7 @@ export abstract class ManagementListComponent<T extends ObjectWithId> | |||||||
|     private modalService: NgbModal, |     private modalService: NgbModal, | ||||||
|     private editDialogComponent: any, |     private editDialogComponent: any, | ||||||
|     private toastService: ToastService, |     private toastService: ToastService, | ||||||
|     private list: DocumentListViewService, |     private queryParamsService: QueryParamsService, | ||||||
|     protected filterRuleType: number, |     protected filterRuleType: number, | ||||||
|     public typeName: string, |     public typeName: string, | ||||||
|     public extraColumns: ManagementListColumn[] |     public extraColumns: ManagementListColumn[] | ||||||
| @@ -140,7 +141,7 @@ export abstract class ManagementListComponent<T extends ObjectWithId> | |||||||
|   } |   } | ||||||
|  |  | ||||||
|   filterDocuments(object: ObjectWithId) { |   filterDocuments(object: ObjectWithId) { | ||||||
|     this.list.quickFilter([ |     this.queryParamsService.loadFilterRules([ | ||||||
|       { rule_type: this.filterRuleType, value: object.id.toString() }, |       { rule_type: this.filterRuleType, value: object.id.toString() }, | ||||||
|     ]) |     ]) | ||||||
|   } |   } | ||||||
|   | |||||||
| @@ -2,7 +2,7 @@ import { Component } from '@angular/core' | |||||||
| import { NgbModal } from '@ng-bootstrap/ng-bootstrap' | import { NgbModal } from '@ng-bootstrap/ng-bootstrap' | ||||||
| import { FILTER_HAS_TAGS_ALL } from 'src/app/data/filter-rule-type' | import { FILTER_HAS_TAGS_ALL } from 'src/app/data/filter-rule-type' | ||||||
| import { PaperlessTag } from 'src/app/data/paperless-tag' | import { PaperlessTag } from 'src/app/data/paperless-tag' | ||||||
| import { DocumentListViewService } from 'src/app/services/document-list-view.service' | import { QueryParamsService } from 'src/app/services/query-params.service' | ||||||
| import { TagService } from 'src/app/services/rest/tag.service' | import { TagService } from 'src/app/services/rest/tag.service' | ||||||
| import { ToastService } from 'src/app/services/toast.service' | import { ToastService } from 'src/app/services/toast.service' | ||||||
| import { TagEditDialogComponent } from '../../common/edit-dialog/tag-edit-dialog/tag-edit-dialog.component' | import { TagEditDialogComponent } from '../../common/edit-dialog/tag-edit-dialog/tag-edit-dialog.component' | ||||||
| @@ -18,14 +18,14 @@ export class TagListComponent extends ManagementListComponent<PaperlessTag> { | |||||||
|     tagService: TagService, |     tagService: TagService, | ||||||
|     modalService: NgbModal, |     modalService: NgbModal, | ||||||
|     toastService: ToastService, |     toastService: ToastService, | ||||||
|     list: DocumentListViewService |     queryParamsService: QueryParamsService | ||||||
|   ) { |   ) { | ||||||
|     super( |     super( | ||||||
|       tagService, |       tagService, | ||||||
|       modalService, |       modalService, | ||||||
|       TagEditDialogComponent, |       TagEditDialogComponent, | ||||||
|       toastService, |       toastService, | ||||||
|       list, |       queryParamsService, | ||||||
|       FILTER_HAS_TAGS_ALL, |       FILTER_HAS_TAGS_ALL, | ||||||
|       $localize`tag`, |       $localize`tag`, | ||||||
|       [ |       [ | ||||||
|   | |||||||
| @@ -1,5 +1,5 @@ | |||||||
| import { Injectable } from '@angular/core' | import { Injectable } from '@angular/core' | ||||||
| import { ActivatedRoute, Router } from '@angular/router' | import { ActivatedRoute, Params, Router } from '@angular/router' | ||||||
| import { Observable } from 'rxjs' | import { Observable } from 'rxjs' | ||||||
| import { | import { | ||||||
|   cloneFilterRules, |   cloneFilterRules, | ||||||
| @@ -9,6 +9,7 @@ import { | |||||||
| import { PaperlessDocument } from '../data/paperless-document' | import { PaperlessDocument } from '../data/paperless-document' | ||||||
| import { PaperlessSavedView } from '../data/paperless-saved-view' | import { PaperlessSavedView } from '../data/paperless-saved-view' | ||||||
| import { DOCUMENT_LIST_SERVICE } from '../data/storage-keys' | import { DOCUMENT_LIST_SERVICE } from '../data/storage-keys' | ||||||
|  | import { QueryParamsService } from './query-params.service' | ||||||
| import { DocumentService, DOCUMENT_SORT_FIELDS } from './rest/document.service' | import { DocumentService, DOCUMENT_SORT_FIELDS } from './rest/document.service' | ||||||
| import { SettingsService, SETTINGS_KEYS } from './settings.service' | import { SettingsService, SETTINGS_KEYS } from './settings.service' | ||||||
|  |  | ||||||
| @@ -220,6 +221,13 @@ export class DocumentListViewService { | |||||||
|     return this.activeListViewState.sortReverse |     return this.activeListViewState.sortReverse | ||||||
|   } |   } | ||||||
|  |  | ||||||
|  |   get sortParams(): Params { | ||||||
|  |     return { | ||||||
|  |       sortField: this.sortField, | ||||||
|  |       sortReverse: this.sortReverse, | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  |  | ||||||
|   get collectionSize(): number { |   get collectionSize(): number { | ||||||
|     return this.activeListViewState.collectionSize |     return this.activeListViewState.collectionSize | ||||||
|   } |   } | ||||||
| @@ -265,14 +273,6 @@ export class DocumentListViewService { | |||||||
|     } |     } | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   quickFilter(filterRules: FilterRule[]) { |  | ||||||
|     const params = this.documentService.filterRulesToQueryParams(filterRules) |  | ||||||
|     this.router.navigate(['/documents'], { |  | ||||||
|       relativeTo: this.route, |  | ||||||
|       queryParams: params, |  | ||||||
|     }) |  | ||||||
|   } |  | ||||||
|  |  | ||||||
|   getLastPage(): number { |   getLastPage(): number { | ||||||
|     return Math.ceil(this.collectionSize / this.currentPageSize) |     return Math.ceil(this.collectionSize / this.currentPageSize) | ||||||
|   } |   } | ||||||
| @@ -435,8 +435,7 @@ export class DocumentListViewService { | |||||||
|   constructor( |   constructor( | ||||||
|     private documentService: DocumentService, |     private documentService: DocumentService, | ||||||
|     private settings: SettingsService, |     private settings: SettingsService, | ||||||
|     private router: Router, |     private queryParamsService: QueryParamsService | ||||||
|     private route: ActivatedRoute |  | ||||||
|   ) { |   ) { | ||||||
|     let documentListViewConfigJson = localStorage.getItem( |     let documentListViewConfigJson = localStorage.getItem( | ||||||
|       DOCUMENT_LIST_SERVICE.CURRENT_VIEW_CONFIG |       DOCUMENT_LIST_SERVICE.CURRENT_VIEW_CONFIG | ||||||
|   | |||||||
							
								
								
									
										101
									
								
								src-ui/src/app/services/query-params.service.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										101
									
								
								src-ui/src/app/services/query-params.service.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,101 @@ | |||||||
|  | import { Injectable } from '@angular/core' | ||||||
|  | import { | ||||||
|  |   ActivatedRoute, | ||||||
|  |   convertToParamMap, | ||||||
|  |   ParamMap, | ||||||
|  |   Params, | ||||||
|  |   Router, | ||||||
|  | } from '@angular/router' | ||||||
|  | import { FilterRule } from '../data/filter-rule' | ||||||
|  | import { FILTER_RULE_TYPES } from '../data/filter-rule-type' | ||||||
|  |  | ||||||
|  | @Injectable({ | ||||||
|  |   providedIn: 'root', | ||||||
|  | }) | ||||||
|  | export class QueryParamsService { | ||||||
|  |   constructor(private router: Router, private route: ActivatedRoute) {} | ||||||
|  |  | ||||||
|  |   private filterParams: Params | ||||||
|  |   private _filterRules: FilterRule[] | ||||||
|  |  | ||||||
|  |   set filterRules(filterRules: FilterRule[]) { | ||||||
|  |     this._filterRules = filterRules | ||||||
|  |     this.filterParams = this.filterRulesToQueryParams(filterRules) | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   get filterRules(): FilterRule[] { | ||||||
|  |     return this._filterRules | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   set params(params: any) { | ||||||
|  |     this.filterParams = params | ||||||
|  |     this._filterRules = this.filterRulesFromQueryParams( | ||||||
|  |       params.keys ? params : convertToParamMap(params) // ParamMap | ||||||
|  |     ) | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   get params(): Params { | ||||||
|  |     return { | ||||||
|  |       ...this.filterParams, | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   private filterRulesToQueryParams(filterRules: FilterRule[]): Object { | ||||||
|  |     if (filterRules) { | ||||||
|  |       let params = {} | ||||||
|  |       for (let rule of filterRules) { | ||||||
|  |         let ruleType = FILTER_RULE_TYPES.find((t) => t.id == rule.rule_type) | ||||||
|  |         if (ruleType.multi) { | ||||||
|  |           params[ruleType.filtervar] = params[ruleType.filtervar] | ||||||
|  |             ? params[ruleType.filtervar] + ',' + rule.value | ||||||
|  |             : rule.value | ||||||
|  |         } else if (ruleType.isnull_filtervar && rule.value == null) { | ||||||
|  |           params[ruleType.isnull_filtervar] = true | ||||||
|  |         } else { | ||||||
|  |           params[ruleType.filtervar] = rule.value | ||||||
|  |         } | ||||||
|  |       } | ||||||
|  |       return params | ||||||
|  |     } else { | ||||||
|  |       return null | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   private filterRulesFromQueryParams(queryParams: ParamMap) { | ||||||
|  |     const allFilterRuleQueryParams: string[] = FILTER_RULE_TYPES.map( | ||||||
|  |       (rt) => rt.filtervar | ||||||
|  |     ) | ||||||
|  |  | ||||||
|  |     // transform query params to filter rules | ||||||
|  |     let filterRulesFromQueryParams: FilterRule[] = [] | ||||||
|  |     allFilterRuleQueryParams | ||||||
|  |       .filter((frqp) => queryParams.has(frqp)) | ||||||
|  |       .forEach((filterQueryParamName) => { | ||||||
|  |         const filterQueryParamValues: string[] = queryParams | ||||||
|  |           .get(filterQueryParamName) | ||||||
|  |           .split(',') | ||||||
|  |  | ||||||
|  |         filterRulesFromQueryParams = filterRulesFromQueryParams.concat( | ||||||
|  |           // map all values to filter rules | ||||||
|  |           filterQueryParamValues.map((val) => { | ||||||
|  |             return { | ||||||
|  |               rule_type: FILTER_RULE_TYPES.find( | ||||||
|  |                 (rt) => rt.filtervar == filterQueryParamName | ||||||
|  |               ).id, | ||||||
|  |               value: val, | ||||||
|  |             } | ||||||
|  |           }) | ||||||
|  |         ) | ||||||
|  |       }) | ||||||
|  |  | ||||||
|  |     return filterRulesFromQueryParams | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   loadFilterRules(filterRules: FilterRule[]) { | ||||||
|  |     this.filterRules = filterRules | ||||||
|  |     this.router.navigate(['/documents'], { | ||||||
|  |       relativeTo: this.route, | ||||||
|  |       queryParams: this.params, | ||||||
|  |     }) | ||||||
|  |   } | ||||||
|  | } | ||||||
| @@ -12,6 +12,7 @@ import { DocumentTypeService } from './document-type.service' | |||||||
| import { TagService } from './tag.service' | import { TagService } from './tag.service' | ||||||
| import { FILTER_RULE_TYPES } from 'src/app/data/filter-rule-type' | import { FILTER_RULE_TYPES } from 'src/app/data/filter-rule-type' | ||||||
| import { PaperlessDocumentSuggestions } from 'src/app/data/paperless-document-suggestions' | import { PaperlessDocumentSuggestions } from 'src/app/data/paperless-document-suggestions' | ||||||
|  | import { QueryParamsService } from '../query-params.service' | ||||||
|  |  | ||||||
| export const DOCUMENT_SORT_FIELDS = [ | export const DOCUMENT_SORT_FIELDS = [ | ||||||
|   { field: 'archive_serial_number', name: $localize`ASN` }, |   { field: 'archive_serial_number', name: $localize`ASN` }, | ||||||
| @@ -52,32 +53,12 @@ export class DocumentService extends AbstractPaperlessService<PaperlessDocument> | |||||||
|     http: HttpClient, |     http: HttpClient, | ||||||
|     private correspondentService: CorrespondentService, |     private correspondentService: CorrespondentService, | ||||||
|     private documentTypeService: DocumentTypeService, |     private documentTypeService: DocumentTypeService, | ||||||
|     private tagService: TagService |     private tagService: TagService, | ||||||
|  |     private queryParamsService: QueryParamsService | ||||||
|   ) { |   ) { | ||||||
|     super(http, 'documents') |     super(http, 'documents') | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   public filterRulesToQueryParams(filterRules: FilterRule[]): Object { |  | ||||||
|     if (filterRules) { |  | ||||||
|       let params = {} |  | ||||||
|       for (let rule of filterRules) { |  | ||||||
|         let ruleType = FILTER_RULE_TYPES.find((t) => t.id == rule.rule_type) |  | ||||||
|         if (ruleType.multi) { |  | ||||||
|           params[ruleType.filtervar] = params[ruleType.filtervar] |  | ||||||
|             ? params[ruleType.filtervar] + ',' + rule.value |  | ||||||
|             : rule.value |  | ||||||
|         } else if (ruleType.isnull_filtervar && rule.value == null) { |  | ||||||
|           params[ruleType.isnull_filtervar] = true |  | ||||||
|         } else { |  | ||||||
|           params[ruleType.filtervar] = rule.value |  | ||||||
|         } |  | ||||||
|       } |  | ||||||
|       return params |  | ||||||
|     } else { |  | ||||||
|       return null |  | ||||||
|     } |  | ||||||
|   } |  | ||||||
|  |  | ||||||
|   addObservablesToDocument(doc: PaperlessDocument) { |   addObservablesToDocument(doc: PaperlessDocument) { | ||||||
|     if (doc.correspondent) { |     if (doc.correspondent) { | ||||||
|       doc.correspondent$ = this.correspondentService.getCached( |       doc.correspondent$ = this.correspondentService.getCached( | ||||||
| @@ -101,12 +82,13 @@ export class DocumentService extends AbstractPaperlessService<PaperlessDocument> | |||||||
|     filterRules?: FilterRule[], |     filterRules?: FilterRule[], | ||||||
|     extraParams = {} |     extraParams = {} | ||||||
|   ): Observable<Results<PaperlessDocument>> { |   ): Observable<Results<PaperlessDocument>> { | ||||||
|  |     this.queryParamsService.filterRules = filterRules | ||||||
|     return this.list( |     return this.list( | ||||||
|       page, |       page, | ||||||
|       pageSize, |       pageSize, | ||||||
|       sortField, |       sortField, | ||||||
|       sortReverse, |       sortReverse, | ||||||
|       Object.assign(extraParams, this.filterRulesToQueryParams(filterRules)) |       Object.assign(extraParams, this.queryParamsService.params) | ||||||
|     ).pipe( |     ).pipe( | ||||||
|       map((results) => { |       map((results) => { | ||||||
|         results.results.forEach((doc) => this.addObservablesToDocument(doc)) |         results.results.forEach((doc) => this.addObservablesToDocument(doc)) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Michael Shamoon
					Michael Shamoon