Skip to content

Commit 13da7d8

Browse files
DSC-1413 refactor
1 parent 1e34198 commit 13da7d8

8 files changed

Lines changed: 62 additions & 60 deletions

File tree

src/app/shared/cookies/browser-klaro.service.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ export class BrowserKlaroService extends KlaroService {
106106
this.klaroConfig.translations.zz.consentNotice.description = 'cookies.consent.content-notice.description.no-privacy';
107107
}
108108

109-
if (hasValue(environment.metricsConsents)) {
110-
environment.metricsConsents.forEach((metric) => {
109+
if (hasValue(environment.info.metricsConsents)) {
110+
environment.info.metricsConsents.forEach((metric) => {
111111
if (metric.enabled) {
112112
this.klaroConfig.services.push(
113113
{
@@ -357,7 +357,7 @@ export class BrowserKlaroService extends KlaroService {
357357
return 'klaro-' + identifier;
358358
}
359359

360-
watchConsentUpdates() {
360+
watchConsentUpdates(): void {
361361
this.lazyKlaro.then(({getManager}) => {
362362
const manager = getManager(this.klaroConfig);
363363
const consentsSubject$ = this.consentsUpdates$;
Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Injectable } from '@angular/core';
22

3-
import { Observable } from 'rxjs';
3+
import { BehaviorSubject, Observable } from 'rxjs';
4+
import { CookieConsents } from './browser-klaro.service';
45

56
/**
67
* Abstract class representing a service for handling Klaro consent preferences and UI
@@ -10,15 +11,25 @@ export abstract class KlaroService {
1011
/**
1112
* Initializes the service
1213
*/
13-
abstract initialize();
14+
abstract initialize(): void;
1415

1516
/**
1617
* Shows a dialog with the current consent preferences
1718
*/
18-
abstract showSettings();
19+
abstract showSettings(): void;
1920

2021
/**
2122
* Return saved preferences stored in the klaro cookie
2223
*/
2324
abstract getSavedPreferences(): Observable<any>;
25+
26+
/**
27+
* Watch for changes in consents
28+
*/
29+
abstract watchConsentUpdates(): void;
30+
31+
/**
32+
* Subject to emit updates in the consents
33+
*/
34+
abstract consentsUpdates$: BehaviorSubject<CookieConsents>;
2435
}

src/app/shared/metric/metric-loader/metric-loader.component.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ import { Metric } from '../../../core/shared/metric.model';
1414
import { BaseMetricComponent } from './base-metric.component';
1515
import { MetricLoaderService } from './metric-loader.service';
1616
import { hasValue } from '../../empty.util';
17-
import { BrowserKlaroService, CookieConsents } from '../../cookies/browser-klaro.service';
17+
import { CookieConsents } from '../../cookies/browser-klaro.service';
1818
import { KlaroService } from '../../cookies/klaro.service';
19-
import { startWith } from 'rxjs/operators';
19+
import { startWith } from 'rxjs/operators';
2020

2121
@Component({
2222
// eslint-disable-next-line @angular-eslint/component-selector
@@ -50,22 +50,19 @@ export class MetricLoaderComponent implements OnInit, OnDestroy {
5050

5151
private thirdPartyMetrics = ['plumX', 'altmetric', 'dimensions'];
5252

53-
private browserKlaroService: BrowserKlaroService;
54-
5553
private hasLoadedScript: boolean;
5654

5755
constructor(
5856
private componentFactoryResolver: ComponentFactoryResolver,
5957
private metricLoaderService: MetricLoaderService,
6058
private klaroService: KlaroService,
6159
) {
62-
this.browserKlaroService = (this.klaroService as BrowserKlaroService);
63-
this.browserKlaroService.watchConsentUpdates();
64-
this.consentUpdates$ = this.browserKlaroService.consentsUpdates$;
60+
this.klaroService.watchConsentUpdates();
61+
this.consentUpdates$ = this.klaroService.consentsUpdates$;
6562
}
6663

6764
ngOnInit() {
68-
this.cookiesSubscription = this.browserKlaroService.getSavedPreferences().subscribe((consents) => {
65+
this.cookiesSubscription = this.klaroService.getSavedPreferences().subscribe((consents) => {
6966
this.loadComponent(this.metric, this.getCanLoadScript(consents));
7067
});
7168
}
@@ -96,6 +93,8 @@ export class MetricLoaderComponent implements OnInit, OnDestroy {
9693
instantiateComponent(component: any, metric: Metric, canLoadScript: boolean, forceRendering?: boolean) {
9794
const factory = this.componentFactoryResolver.resolveComponentFactory(component);
9895
this.componentType = component;
96+
this.container.clear();
97+
9998
const ref = this.container.createComponent(factory);
10099
const componentInstance = ref.instance as BaseMetricComponent;
101100
componentInstance.metric = metric;
@@ -133,14 +132,18 @@ export class MetricLoaderComponent implements OnInit, OnDestroy {
133132
* @private
134133
*/
135134
private reloadComponentOnConsentsChange(componentInstance: BaseMetricComponent, canLoadScript: boolean): void {
135+
if (hasValue(this.settingsSubscription)) {
136+
return;
137+
}
138+
136139
this.settingsSubscription = combineLatest([
137140
this.consentUpdates$,
138141
componentInstance.requestSettingsConsent.pipe(startWith(undefined))
139142
]).subscribe(([consents, request]) => {
140143
canLoadScript = this.getCanLoadScript(consents);
141144

142145
if (request && !canLoadScript) {
143-
this.browserKlaroService.showSettings();
146+
this.klaroService.showSettings();
144147
}
145148

146149
if (canLoadScript && !this.hasLoadedScript) {

src/app/shared/metric/metrics.module.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import { MetricBadgesComponent } from '../object-list/metric-badges/metric-badge
1919
import { MetricDonutsComponent } from '../object-list/metric-donuts/metric-donuts.component';
2020
import { FormsModule } from '@angular/forms';
2121
import { DirectivesModule } from '../../directives/directives.module';
22-
import { BrowserKlaroService } from '../cookies/browser-klaro.service';
2322

2423
const PIPES = [
2524
MetricStyleConfigPipe,
@@ -45,11 +44,6 @@ const COMPONENTS = [
4544
MetricLoaderComponent,
4645
MetricDefaultComponent
4746
];
48-
49-
const PROVIDERS = [
50-
BrowserKlaroService
51-
];
52-
5347
@NgModule({
5448
declarations: [
5549
...PIPES,
@@ -62,9 +56,6 @@ const PROVIDERS = [
6256
useDefaultLang: true
6357
}),
6458
],
65-
providers: [
66-
...PROVIDERS
67-
],
6859
exports: [
6960
...COMPONENTS,
7061
]

src/config/app-config.interface.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import { FollowAuthorityMetadata } from './search-follow-metadata.interface';
3131
import { AdvancedAttachmentRenderingConfig } from './advanced-attachment-rendering.config';
3232
import { AttachmentRenderingConfig } from './attachment-rendering.config';
3333
import { SearchResultConfig } from './search-result-config.interface';
34-
import { ThirdPartyMetric } from './third-party-metric-config';
3534

3635
interface AppConfig extends Config {
3736
ui: UIServerConfig;
@@ -69,7 +68,6 @@ interface AppConfig extends Config {
6968
attachmentRendering: AttachmentRenderingConfig;
7069
advancedAttachmentRendering: AdvancedAttachmentRenderingConfig;
7170
searchResult: SearchResultConfig;
72-
metricsConsents: ThirdPartyMetric[];
7371
}
7472

7573
/**

src/config/default-app-config.ts

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ import {
3434
} from './advanced-attachment-rendering.config';
3535
import { AttachmentRenderingConfig } from './attachment-rendering.config';
3636
import { SearchResultConfig } from './search-result-config.interface';
37-
import { ThirdPartyMetric } from './third-party-metric-config';
3837

3938
export class DefaultAppConfig implements AppConfig {
4039
production = false;
@@ -481,7 +480,22 @@ export class DefaultAppConfig implements AppConfig {
481480
// - All mentions of the privacy policy being removed from the UI (e.g. in the footer)
482481
info: InfoConfig = {
483482
enableEndUserAgreement: true,
484-
enablePrivacyStatement: true
483+
enablePrivacyStatement: true,
484+
//Configuration for third-party metrics in Klaro
485+
metricsConsents: [
486+
{
487+
key: 'plumX',
488+
enabled: true
489+
},
490+
{
491+
key: 'altmetric',
492+
enabled: true
493+
},
494+
{
495+
key: 'dimensions',
496+
enabled: true
497+
},
498+
]
485499
};
486500

487501
// Whether to enable Markdown (https://commonmark.org/) and MathJax (https://www.mathjax.org/)
@@ -736,20 +750,4 @@ export class DefaultAppConfig implements AppConfig {
736750
additionalMetadataFields: [],
737751
authorMetadata: ['dc.contributor.author', 'dc.creator', 'dc.contributor.*'],
738752
};
739-
740-
//Configuration for third-party metrics in Klaro
741-
metricsConsents: ThirdPartyMetric[] = [
742-
{
743-
key: 'plumX',
744-
enabled: true
745-
},
746-
{
747-
key: 'altmetric',
748-
enabled: true
749-
},
750-
{
751-
key: 'dimensions',
752-
enabled: true
753-
},
754-
];
755753
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { Config } from './config.interface';
2+
import { ThirdPartyMetric } from './third-party-metric-config';
23

34
export interface InfoConfig extends Config {
45
enableEndUserAgreement: boolean;
56
enablePrivacyStatement: boolean;
7+
metricsConsents: ThirdPartyMetric[];
68
}

src/environments/environment.test.ts

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,21 @@ export const environment: BuildConfig = {
323323
info: {
324324
enableEndUserAgreement: true,
325325
enablePrivacyStatement: true,
326+
//Configuration for third-party metrics in Klaro
327+
metricsConsents: [
328+
{
329+
key: 'plumX',
330+
enabled: true
331+
},
332+
{
333+
key: 'altmetric',
334+
enabled: true
335+
},
336+
{
337+
key: 'dimensions',
338+
enabled: true
339+
},
340+
]
326341
},
327342
markdown: {
328343
enabled: false,
@@ -553,20 +568,4 @@ export const environment: BuildConfig = {
553568
],
554569
authorMetadata: ['dc.contributor.author', 'dc.contributor.editor', 'dc.contributor.contributor', 'dc.creator'],
555570
},
556-
557-
//Configuration for third-party metrics in Klaro
558-
metricsConsents: [
559-
{
560-
key: 'plumX',
561-
enabled: true
562-
},
563-
{
564-
key: 'altmetric',
565-
enabled: true
566-
},
567-
{
568-
key: 'dimensions',
569-
enabled: true
570-
},
571-
],
572571
};

0 commit comments

Comments
 (0)