From 329962dde28c2e00092b82d270f0ecc29f94f169 Mon Sep 17 00:00:00 2001 From: trivernis Date: Tue, 26 Oct 2021 22:17:27 +0200 Subject: [PATCH] Add negation to search queries Signed-off-by: trivernis --- mediarepo-ui/src-tauri/Cargo.lock | 2 +- mediarepo-ui/src-tauri/Cargo.toml | 2 +- mediarepo-ui/src/app/app.component.ts | 3 +-- .../file-search/file-search.component.html | 2 +- .../file-search/file-search.component.ts | 19 ++++++++++++------- mediarepo-ui/src/app/models/TagQuery.ts | 8 ++++++++ .../src/app/pages/home/home.component.ts | 8 ++++++-- .../services/dataloader/dataloader.service.ts | 2 -- .../src/app/services/file/file.service.ts | 3 ++- .../services/repository/repository.service.ts | 4 +++- 10 files changed, 35 insertions(+), 18 deletions(-) create mode 100644 mediarepo-ui/src/app/models/TagQuery.ts diff --git a/mediarepo-ui/src-tauri/Cargo.lock b/mediarepo-ui/src-tauri/Cargo.lock index df284a3..312f22f 100644 --- a/mediarepo-ui/src-tauri/Cargo.lock +++ b/mediarepo-ui/src-tauri/Cargo.lock @@ -1581,7 +1581,7 @@ checksum = "a3e378b66a060d48947b590737b30a1be76706c8dd7b8ba0f2fe3989c68a853f" [[package]] name = "mediarepo-api" version = "0.1.0" -source = "git+https://github.com/Trivernis/mediarepo-api.git?rev=4600810fca96bf6b457adc39667245a46127e5e8#4600810fca96bf6b457adc39667245a46127e5e8" +source = "git+https://github.com/Trivernis/mediarepo-api.git?rev=547947b05a28bb8a9191d3691d91dc7e3c68af0f#547947b05a28bb8a9191d3691d91dc7e3c68af0f" dependencies = [ "async-trait", "chrono", diff --git a/mediarepo-ui/src-tauri/Cargo.toml b/mediarepo-ui/src-tauri/Cargo.toml index cf835ff..52a2d26 100644 --- a/mediarepo-ui/src-tauri/Cargo.toml +++ b/mediarepo-ui/src-tauri/Cargo.toml @@ -30,7 +30,7 @@ features = ["env-filter"] [dependencies.mediarepo-api] git = "https://github.com/Trivernis/mediarepo-api.git" -rev = "4600810fca96bf6b457adc39667245a46127e5e8" +rev = "547947b05a28bb8a9191d3691d91dc7e3c68af0f" features = ["tauri-plugin"] [features] diff --git a/mediarepo-ui/src/app/app.component.ts b/mediarepo-ui/src/app/app.component.ts index 8955f9d..c10aa00 100644 --- a/mediarepo-ui/src/app/app.component.ts +++ b/mediarepo-ui/src/app/app.component.ts @@ -17,7 +17,6 @@ export class AppComponent implements OnInit{ private router: Router, private snackBar: MatSnackBar, private errorBroker: ErrorBrokerService, - private dataloaderService: DataloaderService, private repoService: RepositoryService, ) { } @@ -25,7 +24,7 @@ export class AppComponent implements OnInit{ async ngOnInit() { this.errorBroker.errorCb = (err: { message: string }) => this.showError(err); this.errorBroker.infoCb = (info: string) => this.showInfo(info); - await this.dataloaderService.loadData(); + await this.repoService.loadRepositories(); if (this.repoService.selectedRepository.getValue() == undefined) { await this.router.navigate(["repositories"]) } diff --git a/mediarepo-ui/src/app/components/file-search/file-search.component.html b/mediarepo-ui/src/app/components/file-search/file-search.component.html index a74f731..783e932 100644 --- a/mediarepo-ui/src/app/components/file-search/file-search.component.html +++ b/mediarepo-ui/src/app/components/file-search/file-search.component.html @@ -1,7 +1,7 @@ - {{tag}} + {{tag.getNormalizedTag()}} diff --git a/mediarepo-ui/src/app/components/file-search/file-search.component.ts b/mediarepo-ui/src/app/components/file-search/file-search.component.ts index fdc631e..a8baa87 100644 --- a/mediarepo-ui/src/app/components/file-search/file-search.component.ts +++ b/mediarepo-ui/src/app/components/file-search/file-search.component.ts @@ -7,6 +7,7 @@ import {MatChipInputEvent} from "@angular/material/chips"; import {MatAutocompleteSelectedEvent} from "@angular/material/autocomplete"; import {map, startWith} from "rxjs/operators"; import {Observable} from "rxjs"; +import {TagQuery} from "../../models/TagQuery"; @Component({ selector: 'app-file-search', @@ -17,7 +18,7 @@ export class FileSearchComponent { public searchInputSeparators = [COMMA]; public formControl = new FormControl(); - public searchTags: string[] = []; + public searchTags: TagQuery[] = []; public suggestionTags: Observable; private allTags: string[] = []; @@ -29,7 +30,7 @@ export class FileSearchComponent { this.suggestionTags = this.formControl.valueChanges.pipe(startWith(null), map( (tag: string | null) => tag ? this.allTags.filter( - (t: string) => t.includes(tag)).slice(0, 20) : this.allTags.slice(0, 20))); + (t: string) => t.includes(tag.replace(/^-/g, ''))).map((t) => tag.startsWith("-")? "-" + t : t).slice(0, 20) : this.allTags.slice(0, 20))); } public async searchForFiles() { @@ -37,10 +38,14 @@ export class FileSearchComponent { } public addSearchTag(tag: string) { - this.searchTags.push(tag); + if (tag.startsWith("-")) { + this.searchTags.push(new TagQuery(tag.replace(/^-/g, ''), true)); + } else { + this.searchTags.push(new TagQuery(tag, false)); + } } - async removeSearchTag(tag: string) { + async removeSearchTag(tag: TagQuery) { const index = this.searchTags.indexOf(tag); if (index >= 0) { this.searchTags.splice(index, 1); @@ -50,8 +55,8 @@ export class FileSearchComponent { async addSearchTagByChip(event: MatChipInputEvent) { const tag = event.value.trim(); - if (tag.length > 0 && this.allTags.includes(tag)) { - this.searchTags.push(tag); + if (tag.length > 0 && this.allTags.includes(tag.replace(/-/g, ''))) { + this.addSearchTag(tag); event.chipInput?.clear(); this.formControl.setValue(null); await this.searchForFiles(); } @@ -59,7 +64,7 @@ export class FileSearchComponent { async addSearchTagByAutocomplete(event: MatAutocompleteSelectedEvent) { const tag = event.option.viewValue; - this.searchTags.push(tag); + this.addSearchTag(tag); this.formControl.setValue(null); this.tagInput.nativeElement.value = ''; await this.searchForFiles(); } diff --git a/mediarepo-ui/src/app/models/TagQuery.ts b/mediarepo-ui/src/app/models/TagQuery.ts new file mode 100644 index 0000000..818f0ee --- /dev/null +++ b/mediarepo-ui/src/app/models/TagQuery.ts @@ -0,0 +1,8 @@ +export class TagQuery { + constructor(public name: string, public negate: boolean) { + } + + public getNormalizedTag(): string { + return this.negate? "-" + this.name : this.name; + } +} diff --git a/mediarepo-ui/src/app/pages/home/home.component.ts b/mediarepo-ui/src/app/pages/home/home.component.ts index 4eabd72..5e3e802 100644 --- a/mediarepo-ui/src/app/pages/home/home.component.ts +++ b/mediarepo-ui/src/app/pages/home/home.component.ts @@ -13,14 +13,18 @@ import {Observable} from "rxjs"; import {map, startWith} from "rxjs/operators"; import {FormControl} from "@angular/forms"; import {FileSearchComponent} from "../../components/file-search/file-search.component"; +import {DataloaderService} from "../../services/dataloader/dataloader.service"; @Component({ selector: 'app-home', templateUrl: './home.component.html', styleUrls: ['./home.component.scss'] }) -export class HomeComponent { +export class HomeComponent implements OnInit { + public async ngOnInit(): Promise { + await this.dataloaderService.loadData(); + } - constructor() {} + constructor(private dataloaderService: DataloaderService) {} } diff --git a/mediarepo-ui/src/app/services/dataloader/dataloader.service.ts b/mediarepo-ui/src/app/services/dataloader/dataloader.service.ts index ff3e901..208b6da 100644 --- a/mediarepo-ui/src/app/services/dataloader/dataloader.service.ts +++ b/mediarepo-ui/src/app/services/dataloader/dataloader.service.ts @@ -12,13 +12,11 @@ export class DataloaderService { constructor( private erroBroker: ErrorBrokerService, - private repositoryService: RepositoryService, private fileService: FileService, private tagService: TagService) { } public async loadData() { try { - await this.repositoryService.loadRepositories(); await this.tagService.loadTags(); await this.fileService.getFiles(); } catch (err) { diff --git a/mediarepo-ui/src/app/services/file/file.service.ts b/mediarepo-ui/src/app/services/file/file.service.ts index 49f9bfd..439e96b 100644 --- a/mediarepo-ui/src/app/services/file/file.service.ts +++ b/mediarepo-ui/src/app/services/file/file.service.ts @@ -4,6 +4,7 @@ import {File} from "../../models/File"; import {invoke} from "@tauri-apps/api/tauri"; import {DomSanitizer, SafeResourceUrl} from "@angular/platform-browser"; import {Thumbnail} from "../../models/Thumbnail"; +import {TagQuery} from "../../models/TagQuery"; @Injectable({ providedIn: 'root' @@ -21,7 +22,7 @@ export class FileService { this.displayedFiles.next(all_files); } - public async findFiles(tags: string[]) { + public async findFiles(tags: TagQuery[]) { let files = await invoke("plugin:mediarepo|find_files", {tags}); this.displayedFiles.next(files); } diff --git a/mediarepo-ui/src/app/services/repository/repository.service.ts b/mediarepo-ui/src/app/services/repository/repository.service.ts index e39f8af..16f44c8 100644 --- a/mediarepo-ui/src/app/services/repository/repository.service.ts +++ b/mediarepo-ui/src/app/services/repository/repository.service.ts @@ -5,6 +5,7 @@ import {invoke} from "@tauri-apps/api/tauri"; import {listen} from "@tauri-apps/api/event"; import {Info} from "../../models/Info"; import {ErrorBrokerService} from "../error-broker/error-broker.service"; +import {DataloaderService} from "../dataloader/dataloader.service"; @Injectable({ providedIn: 'root' @@ -13,7 +14,7 @@ export class RepositoryService { repositories = new BehaviorSubject([]); public selectedRepository = new BehaviorSubject(undefined); - constructor(private errorBroker: ErrorBrokerService) { + constructor(private errorBroker: ErrorBrokerService, private dataloaderService: DataloaderService) { this.registerListener() } @@ -35,6 +36,7 @@ export class RepositoryService { public async setRepository(repo: Repository) { await invoke("plugin:mediarepo|select_repository", {name: repo.name}); this.selectedRepository.next(repo); + await this.dataloaderService.loadData(); } public async addRepository(name: string, path: string) {