-
-
Notifications
You must be signed in to change notification settings - Fork 350
Expand file tree
/
Copy pathteams-groups-editor.component.ts
More file actions
278 lines (233 loc) · 8.87 KB
/
teams-groups-editor.component.ts
File metadata and controls
278 lines (233 loc) · 8.87 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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// Main container for teams/groups editing
import { Component, Input, Output, EventEmitter, OnChanges } from '@angular/core';
import { GroupName, TeamGroups, TeamName, TeamNames } from 'src/app/model/types';
import { perfNow, renameArrayElement } from 'src/app/util/util';
import { SettingsService } from 'src/app/service/settings/settings.service';
enum EditMode {
NONE,
TEAMS,
GROUPS,
}
export class SelectionChangedEvent {
selectedTeam: TeamName | null = null;
selectedGroup: GroupName | null = null;
constructor(selectedTeam: TeamName | null, selectedGroup: GroupName | null) {
this.selectedTeam = selectedTeam;
this.selectedGroup = selectedGroup;
}
}
export class TeamsGroupsChangedEvent {
teams: TeamNames = [];
teamGroups: TeamGroups = {};
teamsRenamed: Record<TeamName, TeamName> = {};
}
@Component({
selector: 'app-teams-groups-editor',
templateUrl: './teams-groups-editor.component.html',
styleUrls: ['./teams-groups-editor.component.css'],
})
export class TeamsGroupsEditorComponent implements OnChanges {
Mode = EditMode;
@Input() teams: TeamNames = [];
@Input() teamGroups: TeamGroups = {};
@Input() highlightedTeams: TeamName[] = [];
@Input() highlightedGroups: GroupName[] = [];
@Input() canEdit: boolean = true;
@Output() selectionChanged = new EventEmitter<SelectionChangedEvent>();
@Output() namesChanged = new EventEmitter<TeamsGroupsChangedEvent>();
editMode: EditMode = EditMode.NONE;
selectedTeam: string | null = null;
selectedGroup: string | null = null;
// Make a local copy of parent
localCopyTeams: TeamNames = [];
localCopyTeamsRenamed: Record<TeamName, TeamName> = {};
localCopyTeamGroups: TeamGroups = {};
constructor(public settings: SettingsService) {}
ngOnChanges() {
this.makeLocalCopy();
let groups: GroupName[] = this.getGroupNames();
if (groups.length > 0 && !this.selectedTeam && !this.selectedGroup) {
console.log(`${perfNow()}: Teams: No team or group selected, selecting first group`);
this.onGroupSelected(groups[0]);
}
}
// Makes a local copy to allow editing without affecting the original data
makeLocalCopy() {
this.localCopyTeams = this.teams.slice();
this.localCopyTeamGroups = this.cloneTeamGroups(this.teamGroups);
}
saveLocalCopy() {
this.teams = this.localCopyTeams.slice();
this.teamGroups = this.cloneTeamGroups(this.localCopyTeamGroups);
}
getGroupNames(): GroupName[] {
return Object.keys(this.teamGroups);
}
getRelatedGroups(team: TeamName): GroupName[] {
return Object.keys(this.localCopyTeamGroups).filter(group =>
this.localCopyTeamGroups[group].includes(team)
);
}
getRelatedTeams(group: GroupName): TeamNames {
return this.localCopyTeamGroups[group] || [];
}
getOriginalTeamName(localName: TeamName): TeamName {
if (this.teams.includes(localName)) {
return localName;
} else {
let index: number = this.localCopyTeams.indexOf(localName);
return this.teams?.[index] || '';
}
}
getOriginalGroupName(localName: GroupName): GroupName {
if (this.teamGroups.hasOwnProperty(localName)) {
return localName;
} else {
// FIXME: This logic is flawed if groups have been renamed
// let index: number = Object.keys(this.localCopyTeamGroups).indexOf(localName);
// return Object.keys(this.teamGroups)?.[index] || '';
return Object.keys(this.teamGroups)?.[0] || '';
}
}
cloneTeamGroups(original: TeamGroups): TeamGroups {
let clone: TeamGroups = {};
for (let group in original) {
clone[group] = original[group].slice();
}
return clone;
}
onTeamsEditModeChange(editing: boolean) {
this.editMode = editing ? EditMode.TEAMS : EditMode.NONE;
}
onGroupsEditModeChange(editing: boolean) {
this.editMode = editing ? EditMode.GROUPS : EditMode.NONE;
}
onTeamGroupToggle(team: TeamName | null, group: GroupName | null) {
if (!team || !group) return;
const members = this.localCopyTeamGroups[group] || [];
if (members.includes(team)) {
this.localCopyTeamGroups[group] = members.filter(t => t !== team);
} else {
this.localCopyTeamGroups[group] = [...members, team];
}
if (this.editMode === EditMode.TEAMS) {
this.highlightedGroups = this.getRelatedGroups(team);
} else if (this.editMode === EditMode.GROUPS) {
this.highlightedTeams = this.getRelatedTeams(group);
} else {
console.warn(`${perfNow()}: onTeamGroupToggle called in unexpected edit mode: ${this.editMode}`); // eslint-disable-line
}
}
onTeamSelected(team: string) {
console.log(`${perfNow()}: Selecting team: ${team}`);
this.selectedGroup = null;
this.highlightedTeams = [];
this.selectedTeam = team;
this.highlightedGroups = this.getRelatedGroups(team);
this.selectionChanged.emit(new SelectionChangedEvent(team, null));
}
onGroupSelected(group: string) {
console.log(`${perfNow()}: Selecting group: ${group}`);
this.selectedTeam = null;
this.highlightedGroups = [];
this.selectedGroup = group;
this.highlightedTeams = this.getRelatedTeams(group);
this.selectionChanged.emit(new SelectionChangedEvent(null, group));
}
onAddTeam() {
let newName: string = this.findNextName(this.localCopyTeams, this.settings.getTeamLabel());
this.localCopyTeams.push(newName);
this.onTeamSelected(newName);
}
onRenameTeam(event: { oldName: string; newName: string }) {
console.log(`${perfNow()}: Rename team: ${event.oldName} to ${event.newName}`);
if (this.localCopyTeams.includes(event.newName)) {
alert('Cannot have duplicate names\n\n(todo: make this alert pretty)');
this.onTeamSelected(event.oldName);
return;
} else if (this.teams.includes(event.newName)) {
alert('Cannot have old names either. Please accept the changes one by one\n\n(todo: make this alert pretty)'); // eslint-disable-line
this.onTeamSelected(event.oldName);
return;
}
this.localCopyTeams = renameArrayElement(this.localCopyTeams, event.oldName, event.newName);
for (let group in this.localCopyTeamGroups) {
// eslint-disable-next-line
this.localCopyTeamGroups[group] = renameArrayElement(this.localCopyTeamGroups[group], event.oldName, event.newName);
}
this.onTeamSelected(event.newName);
}
onDeleteTeam(team: TeamName) {
this.localCopyTeams = this.localCopyTeams.filter(t => t !== team);
// Remove team for any groups
for (let group in this.localCopyTeamGroups) {
this.localCopyTeamGroups[group] = this.localCopyTeamGroups[group].filter(t => t !== team);
}
}
onAddGroup() {
let newName: string = this.findNextName(
this.keys(this.localCopyTeamGroups),
this.settings.getGroupLabel()
);
this.localCopyTeamGroups[newName] = [];
this.onGroupSelected(newName);
}
onRenameGroup(event: { oldName: string; newName: string }) {
console.log(`${perfNow()}: Rename team: ${event.oldName} to ${event.newName}`);
this.localCopyTeamGroups[event.newName] = this.localCopyTeamGroups[event.oldName] || [];
delete this.localCopyTeamGroups[event.oldName];
this.onGroupSelected(event.newName);
}
onDeleteGroup(group: string) {
delete this.localCopyTeamGroups[group];
}
onSave() {
console.log(`${perfNow()}: Saving teams and groups`);
// Identify the teams that have changed names
let teamsRenamed: Record<TeamName, TeamName> = {};
for (let i = 0; i < this.teams.length; i++) {
if (this.teams[i] !== this.localCopyTeams[i]) {
teamsRenamed[this.teams[i]] = this.localCopyTeams[i];
}
}
this.editMode = EditMode.NONE;
// Copy the local copy to the main data
this.saveLocalCopy();
this.namesChanged.emit({
teams: this.teams,
teamGroups: this.teamGroups,
teamsRenamed: teamsRenamed,
});
}
onCancelEdit() {
console.log(`${perfNow()}: Cancel editing teams and groups`);
let selectedItem: string = '';
// Determine what is currently selected
if (this.editMode === EditMode.TEAMS) {
selectedItem = this.getOriginalTeamName(this.selectedTeam || '');
} else if (this.editMode === EditMode.GROUPS) {
selectedItem = this.getOriginalGroupName(this.selectedGroup || '');
}
// Recreate the local copy from original values
this.makeLocalCopy();
// Re-select and highlight the original values
if (this.editMode === EditMode.TEAMS) {
this.onTeamSelected(selectedItem as TeamName);
} else if (this.editMode === EditMode.GROUPS) {
this.onGroupSelected(selectedItem as GroupName);
}
this.editMode = EditMode.NONE;
}
findNextName(existingNames: string[], prefix: string): string {
let i: number = existingNames.length + 1;
let newName: string | null = null;
while (newName == null || existingNames.includes(newName)) {
newName = `${prefix} ${i}`;
i++;
}
return newName;
}
keys(obj: any): string[] {
return Object.keys(obj);
}
}