-
-
Notifications
You must be signed in to change notification settings - Fork 746
/
tag-list.component.ts
135 lines (112 loc) · 3.37 KB
/
tag-list.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import {
AfterViewInit,
ChangeDetectionStrategy,
Component,
EventEmitter,
Input,
OnInit,
Output,
ViewChild
} from '@angular/core';
import {standardListAnimation} from '../../../ui/animations/standard-list.ani';
import {Tag} from '../tag.model';
import {TagService} from '../tag.service';
import {BehaviorSubject, Observable} from 'rxjs';
import {switchMap, take} from 'rxjs/operators';
import {MatDialog} from '@angular/material/dialog';
@Component({
selector: 'tag-list',
templateUrl: './tag-list.component.html',
styleUrls: ['./tag-list.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush,
animations: [standardListAnimation]
})
export class TagListComponent implements OnInit, AfterViewInit {
@Input() set tagIds(val: string[]) {
this._tagIds = val;
this._tagIds$.next(this._tagIds);
}
@Output() addedTagsToTask: EventEmitter<string[]> = new EventEmitter();
@Output() removedTagsFromTask: EventEmitter<string[]> = new EventEmitter();
@Output() replacedTagForTask: EventEmitter<string[]> = new EventEmitter();
@ViewChild('tagListEl', {static: true}) tagListEl;
editingTag: undefined | Partial<Tag> = undefined;
private _tagIds;
private _tagIds$ = new BehaviorSubject([]);
tags$: Observable<Tag[]> = this._tagIds$.pipe(
switchMap((ids) => this._tagService.getByIds$((ids))));
newTag: Partial<Tag> = {
name: '',
color: '#FFDAB9'
};
constructor(
private readonly _tagService: TagService,
private readonly _matDialog: MatDialog
) {
}
ngOnInit(): void {
}
ngAfterViewInit(): void {
}
onTagEditKeydown($event: KeyboardEvent) {
if ($event.key === 'Escape') {
this.stopEditingTag();
} else if ($event.key === 'Enter') {
this.applyEdit();
this.stopEditingTag();
$event.preventDefault();
}
}
applyEdit() {
const oldTagId = this.editingTag.id;
let newTagId: string;
delete this.editingTag.id;
this.tags$
.pipe(take(1))
.subscribe(tags => {
if (tags.map(t => t.name).indexOf(this.editingTag.name) !== -1) {
// TODO: Add proper feedback
console.log('Error: The requested tag is already present on this task!');
} else {
this._tagService.getByName$(this.editingTag.name)
.pipe(take(1))
.subscribe(match => {
if (match) {
newTagId = match.id;
} else {
newTagId = this._tagService.addTag(this.editingTag);
}
if (oldTagId) {
this.replacedTagForTask.emit([oldTagId, newTagId]);
} else {
this.addedTagsToTask.emit([newTagId]);
}
});
}
this.stopEditingTag();
});
}
applyColorEdit() {
const id = this.editingTag.id;
const color = this.editingTag.color;
if (id) {
console.log(`Updating color to ${color}`);
this._tagService.updateColor(id, color);
this.stopEditingTag();
}
}
stopEditingTag() {
this.editingTag = undefined;
}
handleClickOnTag($event: MouseEvent, tag: Partial<Tag>) {
if ($event.ctrlKey && tag.id) {
this.removedTagsFromTask.emit([tag.id]);
} else {
// Clone tag (tags are immutable)
this.editingTag = {name: tag.name, id: tag.id || undefined};
if (tag.color) {
this.editingTag.color = tag.color;
}
}
}
}