mirror of
				https://github.com/paperless-ngx/paperless-ngx.git
				synced 2025-10-30 03:56:23 -05:00 
			
		
		
		
	current view persists during session, code cleanup
This commit is contained in:
		| @@ -46,19 +46,5 @@ | ||||
|  | ||||
|       </ngx-file-drop> | ||||
|     </form> | ||||
|     <h5 class="mt-3">Document conumser status</h5> | ||||
|     <p>This is what it might look like in the future.</p> | ||||
|     <div class="card bg-light mb-2"> | ||||
|       <div class="card-body"> | ||||
|         <p class="card-text"><strong>Filename.pdf:</strong> Running tesseract on page 4/8...</p> | ||||
|         <p><ngb-progressbar type="info" [value]="50"></ngb-progressbar></p> | ||||
|       </div> | ||||
|     </div> | ||||
|     <div class="card bg-light mb-2"> | ||||
|       <div class="card-body"> | ||||
|         <p class="card-text"><strong>Filename2.pdf:</strong> Completed.</p> | ||||
|         <p><ngb-progressbar type="success" [value]="100"></ngb-progressbar></p> | ||||
|       </div> | ||||
|     </div> | ||||
|   </div> | ||||
| </div> | ||||
|   | ||||
| @@ -134,8 +134,8 @@ export class DocumentDetailComponent implements OnInit { | ||||
|  | ||||
|   close() { | ||||
|     this.openDocumentService.closeDocument(this.document) | ||||
|     if (this.documentListViewService.viewConfig) { | ||||
|       this.router.navigate(['view', this.documentListViewService.viewConfig.id]) | ||||
|     if (this.documentListViewService.viewId) { | ||||
|       this.router.navigate(['view', this.documentListViewService.viewId]) | ||||
|     } else { | ||||
|       this.router.navigate(['documents']) | ||||
|     } | ||||
|   | ||||
| @@ -1,4 +1,4 @@ | ||||
| <app-page-header [title]="docs.viewConfig ? docs.viewConfig.title : 'Documents'"> | ||||
| <app-page-header [title]="getTitle()"> | ||||
|  | ||||
|   <div class="btn-group btn-group-toggle mr-2" ngbRadioGroup [(ngModel)]="displayMode" | ||||
|     (ngModelChange)="saveDisplayMode()"> | ||||
| @@ -21,14 +21,13 @@ | ||||
|       </svg> | ||||
|     </label> | ||||
|   </div> | ||||
|   <div class="btn-group btn-group-toggle mr-2" ngbRadioGroup [(ngModel)]="docs.currentSortDirection" | ||||
|     (ngModelChange)="reload()" | ||||
|     *ngIf="!docs.viewConfig"> | ||||
|   <div class="btn-group btn-group-toggle mr-2" ngbRadioGroup [(ngModel)]="docs.sortDirection" | ||||
|     *ngIf="!docs.viewId"> | ||||
|     <div ngbDropdown class="btn-group"> | ||||
|       <button class="btn btn-outline-secondary btn-sm" id="dropdownBasic1" ngbDropdownToggle>Sort by</button> | ||||
|       <div ngbDropdownMenu aria-labelledby="dropdownBasic1"> | ||||
|         <button *ngFor="let f of getSortFields()" ngbDropdownItem (click)="setSort(f.field)" | ||||
|           [class.active]="docs.currentSortField == f.field">{{f.name}}</button> | ||||
|           [class.active]="docs.sortField == f.field">{{f.name}}</button> | ||||
|       </div> | ||||
|     </div> | ||||
|     <label ngbButtonLabel class="btn-outline-secondary btn-sm"> | ||||
| @@ -44,7 +43,7 @@ | ||||
|       </svg> | ||||
|     </label> | ||||
|   </div> | ||||
|   <div class="btn-group" *ngIf="!docs.viewConfig"> | ||||
|   <div class="btn-group" *ngIf="!docs.viewId"> | ||||
|  | ||||
|     <button type="button" class="btn btn-sm btn-outline-secondary" (click)="showFilter=!showFilter"> | ||||
|       <svg class="toolbaricon" fill="currentColor"> | ||||
| @@ -62,7 +61,6 @@ | ||||
|       </div> | ||||
|     </div> | ||||
|  | ||||
|  | ||||
|   </div> | ||||
| </app-page-header> | ||||
|  | ||||
|   | ||||
| @@ -26,13 +26,16 @@ export class DocumentListComponent implements OnInit { | ||||
|   filterRules: FilterRule[] = [] | ||||
|   showFilter = false | ||||
|  | ||||
|   getTitle() { | ||||
|     return this.docs.viewConfigOverride ? this.docs.viewConfigOverride.title : "Documents" | ||||
|   } | ||||
|  | ||||
|   getSortFields() { | ||||
|     return DOCUMENT_SORT_FIELDS | ||||
|   } | ||||
|  | ||||
|   setSort(field: string) { | ||||
|     this.docs.currentSortField = field | ||||
|     this.reload() | ||||
|     this.docs.sortField = field | ||||
|   } | ||||
|  | ||||
|   saveDisplayMode() { | ||||
| @@ -45,11 +48,11 @@ export class DocumentListComponent implements OnInit { | ||||
|     } | ||||
|     this.route.paramMap.subscribe(params => { | ||||
|       if (params.has('id')) { | ||||
|         this.docs.viewConfig = this.savedViewConfigService.getConfig(params.get('id')) | ||||
|         this.docs.viewConfigOverride = this.savedViewConfigService.getConfig(params.get('id')) | ||||
|       } else { | ||||
|         this.filterRules = cloneFilterRules(this.docs.currentFilterRules) | ||||
|         this.filterRules = this.docs.filterRules | ||||
|         this.showFilter = this.filterRules.length > 0 | ||||
|         this.docs.viewConfig = null | ||||
|         this.docs.viewConfigOverride = null | ||||
|       } | ||||
|       this.reload() | ||||
|     }) | ||||
| @@ -60,28 +63,24 @@ export class DocumentListComponent implements OnInit { | ||||
|   } | ||||
|  | ||||
|   applyFilterRules() { | ||||
|     this.docs.setFilterRules(this.filterRules) | ||||
|     this.reload() | ||||
|     this.docs.filterRules = this.filterRules | ||||
|   } | ||||
|  | ||||
|   loadViewConfig(config: SavedViewConfig) { | ||||
|     this.filterRules = cloneFilterRules(config.filterRules) | ||||
|     this.docs.setFilterRules(config.filterRules) | ||||
|     this.docs.currentSortField = config.sortField | ||||
|     this.docs.currentSortDirection = config.sortDirection | ||||
|     this.reload() | ||||
|     this.docs.loadViewConfig(config) | ||||
|   } | ||||
|  | ||||
|   saveViewConfig() { | ||||
|     let modal = this.modalService.open(SaveViewConfigDialogComponent, {backdrop: 'static'}) | ||||
|     modal.componentInstance.saveClicked.subscribe(formValue => { | ||||
|       this.savedViewConfigService.saveConfig({ | ||||
|         filterRules: cloneFilterRules(this.filterRules), | ||||
|         title: formValue.title, | ||||
|         showInDashboard: formValue.showInDashboard, | ||||
|         showInSideBar: formValue.showInSideBar, | ||||
|         sortDirection: this.docs.currentSortDirection, | ||||
|         sortField: this.docs.currentSortField | ||||
|         filterRules: this.docs.filterRules, | ||||
|         sortDirection: this.docs.sortDirection, | ||||
|         sortField: this.docs.sortField | ||||
|       }) | ||||
|       modal.close() | ||||
|     }) | ||||
|   | ||||
| @@ -10,10 +10,10 @@ export interface SavedViewConfig { | ||||
|  | ||||
|   sortDirection: string | ||||
|  | ||||
|   title: string | ||||
|   title?: string | ||||
|  | ||||
|   showInSideBar: boolean | ||||
|   showInSideBar?: boolean | ||||
|  | ||||
|   showInDashboard: boolean | ||||
|   showInDashboard?: boolean | ||||
|  | ||||
| } | ||||
| @@ -2,6 +2,10 @@ export const OPEN_DOCUMENT_SERVICE = { | ||||
|   DOCUMENTS: 'open-documents-service:openDocuments' | ||||
| } | ||||
|  | ||||
| export const DOCUMENT_LIST_SERVICE = { | ||||
|   CURRENT_VIEW_CONFIG: 'document-list-service:currentViewConfig' | ||||
| } | ||||
|  | ||||
| export const GENERAL_SETTINGS = { | ||||
|   DOCUMENT_LIST_SIZE: 'general-settings:documentListSize', | ||||
|   DOCUMENT_LIST_SIZE_DEFAULT: 50 | ||||
|   | ||||
| @@ -3,8 +3,8 @@ import { Observable } from 'rxjs'; | ||||
| import { cloneFilterRules, FilterRule } from '../data/filter-rule'; | ||||
| import { PaperlessDocument } from '../data/paperless-document'; | ||||
| import { SavedViewConfig } from '../data/saved-view-config'; | ||||
| import { GENERAL_SETTINGS } from '../data/storage-keys'; | ||||
| import { DocumentService, SORT_DIRECTION_DESCENDING } from './rest/document.service'; | ||||
| import { DOCUMENT_LIST_SERVICE, GENERAL_SETTINGS } from '../data/storage-keys'; | ||||
| import { DocumentService } from './rest/document.service'; | ||||
|  | ||||
|  | ||||
| @Injectable({ | ||||
| @@ -19,32 +19,23 @@ export class DocumentListViewService { | ||||
|   currentPageSize: number = +localStorage.getItem(GENERAL_SETTINGS.DOCUMENT_LIST_SIZE) || GENERAL_SETTINGS.DOCUMENT_LIST_SIZE_DEFAULT | ||||
|   collectionSize: number | ||||
|    | ||||
|   currentFilterRules: FilterRule[] = [] | ||||
|   currentSortDirection = SORT_DIRECTION_DESCENDING | ||||
|   currentSortField = DocumentListViewService.DEFAULT_SORT_FIELD | ||||
|   private currentViewConfig: SavedViewConfig | ||||
|   //TODO: make private | ||||
|   viewConfigOverride: SavedViewConfig | ||||
|  | ||||
|   viewConfig: SavedViewConfig | ||||
|   get viewId() { | ||||
|     return this.viewConfigOverride?.id | ||||
|   } | ||||
|  | ||||
|   reload(onFinish?) { | ||||
|     let sortField: string | ||||
|     let sortDirection: string | ||||
|     let filterRules: FilterRule[] | ||||
|     if (this.viewConfig) { | ||||
|       sortField = this.viewConfig.sortField | ||||
|       sortDirection = this.viewConfig.sortDirection | ||||
|       filterRules = this.viewConfig.filterRules | ||||
|     } else { | ||||
|       sortField = this.currentSortField | ||||
|       sortDirection = this.currentSortDirection | ||||
|       filterRules = this.currentFilterRules | ||||
|     } | ||||
|     let viewConfig = this.viewConfigOverride || this.currentViewConfig | ||||
|  | ||||
|     this.documentService.list( | ||||
|       this.currentPage, | ||||
|       this.currentPageSize, | ||||
|       sortField, | ||||
|       sortDirection, | ||||
|       filterRules).subscribe( | ||||
|       viewConfig.sortField, | ||||
|       viewConfig.sortDirection, | ||||
|       viewConfig.filterRules).subscribe( | ||||
|         result => { | ||||
|           this.collectionSize = result.count | ||||
|           this.documents = result.results | ||||
| @@ -60,9 +51,43 @@ export class DocumentListViewService { | ||||
|         }) | ||||
|   } | ||||
|  | ||||
|   set filterRules(filterRules: FilterRule[]) { | ||||
|     this.currentViewConfig.filterRules = cloneFilterRules(filterRules) | ||||
|     this.saveCurrentViewConfig() | ||||
|     this.reload() | ||||
|   } | ||||
|  | ||||
|   setFilterRules(filterRules: FilterRule[]) { | ||||
|     this.currentFilterRules = cloneFilterRules(filterRules) | ||||
|   get filterRules(): FilterRule[] { | ||||
|     return cloneFilterRules(this.currentViewConfig.filterRules) | ||||
|   } | ||||
|  | ||||
|   set sortField(field: string) { | ||||
|     this.currentViewConfig.sortField = field | ||||
|     this.saveCurrentViewConfig() | ||||
|     this.reload() | ||||
|   } | ||||
|  | ||||
|   get sortField(): string { | ||||
|     return this.currentViewConfig.sortField | ||||
|   } | ||||
|  | ||||
|   set sortDirection(direction: string) { | ||||
|     this.currentViewConfig.sortDirection = direction | ||||
|     this.saveCurrentViewConfig() | ||||
|     this.reload() | ||||
|   } | ||||
|  | ||||
|   get sortDirection(): string { | ||||
|     return this.currentViewConfig.sortDirection | ||||
|   } | ||||
|  | ||||
|   loadViewConfig(config: SavedViewConfig) { | ||||
|     Object.assign(this.currentViewConfig, config) | ||||
|     this.reload() | ||||
|   } | ||||
|  | ||||
|   private saveCurrentViewConfig() { | ||||
|     sessionStorage.setItem(DOCUMENT_LIST_SERVICE.CURRENT_VIEW_CONFIG, JSON.stringify(this.currentViewConfig)) | ||||
|   } | ||||
|  | ||||
|   getLastPage(): number { | ||||
| @@ -108,5 +133,22 @@ export class DocumentListViewService { | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   constructor(private documentService: DocumentService) { } | ||||
|   constructor(private documentService: DocumentService) {  | ||||
|     let currentViewConfigJson = sessionStorage.getItem(DOCUMENT_LIST_SERVICE.CURRENT_VIEW_CONFIG) | ||||
|     if (currentViewConfigJson) { | ||||
|       try { | ||||
|         this.currentViewConfig = JSON.parse(currentViewConfigJson) | ||||
|       } catch (e) { | ||||
|         sessionStorage.removeItem(DOCUMENT_LIST_SERVICE.CURRENT_VIEW_CONFIG) | ||||
|         this.currentViewConfig = null | ||||
|       } | ||||
|     } | ||||
|     if (!this.currentViewConfig) { | ||||
|       this.currentViewConfig = { | ||||
|         filterRules: [], | ||||
|         sortDirection: 'des', | ||||
|         sortField: 'created' | ||||
|       } | ||||
|       } | ||||
|   } | ||||
| } | ||||
|   | ||||
| @@ -10,7 +10,11 @@ export class SavedViewConfigService { | ||||
|   constructor() {  | ||||
|     let savedConfigs = localStorage.getItem('saved-view-config-service:savedConfigs') | ||||
|     if (savedConfigs) { | ||||
|       try { | ||||
|         this.configs = JSON.parse(savedConfigs) | ||||
|       } catch (e) { | ||||
|         this.configs = [] | ||||
|       } | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Jonas Winkler
					Jonas Winkler