From ab7a499e8f5ad1252905d2688180d01db5ba985d Mon Sep 17 00:00:00 2001
From: jonaswinkler <17569239+jonaswinkler@users.noreply.github.com>
Date: Sun, 4 Apr 2021 00:03:51 +0200
Subject: [PATCH] refactor filter reset
---
.../document-list.component.html | 2 +-
.../document-list/document-list.component.ts | 48 ++-----------------
.../filter-editor/filter-editor.component.ts | 42 ++++++++++++++--
3 files changed, 42 insertions(+), 50 deletions(-)
diff --git a/src-ui/src/app/components/document-list/document-list.component.html b/src-ui/src/app/components/document-list/document-list.component.html
index f1f21b19c..45b6fad37 100644
--- a/src-ui/src/app/components/document-list/document-list.component.html
+++ b/src-ui/src/app/components/document-list/document-list.component.html
@@ -76,7 +76,7 @@
diff --git a/src-ui/src/app/components/document-list/document-list.component.ts b/src-ui/src/app/components/document-list/document-list.component.ts
index aa534e23e..020b38e78 100644
--- a/src-ui/src/app/components/document-list/document-list.component.ts
+++ b/src-ui/src/app/components/document-list/document-list.component.ts
@@ -2,6 +2,7 @@ import { Component, OnDestroy, OnInit, QueryList, ViewChild, ViewChildren } from
import { ActivatedRoute, Router } from '@angular/router';
import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
import { Subscription } from 'rxjs';
+import { FilterRule } from 'src/app/data/filter-rule';
import { FILTER_FULLTEXT_MORELIKE } from 'src/app/data/filter-rule-type';
import { PaperlessDocument } from 'src/app/data/paperless-document';
import { PaperlessSavedView } from 'src/app/data/paperless-saved-view';
@@ -38,7 +39,7 @@ export class DocumentListComponent implements OnInit, OnDestroy {
displayMode = 'smallCards' // largeCards, smallCards, details
- filterRulesModified: boolean = false
+ unmodifiedFilterRules: FilterRule[] = []
private consumptionFinishedSubscription: Subscription
@@ -82,12 +83,12 @@ export class DocumentListComponent implements OnInit, OnDestroy {
}
this.list.activateSavedView(view)
this.list.reload()
- this.rulesChanged()
+ this.unmodifiedFilterRules = view.filter_rules
})
} else {
this.list.activateSavedView(null)
this.list.reload()
- this.rulesChanged()
+ this.unmodifiedFilterRules = []
}
})
}
@@ -101,7 +102,6 @@ export class DocumentListComponent implements OnInit, OnDestroy {
loadViewConfig(view: PaperlessSavedView) {
this.list.loadSavedView(view)
this.list.reload()
- this.rulesChanged()
}
saveViewConfig() {
@@ -142,46 +142,6 @@ export class DocumentListComponent implements OnInit, OnDestroy {
})
}
- resetFilters(): void {
- this.filterRulesModified = false
- if (this.list.activeSavedViewId) {
- this.savedViewService.getCached(this.list.activeSavedViewId).subscribe(viewUntouched => {
- this.list.filterRules = viewUntouched.filter_rules
- this.list.reload()
- })
- } else {
- this.list.filterRules = []
- this.list.reload()
- }
- }
-
- rulesChanged() {
- let modified = false
- if (this.list.activeSavedViewId == null) {
- modified = this.list.filterRules.length > 0 // documents list is modified if it has any filters
- } else {
- // compare savedView current filters vs original
- this.savedViewService.getCached(this.list.activeSavedViewId).subscribe(view => {
- let filterRulesInitial = view.filter_rules
-
- if (this.list.filterRules.length !== filterRulesInitial.length) modified = true
- else {
- modified = this.list.filterRules.some(rule => {
- return (filterRulesInitial.find(fri => fri.rule_type == rule.rule_type && fri.value == rule.value) == undefined)
- })
-
- if (!modified) {
- // only check other direction if we havent already determined is modified
- modified = filterRulesInitial.some(rule => {
- this.list.filterRules.find(fr => fr.rule_type == rule.rule_type && fr.value == rule.value) == undefined
- })
- }
- }
- })
- }
- this.filterRulesModified = modified
- }
-
toggleSelected(document: PaperlessDocument, event: MouseEvent): void {
if (!event.shiftKey) this.list.toggleSelected(document)
else this.list.selectRangeTo(document)
diff --git a/src-ui/src/app/components/document-list/filter-editor/filter-editor.component.ts b/src-ui/src/app/components/document-list/filter-editor/filter-editor.component.ts
index 16c342308..c1fd8536f 100644
--- a/src-ui/src/app/components/document-list/filter-editor/filter-editor.component.ts
+++ b/src-ui/src/app/components/document-list/filter-editor/filter-editor.component.ts
@@ -109,8 +109,23 @@ export class FilterEditorComponent implements OnInit, OnDestroy {
dateAddedBefore: string
dateAddedAfter: string
+ _unmodifiedFilterRules: FilterRule[] = []
+ _filterRules: FilterRule[] = []
+
+ @Input()
+ set unmodifiedFilterRules(value: FilterRule[]) {
+ this._unmodifiedFilterRules = value
+ this.checkIfRulesHaveChanged()
+ }
+
+ get unmodifiedFilterRules(): FilterRule[] {
+ return this._unmodifiedFilterRules
+ }
+
@Input()
set filterRules (value: FilterRule[]) {
+ this._filterRules = value
+
this.documentTypeSelectionModel.clear(false)
this.tagSelectionModel.clear(false)
this.correspondentSelectionModel.clear(false)
@@ -172,6 +187,7 @@ export class FilterEditorComponent implements OnInit, OnDestroy {
break
}
})
+ this.checkIfRulesHaveChanged()
}
get filterRules(): FilterRule[] {
@@ -222,12 +238,27 @@ export class FilterEditorComponent implements OnInit, OnDestroy {
@Output()
filterRulesChange = new EventEmitter()
- @Output()
- reset = new EventEmitter()
-
- @Input()
rulesModified: boolean = false
+ private checkIfRulesHaveChanged() {
+ let modified = false
+ if (this._unmodifiedFilterRules.length != this._filterRules.length) {
+ modified = true
+ } else {
+ modified = this._unmodifiedFilterRules.some(rule => {
+ return (this._filterRules.find(fri => fri.rule_type == rule.rule_type && fri.value == rule.value) == undefined)
+ })
+
+ if (!modified) {
+ // only check other direction if we havent already determined is modified
+ modified = this._filterRules.some(rule => {
+ this._unmodifiedFilterRules.find(fr => fr.rule_type == rule.rule_type && fr.value == rule.value) == undefined
+ })
+ }
+ }
+ this.rulesModified = modified
+ }
+
updateRules() {
this.filterRulesChange.next(this.filterRules)
}
@@ -265,7 +296,8 @@ export class FilterEditorComponent implements OnInit, OnDestroy {
resetSelected() {
this.textFilterTarget = TEXT_FILTER_TARGET_TITLE_CONTENT
- this.reset.next()
+ this.filterRules = this._unmodifiedFilterRules
+ this.updateRules()
}
toggleTag(tagId: number) {