Prettier code cleanup for .ts files

See #182
This commit is contained in:
Michael Shamoon
2022-03-11 10:53:32 -08:00
parent d56c9dc94b
commit bd4a705769
159 changed files with 3882 additions and 2716 deletions

View File

@@ -1,16 +1,16 @@
import { TestBed } from '@angular/core/testing';
import { TestBed } from '@angular/core/testing'
import { ConsumerStatusService } from './consumer-status.service';
import { ConsumerStatusService } from './consumer-status.service'
describe('ConsumerStatusService', () => {
let service: ConsumerStatusService;
let service: ConsumerStatusService
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(ConsumerStatusService);
});
TestBed.configureTestingModule({})
service = TestBed.inject(ConsumerStatusService)
})
it('should be created', () => {
expect(service).toBeTruthy();
});
});
expect(service).toBeTruthy()
})
})

View File

@@ -1,34 +1,33 @@
import { Injectable } from '@angular/core';
import { Subject } from 'rxjs';
import { environment } from 'src/environments/environment';
import { WebsocketConsumerStatusMessage } from '../data/websocket-consumer-status-message';
import { Injectable } from '@angular/core'
import { Subject } from 'rxjs'
import { environment } from 'src/environments/environment'
import { WebsocketConsumerStatusMessage } from '../data/websocket-consumer-status-message'
export enum FileStatusPhase {
STARTED = 0,
UPLOADING = 1,
PROCESSING = 2,
SUCCESS = 3,
FAILED = 4
FAILED = 4,
}
export const FILE_STATUS_MESSAGES = {
"document_already_exists": $localize`Document already exists.`,
"file_not_found": $localize`File not found.`,
"pre_consume_script_not_found": $localize`:Pre-Consume is a term that appears like that in the documentation as well and does not need a specific translation:Pre-consume script does not exist.`,
"pre_consume_script_error": $localize`:Pre-Consume is a term that appears like that in the documentation as well and does not need a specific translation:Error while executing pre-consume script.`,
"post_consume_script_not_found": $localize`:Post-Consume is a term that appears like that in the documentation as well and does not need a specific translation:Post-consume script does not exist.`,
"post_consume_script_error": $localize`:Post-Consume is a term that appears like that in the documentation as well and does not need a specific translation:Error while executing post-consume script.`,
"new_file": $localize`Received new file.`,
"unsupported_type": $localize`File type not supported.`,
"parsing_document": $localize`Processing document...`,
"generating_thumbnail": $localize`Generating thumbnail...`,
"parse_date": $localize`Retrieving date from document...`,
"save_document": $localize`Saving document...`,
"finished": $localize`Finished.`
document_already_exists: $localize`Document already exists.`,
file_not_found: $localize`File not found.`,
pre_consume_script_not_found: $localize`:Pre-Consume is a term that appears like that in the documentation as well and does not need a specific translation:Pre-consume script does not exist.`,
pre_consume_script_error: $localize`:Pre-Consume is a term that appears like that in the documentation as well and does not need a specific translation:Error while executing pre-consume script.`,
post_consume_script_not_found: $localize`:Post-Consume is a term that appears like that in the documentation as well and does not need a specific translation:Post-consume script does not exist.`,
post_consume_script_error: $localize`:Post-Consume is a term that appears like that in the documentation as well and does not need a specific translation:Error while executing post-consume script.`,
new_file: $localize`Received new file.`,
unsupported_type: $localize`File type not supported.`,
parsing_document: $localize`Processing document...`,
generating_thumbnail: $localize`Generating thumbnail...`,
parse_date: $localize`Retrieving date from document...`,
save_document: $localize`Saving document...`,
finished: $localize`Finished.`,
}
export class FileStatus {
filename: string
taskId: string
@@ -48,16 +47,22 @@ export class FileStatus {
case FileStatusPhase.STARTED:
return 0.0
case FileStatusPhase.UPLOADING:
return this.currentPhaseProgress / this.currentPhaseMaxProgress * 0.2
return (this.currentPhaseProgress / this.currentPhaseMaxProgress) * 0.2
case FileStatusPhase.PROCESSING:
return (this.currentPhaseProgress / this.currentPhaseMaxProgress * 0.8) + 0.2
return (
(this.currentPhaseProgress / this.currentPhaseMaxProgress) * 0.8 + 0.2
)
case FileStatusPhase.SUCCESS:
case FileStatusPhase.FAILED:
return 1.0
}
}
updateProgress(status: FileStatusPhase, currentProgress?: number, maxProgress?: number) {
updateProgress(
status: FileStatusPhase,
currentProgress?: number,
maxProgress?: number
) {
if (status >= this.phase) {
this.phase = status
if (currentProgress != null) {
@@ -68,15 +73,13 @@ export class FileStatus {
}
}
}
}
@Injectable({
providedIn: 'root'
providedIn: 'root',
})
export class ConsumerStatusService {
constructor() { }
constructor() {}
private statusWebSocket: WebSocket
@@ -87,7 +90,11 @@ export class ConsumerStatusService {
private documentConsumptionFailedSubject = new Subject<FileStatus>()
private get(taskId: string, filename?: string) {
let status = this.consumerStatus.find(e => e.taskId == taskId) || this.consumerStatus.find(e => e.filename == filename && e.taskId == null)
let status =
this.consumerStatus.find((e) => e.taskId == taskId) ||
this.consumerStatus.find(
(e) => e.filename == filename && e.taskId == null
)
let created = false
if (!status) {
status = new FileStatus()
@@ -96,7 +103,7 @@ export class ConsumerStatusService {
}
status.taskId = taskId
status.filename = filename
return {'status': status, 'created': created}
return { status: status, created: created }
}
newFileUpload(filename: string): FileStatus {
@@ -108,33 +115,48 @@ export class ConsumerStatusService {
getConsumerStatus(phase?: FileStatusPhase) {
if (phase != null) {
return this.consumerStatus.filter(s => s.phase == phase)
return this.consumerStatus.filter((s) => s.phase == phase)
} else {
return this.consumerStatus
}
}
getConsumerStatusNotCompleted() {
return this.consumerStatus.filter(s => s.phase < FileStatusPhase.SUCCESS)
return this.consumerStatus.filter((s) => s.phase < FileStatusPhase.SUCCESS)
}
getConsumerStatusCompleted() {
return this.consumerStatus.filter(s => s.phase == FileStatusPhase.FAILED || s.phase == FileStatusPhase.SUCCESS)
return this.consumerStatus.filter(
(s) =>
s.phase == FileStatusPhase.FAILED || s.phase == FileStatusPhase.SUCCESS
)
}
connect() {
this.disconnect()
this.statusWebSocket = new WebSocket(`${environment.webSocketProtocol}//${environment.webSocketHost}${environment.webSocketBaseUrl}status/`);
this.statusWebSocket = new WebSocket(
`${environment.webSocketProtocol}//${environment.webSocketHost}${environment.webSocketBaseUrl}status/`
)
this.statusWebSocket.onmessage = (ev) => {
let statusMessage: WebsocketConsumerStatusMessage = JSON.parse(ev['data'])
let statusMessageGet = this.get(statusMessage.task_id, statusMessage.filename)
let statusMessageGet = this.get(
statusMessage.task_id,
statusMessage.filename
)
let status = statusMessageGet.status
let created = statusMessageGet.created
status.updateProgress(FileStatusPhase.PROCESSING, statusMessage.current_progress, statusMessage.max_progress)
if (statusMessage.message && statusMessage.message in FILE_STATUS_MESSAGES) {
status.updateProgress(
FileStatusPhase.PROCESSING,
statusMessage.current_progress,
statusMessage.max_progress
)
if (
statusMessage.message &&
statusMessage.message in FILE_STATUS_MESSAGES
) {
status.message = FILE_STATUS_MESSAGES[statusMessage.message]
} else if (statusMessage.message) {
status.message = statusMessage.message
@@ -144,11 +166,11 @@ export class ConsumerStatusService {
if (created && statusMessage.status == 'STARTING') {
this.documentDetectedSubject.next(status)
}
if (statusMessage.status == "SUCCESS") {
if (statusMessage.status == 'SUCCESS') {
status.phase = FileStatusPhase.SUCCESS
this.documentConsumptionFinishedSubject.next(status)
}
if (statusMessage.status == "FAILED") {
if (statusMessage.status == 'FAILED') {
status.phase = FileStatusPhase.FAILED
this.documentConsumptionFailedSubject.next(status)
}
@@ -171,9 +193,11 @@ export class ConsumerStatusService {
dismiss(status: FileStatus) {
let index
if (status.taskId != null) {
index = this.consumerStatus.findIndex(s => s.taskId == status.taskId)
index = this.consumerStatus.findIndex((s) => s.taskId == status.taskId)
} else {
index = this.consumerStatus.findIndex(s => s.filename == status.filename)
index = this.consumerStatus.findIndex(
(s) => s.filename == status.filename
)
}
if (index > -1) {
@@ -182,7 +206,9 @@ export class ConsumerStatusService {
}
dismissCompleted() {
this.consumerStatus = this.consumerStatus.filter(status => status.phase != FileStatusPhase.SUCCESS)
this.consumerStatus = this.consumerStatus.filter(
(status) => status.phase != FileStatusPhase.SUCCESS
)
}
onDocumentConsumptionFinished() {
@@ -196,5 +222,4 @@ export class ConsumerStatusService {
onDocumentDetected() {
return this.documentDetectedSubject
}
}

View File

@@ -1,16 +1,16 @@
import { TestBed } from '@angular/core/testing';
import { TestBed } from '@angular/core/testing'
import { DocumentListViewService } from './document-list-view.service';
import { DocumentListViewService } from './document-list-view.service'
describe('DocumentListViewService', () => {
let service: DocumentListViewService;
let service: DocumentListViewService
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(DocumentListViewService);
});
TestBed.configureTestingModule({})
service = TestBed.inject(DocumentListViewService)
})
it('should be created', () => {
expect(service).toBeTruthy();
});
});
expect(service).toBeTruthy()
})
})

View File

@@ -1,18 +1,21 @@
import { Injectable } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { Observable } from 'rxjs';
import { cloneFilterRules, FilterRule, isFullTextFilterRule } from '../data/filter-rule';
import { PaperlessDocument } from '../data/paperless-document';
import { PaperlessSavedView } from '../data/paperless-saved-view';
import { DOCUMENT_LIST_SERVICE } from '../data/storage-keys';
import { DocumentService } from './rest/document.service';
import { SettingsService, SETTINGS_KEYS } from './settings.service';
import { Injectable } from '@angular/core'
import { ActivatedRoute, Router } from '@angular/router'
import { Observable } from 'rxjs'
import {
cloneFilterRules,
FilterRule,
isFullTextFilterRule,
} from '../data/filter-rule'
import { PaperlessDocument } from '../data/paperless-document'
import { PaperlessSavedView } from '../data/paperless-saved-view'
import { DOCUMENT_LIST_SERVICE } from '../data/storage-keys'
import { DocumentService } from './rest/document.service'
import { SettingsService, SETTINGS_KEYS } from './settings.service'
/**
* Captures the current state of the list view.
*/
interface ListViewState {
/**
* Title of the document list view. Either "Documents" (localized) or the name of a saved view.
*/
@@ -49,7 +52,6 @@ interface ListViewState {
* Contains the IDs of all selected documents.
*/
selected?: Set<number>
}
/**
@@ -59,10 +61,9 @@ interface ListViewState {
* and saved views on request. See below.
*/
@Injectable({
providedIn: 'root'
providedIn: 'root',
})
export class DocumentListViewService {
isReloading: boolean = false
error: string = null
@@ -89,16 +90,19 @@ export class DocumentListViewService {
documents: [],
currentPage: 1,
collectionSize: null,
sortField: "created",
sortField: 'created',
sortReverse: true,
filterRules: [],
selected: new Set<number>()
selected: new Set<number>(),
}
}
private get activeListViewState() {
if (!this.listViewStates.has(this._activeSavedViewId)) {
this.listViewStates.set(this._activeSavedViewId, this.defaultListViewState())
this.listViewStates.set(
this._activeSavedViewId,
this.defaultListViewState()
)
}
return this.listViewStates.get(this._activeSavedViewId)
}
@@ -131,13 +135,16 @@ export class DocumentListViewService {
this.error = null
let activeListViewState = this.activeListViewState
this.documentService.listFiltered(
activeListViewState.currentPage,
this.currentPageSize,
activeListViewState.sortField,
activeListViewState.sortReverse,
activeListViewState.filterRules).subscribe(
result => {
this.documentService
.listFiltered(
activeListViewState.currentPage,
this.currentPageSize,
activeListViewState.sortField,
activeListViewState.sortReverse,
activeListViewState.filterRules
)
.subscribe(
(result) => {
this.isReloading = false
activeListViewState.collectionSize = result.count
activeListViewState.documents = result.results
@@ -146,7 +153,7 @@ export class DocumentListViewService {
}
this.rangeSelectionAnchorIndex = this.lastRangeSelectionToIndex = null
},
error => {
(error) => {
this.isReloading = false
if (activeListViewState.currentPage != 1 && error.status == 404) {
// this happens when applying a filter: the current page might not be available anymore due to the reduced result set.
@@ -155,12 +162,16 @@ export class DocumentListViewService {
} else {
this.error = error.error
}
})
}
)
}
set filterRules(filterRules: FilterRule[]) {
if (!isFullTextFilterRule(filterRules) && this.activeListViewState.sortField == "score") {
this.activeListViewState.sortField = "created"
if (
!isFullTextFilterRule(filterRules) &&
this.activeListViewState.sortField == 'score'
) {
this.activeListViewState.sortField = 'created'
}
this.activeListViewState.filterRules = filterRules
this.reload()
@@ -228,9 +239,12 @@ export class DocumentListViewService {
currentPage: this.activeListViewState.currentPage,
filterRules: this.activeListViewState.filterRules,
sortField: this.activeListViewState.sortField,
sortReverse: this.activeListViewState.sortReverse
sortReverse: this.activeListViewState.sortReverse,
}
localStorage.setItem(DOCUMENT_LIST_SERVICE.CURRENT_VIEW_CONFIG, JSON.stringify(savedState))
localStorage.setItem(
DOCUMENT_LIST_SERVICE.CURRENT_VIEW_CONFIG,
JSON.stringify(savedState)
)
}
}
@@ -239,15 +253,15 @@ export class DocumentListViewService {
this.activeListViewState.filterRules = filterRules
this.activeListViewState.currentPage = 1
if (isFullTextFilterRule(filterRules)) {
this.activeListViewState.sortField = "score"
this.activeListViewState.sortField = 'score'
this.activeListViewState.sortReverse = false
}
this.reduceSelectionToFilter()
this.saveDocumentListView()
if (this.router.url == "/documents") {
if (this.router.url == '/documents') {
this.reload()
} else {
this.router.navigate(["documents"])
this.router.navigate(['documents'])
}
}
@@ -257,8 +271,12 @@ export class DocumentListViewService {
hasNext(doc: number) {
if (this.documents) {
let index = this.documents.findIndex(d => d.id == doc)
return index != -1 && (this.currentPage < this.getLastPage() || (index + 1) < this.documents.length)
let index = this.documents.findIndex((d) => d.id == doc)
return (
index != -1 &&
(this.currentPage < this.getLastPage() ||
index + 1 < this.documents.length)
)
}
}
@@ -270,13 +288,12 @@ export class DocumentListViewService {
}
getNext(currentDocId: number): Observable<number> {
return new Observable(nextDocId => {
return new Observable((nextDocId) => {
if (this.documents != null) {
let index = this.documents.findIndex((d) => d.id == currentDocId)
let index = this.documents.findIndex(d => d.id == currentDocId)
if (index != -1 && (index + 1) < this.documents.length) {
nextDocId.next(this.documents[index+1].id)
if (index != -1 && index + 1 < this.documents.length) {
nextDocId.next(this.documents[index + 1].id)
nextDocId.complete()
} else if (index != -1 && this.currentPage < this.getLastPage()) {
this.currentPage += 1
@@ -331,23 +348,27 @@ export class DocumentListViewService {
reduceSelectionToFilter() {
if (this.selected.size > 0) {
this.documentService.listAllFilteredIds(this.filterRules).subscribe(ids => {
for (let id of this.selected) {
if (!ids.includes(id)) {
this.selected.delete(id)
this.documentService
.listAllFilteredIds(this.filterRules)
.subscribe((ids) => {
for (let id of this.selected) {
if (!ids.includes(id)) {
this.selected.delete(id)
}
}
}
})
})
}
}
selectAll() {
this.documentService.listAllFilteredIds(this.filterRules).subscribe(ids => ids.forEach(id => this.selected.add(id)))
this.documentService
.listAllFilteredIds(this.filterRules)
.subscribe((ids) => ids.forEach((id) => this.selected.add(id)))
}
selectPage() {
this.selected.clear()
this.documents.forEach(doc => {
this.documents.forEach((doc) => {
this.selected.add(doc.id)
})
}
@@ -366,36 +387,58 @@ export class DocumentListViewService {
selectRangeTo(d: PaperlessDocument) {
if (this.rangeSelectionAnchorIndex !== null) {
const documentToIndex = this.documentIndexInCurrentView(d.id)
const fromIndex = Math.min(this.rangeSelectionAnchorIndex, documentToIndex)
const fromIndex = Math.min(
this.rangeSelectionAnchorIndex,
documentToIndex
)
const toIndex = Math.max(this.rangeSelectionAnchorIndex, documentToIndex)
if (this.lastRangeSelectionToIndex !== null) {
// revert the old selection
this.documents.slice(Math.min(this.rangeSelectionAnchorIndex, this.lastRangeSelectionToIndex), Math.max(this.rangeSelectionAnchorIndex, this.lastRangeSelectionToIndex) + 1).forEach(d => {
this.selected.delete(d.id)
})
this.documents
.slice(
Math.min(
this.rangeSelectionAnchorIndex,
this.lastRangeSelectionToIndex
),
Math.max(
this.rangeSelectionAnchorIndex,
this.lastRangeSelectionToIndex
) + 1
)
.forEach((d) => {
this.selected.delete(d.id)
})
}
this.documents.slice(fromIndex, toIndex + 1).forEach(d => {
this.documents.slice(fromIndex, toIndex + 1).forEach((d) => {
this.selected.add(d.id)
})
this.lastRangeSelectionToIndex = documentToIndex
} else { // e.g. shift key but was first click
} else {
// e.g. shift key but was first click
this.toggleSelected(d)
}
}
documentIndexInCurrentView(documentID: number): number {
return this.documents.map(d => d.id).indexOf(documentID)
return this.documents.map((d) => d.id).indexOf(documentID)
}
constructor(private documentService: DocumentService, private settings: SettingsService, private router: Router, private route: ActivatedRoute) {
let documentListViewConfigJson = localStorage.getItem(DOCUMENT_LIST_SERVICE.CURRENT_VIEW_CONFIG)
constructor(
private documentService: DocumentService,
private settings: SettingsService,
private router: Router,
private route: ActivatedRoute
) {
let documentListViewConfigJson = localStorage.getItem(
DOCUMENT_LIST_SERVICE.CURRENT_VIEW_CONFIG
)
if (documentListViewConfigJson) {
try {
let savedState: ListViewState = JSON.parse(documentListViewConfigJson)
// Remove null elements from the restored state
Object.keys(savedState).forEach(k => {
Object.keys(savedState).forEach((k) => {
if (savedState[k] == null) {
delete savedState[k]
}

View File

@@ -1,16 +1,16 @@
import { TestBed } from '@angular/core/testing';
import { TestBed } from '@angular/core/testing'
import { OpenDocumentsService } from './open-documents.service';
import { OpenDocumentsService } from './open-documents.service'
describe('OpenDocumentsService', () => {
let service: OpenDocumentsService;
let service: OpenDocumentsService
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(OpenDocumentsService);
});
TestBed.configureTestingModule({})
service = TestBed.inject(OpenDocumentsService)
})
it('should be created', () => {
expect(service).toBeTruthy();
});
});
expect(service).toBeTruthy()
})
})

View File

@@ -1,23 +1,27 @@
import { Injectable } from '@angular/core';
import { PaperlessDocument } from '../data/paperless-document';
import { OPEN_DOCUMENT_SERVICE } from '../data/storage-keys';
import { DocumentService } from './rest/document.service';
import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
import { ConfirmDialogComponent } from 'src/app/components/common/confirm-dialog/confirm-dialog.component';
import { Observable, Subject, of } from 'rxjs';
import { first } from 'rxjs/operators';
import { Injectable } from '@angular/core'
import { PaperlessDocument } from '../data/paperless-document'
import { OPEN_DOCUMENT_SERVICE } from '../data/storage-keys'
import { DocumentService } from './rest/document.service'
import { NgbModal } from '@ng-bootstrap/ng-bootstrap'
import { ConfirmDialogComponent } from 'src/app/components/common/confirm-dialog/confirm-dialog.component'
import { Observable, Subject, of } from 'rxjs'
import { first } from 'rxjs/operators'
@Injectable({
providedIn: 'root'
providedIn: 'root',
})
export class OpenDocumentsService {
private MAX_OPEN_DOCUMENTS = 5
constructor(private documentService: DocumentService, private modalService: NgbModal) {
constructor(
private documentService: DocumentService,
private modalService: NgbModal
) {
if (sessionStorage.getItem(OPEN_DOCUMENT_SERVICE.DOCUMENTS)) {
try {
this.openDocuments = JSON.parse(sessionStorage.getItem(OPEN_DOCUMENT_SERVICE.DOCUMENTS))
this.openDocuments = JSON.parse(
sessionStorage.getItem(OPEN_DOCUMENT_SERVICE.DOCUMENTS)
)
} catch (e) {
sessionStorage.removeItem(OPEN_DOCUMENT_SERVICE.DOCUMENTS)
this.openDocuments = []
@@ -29,14 +33,17 @@ export class OpenDocumentsService {
private dirtyDocuments: Set<number> = new Set<number>()
refreshDocument(id: number) {
let index = this.openDocuments.findIndex(doc => doc.id == id)
let index = this.openDocuments.findIndex((doc) => doc.id == id)
if (index > -1) {
this.documentService.get(id).subscribe(doc => {
this.openDocuments[index] = doc
}, error => {
this.openDocuments.splice(index, 1)
this.save()
})
this.documentService.get(id).subscribe(
(doc) => {
this.openDocuments[index] = doc
},
(error) => {
this.openDocuments.splice(index, 1)
this.save()
}
)
}
}
@@ -45,11 +52,11 @@ export class OpenDocumentsService {
}
getOpenDocument(id: number): PaperlessDocument {
return this.openDocuments.find(d => d.id == id)
return this.openDocuments.find((d) => d.id == id)
}
openDocument(doc: PaperlessDocument) {
if (this.openDocuments.find(d => d.id == doc.id) == null) {
if (this.openDocuments.find((d) => d.id == doc.id) == null) {
this.openDocuments.unshift(doc)
if (this.openDocuments.length > this.MAX_OPEN_DOCUMENTS) {
this.openDocuments.pop()
@@ -64,18 +71,20 @@ export class OpenDocumentsService {
}
closeDocument(doc: PaperlessDocument): Observable<boolean> {
let index = this.openDocuments.findIndex(d => d.id == doc.id)
if (index == -1) return of(true);
let index = this.openDocuments.findIndex((d) => d.id == doc.id)
if (index == -1) return of(true)
if (!this.dirtyDocuments.has(doc.id)) {
this.openDocuments.splice(index, 1)
this.save()
return of(true)
} else {
let modal = this.modalService.open(ConfirmDialogComponent, {backdrop: 'static'})
let modal = this.modalService.open(ConfirmDialogComponent, {
backdrop: 'static',
})
modal.componentInstance.title = $localize`Unsaved Changes`
modal.componentInstance.messageBold = $localize`You have unsaved changes.`
modal.componentInstance.message = $localize`Are you sure you want to close this document?`
modal.componentInstance.btnClass = "btn-warning"
modal.componentInstance.btnClass = 'btn-warning'
modal.componentInstance.btnCaption = $localize`Close document`
modal.componentInstance.confirmClicked.pipe(first()).subscribe(() => {
modal.componentInstance.buttonsEnabled = false
@@ -92,11 +101,13 @@ export class OpenDocumentsService {
closeAll(): Observable<boolean> {
if (this.dirtyDocuments.size) {
let modal = this.modalService.open(ConfirmDialogComponent, {backdrop: 'static'})
let modal = this.modalService.open(ConfirmDialogComponent, {
backdrop: 'static',
})
modal.componentInstance.title = $localize`Unsaved Changes`
modal.componentInstance.messageBold = $localize`You have unsaved changes.`
modal.componentInstance.message = $localize`Are you sure you want to close all documents?`
modal.componentInstance.btnClass = "btn-warning"
modal.componentInstance.btnClass = 'btn-warning'
modal.componentInstance.btnCaption = $localize`Close documents`
modal.componentInstance.confirmClicked.pipe(first()).subscribe(() => {
modal.componentInstance.buttonsEnabled = false
@@ -117,7 +128,9 @@ export class OpenDocumentsService {
}
save() {
sessionStorage.setItem(OPEN_DOCUMENT_SERVICE.DOCUMENTS, JSON.stringify(this.openDocuments))
sessionStorage.setItem(
OPEN_DOCUMENT_SERVICE.DOCUMENTS,
JSON.stringify(this.openDocuments)
)
}
}

View File

@@ -1,14 +1,20 @@
import { ObjectWithId } from 'src/app/data/object-with-id'
import { AbstractPaperlessService } from './abstract-paperless-service'
export abstract class AbstractNameFilterService<T extends ObjectWithId> extends AbstractPaperlessService<T> {
listFiltered(page?: number, pageSize?: number, sortField?: string, sortReverse?: boolean, nameFilter?: string) {
export abstract class AbstractNameFilterService<
T extends ObjectWithId
> extends AbstractPaperlessService<T> {
listFiltered(
page?: number,
pageSize?: number,
sortField?: string,
sortReverse?: boolean,
nameFilter?: string
) {
let params = {}
if (nameFilter) {
params = {'name__icontains': nameFilter}
params = { name__icontains: nameFilter }
}
return this.list(page, pageSize, sortField, sortReverse, params)
}
}

View File

@@ -1,7 +1,7 @@
import { AbstractPaperlessService } from './abstract-paperless-service';
import { AbstractPaperlessService } from './abstract-paperless-service'
describe('AbstractPaperlessService', () => {
it('should create an instance', () => {
expect(new AbstractPaperlessService()).toBeTruthy();
});
});
expect(new AbstractPaperlessService()).toBeTruthy()
})
})

View File

@@ -6,10 +6,9 @@ import { Results } from 'src/app/data/results'
import { environment } from 'src/environments/environment'
export abstract class AbstractPaperlessService<T extends ObjectWithId> {
protected baseUrl: string = environment.apiBaseUrl
constructor(protected http: HttpClient, private resourceName: string) { }
constructor(protected http: HttpClient, private resourceName: string) {}
protected getResourceUrl(id?: number, action?: string): string {
let url = `${this.baseUrl}${this.resourceName}/`
@@ -30,7 +29,13 @@ export abstract class AbstractPaperlessService<T extends ObjectWithId> {
}
}
list(page?: number, pageSize?: number, sortField?: string, sortReverse?: boolean, extraParams?): Observable<Results<T>> {
list(
page?: number,
pageSize?: number,
sortField?: string,
sortReverse?: boolean,
extraParams?
): Observable<Results<T>> {
let httpParams = new HttpParams()
if (page) {
httpParams = httpParams.set('page', page.toString())
@@ -47,30 +52,39 @@ export abstract class AbstractPaperlessService<T extends ObjectWithId> {
httpParams = httpParams.set(extraParamKey, extraParams[extraParamKey])
}
}
return this.http.get<Results<T>>(this.getResourceUrl(), {params: httpParams})
return this.http.get<Results<T>>(this.getResourceUrl(), {
params: httpParams,
})
}
private _listAll: Observable<Results<T>>
listAll(sortField?: string, sortReverse?: boolean, extraParams?): Observable<Results<T>> {
listAll(
sortField?: string,
sortReverse?: boolean,
extraParams?
): Observable<Results<T>> {
if (!this._listAll) {
this._listAll = this.list(1, 100000, sortField, sortReverse, extraParams).pipe(
publishReplay(1),
refCount()
)
this._listAll = this.list(
1,
100000,
sortField,
sortReverse,
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))
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)))
map((list) => ids.map((id) => list.results.find((o) => o.id == id)))
)
}
@@ -101,5 +115,4 @@ export abstract class AbstractPaperlessService<T extends ObjectWithId> {
this.clearCache()
return this.http.patch<T>(this.getResourceUrl(o.id), o)
}
}

View File

@@ -1,16 +1,16 @@
import { TestBed } from '@angular/core/testing';
import { TestBed } from '@angular/core/testing'
import { CorrespondentService } from './correspondent.service';
import { CorrespondentService } from './correspondent.service'
describe('CorrespondentService', () => {
let service: CorrespondentService;
let service: CorrespondentService
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(CorrespondentService);
});
TestBed.configureTestingModule({})
service = TestBed.inject(CorrespondentService)
})
it('should be created', () => {
expect(service).toBeTruthy();
});
});
expect(service).toBeTruthy()
})
})

View File

@@ -1,15 +1,13 @@
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { PaperlessCorrespondent } from 'src/app/data/paperless-correspondent';
import { AbstractNameFilterService } from './abstract-name-filter-service';
import { HttpClient } from '@angular/common/http'
import { Injectable } from '@angular/core'
import { PaperlessCorrespondent } from 'src/app/data/paperless-correspondent'
import { AbstractNameFilterService } from './abstract-name-filter-service'
@Injectable({
providedIn: 'root'
providedIn: 'root',
})
export class CorrespondentService extends AbstractNameFilterService<PaperlessCorrespondent> {
constructor(http: HttpClient) {
super(http, 'correspondents')
}
}

View File

@@ -1,16 +1,16 @@
import { TestBed } from '@angular/core/testing';
import { TestBed } from '@angular/core/testing'
import { DocumentTypeService } from './document-type.service';
import { DocumentTypeService } from './document-type.service'
describe('DocumentTypeService', () => {
let service: DocumentTypeService;
let service: DocumentTypeService
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(DocumentTypeService);
});
TestBed.configureTestingModule({})
service = TestBed.inject(DocumentTypeService)
})
it('should be created', () => {
expect(service).toBeTruthy();
});
});
expect(service).toBeTruthy()
})
})

View File

@@ -1,13 +1,12 @@
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { PaperlessDocumentType } from 'src/app/data/paperless-document-type';
import { AbstractNameFilterService } from './abstract-name-filter-service';
import { HttpClient } from '@angular/common/http'
import { Injectable } from '@angular/core'
import { PaperlessDocumentType } from 'src/app/data/paperless-document-type'
import { AbstractNameFilterService } from './abstract-name-filter-service'
@Injectable({
providedIn: 'root'
providedIn: 'root',
})
export class DocumentTypeService extends AbstractNameFilterService<PaperlessDocumentType> {
constructor(http: HttpClient) {
super(http, 'document_types')
}

View File

@@ -1,16 +1,16 @@
import { TestBed } from '@angular/core/testing';
import { TestBed } from '@angular/core/testing'
import { DocumentService } from './document.service';
import { DocumentService } from './document.service'
describe('DocumentService', () => {
let service: DocumentService;
let service: DocumentService
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(DocumentService);
});
TestBed.configureTestingModule({})
service = TestBed.inject(DocumentService)
})
it('should be created', () => {
expect(service).toBeTruthy();
});
});
expect(service).toBeTruthy()
})
})

View File

@@ -1,31 +1,34 @@
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, HttpParams } 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';
import { FILTER_RULE_TYPES } from 'src/app/data/filter-rule-type';
import { PaperlessDocumentSuggestions } from 'src/app/data/paperless-document-suggestions';
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, HttpParams } 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'
import { FILTER_RULE_TYPES } from 'src/app/data/filter-rule-type'
import { PaperlessDocumentSuggestions } from 'src/app/data/paperless-document-suggestions'
export const DOCUMENT_SORT_FIELDS = [
{ field: 'archive_serial_number', name: $localize`ASN` },
{ field: "correspondent__name", name: $localize`Correspondent` },
{ field: 'correspondent__name', name: $localize`Correspondent` },
{ field: 'title', name: $localize`Title` },
{ field: "document_type__name", name: $localize`Document type` },
{ field: 'document_type__name', name: $localize`Document type` },
{ field: 'created', name: $localize`Created` },
{ field: 'added', name: $localize`Added` },
{ field: 'modified', name: $localize`Modified` }
{ field: 'modified', name: $localize`Modified` },
]
export const DOCUMENT_SORT_FIELDS_FULLTEXT = [
...DOCUMENT_SORT_FIELDS,
{ field: 'score', name: $localize`:Score is a value returned by the full text search engine and specifies how well a result matches the given query:Search score` }
{
field: 'score',
name: $localize`:Score is a value returned by the full text search engine and specifies how well a result matches the given query:Search score`,
},
]
export interface SelectionDataItem {
@@ -40,13 +43,17 @@ export interface SelectionData {
}
@Injectable({
providedIn: 'root'
providedIn: 'root',
})
export class DocumentService extends AbstractPaperlessService<PaperlessDocument> {
private _searchQuery: string
constructor(http: HttpClient, private correspondentService: CorrespondentService, private documentTypeService: DocumentTypeService, private tagService: TagService) {
constructor(
http: HttpClient,
private correspondentService: CorrespondentService,
private documentTypeService: DocumentTypeService,
private tagService: TagService
) {
super(http, 'documents')
}
@@ -54,9 +61,11 @@ export class DocumentService extends AbstractPaperlessService<PaperlessDocument>
if (filterRules) {
let params = {}
for (let rule of filterRules) {
let ruleType = FILTER_RULE_TYPES.find(t => t.id == rule.rule_type)
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
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 {
@@ -71,7 +80,9 @@ export class DocumentService extends AbstractPaperlessService<PaperlessDocument>
addObservablesToDocument(doc: PaperlessDocument) {
if (doc.correspondent) {
doc.correspondent$ = this.correspondentService.getCached(doc.correspondent)
doc.correspondent$ = this.correspondentService.getCached(
doc.correspondent
)
}
if (doc.document_type) {
doc.document_type$ = this.documentTypeService.getCached(doc.document_type)
@@ -82,26 +93,39 @@ export class DocumentService extends AbstractPaperlessService<PaperlessDocument>
return doc
}
listFiltered(page?: number, pageSize?: number, sortField?: string, sortReverse?: boolean, filterRules?: FilterRule[], extraParams = {}): Observable<Results<PaperlessDocument>> {
return this.list(page, pageSize, sortField, sortReverse, Object.assign(extraParams, this.filterRulesToQueryParams(filterRules))).pipe(
map(results => {
results.results.forEach(doc => this.addObservablesToDocument(doc))
listFiltered(
page?: number,
pageSize?: number,
sortField?: string,
sortReverse?: boolean,
filterRules?: FilterRule[],
extraParams = {}
): Observable<Results<PaperlessDocument>> {
return this.list(
page,
pageSize,
sortField,
sortReverse,
Object.assign(extraParams, this.filterRulesToQueryParams(filterRules))
).pipe(
map((results) => {
results.results.forEach((doc) => this.addObservablesToDocument(doc))
return results
})
)
}
listAllFilteredIds(filterRules?: FilterRule[]): Observable<number[]> {
return this.listFiltered(1, 100000, null, null, filterRules, {"fields": "id"}).pipe(
map(response => response.results.map(doc => doc.id))
)
return this.listFiltered(1, 100000, null, null, filterRules, {
fields: 'id',
}).pipe(map((response) => response.results.map((doc) => doc.id)))
}
getPreviewUrl(id: number, original: boolean = false): string {
let url = this.getResourceUrl(id, 'preview')
if (this._searchQuery) url += `#search="${this._searchQuery}"`
if (original) {
url += "?original=true"
url += '?original=true'
}
return url
}
@@ -113,41 +137,55 @@ export class DocumentService extends AbstractPaperlessService<PaperlessDocument>
getDownloadUrl(id: number, original: boolean = false): string {
let url = this.getResourceUrl(id, 'download')
if (original) {
url += "?original=true"
url += '?original=true'
}
return url
}
uploadDocument(formData) {
return this.http.post(this.getResourceUrl(null, 'post_document'), formData, {reportProgress: true, observe: "events"})
return this.http.post(
this.getResourceUrl(null, 'post_document'),
formData,
{ reportProgress: true, observe: 'events' }
)
}
getMetadata(id: number): Observable<PaperlessDocumentMetadata> {
return this.http.get<PaperlessDocumentMetadata>(this.getResourceUrl(id, 'metadata'))
return this.http.get<PaperlessDocumentMetadata>(
this.getResourceUrl(id, 'metadata')
)
}
bulkEdit(ids: number[], method: string, args: any) {
return this.http.post(this.getResourceUrl(null, 'bulk_edit'), {
'documents': ids,
'method': method,
'parameters': args
documents: ids,
method: method,
parameters: args,
})
}
getSelectionData(ids: number[]): Observable<SelectionData> {
return this.http.post<SelectionData>(this.getResourceUrl(null, 'selection_data'), {"documents": ids})
return this.http.post<SelectionData>(
this.getResourceUrl(null, 'selection_data'),
{ documents: ids }
)
}
getSuggestions(id: number): Observable<PaperlessDocumentSuggestions> {
return this.http.get<PaperlessDocumentSuggestions>(this.getResourceUrl(id, 'suggestions'))
return this.http.get<PaperlessDocumentSuggestions>(
this.getResourceUrl(id, 'suggestions')
)
}
bulkDownload(ids: number[], content="both") {
return this.http.post(this.getResourceUrl(null, 'bulk_download'), {"documents": ids, "content": content}, { responseType: 'blob' })
bulkDownload(ids: number[], content = 'both') {
return this.http.post(
this.getResourceUrl(null, 'bulk_download'),
{ documents: ids, content: content },
{ responseType: 'blob' }
)
}
public set searchQuery(query: string) {
this._searchQuery = query
}
}

View File

@@ -1,16 +1,16 @@
import { TestBed } from '@angular/core/testing';
import { TestBed } from '@angular/core/testing'
import { LogService } from './log.service';
import { LogService } from './log.service'
describe('LogService', () => {
let service: LogService;
let service: LogService
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(LogService);
});
TestBed.configureTestingModule({})
service = TestBed.inject(LogService)
})
it('should be created', () => {
expect(service).toBeTruthy();
});
});
expect(service).toBeTruthy()
})
})

View File

@@ -1,15 +1,13 @@
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { environment } from 'src/environments/environment';
import { HttpClient } from '@angular/common/http'
import { Injectable } from '@angular/core'
import { Observable } from 'rxjs'
import { environment } from 'src/environments/environment'
@Injectable({
providedIn: 'root'
providedIn: 'root',
})
export class LogService {
constructor(private http: HttpClient) {
}
constructor(private http: HttpClient) {}
list(): Observable<string[]> {
return this.http.get<string[]>(`${environment.apiBaseUrl}logs/`)

View File

@@ -1,16 +1,16 @@
import { TestBed } from '@angular/core/testing';
import { TestBed } from '@angular/core/testing'
import { SavedViewService } from './saved-view.service';
import { SavedViewService } from './saved-view.service'
describe('SavedViewService', () => {
let service: SavedViewService;
let service: SavedViewService
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(SavedViewService);
});
TestBed.configureTestingModule({})
service = TestBed.inject(SavedViewService)
})
it('should be created', () => {
expect(service).toBeTruthy();
});
});
expect(service).toBeTruthy()
})
})

View File

@@ -1,22 +1,21 @@
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { combineLatest, Observable } from 'rxjs';
import { tap } from 'rxjs/operators';
import { PaperlessSavedView } from 'src/app/data/paperless-saved-view';
import { AbstractPaperlessService } from './abstract-paperless-service';
import { HttpClient } from '@angular/common/http'
import { Injectable } from '@angular/core'
import { combineLatest, Observable } from 'rxjs'
import { tap } from 'rxjs/operators'
import { PaperlessSavedView } from 'src/app/data/paperless-saved-view'
import { AbstractPaperlessService } from './abstract-paperless-service'
@Injectable({
providedIn: 'root'
providedIn: 'root',
})
export class SavedViewService extends AbstractPaperlessService<PaperlessSavedView> {
constructor(http: HttpClient) {
super(http, 'saved_views')
this.reload()
}
private reload() {
this.listAll().subscribe(r => this.savedViews = r.results)
this.listAll().subscribe((r) => (this.savedViews = r.results))
}
private savedViews: PaperlessSavedView[] = []
@@ -26,34 +25,28 @@ export class SavedViewService extends AbstractPaperlessService<PaperlessSavedVie
}
get sidebarViews() {
return this.savedViews.filter(v => v.show_in_sidebar)
return this.savedViews.filter((v) => v.show_in_sidebar)
}
get dashboardViews() {
return this.savedViews.filter(v => v.show_on_dashboard)
return this.savedViews.filter((v) => v.show_on_dashboard)
}
create(o: PaperlessSavedView) {
return super.create(o).pipe(
tap(() => this.reload())
)
return super.create(o).pipe(tap(() => this.reload()))
}
update(o: PaperlessSavedView) {
return super.update(o).pipe(
tap(() => this.reload())
)
return super.update(o).pipe(tap(() => this.reload()))
}
patchMany(objects: PaperlessSavedView[]): Observable<PaperlessSavedView[]> {
return combineLatest(objects.map(o => super.patch(o))).pipe(
return combineLatest(objects.map((o) => super.patch(o))).pipe(
tap(() => this.reload())
)
}
delete(o: PaperlessSavedView) {
return super.delete(o).pipe(
tap(() => this.reload())
)
return super.delete(o).pipe(tap(() => this.reload()))
}
}

View File

@@ -1,16 +1,16 @@
import { TestBed } from '@angular/core/testing';
import { TestBed } from '@angular/core/testing'
import { SearchService } from './search.service';
import { SearchService } from './search.service'
describe('SearchService', () => {
let service: SearchService;
let service: SearchService
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(SearchService);
});
TestBed.configureTestingModule({})
service = TestBed.inject(SearchService)
})
it('should be created', () => {
expect(service).toBeTruthy();
});
});
expect(service).toBeTruthy()
})
})

View File

@@ -1,19 +1,20 @@
import { HttpClient, HttpParams } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { environment } from 'src/environments/environment';
import { DocumentService } from './document.service';
import { HttpClient, HttpParams } from '@angular/common/http'
import { Injectable } from '@angular/core'
import { Observable } from 'rxjs'
import { map } from 'rxjs/operators'
import { environment } from 'src/environments/environment'
import { DocumentService } from './document.service'
@Injectable({
providedIn: 'root'
providedIn: 'root',
})
export class SearchService {
constructor(private http: HttpClient) { }
constructor(private http: HttpClient) {}
autocomplete(term: string): Observable<string[]> {
return this.http.get<string[]>(`${environment.apiBaseUrl}search/autocomplete/`, {params: new HttpParams().set('term', term)})
return this.http.get<string[]>(
`${environment.apiBaseUrl}search/autocomplete/`,
{ params: new HttpParams().set('term', term) }
)
}
}

View File

@@ -1,16 +1,16 @@
import { TestBed } from '@angular/core/testing';
import { TestBed } from '@angular/core/testing'
import { TagService } from './tag.service';
import { TagService } from './tag.service'
describe('TagService', () => {
let service: TagService;
let service: TagService
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(TagService);
});
TestBed.configureTestingModule({})
service = TestBed.inject(TagService)
})
it('should be created', () => {
expect(service).toBeTruthy();
});
});
expect(service).toBeTruthy()
})
})

View File

@@ -1,13 +1,12 @@
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { PaperlessTag } from 'src/app/data/paperless-tag';
import { AbstractNameFilterService } from './abstract-name-filter-service';
import { HttpClient } from '@angular/common/http'
import { Injectable } from '@angular/core'
import { PaperlessTag } from 'src/app/data/paperless-tag'
import { AbstractNameFilterService } from './abstract-name-filter-service'
@Injectable({
providedIn: 'root'
providedIn: 'root',
})
export class TagService extends AbstractNameFilterService<PaperlessTag> {
constructor(http: HttpClient) {
super(http, 'tags')
}

View File

@@ -1,16 +1,16 @@
import { TestBed } from '@angular/core/testing';
import { TestBed } from '@angular/core/testing'
import { SettingsService } from './settings.service';
import { SettingsService } from './settings.service'
describe('SettingsService', () => {
let service: SettingsService;
let service: SettingsService
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(SettingsService);
});
TestBed.configureTestingModule({})
service = TestBed.inject(SettingsService)
})
it('should be created', () => {
expect(service).toBeTruthy();
});
});
expect(service).toBeTruthy()
})
})

View File

@@ -1,8 +1,15 @@
import { DOCUMENT } from '@angular/common';
import { Inject, Injectable, LOCALE_ID, Renderer2, RendererFactory2, RendererStyleFlags2 } from '@angular/core';
import { Meta } from '@angular/platform-browser';
import { CookieService } from 'ngx-cookie-service';
import { hexToHsl } from 'src/app/utils/color';
import { DOCUMENT } from '@angular/common'
import {
Inject,
Injectable,
LOCALE_ID,
Renderer2,
RendererFactory2,
RendererStyleFlags2,
} from '@angular/core'
import { Meta } from '@angular/platform-browser'
import { CookieService } from 'ngx-cookie-service'
import { hexToHsl } from 'src/app/utils/color'
export interface PaperlessSettings {
key: string
@@ -22,7 +29,8 @@ export interface LanguageOption {
}
export const SETTINGS_KEYS = {
BULK_EDIT_CONFIRMATION_DIALOGS: 'general-settings:bulk-edit:confirmation-dialogs',
BULK_EDIT_CONFIRMATION_DIALOGS:
'general-settings:bulk-edit:confirmation-dialogs',
BULK_EDIT_APPLY_ON_CLOSE: 'general-settings:bulk-edit:apply-on-close',
DOCUMENT_LIST_SIZE: 'general-settings:documentListSize',
DARK_MODE_USE_SYSTEM: 'general-settings:dark-mode:use-system',
@@ -32,35 +40,66 @@ export const SETTINGS_KEYS = {
USE_NATIVE_PDF_VIEWER: 'general-settings:document-details:native-pdf-viewer',
DATE_LOCALE: 'general-settings:date-display:date-locale',
DATE_FORMAT: 'general-settings:date-display:date-format',
NOTIFICATIONS_CONSUMER_NEW_DOCUMENT: 'general-settings:notifications:consumer-new-documents',
NOTIFICATIONS_CONSUMER_SUCCESS: 'general-settings:notifications:consumer-success',
NOTIFICATIONS_CONSUMER_FAILED: 'general-settings:notifications:consumer-failed',
NOTIFICATIONS_CONSUMER_SUPPRESS_ON_DASHBOARD: 'general-settings:notifications:consumer-suppress-on-dashboard',
NOTIFICATIONS_CONSUMER_NEW_DOCUMENT:
'general-settings:notifications:consumer-new-documents',
NOTIFICATIONS_CONSUMER_SUCCESS:
'general-settings:notifications:consumer-success',
NOTIFICATIONS_CONSUMER_FAILED:
'general-settings:notifications:consumer-failed',
NOTIFICATIONS_CONSUMER_SUPPRESS_ON_DASHBOARD:
'general-settings:notifications:consumer-suppress-on-dashboard',
}
const SETTINGS: PaperlessSettings[] = [
{key: SETTINGS_KEYS.BULK_EDIT_CONFIRMATION_DIALOGS, type: "boolean", default: true},
{key: SETTINGS_KEYS.BULK_EDIT_APPLY_ON_CLOSE, type: "boolean", default: false},
{key: SETTINGS_KEYS.DOCUMENT_LIST_SIZE, type: "number", default: 50},
{key: SETTINGS_KEYS.DARK_MODE_USE_SYSTEM, type: "boolean", default: true},
{key: SETTINGS_KEYS.DARK_MODE_ENABLED, type: "boolean", default: false},
{key: SETTINGS_KEYS.DARK_MODE_THUMB_INVERTED, type: "boolean", default: true},
{key: SETTINGS_KEYS.THEME_COLOR, type: "string", default: ""},
{key: SETTINGS_KEYS.USE_NATIVE_PDF_VIEWER, type: "boolean", default: false},
{key: SETTINGS_KEYS.DATE_LOCALE, type: "string", default: ""},
{key: SETTINGS_KEYS.DATE_FORMAT, type: "string", default: "mediumDate"},
{key: SETTINGS_KEYS.NOTIFICATIONS_CONSUMER_NEW_DOCUMENT, type: "boolean", default: true},
{key: SETTINGS_KEYS.NOTIFICATIONS_CONSUMER_SUCCESS, type: "boolean", default: true},
{key: SETTINGS_KEYS.NOTIFICATIONS_CONSUMER_FAILED, type: "boolean", default: true},
{key: SETTINGS_KEYS.NOTIFICATIONS_CONSUMER_SUPPRESS_ON_DASHBOARD, type: "boolean", default: true},
{
key: SETTINGS_KEYS.BULK_EDIT_CONFIRMATION_DIALOGS,
type: 'boolean',
default: true,
},
{
key: SETTINGS_KEYS.BULK_EDIT_APPLY_ON_CLOSE,
type: 'boolean',
default: false,
},
{ key: SETTINGS_KEYS.DOCUMENT_LIST_SIZE, type: 'number', default: 50 },
{ key: SETTINGS_KEYS.DARK_MODE_USE_SYSTEM, type: 'boolean', default: true },
{ key: SETTINGS_KEYS.DARK_MODE_ENABLED, type: 'boolean', default: false },
{
key: SETTINGS_KEYS.DARK_MODE_THUMB_INVERTED,
type: 'boolean',
default: true,
},
{ key: SETTINGS_KEYS.THEME_COLOR, type: 'string', default: '' },
{ key: SETTINGS_KEYS.USE_NATIVE_PDF_VIEWER, type: 'boolean', default: false },
{ key: SETTINGS_KEYS.DATE_LOCALE, type: 'string', default: '' },
{ key: SETTINGS_KEYS.DATE_FORMAT, type: 'string', default: 'mediumDate' },
{
key: SETTINGS_KEYS.NOTIFICATIONS_CONSUMER_NEW_DOCUMENT,
type: 'boolean',
default: true,
},
{
key: SETTINGS_KEYS.NOTIFICATIONS_CONSUMER_SUCCESS,
type: 'boolean',
default: true,
},
{
key: SETTINGS_KEYS.NOTIFICATIONS_CONSUMER_FAILED,
type: 'boolean',
default: true,
},
{
key: SETTINGS_KEYS.NOTIFICATIONS_CONSUMER_SUPPRESS_ON_DASHBOARD,
type: 'boolean',
default: true,
},
]
@Injectable({
providedIn: 'root'
providedIn: 'root',
})
export class SettingsService {
private renderer: Renderer2;
private renderer: Renderer2
constructor(
private rendererFactory: RendererFactory2,
@@ -69,67 +108,177 @@ export class SettingsService {
private meta: Meta,
@Inject(LOCALE_ID) private localeId: string
) {
this.renderer = rendererFactory.createRenderer(null, null);
this.renderer = rendererFactory.createRenderer(null, null)
this.updateAppearanceSettings()
}
public updateAppearanceSettings(darkModeUseSystem = null, darkModeEnabled = null, themeColor = null): void {
public updateAppearanceSettings(
darkModeUseSystem = null,
darkModeEnabled = null,
themeColor = null
): void {
darkModeUseSystem ??= this.get(SETTINGS_KEYS.DARK_MODE_USE_SYSTEM)
darkModeEnabled ??= this.get(SETTINGS_KEYS.DARK_MODE_ENABLED)
themeColor ??= this.get(SETTINGS_KEYS.THEME_COLOR);
themeColor ??= this.get(SETTINGS_KEYS.THEME_COLOR)
if (darkModeUseSystem) {
this.renderer.addClass(this.document.body, 'color-scheme-system')
this.renderer.removeClass(this.document.body, 'color-scheme-dark')
} else {
this.renderer.removeClass(this.document.body, 'color-scheme-system')
darkModeEnabled ? this.renderer.addClass(this.document.body, 'color-scheme-dark') : this.renderer.removeClass(this.document.body, 'color-scheme-dark')
darkModeEnabled
? this.renderer.addClass(this.document.body, 'color-scheme-dark')
: this.renderer.removeClass(this.document.body, 'color-scheme-dark')
}
if (themeColor) {
const hsl = hexToHsl(themeColor)
this.renderer.setStyle(document.documentElement, '--pngx-primary',`${+hsl.h * 360},${hsl.s * 100}%`, RendererStyleFlags2.DashCase)
this.renderer.setStyle(document.documentElement, '--pngx-primary-lightness',`${hsl.l * 100}%`, RendererStyleFlags2.DashCase)
this.renderer.setStyle(
document.documentElement,
'--pngx-primary',
`${+hsl.h * 360},${hsl.s * 100}%`,
RendererStyleFlags2.DashCase
)
this.renderer.setStyle(
document.documentElement,
'--pngx-primary-lightness',
`${hsl.l * 100}%`,
RendererStyleFlags2.DashCase
)
} else {
this.renderer.removeStyle(document.documentElement, '--pngx-primary', RendererStyleFlags2.DashCase)
this.renderer.removeStyle(document.documentElement, '--pngx-primary-lightness', RendererStyleFlags2.DashCase)
this.renderer.removeStyle(
document.documentElement,
'--pngx-primary',
RendererStyleFlags2.DashCase
)
this.renderer.removeStyle(
document.documentElement,
'--pngx-primary-lightness',
RendererStyleFlags2.DashCase
)
}
}
getLanguageOptions(): LanguageOption[] {
const languages = [
{code: "en-us", name: $localize`English (US)`, englishName: "English (US)", dateInputFormat: "mm/dd/yyyy"},
{code: "cs-cz", name: $localize`Czech`, englishName: "Czech", dateInputFormat: "dd.mm.yyyy"},
{code: "da-dk", name: $localize`Danish`, englishName: "Danish", dateInputFormat: "dd.mm.yyyy"},
{code: "de-de", name: $localize`German`, englishName: "German", dateInputFormat: "dd.mm.yyyy"},
{code: "en-gb", name: $localize`English (GB)`, englishName: "English (GB)", dateInputFormat: "dd/mm/yyyy"},
{code: "es-es", name: $localize`Spanish`, englishName: "Spanish", dateInputFormat: "dd/mm/yyyy"},
{code: "fr-fr", name: $localize`French`, englishName: "French", dateInputFormat: "dd/mm/yyyy"},
{code: "it-it", name: $localize`Italian`, englishName: "Italian", dateInputFormat: "dd/mm/yyyy"},
{code: "lb-lu", name: $localize`Luxembourgish`, englishName: "Luxembourgish", dateInputFormat: "dd.mm.yyyy"},
{code: "nl-nl", name: $localize`Dutch`, englishName: "Dutch", dateInputFormat: "dd-mm-yyyy"},
{code: "pl-pl", name: $localize`Polish`, englishName: "Polish", dateInputFormat: "dd.mm.yyyy"},
{code: "pt-br", name: $localize`Portuguese (Brazil)`, englishName: "Portuguese (Brazil)", dateInputFormat: "dd/mm/yyyy"},
{code: "pt-pt", name: $localize`Portuguese`, englishName: "Portuguese", dateInputFormat: "dd/mm/yyyy"},
{code: "ro-ro", name: $localize`Romanian`, englishName: "Romanian", dateInputFormat: "dd.mm.yyyy"},
{code: "ru-ru", name: $localize`Russian`, englishName: "Russian", dateInputFormat: "dd.mm.yyyy"},
{code: "sv-se", name: $localize`Swedish`, englishName: "Swedish", dateInputFormat: "yyyy-mm-dd"}
{
code: 'en-us',
name: $localize`English (US)`,
englishName: 'English (US)',
dateInputFormat: 'mm/dd/yyyy',
},
{
code: 'cs-cz',
name: $localize`Czech`,
englishName: 'Czech',
dateInputFormat: 'dd.mm.yyyy',
},
{
code: 'da-dk',
name: $localize`Danish`,
englishName: 'Danish',
dateInputFormat: 'dd.mm.yyyy',
},
{
code: 'de-de',
name: $localize`German`,
englishName: 'German',
dateInputFormat: 'dd.mm.yyyy',
},
{
code: 'en-gb',
name: $localize`English (GB)`,
englishName: 'English (GB)',
dateInputFormat: 'dd/mm/yyyy',
},
{
code: 'es-es',
name: $localize`Spanish`,
englishName: 'Spanish',
dateInputFormat: 'dd/mm/yyyy',
},
{
code: 'fr-fr',
name: $localize`French`,
englishName: 'French',
dateInputFormat: 'dd/mm/yyyy',
},
{
code: 'it-it',
name: $localize`Italian`,
englishName: 'Italian',
dateInputFormat: 'dd/mm/yyyy',
},
{
code: 'lb-lu',
name: $localize`Luxembourgish`,
englishName: 'Luxembourgish',
dateInputFormat: 'dd.mm.yyyy',
},
{
code: 'nl-nl',
name: $localize`Dutch`,
englishName: 'Dutch',
dateInputFormat: 'dd-mm-yyyy',
},
{
code: 'pl-pl',
name: $localize`Polish`,
englishName: 'Polish',
dateInputFormat: 'dd.mm.yyyy',
},
{
code: 'pt-br',
name: $localize`Portuguese (Brazil)`,
englishName: 'Portuguese (Brazil)',
dateInputFormat: 'dd/mm/yyyy',
},
{
code: 'pt-pt',
name: $localize`Portuguese`,
englishName: 'Portuguese',
dateInputFormat: 'dd/mm/yyyy',
},
{
code: 'ro-ro',
name: $localize`Romanian`,
englishName: 'Romanian',
dateInputFormat: 'dd.mm.yyyy',
},
{
code: 'ru-ru',
name: $localize`Russian`,
englishName: 'Russian',
dateInputFormat: 'dd.mm.yyyy',
},
{
code: 'sv-se',
name: $localize`Swedish`,
englishName: 'Swedish',
dateInputFormat: 'yyyy-mm-dd',
},
]
// Sort languages by localized name at runtime
languages.sort((a, b) => { return a.name < b.name ? -1 : 1 })
languages.sort((a, b) => {
return a.name < b.name ? -1 : 1
})
return languages
}
getDateLocaleOptions(): LanguageOption[] {
let isoOption: LanguageOption = {code: "iso-8601", name: $localize`ISO 8601`, dateInputFormat: "yyyy-mm-dd"}
let isoOption: LanguageOption = {
code: 'iso-8601',
name: $localize`ISO 8601`,
dateInputFormat: 'yyyy-mm-dd',
}
return [isoOption].concat(this.getLanguageOptions())
}
private getLanguageCookieName() {
let prefix = ""
let prefix = ''
if (this.meta.getTag('name=cookie_prefix')) {
prefix = this.meta.getTag('name=cookie_prefix').content
}
@@ -149,12 +298,18 @@ export class SettingsService {
}
getLocalizedDateInputFormat(): string {
let dateLocale = this.get(SETTINGS_KEYS.DATE_LOCALE) || this.getLanguage() || this.localeId.toLowerCase()
return this.getDateLocaleOptions().find(o => o.code == dateLocale)?.dateInputFormat || "yyyy-mm-dd"
let dateLocale =
this.get(SETTINGS_KEYS.DATE_LOCALE) ||
this.getLanguage() ||
this.localeId.toLowerCase()
return (
this.getDateLocaleOptions().find((o) => o.code == dateLocale)
?.dateInputFormat || 'yyyy-mm-dd'
)
}
get(key: string): any {
let setting = SETTINGS.find(s => s.key == key)
let setting = SETTINGS.find((s) => s.key == key)
if (!setting) {
return null
@@ -164,11 +319,11 @@ export class SettingsService {
if (value != null) {
switch (setting.type) {
case "boolean":
case 'boolean':
return JSON.parse(value)
case "number":
case 'number':
return +value
case "string":
case 'string':
return value
default:
return value

View File

@@ -1,16 +1,16 @@
import { TestBed } from '@angular/core/testing';
import { TestBed } from '@angular/core/testing'
import { ToastService } from './toast.service';
import { ToastService } from './toast.service'
describe('ToastService', () => {
let service: ToastService;
let service: ToastService
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(ToastService);
});
TestBed.configureTestingModule({})
service = TestBed.inject(ToastService)
})
it('should be created', () => {
expect(service).toBeTruthy();
});
});
expect(service).toBeTruthy()
})
})

View File

@@ -1,8 +1,7 @@
import { Injectable } from '@angular/core';
import { Subject, zip } from 'rxjs';
import { Injectable } from '@angular/core'
import { Subject, zip } from 'rxjs'
export interface Toast {
title: string
content: string
@@ -12,15 +11,13 @@ export interface Toast {
action?: any
actionName?: string
}
@Injectable({
providedIn: 'root'
providedIn: 'root',
})
export class ToastService {
constructor() { }
constructor() {}
private toasts: Toast[] = []
@@ -32,15 +29,15 @@ export class ToastService {
}
showError(content: string, delay: number = 10000) {
this.show({title: $localize`Error`, content: content, delay: delay})
this.show({ title: $localize`Error`, content: content, delay: delay })
}
showInfo(content: string, delay: number = 5000) {
this.show({title: $localize`Information`, content: content, delay: delay})
this.show({ title: $localize`Information`, content: content, delay: delay })
}
closeToast(toast: Toast) {
let index = this.toasts.findIndex(t => t == toast)
let index = this.toasts.findIndex((t) => t == toast)
if (index > -1) {
this.toasts.splice(index, 1)
this.toastsSubject.next(this.toasts)
@@ -50,5 +47,4 @@ export class ToastService {
getToasts() {
return this.toastsSubject
}
}