Merge branch 'dev' into feature-bulk-edit

This commit is contained in:
jonaswinkler
2020-12-06 02:12:15 +01:00
115 changed files with 3607 additions and 1553 deletions

View File

@@ -82,6 +82,12 @@ export class DocumentListViewService {
this.reload()
}
clear() {
this.collectionSize = null
this.documents = []
this.currentPage = 1
}
reload(onFinish?) {
this.isReloading = true
this.documentService.list(

View File

@@ -1,5 +1,6 @@
import { HttpClient, HttpParams } from '@angular/common/http'
import { Observable } from 'rxjs'
import { Observable, of, Subject } from 'rxjs'
import { map, publishReplay, refCount } from 'rxjs/operators'
import { ObjectWithId } from 'src/app/data/object-with-id'
import { Results } from 'src/app/data/results'
import { environment } from 'src/environments/environment'
@@ -51,8 +52,28 @@ export abstract class AbstractPaperlessService<T extends ObjectWithId> {
return this.http.get<Results<T>>(this.getResourceUrl(), {params: httpParams})
}
private _listAll: Observable<Results<T>>
listAll(ordering?: string, extraParams?): Observable<Results<T>> {
return this.list(1, 100000, ordering, extraParams)
if (!this._listAll) {
this._listAll = this.list(1, 100000, ordering, extraParams).pipe(
publishReplay(1),
refCount()
)
}
return this._listAll
}
getCached(id: number): Observable<T> {
return this.listAll().pipe(
map(list => list.results.find(o => o.id == id))
)
}
getCachedMany(ids: number[]): Observable<T[]> {
return this.listAll().pipe(
map(list => ids.map(id => list.results.find(o => o.id == id)))
)
}
get(id: number): Observable<T> {
@@ -60,14 +81,17 @@ export abstract class AbstractPaperlessService<T extends ObjectWithId> {
}
create(o: T): Observable<T> {
this._listAll = null
return this.http.post<T>(this.getResourceUrl(), o)
}
delete(o: T): Observable<any> {
this._listAll = null
return this.http.delete(this.getResourceUrl(o.id))
}
update(o: T): Observable<T> {
this._listAll = null
return this.http.put<T>(this.getResourceUrl(o.id), o)
}
}

View File

@@ -1,10 +1,15 @@
import { Injectable } from '@angular/core';
import { PaperlessDocument } from 'src/app/data/paperless-document';
import { PaperlessDocumentMetadata } from 'src/app/data/paperless-document-metadata';
import { AbstractPaperlessService } from './abstract-paperless-service';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import { Results } from 'src/app/data/results';
import { FilterRule } from 'src/app/data/filter-rule';
import { map } from 'rxjs/operators';
import { CorrespondentService } from './correspondent.service';
import { DocumentTypeService } from './document-type.service';
import { TagService } from './tag.service';
export const DOCUMENT_SORT_FIELDS = [
@@ -26,7 +31,7 @@ export const SORT_DIRECTION_DESCENDING = "des"
})
export class DocumentService extends AbstractPaperlessService<PaperlessDocument> {
constructor(http: HttpClient) {
constructor(http: HttpClient, private correspondentService: CorrespondentService, private documentTypeService: DocumentTypeService, private tagService: TagService) {
super(http, 'documents')
}
@@ -46,26 +51,56 @@ export class DocumentService extends AbstractPaperlessService<PaperlessDocument>
}
}
list(page?: number, pageSize?: number, sortField?: string, sortDirection?: string, filterRules?: FilterRule[]): Observable<Results<PaperlessDocument>> {
return super.list(page, pageSize, sortField, sortDirection, this.filterRulesToQueryParams(filterRules))
addObservablesToDocument(doc: PaperlessDocument) {
if (doc.correspondent) {
doc.correspondent$ = this.correspondentService.getCached(doc.correspondent)
}
if (doc.document_type) {
doc.document_type$ = this.documentTypeService.getCached(doc.document_type)
}
if (doc.tags) {
doc.tags$ = this.tagService.getCachedMany(doc.tags)
}
return doc
}
getPreviewUrl(id: number): string {
return this.getResourceUrl(id, 'preview')
list(page?: number, pageSize?: number, sortField?: string, sortDirection?: string, filterRules?: FilterRule[]): Observable<Results<PaperlessDocument>> {
return super.list(page, pageSize, sortField, sortDirection, this.filterRulesToQueryParams(filterRules)).pipe(
map(results => {
results.results.forEach(doc => this.addObservablesToDocument(doc))
return results
})
)
}
getPreviewUrl(id: number, original: boolean = false): string {
let url = this.getResourceUrl(id, 'preview')
if (original) {
url += "?original=true"
}
return url
}
getThumbUrl(id: number): string {
return this.getResourceUrl(id, 'thumb')
}
getDownloadUrl(id: number): string {
return this.getResourceUrl(id, 'download')
getDownloadUrl(id: number, original: boolean = false): string {
let url = this.getResourceUrl(id, 'download')
if (original) {
url += "?original=true"
}
return url
}
uploadDocument(formData) {
return this.http.post(this.getResourceUrl(null, 'post_document'), formData)
}
getMetadata(id: number): Observable<PaperlessDocumentMetadata> {
return this.http.get<PaperlessDocumentMetadata>(this.getResourceUrl(id, 'metadata'))
}
bulk_edit(ids: number[], method: string, args: any[]) {
return this.http.post(this.getResourceUrl(null, 'bulk_edit'), {
'ids': ids,

View File

@@ -1,9 +1,11 @@
import { HttpClient, HttpParams } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { PaperlessDocument } from 'src/app/data/paperless-document';
import { SearchResult } from 'src/app/data/search-result';
import { environment } from 'src/environments/environment';
import { DocumentService } from './document.service';
@Injectable({
@@ -11,14 +13,19 @@ import { environment } from 'src/environments/environment';
})
export class SearchService {
constructor(private http: HttpClient) { }
constructor(private http: HttpClient, private documentService: DocumentService) { }
search(query: string, page?: number): Observable<SearchResult> {
let httpParams = new HttpParams().set('query', query)
if (page) {
httpParams = httpParams.set('page', page.toString())
}
return this.http.get<SearchResult>(`${environment.apiBaseUrl}search/`, {params: httpParams})
return this.http.get<SearchResult>(`${environment.apiBaseUrl}search/`, {params: httpParams}).pipe(
map(result => {
result.results.forEach(hit => this.documentService.addObservablesToDocument(hit.document))
return result
})
)
}
autocomplete(term: string): Observable<string[]> {