-
Notifications
You must be signed in to change notification settings - Fork 22
/
grouping-context.service.ts
112 lines (97 loc) · 3.82 KB
/
grouping-context.service.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
import { Injectable, Injector } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { BehaviorSubject, Observable } from 'rxjs';
import { Group } from '../../models/github/group.interface';
import { Issue } from '../../models/issue.model';
import { AssigneeGroupingStrategy } from './assignee-grouping-strategy.service';
import { GroupingStrategy } from './grouping-strategy.interface';
import { MilestoneGroupingStrategy } from './milestone-grouping-strategy.service';
export enum GroupBy {
Assignee = 'assignee',
Milestone = 'milestone'
}
export const DEFAULT_GROUPBY = GroupBy.Assignee;
/**
* A service responsible for managing the current grouping strategy and providing grouped data.
*/
@Injectable({
providedIn: 'root'
})
export class GroupingContextService {
public static readonly GROUP_BY_QUERY_PARAM_KEY = 'groupby';
private currGroupBySubject: BehaviorSubject<GroupBy>;
currGroupBy: GroupBy;
currGroupBy$: Observable<GroupBy>;
private groupingStrategyMap: Map<string, GroupingStrategy>;
constructor(private injector: Injector, private route: ActivatedRoute, private router: Router) {
this.currGroupBy = DEFAULT_GROUPBY;
this.currGroupBySubject = new BehaviorSubject<GroupBy>(this.currGroupBy);
this.currGroupBy$ = this.currGroupBySubject.asObservable();
this.groupingStrategyMap = new Map<string, GroupingStrategy>();
// Initialize the grouping strategy map with available strategies
this.groupingStrategyMap.set(GroupBy.Assignee, this.injector.get(AssigneeGroupingStrategy));
this.groupingStrategyMap.set(GroupBy.Milestone, this.injector.get(MilestoneGroupingStrategy));
}
/**
* Initializes the service from URL parameters.
*/
initializeFromUrlParams() {
const groupByParam = this.route.snapshot.queryParamMap.get(GroupingContextService.GROUP_BY_QUERY_PARAM_KEY);
if (groupByParam && Object.values(GroupBy).includes(groupByParam as GroupBy)) {
this.setCurrentGroupingType(groupByParam as GroupBy);
} else {
this.setCurrentGroupingType(DEFAULT_GROUPBY);
}
}
/**
* Sets the current grouping type and updates the corresponding query parameter in the URL.
* @param groupBy The grouping type to set.
*/
setCurrentGroupingType(groupBy: GroupBy): void {
if (groupBy !== this.currGroupBy) {
this.currGroupBy = groupBy;
this.currGroupBySubject.next(this.currGroupBy);
}
this.router.navigate([], {
relativeTo: this.route,
queryParams: {
[GroupingContextService.GROUP_BY_QUERY_PARAM_KEY]: groupBy
},
queryParamsHandling: 'merge',
replaceUrl: true
});
}
/**
* Retrieves data for a specific group.
* @param issues - An array of issues to be grouped.
* @param group - The group by which issues are to be grouped.
* @returns An array of issues belonging to the specified group.
*/
getDataForGroup(issues: Issue[], group: Group): Issue[] {
const strategy = this.groupingStrategyMap.get(this.currGroupBy);
return strategy.getDataForGroup(issues, group);
}
/**
* Retrieves all groups available for current grouping strategy.
* @returns An Observable emitting an array of groups.
*/
getGroups(): Observable<Group[]> {
const strategy = this.groupingStrategyMap.get(this.currGroupBy);
return strategy.getGroups();
}
/**
* Determines whether a group should be shown on hidden list if it contains no issues.
* @param group - The group to check.
* @returns A boolean indicating whether the group should be shown on hidden list if empty.
*/
isInHiddenList(group: Group): boolean {
const strategy = this.groupingStrategyMap.get(this.currGroupBy);
return strategy.isInHiddenList(group);
}
/**
* Resets the current grouping type to the default.
*/
reset(): void {
this.setCurrentGroupingType(DEFAULT_GROUPBY);
}
}