You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
90 lines
3.1 KiB
TypeScript
90 lines
3.1 KiB
TypeScript
3 years ago
|
import {BehaviorSubject} from "rxjs";
|
||
|
import {TabCategory} from "./TabCategory";
|
||
|
import {FileService} from "../services/file/file.service";
|
||
|
import {File} from "./File";
|
||
3 years ago
|
import {
|
||
|
FilterExpression,
|
||
|
OrFilterExpression,
|
||
|
SingleFilterExpression
|
||
|
} from "./FilterExpression";
|
||
3 years ago
|
import {SortKey} from "./SortKey";
|
||
3 years ago
|
import {TagQuery} from "./TagQuery";
|
||
3 years ago
|
import {debounceTime} from "rxjs/operators";
|
||
3 years ago
|
|
||
|
export class TabState {
|
||
|
public uuid: number;
|
||
|
public category: TabCategory;
|
||
3 years ago
|
public mode = new BehaviorSubject<"grid" | "gallery">("grid");
|
||
|
public selectedFileHash = new BehaviorSubject<string | undefined>(undefined);
|
||
3 years ago
|
public loading = new BehaviorSubject<boolean>(false);
|
||
3 years ago
|
|
||
3 years ago
|
public files = new BehaviorSubject<File[]>([]);
|
||
3 years ago
|
public filters = new BehaviorSubject<FilterExpression[]>([]);
|
||
|
public sortKeys = new BehaviorSubject<SortKey[]>(
|
||
|
[new SortKey("FileImportedTime",
|
||
|
"Ascending", undefined)]);
|
||
3 years ago
|
|
||
|
private fileService: FileService;
|
||
|
|
||
|
constructor(uuid: number, category: TabCategory, fileService: FileService) {
|
||
|
this.category = category;
|
||
|
this.uuid = uuid;
|
||
|
this.fileService = fileService;
|
||
3 years ago
|
if (this.category === TabCategory.Files) {
|
||
|
this.filters.pipe(debounceTime(500))
|
||
|
.subscribe(async () => await this.findFiles());
|
||
|
this.sortKeys.pipe(debounceTime(100))
|
||
|
.subscribe(async () => await this.findFiles());
|
||
|
}
|
||
3 years ago
|
}
|
||
|
|
||
3 years ago
|
public async findFiles() {
|
||
3 years ago
|
this.loading.next(true);
|
||
3 years ago
|
const files = await this.fileService.findFiles(this.filters.value,
|
||
|
this.sortKeys.value);
|
||
3 years ago
|
this.files.next(files);
|
||
3 years ago
|
this.loading.next(false);
|
||
3 years ago
|
}
|
||
|
|
||
3 years ago
|
public setFilters(filters: FilterExpression[]) {
|
||
|
this.filters.next(filters);
|
||
|
}
|
||
|
|
||
|
public setSortKeys(keys: SortKey[]) {
|
||
3 years ago
|
this.sortKeys.next(keys);
|
||
3 years ago
|
}
|
||
3 years ago
|
|
||
|
public static fromDTO(dto: any, fileService: FileService): TabState {
|
||
|
const state = new TabState(dto.uuid, dto.category, fileService);
|
||
|
const filters = dto.filters.map((f: {filter: any, filter_type: any}) => {
|
||
|
if (f.filter_type === "OrExpression") {
|
||
|
return new OrFilterExpression(f.filter.map((f: any) => new TagQuery(f.tag, f.negate)))
|
||
|
} else {
|
||
|
return new SingleFilterExpression(new TagQuery(f.filter.tag, f.filter.negate))
|
||
|
}
|
||
|
})
|
||
|
const sortKeys = dto.sortKeys.map((s: {sortType: any, sortDirection: any, namespaceName: any}) =>
|
||
|
new SortKey(s.sortType, s.sortDirection, s.namespaceName)
|
||
|
);
|
||
|
state.filters.next(filters);
|
||
|
state.sortKeys.next(sortKeys);
|
||
3 years ago
|
state.mode.next(dto.mode ?? "grid");
|
||
3 years ago
|
state.selectedFileHash.next(dto.selectedFileHash);
|
||
|
state.files.next(dto.files);
|
||
3 years ago
|
|
||
|
return state
|
||
|
}
|
||
|
|
||
|
public getDTO(): any {
|
||
|
return {
|
||
|
uuid: this.uuid,
|
||
|
category: this.category,
|
||
|
filters: this.filters.value,
|
||
|
sortKeys: this.sortKeys.value,
|
||
3 years ago
|
mode: this.mode.value,
|
||
3 years ago
|
selectedFileHash: this.selectedFileHash.value,
|
||
|
files: this.files.value,
|
||
3 years ago
|
};
|
||
|
}
|
||
3 years ago
|
}
|