mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2025-07-28 18:24:38 -05:00
Saved views, some refactoring
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { Observable } from 'rxjs';
|
||||
import { FilterRuleSet } from '../components/filter-editor/filter-editor.component';
|
||||
import { cloneFilterRules, FilterRule, filterRulesToQueryParams } from '../data/filter-rule';
|
||||
import { PaperlessDocument } from '../data/paperless-document';
|
||||
import { SavedViewConfig } from '../data/saved-view-config';
|
||||
import { DocumentService } from './rest/document.service';
|
||||
|
||||
@Injectable({
|
||||
@@ -24,17 +25,28 @@ export class DocumentListViewService {
|
||||
currentPage = 1
|
||||
collectionSize: number
|
||||
|
||||
currentFilter = new FilterRuleSet()
|
||||
|
||||
currentFilterRules: FilterRule[] = []
|
||||
currentSortDirection = 'des'
|
||||
currentSortField = DocumentListViewService.DEFAULT_SORT_FIELD
|
||||
|
||||
viewConfig: SavedViewConfig
|
||||
|
||||
reload(onFinish?) {
|
||||
let ordering: string
|
||||
let filterRules: FilterRule[]
|
||||
if (this.viewConfig) {
|
||||
ordering = this.getOrderingQueryParam(this.viewConfig.sortField, this.viewConfig.sortDirection)
|
||||
filterRules = this.viewConfig.filterRules
|
||||
} else {
|
||||
ordering = this.getOrderingQueryParam(this.currentSortField, this.currentSortDirection)
|
||||
filterRules = this.currentFilterRules
|
||||
}
|
||||
|
||||
this.documentService.list(
|
||||
this.currentPage,
|
||||
null,
|
||||
this.getOrderingQueryParam(),
|
||||
this.currentFilter.toQueryParams()).subscribe(
|
||||
ordering,
|
||||
filterRulesToQueryParams(filterRules)).subscribe(
|
||||
result => {
|
||||
this.collectionSize = result.count
|
||||
this.documents = result.results
|
||||
@@ -50,16 +62,17 @@ export class DocumentListViewService {
|
||||
})
|
||||
}
|
||||
|
||||
getOrderingQueryParam() {
|
||||
if (DocumentListViewService.SORT_FIELDS.find(f => f.field == this.currentSortField)) {
|
||||
return (this.currentSortDirection == 'des' ? '-' : '') + this.currentSortField
|
||||
getOrderingQueryParam(sortField: string, sortDirection: string) {
|
||||
if (DocumentListViewService.SORT_FIELDS.find(f => f.field == sortField)) {
|
||||
return (sortDirection == 'des' ? '-' : '') + sortField
|
||||
} else {
|
||||
return DocumentListViewService.DEFAULT_SORT_FIELD
|
||||
}
|
||||
}
|
||||
|
||||
setFilter(filter: FilterRuleSet) {
|
||||
this.currentFilter = filter
|
||||
//TODO: refactor
|
||||
setFilterRules(filterRules: FilterRule[]) {
|
||||
this.currentFilterRules = cloneFilterRules(filterRules)
|
||||
}
|
||||
|
||||
getLastPage(): number {
|
||||
|
16
src-ui/src/app/services/saved-view-config.service.spec.ts
Normal file
16
src-ui/src/app/services/saved-view-config.service.spec.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
|
||||
import { SavedViewConfigService } from './saved-view-config.service';
|
||||
|
||||
describe('SavedViewConfigService', () => {
|
||||
let service: SavedViewConfigService;
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({});
|
||||
service = TestBed.inject(SavedViewConfigService);
|
||||
});
|
||||
|
||||
it('should be created', () => {
|
||||
expect(service).toBeTruthy();
|
||||
});
|
||||
});
|
54
src-ui/src/app/services/saved-view-config.service.ts
Normal file
54
src-ui/src/app/services/saved-view-config.service.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
import { SavedViewConfig } from '../data/saved-view-config';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class SavedViewConfigService {
|
||||
|
||||
constructor() {
|
||||
let savedConfigs = localStorage.getItem('saved-view-config-service:savedConfigs')
|
||||
if (savedConfigs) {
|
||||
this.configs = JSON.parse(savedConfigs)
|
||||
}
|
||||
}
|
||||
|
||||
private configs: SavedViewConfig[] = []
|
||||
|
||||
getConfigs(): SavedViewConfig[] {
|
||||
return this.configs
|
||||
}
|
||||
|
||||
getDashboardConfigs(): SavedViewConfig[] {
|
||||
return this.configs.filter(sf => sf.showInDashboard)
|
||||
}
|
||||
|
||||
getSideBarConfigs(): SavedViewConfig[] {
|
||||
return this.configs.filter(sf => sf.showInSideBar)
|
||||
}
|
||||
|
||||
getConfig(id: string): SavedViewConfig {
|
||||
return this.configs.find(sf => sf.id == id)
|
||||
}
|
||||
|
||||
saveConfig(config: SavedViewConfig) {
|
||||
config.id = uuidv4()
|
||||
this.configs.push(config)
|
||||
|
||||
this.save()
|
||||
}
|
||||
|
||||
private save() {
|
||||
localStorage.setItem('saved-view-config-service:savedConfigs', JSON.stringify(this.configs))
|
||||
}
|
||||
|
||||
deleteConfig(config: SavedViewConfig) {
|
||||
let index = this.configs.findIndex(vc => vc.id == config.id)
|
||||
if (index != -1) {
|
||||
this.configs.splice(index, 1)
|
||||
this.save()
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user