-
-
Notifications
You must be signed in to change notification settings - Fork 350
Expand file tree
/
Copy pathselectable-list.component.ts
More file actions
103 lines (92 loc) · 3.08 KB
/
selectable-list.component.ts
File metadata and controls
103 lines (92 loc) · 3.08 KB
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
import { Component, Input, Output, EventEmitter, ViewChild, ElementRef } from '@angular/core';
import { perfNow } from 'src/app/util/util';
@Component({
selector: 'app-selectable-list',
templateUrl: './selectable-list.component.html',
styleUrls: ['./selectable-list.component.css'],
})
export class SelectableListComponent {
@Input() title: string = '';
@Input() items: string[] = [];
@Input() selectedItem: string | null = null;
@Input() highlightedItems: string[] = [];
@Input() canEdit = true;
@Input() editMode = false;
@Input() addLabel = 'Add';
@Input() typeLabel = '';
@Input() relationshipEditMode = false;
@Output() itemSelected = new EventEmitter<string>();
@Output() addItem = new EventEmitter<void>();
@Output() cancel = new EventEmitter<void>();
@Output() save = new EventEmitter<void>();
@Output() renameItem = new EventEmitter<{ oldName: string; newName: string }>();
@Output() deleteItem = new EventEmitter<string>();
@Output() relationshipToggle = new EventEmitter<string>();
@Output() editModeChange = new EventEmitter<boolean>();
@ViewChild('editInput') editInputRef: ElementRef<HTMLInputElement> | undefined;
editingName: string = '';
editingOrgName: string = '';
onItemClicked(name: string) {
console.log(`Item clicked: ${name}`);
if (!this.relationshipEditMode) {
this.itemSelected.emit(name);
} else {
this.relationshipToggle.emit(name);
}
}
toggleEditMode() {
this.editMode = !this.editMode;
if (this.editMode) {
if (!this.selectedItem && this.items.length > 0) {
this.onItemClicked(this.items[0]);
}
}
this.editModeChange.emit(this.editMode);
}
startEditItem(name: string) {
this.editingName = name;
this.editingOrgName = name;
this.itemSelected.emit(name); // Select the item when editing starts
setTimeout(() => {
if (this.editInputRef) {
this.editInputRef.nativeElement.focus();
this.editInputRef.nativeElement.select();
}
});
}
handleDoubleClick(name: string) {
if (!this.canEdit) {
return;
}
if (!this.editMode) {
this.editMode = true;
this.editModeChange.emit(this.editMode);
}
this.startEditItem(name);
}
cancelEditItem(oldName: string) {
console.log(`${perfNow()}: Cancel editing: ${oldName}`);
this.editingName = '';
this.editingOrgName = '';
}
saveEditedItem(oldName: string) {
let newName: string = this.editingName?.trim() || oldName;
console.log(`${perfNow()}: Save Item: Setting new name: ${newName}`);
if (this.editingName?.trim() && this.editingName !== oldName) {
this.renameItem.emit({ oldName, newName });
}
this.editingName = '';
this.editingOrgName = '';
}
deleteListItem(name: string) {
console.log(`${perfNow()}: Delete Item: ${name}`);
let index: number = this.items.indexOf(name);
this.deleteItem.emit(name);
// Select next item
if (index < this.items.length - 1) {
this.onItemClicked(this.items[index + 1]);
} else if (index > 0) {
this.onItemClicked(this.items[index - 1]);
}
}
}