refactor filter reset

This commit is contained in:
jonaswinkler 2021-04-04 00:03:51 +02:00
parent fffe4f694f
commit ab7a499e8f
3 changed files with 42 additions and 50 deletions

View File

@ -76,7 +76,7 @@
</app-page-header> </app-page-header>
<div class="w-100 mb-2 mb-sm-4"> <div class="w-100 mb-2 mb-sm-4">
<app-filter-editor [hidden]="isBulkEditing" [(filterRules)]="list.filterRules" [rulesModified]="filterRulesModified" (filterRulesChange)="rulesChanged()" (reset)="resetFilters()" #filterEditor></app-filter-editor> <app-filter-editor [hidden]="isBulkEditing" [(filterRules)]="list.filterRules" [unmodifiedFilterRules]="unmodifiedFilterRules" #filterEditor></app-filter-editor>
<app-bulk-editor [hidden]="!isBulkEditing"></app-bulk-editor> <app-bulk-editor [hidden]="!isBulkEditing"></app-bulk-editor>
</div> </div>

View File

@ -2,6 +2,7 @@ import { Component, OnDestroy, OnInit, QueryList, ViewChild, ViewChildren } from
import { ActivatedRoute, Router } from '@angular/router'; import { ActivatedRoute, Router } from '@angular/router';
import { NgbModal } from '@ng-bootstrap/ng-bootstrap'; import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
import { Subscription } from 'rxjs'; import { Subscription } from 'rxjs';
import { FilterRule } from 'src/app/data/filter-rule';
import { FILTER_FULLTEXT_MORELIKE } from 'src/app/data/filter-rule-type'; import { FILTER_FULLTEXT_MORELIKE } from 'src/app/data/filter-rule-type';
import { PaperlessDocument } from 'src/app/data/paperless-document'; import { PaperlessDocument } from 'src/app/data/paperless-document';
import { PaperlessSavedView } from 'src/app/data/paperless-saved-view'; import { PaperlessSavedView } from 'src/app/data/paperless-saved-view';
@ -38,7 +39,7 @@ export class DocumentListComponent implements OnInit, OnDestroy {
displayMode = 'smallCards' // largeCards, smallCards, details displayMode = 'smallCards' // largeCards, smallCards, details
filterRulesModified: boolean = false unmodifiedFilterRules: FilterRule[] = []
private consumptionFinishedSubscription: Subscription private consumptionFinishedSubscription: Subscription
@ -82,12 +83,12 @@ export class DocumentListComponent implements OnInit, OnDestroy {
} }
this.list.activateSavedView(view) this.list.activateSavedView(view)
this.list.reload() this.list.reload()
this.rulesChanged() this.unmodifiedFilterRules = view.filter_rules
}) })
} else { } else {
this.list.activateSavedView(null) this.list.activateSavedView(null)
this.list.reload() this.list.reload()
this.rulesChanged() this.unmodifiedFilterRules = []
} }
}) })
} }
@ -101,7 +102,6 @@ export class DocumentListComponent implements OnInit, OnDestroy {
loadViewConfig(view: PaperlessSavedView) { loadViewConfig(view: PaperlessSavedView) {
this.list.loadSavedView(view) this.list.loadSavedView(view)
this.list.reload() this.list.reload()
this.rulesChanged()
} }
saveViewConfig() { 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 { toggleSelected(document: PaperlessDocument, event: MouseEvent): void {
if (!event.shiftKey) this.list.toggleSelected(document) if (!event.shiftKey) this.list.toggleSelected(document)
else this.list.selectRangeTo(document) else this.list.selectRangeTo(document)

View File

@ -109,8 +109,23 @@ export class FilterEditorComponent implements OnInit, OnDestroy {
dateAddedBefore: string dateAddedBefore: string
dateAddedAfter: string dateAddedAfter: string
_unmodifiedFilterRules: FilterRule[] = []
_filterRules: FilterRule[] = []
@Input()
set unmodifiedFilterRules(value: FilterRule[]) {
this._unmodifiedFilterRules = value
this.checkIfRulesHaveChanged()
}
get unmodifiedFilterRules(): FilterRule[] {
return this._unmodifiedFilterRules
}
@Input() @Input()
set filterRules (value: FilterRule[]) { set filterRules (value: FilterRule[]) {
this._filterRules = value
this.documentTypeSelectionModel.clear(false) this.documentTypeSelectionModel.clear(false)
this.tagSelectionModel.clear(false) this.tagSelectionModel.clear(false)
this.correspondentSelectionModel.clear(false) this.correspondentSelectionModel.clear(false)
@ -172,6 +187,7 @@ export class FilterEditorComponent implements OnInit, OnDestroy {
break break
} }
}) })
this.checkIfRulesHaveChanged()
} }
get filterRules(): FilterRule[] { get filterRules(): FilterRule[] {
@ -222,12 +238,27 @@ export class FilterEditorComponent implements OnInit, OnDestroy {
@Output() @Output()
filterRulesChange = new EventEmitter<FilterRule[]>() filterRulesChange = new EventEmitter<FilterRule[]>()
@Output()
reset = new EventEmitter()
@Input()
rulesModified: boolean = false 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() { updateRules() {
this.filterRulesChange.next(this.filterRules) this.filterRulesChange.next(this.filterRules)
} }
@ -265,7 +296,8 @@ export class FilterEditorComponent implements OnInit, OnDestroy {
resetSelected() { resetSelected() {
this.textFilterTarget = TEXT_FILTER_TARGET_TITLE_CONTENT this.textFilterTarget = TEXT_FILTER_TARGET_TITLE_CONTENT
this.reset.next() this.filterRules = this._unmodifiedFilterRules
this.updateRules()
} }
toggleTag(tagId: number) { toggleTag(tagId: number) {