Skip to content

Commit ca6f87d

Browse files
committed
feat(core): Allow changing name for queryParamName directive.
This commit introduced support for dynamically changing the query parameter to which a queryParamName directive refers. relates #44 Signed-off-by: Ingo Bürk <ingo.buerk@tngtech.com>
1 parent 5c76c67 commit ca6f87d

2 files changed

Lines changed: 24 additions & 20 deletions

File tree

projects/ngqp/core/src/lib/directives/query-param-group.service.ts

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export class QueryParamGroupService implements OnDestroy {
4545
private queryParamGroup: QueryParamGroup;
4646

4747
/** List of {@link QueryParamNameDirective} directives registered to this service. */
48-
private directives: QueryParamNameDirective[] = [];
48+
private directives = new Map<string, QueryParamNameDirective[]>();
4949

5050
/**
5151
* Queue of navigation parameters
@@ -120,23 +120,29 @@ export class QueryParamGroupService implements OnDestroy {
120120

121121
directive.valueAccessor.registerOnChange((newValue: any) => debouncedQueue$.next(newValue));
122122

123-
this.directives.push(directive);
123+
this.directives.set(directive.name, [...(this.directives.get(directive.name) || []), directive]);
124124
}
125125

126126
/**
127-
* Deregisters a {@link QueryParamNameDirective} directive.
127+
* Deregisters a {@link QueryParamNameDirective} directive by referencing its name.
128128
*/
129-
public deregisterQueryParamDirective(directive: QueryParamNameDirective): void {
130-
const index = this.directives.indexOf(directive);
131-
if (index === -1) {
129+
public deregisterQueryParamDirective(queryParamName: string): void {
130+
if (!queryParamName) {
132131
return;
133132
}
134133

135-
this.directives.splice(index, 1);
136-
directive.valueAccessor.registerOnChange(NOP);
137-
directive.valueAccessor.registerOnTouched(NOP);
134+
const directives = this.directives.get(queryParamName);
135+
if (!directives) {
136+
return;
137+
}
138138

139-
const queryParam: QueryParam<any> = this.queryParamGroup.get(directive.name);
139+
directives.forEach(directive => {
140+
directive.valueAccessor.registerOnChange(NOP);
141+
directive.valueAccessor.registerOnTouched(NOP);
142+
});
143+
144+
this.directives.delete(queryParamName);
145+
const queryParam: QueryParam<any> = this.queryParamGroup.get(queryParamName);
140146
if (queryParam) {
141147
queryParam._clearChangeFunctions();
142148
}
@@ -196,9 +202,10 @@ export class QueryParamGroupService implements OnDestroy {
196202
? this.deserialize(queryParam, queryParamMap.getAll(queryParam.urlParam))
197203
: this.deserialize(queryParam, queryParamMap.get(queryParam.urlParam));
198204

199-
this.directives
200-
.filter(directive => directive.name === queryParamName)
201-
.forEach(directive => directive.valueAccessor.writeValue(newValue));
205+
const directives = this.directives.get(queryParamName);
206+
if (directives) {
207+
directives.forEach(directive => directive.valueAccessor.writeValue(newValue));
208+
}
202209

203210
groupValue[ queryParamName ] = newValue;
204211
});

projects/ngqp/core/src/lib/directives/query-param-name.directive.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,22 +42,19 @@ export class QueryParamNameDirective implements OnChanges, OnDestroy {
4242
const nameChange = changes['name'];
4343
if (nameChange) {
4444
if (!nameChange.firstChange) {
45-
throw new Error(`You tried to switch from queryParamName=${nameChange.previousValue} to queryParamName=${nameChange.currentValue} which is currently not supported.`);
45+
this.groupService.deregisterQueryParamDirective(nameChange.previousValue);
4646
}
4747

48-
const name = nameChange.currentValue;
49-
if (!name) {
50-
throw new Error(`queryParamName has been added, but without specifying the name.`);
48+
if (nameChange.currentValue) {
49+
this.groupService.registerQueryParamDirective(this);
5150
}
52-
53-
this.groupService.registerQueryParamDirective(this);
5451
}
5552
}
5653

5754
/** @ignore */
5855
public ngOnDestroy() {
5956
if (this.groupService) {
60-
this.groupService.deregisterQueryParamDirective(this);
57+
this.groupService.deregisterQueryParamDirective(this.name);
6158
}
6259
}
6360

0 commit comments

Comments
 (0)