|
|
|
@ -1,4 +1,11 @@
|
|
|
|
|
import {Component, Input, OnInit, ViewChild} from '@angular/core';
|
|
|
|
|
import {
|
|
|
|
|
Component,
|
|
|
|
|
Input,
|
|
|
|
|
OnChanges,
|
|
|
|
|
OnInit,
|
|
|
|
|
SimpleChanges,
|
|
|
|
|
ViewChild
|
|
|
|
|
} from '@angular/core';
|
|
|
|
|
import {FormControl} from "@angular/forms";
|
|
|
|
|
import {File} from "../../models/File";
|
|
|
|
|
import {Tag} from "../../models/Tag";
|
|
|
|
@ -13,12 +20,13 @@ import {TagService} from "../../services/tag/tag.service";
|
|
|
|
|
templateUrl: './file-edit.component.html',
|
|
|
|
|
styleUrls: ['./file-edit.component.scss']
|
|
|
|
|
})
|
|
|
|
|
export class FileEditComponent implements OnInit {
|
|
|
|
|
export class FileEditComponent implements OnInit, OnChanges {
|
|
|
|
|
|
|
|
|
|
@Input() files: File[] = [];
|
|
|
|
|
@Input() tags: Tag[] = [];
|
|
|
|
|
public tags: Tag[] = [];
|
|
|
|
|
|
|
|
|
|
private allTags: Tag[] = [];
|
|
|
|
|
private fileTags: {[key: number]: Tag[]} = {};
|
|
|
|
|
|
|
|
|
|
public suggestionTags: Observable<string[]>;
|
|
|
|
|
public tagInputForm = new FormControl("");
|
|
|
|
@ -38,6 +46,13 @@ export class FileEditComponent implements OnInit {
|
|
|
|
|
async ngOnInit() {
|
|
|
|
|
this.tagService.tags.subscribe(tags => this.allTags = tags);
|
|
|
|
|
await this.tagService.loadTags();
|
|
|
|
|
await this.loadFileTags();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async ngOnChanges(changes: SimpleChanges) {
|
|
|
|
|
if (changes["files"]) {
|
|
|
|
|
await this.loadFileTags()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async editTagByAutocomplete($event: MatAutocompleteSelectedEvent) {
|
|
|
|
@ -45,13 +60,13 @@ export class FileEditComponent implements OnInit {
|
|
|
|
|
await this.editTag(tag);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async editTag(tag: string): Promise<void> {
|
|
|
|
|
public async editTag(tag: string): Promise<void> {
|
|
|
|
|
if (tag.length > 0) {
|
|
|
|
|
let tagInstance = this.allTags.find(t => t.getNormalizedOutput() === tag);
|
|
|
|
|
|
|
|
|
|
if (!tagInstance) {
|
|
|
|
|
// TODO: Create tag
|
|
|
|
|
tagInstance = new Tag(0, "", undefined);
|
|
|
|
|
tagInstance = (await this.tagService.createTags([tag]))[0];
|
|
|
|
|
this.allTags.push(tagInstance);
|
|
|
|
|
}
|
|
|
|
|
switch (this.editMode) {
|
|
|
|
|
case "Toggle":
|
|
|
|
@ -69,10 +84,29 @@ export class FileEditComponent implements OnInit {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async toggleTag(tag: Tag) {
|
|
|
|
|
|
|
|
|
|
for (const file of this.files) {
|
|
|
|
|
const fileTags = this.fileTags[file.id];
|
|
|
|
|
let addedTags = [];
|
|
|
|
|
let removedTags = [];
|
|
|
|
|
if (fileTags.findIndex(i => i.id === tag.id) < 0) {
|
|
|
|
|
addedTags.push(tag.id);
|
|
|
|
|
} else {
|
|
|
|
|
removedTags.push(tag.id);
|
|
|
|
|
}
|
|
|
|
|
this.fileTags[file.id] = await this.tagService.changeFileTags(file.id, addedTags, removedTags);
|
|
|
|
|
}
|
|
|
|
|
this.mapFileTagsToTagList();
|
|
|
|
|
const index = this.tags.indexOf(tag);
|
|
|
|
|
index >= 0 && this.tagScroll.scrollToIndex(index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async addTag(tag: Tag) {
|
|
|
|
|
for (const file of this.files) {
|
|
|
|
|
if (this.fileTags[file.id].findIndex(t => t.id === tag.id) < 0) {
|
|
|
|
|
this.fileTags[file.id] = await this.tagService.changeFileTags(file.id,
|
|
|
|
|
[tag.id], []);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (this.tags.findIndex(t => t.getNormalizedOutput() === tag.getNormalizedOutput()) < 0) {
|
|
|
|
|
this.tags.push(tag);
|
|
|
|
|
this.tags = this.tags.sort(
|
|
|
|
@ -84,6 +118,12 @@ export class FileEditComponent implements OnInit {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async removeTag(tag: Tag) {
|
|
|
|
|
for (const file of this.files) {
|
|
|
|
|
if (this.fileTags[file.id].findIndex(t => t.id === tag.id) >= 0) {
|
|
|
|
|
this.fileTags[file.id] = await this.tagService.changeFileTags(file.id,
|
|
|
|
|
[], [tag.id]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
const index = this.tags.indexOf(tag);
|
|
|
|
|
if (index >= 0) {
|
|
|
|
|
this.tags.splice(index, 1);
|
|
|
|
@ -97,4 +137,26 @@ export class FileEditComponent implements OnInit {
|
|
|
|
|
t => t.includes(tag))
|
|
|
|
|
.slice(0, 20);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async loadFileTags() {
|
|
|
|
|
for (const file of this.files) {
|
|
|
|
|
this.fileTags[file.id] = await this.tagService.getTagsForFiles([file.hash]);
|
|
|
|
|
}
|
|
|
|
|
this.mapFileTagsToTagList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private mapFileTagsToTagList() {
|
|
|
|
|
let tags: Tag[] = [];
|
|
|
|
|
for (const file of this.files) {
|
|
|
|
|
const fileTags = this.fileTags[file.id];
|
|
|
|
|
tags.push(...fileTags.filter(t => tags.findIndex(tag => tag.id === t.id) < 0));
|
|
|
|
|
}
|
|
|
|
|
this.tags = tags.sort((a, b) => a.getNormalizedOutput().localeCompare(b.getNormalizedOutput()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async handleTagInputKeydown($event: KeyboardEvent) {
|
|
|
|
|
if ($event.key === "Enter") {
|
|
|
|
|
await this.editTag(this.tagInputForm.value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|