|
|
|
@ -1,58 +1,37 @@
|
|
|
|
|
import {Component, HostListener, Inject, ViewChildren} from "@angular/core";
|
|
|
|
|
import {Component, Inject, OnChanges, SimpleChanges} from "@angular/core";
|
|
|
|
|
import {MAT_DIALOG_DATA, MatDialogRef} from "@angular/material/dialog";
|
|
|
|
|
import {SortDialogComponent} from "../sort-dialog/sort-dialog.component";
|
|
|
|
|
import {
|
|
|
|
|
GenericFilter,
|
|
|
|
|
OrFilterExpression,
|
|
|
|
|
SingleFilterExpression
|
|
|
|
|
} from "../../../../../models/GenericFilter";
|
|
|
|
|
import {TagQuery} from "../../../../../models/TagQuery";
|
|
|
|
|
import {Tag} from "../../../../../../api/models/Tag";
|
|
|
|
|
import {
|
|
|
|
|
TagFilterListItemComponent
|
|
|
|
|
} from "./tag-filter-list-item/tag-filter-list-item.component";
|
|
|
|
|
import {Selectable} from "../../../../../models/Selectable";
|
|
|
|
|
import {SearchFilters} from "../../../../../../api/models/SearchFilters";
|
|
|
|
|
import {FilterExpression, FilterQuery} from "../../../../../../api/api-types/files";
|
|
|
|
|
import {enumerate, removeByValue} from "../../../../../utils/list-utils";
|
|
|
|
|
|
|
|
|
|
type IndexableSelection<T> = {
|
|
|
|
|
[key: number]: T
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
|
selector: "app-filter-dialog",
|
|
|
|
|
templateUrl: "./filter-dialog.component.html",
|
|
|
|
|
styleUrls: ["./filter-dialog.component.scss"]
|
|
|
|
|
})
|
|
|
|
|
export class FilterDialogComponent {
|
|
|
|
|
|
|
|
|
|
public filters: Selectable<GenericFilter>[];
|
|
|
|
|
export class FilterDialogComponent implements OnChanges {
|
|
|
|
|
public availableTags: Tag[] = [];
|
|
|
|
|
public mode: "AND" | "OR" = "AND";
|
|
|
|
|
|
|
|
|
|
@ViewChildren(
|
|
|
|
|
TagFilterListItemComponent) filterListItems!: TagFilterListItemComponent[];
|
|
|
|
|
|
|
|
|
|
private selectedQueries: TagQuery[] = [];
|
|
|
|
|
public filters = new SearchFilters([]);
|
|
|
|
|
public renderedFilterEntries: [number, FilterExpression][] = [];
|
|
|
|
|
private selectedIndices: IndexableSelection<number[]> = {};
|
|
|
|
|
|
|
|
|
|
constructor(public dialogRef: MatDialogRef<SortDialogComponent>, @Inject(
|
|
|
|
|
MAT_DIALOG_DATA) data: any) {
|
|
|
|
|
this.filters = data.filterEntries.map(
|
|
|
|
|
(f: GenericFilter) => new Selectable<GenericFilter>(f,
|
|
|
|
|
false)) ?? [];
|
|
|
|
|
this.availableTags = data.availableTags ?? [];
|
|
|
|
|
this.filters = data.filters;
|
|
|
|
|
this.buildRenderedEntries();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static checkFiltersEqual(l: GenericFilter, r: GenericFilter): boolean {
|
|
|
|
|
const lTags = l.queryList().map(q => q.getNormalizedTag()).sort();
|
|
|
|
|
const rTags = r.queryList().map(q => q.getNormalizedTag()).sort();
|
|
|
|
|
let match = false;
|
|
|
|
|
|
|
|
|
|
if (lTags.length == rTags.length) {
|
|
|
|
|
match = true;
|
|
|
|
|
|
|
|
|
|
for (const tag of lTags) {
|
|
|
|
|
match = rTags.includes(tag);
|
|
|
|
|
if (!match) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public ngOnChanges(changes: SimpleChanges): void {
|
|
|
|
|
if (changes["filters"]) {
|
|
|
|
|
this.buildRenderedEntries();
|
|
|
|
|
}
|
|
|
|
|
return match;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public cancelFilter(): void {
|
|
|
|
@ -60,118 +39,105 @@ export class FilterDialogComponent {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public confirmFilter(): void {
|
|
|
|
|
this.dialogRef.close(this.filters.map(f => f.data));
|
|
|
|
|
this.dialogRef.close(this.filters);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public removeFilter(event: TagFilterListItemComponent): void {
|
|
|
|
|
const filter = event.expression;
|
|
|
|
|
const index = this.filters.findIndex(f => f === filter);
|
|
|
|
|
if (index >= 0) {
|
|
|
|
|
this.filters.splice(index, 1);
|
|
|
|
|
}
|
|
|
|
|
this.unselectAll();
|
|
|
|
|
public entrySelect(index: number, subindex: number = -1): void {
|
|
|
|
|
this.selectedIndices[index] = this.selectedIndices[index] ?? [];
|
|
|
|
|
this.selectedIndices[index].push(subindex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public addFilter(tag: string) {
|
|
|
|
|
const query = TagQuery.fromString(tag);
|
|
|
|
|
|
|
|
|
|
if (this.mode === "AND" || this.filters.length === 0) {
|
|
|
|
|
this.filters.push(
|
|
|
|
|
new Selectable<GenericFilter>(
|
|
|
|
|
new SingleFilterExpression(query),
|
|
|
|
|
false));
|
|
|
|
|
tag = tag.replace(/^-/g, "");
|
|
|
|
|
|
|
|
|
|
if (this.filters.filter(t => t.data.partiallyEq(tag)).length > 1) {
|
|
|
|
|
const index = this.filters.findIndex(
|
|
|
|
|
t => t.data.partiallyEq(tag));
|
|
|
|
|
this.filters.splice(index, 1);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
let queryList = this.filters.pop()?.data.queryList() ?? [];
|
|
|
|
|
|
|
|
|
|
queryList.push(query);
|
|
|
|
|
const filterExpression = new OrFilterExpression(queryList);
|
|
|
|
|
filterExpression.removeDuplicates();
|
|
|
|
|
this.filters.push(
|
|
|
|
|
new Selectable<GenericFilter>(filterExpression,
|
|
|
|
|
false));
|
|
|
|
|
}
|
|
|
|
|
this.unselectAll();
|
|
|
|
|
public entryUnselect(index: number, subindex: number = -1): void {
|
|
|
|
|
this.selectedIndices[index] = this.selectedIndices[index] ?? [];
|
|
|
|
|
removeByValue(this.selectedIndices[index], subindex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public addToSelection(query: TagQuery): void {
|
|
|
|
|
this.selectedQueries.push(query);
|
|
|
|
|
public addFilter(expression: FilterExpression): void {
|
|
|
|
|
this.filters.addFilterExpression(expression);
|
|
|
|
|
this.buildRenderedEntries();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public removeFromSelection(query: TagQuery): void {
|
|
|
|
|
const index = this.selectedQueries.indexOf(query);
|
|
|
|
|
if (index > 0) {
|
|
|
|
|
this.selectedQueries.splice(index, 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public removeSelectedFilters(): void {
|
|
|
|
|
const orderedIndices = Object.keys(this.selectedIndices).map(k => Number(k)).sort().reverse();
|
|
|
|
|
|
|
|
|
|
public unselectAll() {
|
|
|
|
|
this.filters.forEach(filter => filter.selected = false);
|
|
|
|
|
this.selectedQueries = [];
|
|
|
|
|
this.filterListItems.forEach(i => i.selectedIndices = []);
|
|
|
|
|
}
|
|
|
|
|
for (const indexStr of orderedIndices) {
|
|
|
|
|
const index = indexStr;
|
|
|
|
|
const subIndices: number[] = this.selectedIndices[index];
|
|
|
|
|
|
|
|
|
|
public convertSelectionToAndExpression(): void {
|
|
|
|
|
for (const query of this.selectedQueries) {
|
|
|
|
|
this.filters.push(
|
|
|
|
|
new Selectable<GenericFilter>(
|
|
|
|
|
new SingleFilterExpression(query),
|
|
|
|
|
false));
|
|
|
|
|
if (subIndices.length === 1 && subIndices[0] === -1) {
|
|
|
|
|
this.filters.removeFilterAtIndex(index);
|
|
|
|
|
} else if (subIndices.length > 0) {
|
|
|
|
|
for (const subIndex of subIndices.sort().reverse()) { // need to remove from the top down to avoid index shifting
|
|
|
|
|
this.filters.removeSubfilterAtIndex(index, subIndex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
this.removeFilterDuplicates();
|
|
|
|
|
this.unselectAll();
|
|
|
|
|
this.selectedIndices = {};
|
|
|
|
|
this.buildRenderedEntries();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public convertSelectionToOrExpression(): void {
|
|
|
|
|
const queries = this.selectedQueries;
|
|
|
|
|
const expression = new OrFilterExpression(queries);
|
|
|
|
|
this.filters.push(new Selectable<GenericFilter>(expression, false));
|
|
|
|
|
this.removeFilterDuplicates();
|
|
|
|
|
this.unselectAll();
|
|
|
|
|
public createAndFromSelection(deleteOriginal: boolean): void {
|
|
|
|
|
const expressions: FilterExpression[] = [];
|
|
|
|
|
|
|
|
|
|
for (const indexStr in this.selectedIndices) {
|
|
|
|
|
const index = Number(indexStr);
|
|
|
|
|
const subindices = this.selectedIndices[index];
|
|
|
|
|
|
|
|
|
|
if (subindices.length === 1 && subindices[0] === -1) {
|
|
|
|
|
expressions.push(this.filters.getFilters()[index]);
|
|
|
|
|
} else {
|
|
|
|
|
for (const subIndex of subindices) {
|
|
|
|
|
const query = this.filters.getSubfilterAtIndex(index, subIndex);
|
|
|
|
|
if (query) {
|
|
|
|
|
expressions.push({ Query: query });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (deleteOriginal) {
|
|
|
|
|
this.removeSelectedFilters();
|
|
|
|
|
} else {
|
|
|
|
|
this.selectedIndices = {};
|
|
|
|
|
}
|
|
|
|
|
expressions.forEach(e => this.filters.addFilterExpression(e));
|
|
|
|
|
this.buildRenderedEntries();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public invertSelection(): void {
|
|
|
|
|
this.selectedQueries.forEach(query => query.negate = !query.negate);
|
|
|
|
|
}
|
|
|
|
|
public createOrFromSelection(deleteOriginal: boolean): void {
|
|
|
|
|
const queries: FilterQuery[] = [];
|
|
|
|
|
|
|
|
|
|
private removeFilterDuplicates() {
|
|
|
|
|
const filters = this.filters;
|
|
|
|
|
let newFilters: Selectable<GenericFilter>[] = [];
|
|
|
|
|
for (const indexStr in this.selectedIndices) {
|
|
|
|
|
const index = Number(indexStr);
|
|
|
|
|
const subindices = this.selectedIndices[index];
|
|
|
|
|
|
|
|
|
|
for (const filterItem of filters) {
|
|
|
|
|
if (filterItem.data.filter_type == "OrExpression") {
|
|
|
|
|
(filterItem.data as OrFilterExpression).removeDuplicates();
|
|
|
|
|
}
|
|
|
|
|
if (newFilters.findIndex(
|
|
|
|
|
f => FilterDialogComponent.checkFiltersEqual(f.data,
|
|
|
|
|
filterItem.data)) < 0) {
|
|
|
|
|
if (filterItem.data.filter_type == "OrExpression" && filterItem.data.queryList().length === 1) {
|
|
|
|
|
filterItem.data = new SingleFilterExpression(
|
|
|
|
|
filterItem.data.queryList()[0]);
|
|
|
|
|
if (subindices.length === 1 && subindices[0] === -1) {
|
|
|
|
|
const filterEntry = this.filters.getFilters()[index];
|
|
|
|
|
if ("Query" in filterEntry) {
|
|
|
|
|
queries.push(filterEntry.Query);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
for (const subIndex of subindices) {
|
|
|
|
|
const query = this.filters.getSubfilterAtIndex(index, subIndex);
|
|
|
|
|
if (query) {
|
|
|
|
|
queries.push(query);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
newFilters.push(filterItem);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
this.filters = newFilters;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@HostListener("window:keydown", ["$event"])
|
|
|
|
|
private async handleKeydownEvent(event: KeyboardEvent) {
|
|
|
|
|
if (event.key === "Shift") {
|
|
|
|
|
this.mode = "OR";
|
|
|
|
|
if (deleteOriginal) {
|
|
|
|
|
this.removeSelectedFilters();
|
|
|
|
|
} else {
|
|
|
|
|
this.selectedIndices = {};
|
|
|
|
|
}
|
|
|
|
|
if (queries.length > 1) {
|
|
|
|
|
this.filters.addFilterExpression({ OrExpression: queries });
|
|
|
|
|
} else if (queries.length === 1) {
|
|
|
|
|
this.filters.addFilterExpression({ Query: queries[0] });
|
|
|
|
|
}
|
|
|
|
|
this.buildRenderedEntries();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@HostListener("window:keyup", ["$event"])
|
|
|
|
|
private async handleKeyupEvent(event: KeyboardEvent) {
|
|
|
|
|
if (event.key === "Shift") {
|
|
|
|
|
this.mode = "AND";
|
|
|
|
|
}
|
|
|
|
|
private buildRenderedEntries() {
|
|
|
|
|
this.renderedFilterEntries = enumerate(this.filters.getFilters());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|