documents now remain open fixes #16

This commit is contained in:
Jonas Winkler
2020-11-04 01:42:23 +01:00
parent 828752142a
commit 8dd773e059
3 changed files with 26 additions and 7 deletions

View File

@@ -1,26 +1,38 @@
import { Injectable } from '@angular/core';
import { Observable, Subject } from 'rxjs';
import { PaperlessDocument } from '../data/paperless-document';
import { OPEN_DOCUMENT_SERVICE } from '../data/storage-keys';
@Injectable({
providedIn: 'root'
})
export class OpenDocumentsService {
constructor() { }
constructor() {
if (sessionStorage.getItem(OPEN_DOCUMENT_SERVICE.DOCUMENTS)) {
try {
this.openDocuments = JSON.parse(sessionStorage.getItem(OPEN_DOCUMENT_SERVICE.DOCUMENTS))
} catch (e) {
sessionStorage.removeItem(OPEN_DOCUMENT_SERVICE.DOCUMENTS)
this.openDocuments = []
}
}
}
private openDocuments: PaperlessDocument[] = []
private openDocumentsSubject: Subject<PaperlessDocument[]> = new Subject()
getOpenDocuments(): PaperlessDocument[] {
return this.openDocuments
}
getOpenDocuments(): Observable<PaperlessDocument[]> {
return this.openDocumentsSubject
getOpenDocument(id: number): PaperlessDocument {
return this.openDocuments.find(d => d.id == id)
}
openDocument(doc: PaperlessDocument) {
if (this.openDocuments.find(d => d.id == doc.id) == null) {
this.openDocuments.push(doc)
this.openDocumentsSubject.next(this.openDocuments)
this.save()
}
}
@@ -28,8 +40,12 @@ export class OpenDocumentsService {
let index = this.openDocuments.findIndex(d => d.id == doc.id)
if (index > -1) {
this.openDocuments.splice(index, 1)
this.openDocumentsSubject.next(this.openDocuments)
this.save()
}
}
save() {
sessionStorage.setItem(OPEN_DOCUMENT_SERVICE.DOCUMENTS, JSON.stringify(this.openDocuments))
}
}