@@ -6,7 +6,17 @@ import { perfNow, renameArrayElement } from 'src/app/util/util';
66
77enum EditMode { NONE , TEAMS , GROUPS } ;
88
9- export class TeamsGroupsChanged {
9+ export class SelectionChangedEvent {
10+ selectedTeam : TeamName | null = null ;
11+ selectedGroup : GroupName | null = null ;
12+
13+ constructor ( selectedTeam : TeamName | null , selectedGroup : GroupName | null ) {
14+ this . selectedTeam = selectedTeam ;
15+ this . selectedGroup = selectedGroup ;
16+ }
17+ }
18+
19+ export class TeamsGroupsChangedEvent {
1020 teams : TeamNames = [ ] ;
1121 teamGroups : TeamGroups = { } ;
1222 teamsRenamed : Record < TeamName , TeamName > = { } ;
@@ -21,44 +31,54 @@ export class TeamsGroupsEditorComponent {
2131 Mode = EditMode ;
2232 @Input ( ) teams : TeamNames = [ ] ;
2333 @Input ( ) teamGroups : TeamGroups = { } ;
24- @Input ( ) highlightedTeams : string [ ] = [ ] ;
25- @Input ( ) highlightedGroups : string [ ] = [ ] ;
26- @Output ( ) changed = new EventEmitter < TeamsGroupsChanged > ( ) ;
34+ @Input ( ) highlightedTeams : TeamName [ ] = [ ] ;
35+ @Input ( ) highlightedGroups : GroupName [ ] = [ ] ;
36+ @Input ( ) canEdit : boolean = true ;
37+ @Output ( ) selectionChanged = new EventEmitter < SelectionChangedEvent > ( ) ;
38+ @Output ( ) namesChanged = new EventEmitter < TeamsGroupsChangedEvent > ( ) ;
2739
2840 editMode : EditMode = EditMode . NONE ;
29- selectedTeamId : string | null = null ;
30- selectedGroupId : string | null = null ;
41+ selectedTeam : string | null = null ;
42+ selectedGroup : string | null = null ;
3143
3244 // Make a local copy of parent
3345 localCopyTeams : TeamNames = [ ] ;
3446 localCopyTeamsRenamed : Record < TeamName , TeamName > = { } ;
3547 localCopyTeamGroups : TeamGroups = { } ;
36- localCopyGroupNames : GroupName [ ] = [ ] ;
3748
3849
3950
4051 ngOnChanges ( ) {
4152 this . makeLocalCopy ( ) ;
42- console . log ( 'EDitor: ngOnChanges(): groupNames: ' , this . localCopyGroupNames ) ;
43- if ( this . teams . length > 0 ) {
53+ if ( this . teams . length > 0 && ! this . selectedTeam && ! this . selectedGroup ) {
54+ console . log ( ` ${ perfNow ( ) } : Teams: No team or group selected, selecting first team` ) ;
4455 this . onTeamSelected ( this . teams [ 0 ] ) ;
4556 }
4657 }
4758
48- ngOnInit ( ) {
49- }
50-
59+ // Makes a local copy to allow editing without affecting the original data
5160 makeLocalCopy ( ) {
5261 this . localCopyTeams = this . teams . slice ( ) ;
5362 this . localCopyTeamGroups = this . cloneTeamGroups ( this . teamGroups ) ;
54- this . localCopyGroupNames = Object . keys ( this . teamGroups ) ;
5563 }
5664
5765 saveLocalCopy ( ) {
5866 this . teams = this . localCopyTeams . slice ( ) ;
5967 this . teamGroups = this . cloneTeamGroups ( this . localCopyTeamGroups ) ;
6068 }
6169
70+ getGroupNames ( ) : GroupName [ ] {
71+ return Object . keys ( this . teamGroups ) ;
72+ }
73+
74+ getRelatedGroups ( team : TeamName ) : GroupName [ ] {
75+ return Object . keys ( this . localCopyTeamGroups ) . filter ( group => this . localCopyTeamGroups [ group ] . includes ( team ) ) ;
76+ }
77+
78+ getRelatedTeams ( group : GroupName ) : TeamNames {
79+ return this . localCopyTeamGroups [ group ] || [ ] ;
80+ }
81+
6282 cloneTeamGroups ( original : TeamGroups ) : TeamGroups {
6383 let clone : TeamGroups = { } ;
6484 for ( let group in original ) {
@@ -75,33 +95,46 @@ export class TeamsGroupsEditorComponent {
7595 this . editMode = editing ? EditMode . GROUPS : EditMode . NONE ;
7696 }
7797
78- onTeamGroupToggle ( team : string | null , group : string | null ) {
98+ onTeamGroupToggle ( team : TeamName | null , group : GroupName | null ) {
7999 if ( ! team || ! group ) return ;
80100 const members = this . localCopyTeamGroups [ group ] || [ ] ;
81101 if ( members . includes ( team ) ) {
82102 this . localCopyTeamGroups [ group ] = members . filter ( t => t !== team ) ;
83103 } else {
84104 this . localCopyTeamGroups [ group ] = [ ...members , team ] ;
85105 }
106+
107+ if ( this . editMode === EditMode . TEAMS ) {
108+ this . highlightedGroups = this . getRelatedGroups ( team ) ;
109+ } else if ( this . editMode === EditMode . GROUPS ) {
110+ this . highlightedTeams = this . getRelatedTeams ( group ) ;
111+ } else {
112+ console . warn ( `${ perfNow ( ) } : onTeamGroupToggle called in unexpected edit mode: ${ this . editMode } ` ) ;
113+ }
86114 }
87115
88- onTeamSelected ( teamId : string ) {
89- console . log ( `${ perfNow ( ) } : Selecting team: ${ teamId } ` ) ;
90- this . selectedGroupId = null ;
116+ onTeamSelected ( team : string ) {
117+ console . log ( `${ perfNow ( ) } : Selecting team: ${ team } ` ) ;
118+ this . selectedGroup = null ;
91119 this . highlightedTeams = [ ] ;
92- this . selectedTeamId = teamId ;
93- this . highlightedGroups = this . localCopyGroupNames . filter ( group => ( this . localCopyTeamGroups [ group ] || [ ] ) . includes ( teamId ) ) ;
120+ this . selectedTeam = team ;
121+ this . highlightedGroups = this . getRelatedGroups ( team ) ;
122+
123+ this . selectionChanged . emit ( new SelectionChangedEvent ( team , null ) ) ;
94124 }
95125
96- onGroupSelected ( groupId : string ) {
97- this . selectedTeamId = null ;
126+ onGroupSelected ( group : string ) {
127+ console . log ( `${ perfNow ( ) } : Selecting group: ${ group } ` ) ;
128+ this . selectedTeam = null ;
98129 this . highlightedGroups = [ ] ;
99- this . selectedGroupId = groupId ;
100- this . highlightedTeams = this . localCopyTeamGroups [ groupId ] || [ ] ;
130+ this . selectedGroup = group ;
131+ this . highlightedTeams = this . getRelatedTeams ( group ) ;
132+
133+ this . selectionChanged . emit ( new SelectionChangedEvent ( null , group ) ) ;
101134 }
102135
103136 onAddTeam ( ) {
104- let newName : string = `Team ${ this . localCopyTeams . length + 1 } ` ;
137+ let newName : string = this . findNextName ( this . localCopyTeams , 'Team' ) ;
105138 this . localCopyTeams . push ( newName ) ;
106139 this . onTeamSelected ( newName ) ;
107140 }
@@ -127,32 +160,29 @@ export class TeamsGroupsEditorComponent {
127160
128161 }
129162
130- onDeleteTeam ( teamId : TeamName ) {
131- this . localCopyTeams = this . localCopyTeams . filter ( team => team !== teamId ) ;
163+ onDeleteTeam ( team : TeamName ) {
164+ this . localCopyTeams = this . localCopyTeams . filter ( t => t !== team ) ;
132165 for ( let group in this . localCopyTeamGroups ) {
133- this . localCopyTeamGroups [ group ] = this . localCopyTeamGroups [ group ] . filter ( team => team !== teamId ) ;
166+ this . localCopyTeamGroups [ group ] = this . localCopyTeamGroups [ group ] . filter ( team => team !== team ) ;
134167 }
135168
136169 this . onTeamSelected ( '' ) ;
137170 }
138171
139172 onAddGroup ( ) {
140- let newName : string = `Group ${ this . localCopyGroupNames . length + 1 } ` ;
141- this . localCopyGroupNames . push ( newName ) ;
173+ let newName : string = this . findNextName ( this . keys ( this . localCopyTeamGroups ) , 'Group' ) ;
142174 this . localCopyTeamGroups [ newName ] = [ ] ;
143175 this . onGroupSelected ( newName ) ;
144176 }
145177
146178 onRenameGroup ( event : { oldName : string , newName : string } ) {
147179 console . log ( `${ perfNow ( ) } : Rename team: ${ event . oldName } to ${ event . newName } ` ) ;
148- this . localCopyGroupNames = renameArrayElement ( this . localCopyGroupNames , event . oldName , event . newName ) ;
149180 this . localCopyTeamGroups [ event . newName ] = this . localCopyTeamGroups [ event . oldName ] || [ ] ;
150181 delete this . localCopyTeamGroups [ event . oldName ] ;
151182 }
152183
153- onDeleteGroup ( groupId : string ) {
154- delete this . localCopyTeamGroups [ groupId ] ;
155- this . localCopyGroupNames = this . localCopyGroupNames . filter ( group => group !== groupId ) ;
184+ onDeleteGroup ( group : string ) {
185+ delete this . localCopyTeamGroups [ group ] ;
156186 }
157187
158188 onSave ( ) {
@@ -167,28 +197,41 @@ export class TeamsGroupsEditorComponent {
167197 }
168198
169199 this . editMode = EditMode . NONE ;
170- this . highlightedTeams = [ ] ;
171- this . highlightedGroups = [ ] ;
172- this . selectedTeamId = null ;
173- this . selectedGroupId = null ;
174200
175201 // Copy the local copy to the main data
176202 this . saveLocalCopy ( ) ;
177203
178- this . changed . emit (
204+ this . namesChanged . emit (
179205 { teams : this . teams , teamGroups : this . teamGroups , teamsRenamed : teamsRenamed }
180206 ) ;
181207 }
182208
183209 onCancelEdit ( ) {
184210 console . log ( `${ perfNow ( ) } : Cancel editing teams and groups` ) ;
185- this . editMode = EditMode . NONE ;
186- this . selectedTeamId = null ;
187- this . selectedGroupId = null ;
188- this . highlightedTeams = [ ] ;
189- this . highlightedGroups = [ ] ;
190211
191- // Make a _new_ local copy of the original values
212+ // Recreate the local copy from original values
192213 this . makeLocalCopy ( ) ;
214+
215+ // Re-select and highlight the original values
216+ if ( this . editMode === EditMode . TEAMS ) {
217+ this . onTeamSelected ( this . selectedTeam || '' ) ;
218+ } else if ( this . editMode === EditMode . GROUPS ) {
219+ this . onGroupSelected ( this . selectedGroup || '' ) ;
220+ }
221+ this . editMode = EditMode . NONE ;
222+ }
223+
224+ findNextName ( existingNames : string [ ] , prefix : string ) : string {
225+ let i : number = existingNames . length + 1 ;
226+ let newName : string | null = null ;
227+ while ( newName == null || existingNames . includes ( newName ) ) {
228+ newName = `${ prefix } ${ i } ` ;
229+ i ++ ;
230+ }
231+ return newName ;
232+ }
233+
234+ keys ( obj : any ) : string [ ] {
235+ return Object . keys ( obj ) ;
193236 }
194237}
0 commit comments