Skip to content

Commit f1dfd0c

Browse files
authored
Merge pull request DSpace#1992 from atmire/theme-name-as-data-attr-7.5-next
Make it easier to know which theme is being used for a component
2 parents 34e60df + 7ede96b commit f1dfd0c

2 files changed

Lines changed: 47 additions & 2 deletions

File tree

src/app/shared/theme-support/themed.component.spec.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ describe('ThemedComponent', () => {
7171
expect((component as any).compRef.instance.testInput).toEqual('changed');
7272
});
7373
}));
74+
75+
it(`should set usedTheme to the name of the matched theme`, waitForAsync(() => {
76+
fixture.whenStable().then(() => {
77+
expect(component.usedTheme).toEqual('custom');
78+
});
79+
}));
7480
});
7581

7682
describe('when the current theme doesn\'t match a themed component', () => {
@@ -92,6 +98,12 @@ describe('ThemedComponent', () => {
9298
expect((component as any).compRef.instance.testInput).toEqual('changed');
9399
});
94100
}));
101+
102+
it(`should set usedTheme to the name of the base theme`, waitForAsync(() => {
103+
fixture.whenStable().then(() => {
104+
expect(component.usedTheme).toEqual('base');
105+
});
106+
}));
95107
});
96108

97109
describe('and it extends another theme', () => {
@@ -117,6 +129,12 @@ describe('ThemedComponent', () => {
117129
expect((component as any).compRef.instance.testInput).toEqual('changed');
118130
});
119131
}));
132+
133+
it(`should set usedTheme to the name of the base theme`, waitForAsync(() => {
134+
fixture.whenStable().then(() => {
135+
expect(component.usedTheme).toEqual('base');
136+
});
137+
}));
120138
});
121139

122140
describe('that does match it', () => {
@@ -141,6 +159,12 @@ describe('ThemedComponent', () => {
141159
expect((component as any).compRef.instance.testInput).toEqual('changed');
142160
});
143161
}));
162+
163+
it(`should set usedTheme to the name of the matched theme`, waitForAsync(() => {
164+
fixture.whenStable().then(() => {
165+
expect(component.usedTheme).toEqual('custom');
166+
});
167+
}));
144168
});
145169

146170
describe('that extends another theme that doesn\'t match it either', () => {
@@ -167,6 +191,12 @@ describe('ThemedComponent', () => {
167191
expect((component as any).compRef.instance.testInput).toEqual('changed');
168192
});
169193
}));
194+
195+
it(`should set usedTheme to the name of the base theme`, waitForAsync(() => {
196+
fixture.whenStable().then(() => {
197+
expect(component.usedTheme).toEqual('base');
198+
});
199+
}));
170200
});
171201

172202
describe('that extends another theme that does match it', () => {
@@ -193,6 +223,12 @@ describe('ThemedComponent', () => {
193223
expect((component as any).compRef.instance.testInput).toEqual('changed');
194224
});
195225
}));
226+
227+
it(`should set usedTheme to the name of the matched theme`, waitForAsync(() => {
228+
fixture.whenStable().then(() => {
229+
expect(component.usedTheme).toEqual('custom');
230+
});
231+
}));
196232
});
197233
});
198234
});

src/app/shared/theme-support/themed.component.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ import {
88
OnDestroy,
99
ComponentFactoryResolver,
1010
ChangeDetectorRef,
11-
OnChanges
11+
OnChanges,
12+
HostBinding
1213
} from '@angular/core';
1314
import { hasValue, isNotEmpty } from '../empty.util';
1415
import { from as fromPromise, Observable, of as observableOf, Subscription, BehaviorSubject } from 'rxjs';
1516
import { ThemeService } from './theme.service';
16-
import { catchError, switchMap, map } from 'rxjs/operators';
17+
import { catchError, switchMap, map, tap } from 'rxjs/operators';
1718
import { GenericConstructor } from '../../core/shared/generic-constructor';
19+
import { BASE_THEME_NAME } from './theme.constants';
1820

1921
@Component({
2022
selector: 'ds-themed',
@@ -36,6 +38,11 @@ export abstract class ThemedComponent<T> implements OnInit, OnDestroy, OnChanges
3638

3739
protected inAndOutputNames: (keyof T & keyof this)[] = [];
3840

41+
/**
42+
* A data attribute on the ThemedComponent to indicate which theme the rendered component came from.
43+
*/
44+
@HostBinding('attr.data-used-theme') usedTheme: string;
45+
3946
constructor(
4047
protected resolver: ComponentFactoryResolver,
4148
protected cdr: ChangeDetectorRef,
@@ -86,6 +93,7 @@ export abstract class ThemedComponent<T> implements OnInit, OnDestroy, OnChanges
8693
} else {
8794
// otherwise import and return the default component
8895
return fromPromise(this.importUnthemedComponent()).pipe(
96+
tap(() => this.usedTheme = BASE_THEME_NAME),
8997
map((unthemedFile: any) => {
9098
return unthemedFile[this.getComponentName()];
9199
})
@@ -130,6 +138,7 @@ export abstract class ThemedComponent<T> implements OnInit, OnDestroy, OnChanges
130138
private resolveThemedComponent(themeName?: string, checkedThemeNames: string[] = []): Observable<any> {
131139
if (isNotEmpty(themeName)) {
132140
return fromPromise(this.importThemedComponent(themeName)).pipe(
141+
tap(() => this.usedTheme = themeName),
133142
catchError(() => {
134143
// Try the next ancestor theme instead
135144
const nextTheme = this.themeService.getThemeConfigFor(themeName)?.extends;

0 commit comments

Comments
 (0)