Skip to content

Commit 8305032

Browse files
authored
Merge pull request DSpace#1912 from atmire/Listable-object-decorator-refactor
Listable-object decorator refactor
2 parents e50c278 + 1dc6547 commit 8305032

2 files changed

Lines changed: 169 additions & 30 deletions

File tree

src/app/shared/object-collection/shared/listable-object/listable-object.decorator.spec.ts

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable max-classes-per-file */
22
import { Item } from '../../../../core/shared/item.model';
33
import { ViewMode } from '../../../../core/shared/view-mode.model';
4-
import { getListableObjectComponent, listableObjectComponent } from './listable-object.decorator';
4+
import { DEFAULT_VIEW_MODE, getListableObjectComponent, listableObjectComponent } from './listable-object.decorator';
55
import { Context } from '../../../../core/shared/context.model';
66
import { environment } from '../../../../../environments/environment';
77

@@ -13,6 +13,10 @@ describe('ListableObject decorator function', () => {
1313
const type3 = 'TestType3';
1414
const typeAncestor = 'TestTypeAncestor';
1515
const typeUnthemed = 'TestTypeUnthemed';
16+
const typeLowPriority = 'TypeLowPriority';
17+
const typeLowPriority2 = 'TypeLowPriority2';
18+
const typeMidPriority = 'TypeMidPriority';
19+
const typeHighPriority = 'TypeHighPriority';
1620

1721
class Test1List {
1822
}
@@ -38,6 +42,21 @@ describe('ListableObject decorator function', () => {
3842
class TestUnthemedComponent {
3943
}
4044

45+
class TestDefaultLowPriorityComponent {
46+
}
47+
48+
class TestLowPriorityComponent {
49+
}
50+
51+
class TestDefaultMidPriorityComponent {
52+
}
53+
54+
class TestMidPriorityComponent {
55+
}
56+
57+
class TestHighPriorityComponent {
58+
}
59+
4160
/* eslint-enable max-classes-per-file */
4261

4362
beforeEach(() => {
@@ -54,6 +73,15 @@ describe('ListableObject decorator function', () => {
5473
listableObjectComponent(typeAncestor, ViewMode.ListElement, Context.Any, 'ancestor')(TestAncestorComponent);
5574
listableObjectComponent(typeUnthemed, ViewMode.ListElement, Context.Any)(TestUnthemedComponent);
5675

76+
// Register component with different priorities for expected parameters:
77+
// ViewMode.DetailedListElement, Context.Search, 'custom'
78+
listableObjectComponent(typeLowPriority, DEFAULT_VIEW_MODE, undefined, undefined)(TestDefaultLowPriorityComponent);
79+
listableObjectComponent(typeLowPriority, DEFAULT_VIEW_MODE, Context.Search, 'custom')(TestLowPriorityComponent);
80+
listableObjectComponent(typeLowPriority2, DEFAULT_VIEW_MODE, undefined, undefined)(TestDefaultLowPriorityComponent);
81+
listableObjectComponent(typeMidPriority, ViewMode.DetailedListElement, undefined, undefined)(TestDefaultMidPriorityComponent);
82+
listableObjectComponent(typeMidPriority, ViewMode.DetailedListElement, undefined, 'custom')(TestMidPriorityComponent);
83+
listableObjectComponent(typeHighPriority, ViewMode.DetailedListElement, Context.Search, undefined)(TestHighPriorityComponent);
84+
5785
ogEnvironmentThemes = environment.themes;
5886
});
5987

@@ -81,7 +109,7 @@ describe('ListableObject decorator function', () => {
81109
});
82110
});
83111

84-
describe('If there isn\'nt an exact match', () => {
112+
describe('If there isn\'t an exact match', () => {
85113
describe('If there is a match for one of the entity types and the view mode', () => {
86114
it('should return the class with the matching entity type and view mode and default context', () => {
87115
const component = getListableObjectComponent([type3], ViewMode.ListElement, Context.Workspace);
@@ -152,4 +180,45 @@ describe('ListableObject decorator function', () => {
152180
});
153181
});
154182
});
183+
184+
describe('priorities', () => {
185+
beforeEach(() => {
186+
environment.themes = [
187+
{
188+
name: 'custom',
189+
}
190+
];
191+
});
192+
193+
describe('If a component with default ViewMode contains specific context and/or theme', () => {
194+
it('requesting a specific ViewMode should return the one with the requested context and/or theme', () => {
195+
const component = getListableObjectComponent([typeLowPriority], ViewMode.DetailedListElement, Context.Search, 'custom');
196+
expect(component).toEqual(TestLowPriorityComponent);
197+
});
198+
});
199+
200+
describe('If a component with default Context contains specific ViewMode and/or theme', () => {
201+
it('requesting a specific Context should return the one with the requested view-mode and/or theme', () => {
202+
const component = getListableObjectComponent([typeMidPriority], ViewMode.DetailedListElement, Context.Search, 'custom');
203+
expect(component).toEqual(TestMidPriorityComponent);
204+
});
205+
});
206+
207+
describe('If multiple components exist, each containing a different default value for one of the requested parameters', () => {
208+
it('the component with the latest default value in the list should be returned', () => {
209+
let component = getListableObjectComponent([typeMidPriority, typeLowPriority], ViewMode.DetailedListElement, Context.Search, 'custom');
210+
expect(component).toEqual(TestMidPriorityComponent);
211+
212+
component = getListableObjectComponent([typeLowPriority, typeMidPriority, typeHighPriority], ViewMode.DetailedListElement, Context.Search, 'custom');
213+
expect(component).toEqual(TestHighPriorityComponent);
214+
});
215+
});
216+
217+
describe('If two components exist for two different types, both configured for the same view-mode, but one for a specific context and/or theme', () => {
218+
it('requesting a component for that specific context and/or theme while providing both types should return the most relevant one', () => {
219+
const component = getListableObjectComponent([typeLowPriority2, typeLowPriority], ViewMode.DetailedListElement, Context.Search, 'custom');
220+
expect(component).toEqual(TestLowPriorityComponent);
221+
});
222+
});
223+
});
155224
});

src/app/shared/object-collection/shared/listable-object/listable-object.decorator.ts

Lines changed: 98 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,53 @@ export const DEFAULT_VIEW_MODE = ViewMode.ListElement;
1111
export const DEFAULT_CONTEXT = Context.Any;
1212
export const DEFAULT_THEME = '*';
1313

14+
/**
15+
* A class used to compare two matches and their relevancy to determine which of the two gains priority over the other
16+
*
17+
* "level" represents the index of the first default value that was used to find the match with:
18+
* ViewMode being index 0, Context index 1 and theme index 2. Examples:
19+
* - If a default value was used for context, but not view-mode and theme, the "level" will be 1
20+
* - If a default value was used for view-mode and context, but not for theme, the "level" will be 0
21+
* - If no default value was used for any of the fields, the "level" will be 3
22+
*
23+
* "relevancy" represents the amount of values that didn't require a default value to fall back on. Examples:
24+
* - If a default value was used for theme, but not view-mode and context, the "relevancy" will be 2
25+
* - If a default value was used for view-mode and context, but not for theme, the "relevancy" will be 1
26+
* - If a default value was used for all fields, the "relevancy" will be 0
27+
* - If no default value was used for any of the fields, the "relevancy" will be 3
28+
*
29+
* To determine which of two MatchRelevancies is the most relevant, we compare "level" and "relevancy" in that order.
30+
* If any of the two is higher than the other, that match is most relevant. Examples:
31+
* - { level: 1, relevancy: 1 } is more relevant than { level: 0, relevancy: 2 }
32+
* - { level: 1, relevancy: 1 } is less relevant than { level: 1, relevancy: 2 }
33+
* - { level: 1, relevancy: 1 } is more relevant than { level: 1, relevancy: 0 }
34+
* - { level: 1, relevancy: 1 } is less relevant than { level: 2, relevancy: 0 }
35+
* - { level: 1, relevancy: 1 } is more relevant than null
36+
*/
37+
class MatchRelevancy {
38+
constructor(public match: any,
39+
public level: number,
40+
public relevancy: number) {
41+
}
42+
43+
isMoreRelevantThan(otherMatch: MatchRelevancy): boolean {
44+
if (hasNoValue(otherMatch)) {
45+
return true;
46+
}
47+
if (otherMatch.level > this.level) {
48+
return false;
49+
}
50+
if (otherMatch.level === this.level && otherMatch.relevancy > this.relevancy) {
51+
return false;
52+
}
53+
return true;
54+
}
55+
56+
isLessRelevantThan(otherMatch: MatchRelevancy): boolean {
57+
return !this.isMoreRelevantThan(otherMatch);
58+
}
59+
}
60+
1461
/**
1562
* Factory to allow us to inject getThemeConfigFor so we can mock it in tests
1663
*/
@@ -48,47 +95,70 @@ export function listableObjectComponent(objectType: string | GenericConstructor<
4895

4996
/**
5097
* Getter to retrieve the matching listable object component
98+
*
99+
* Looping over the provided types, it'll attempt to find the best match depending on the {@link MatchRelevancy} returned by getMatch()
100+
* The most relevant match between types is kept and eventually returned
101+
*
51102
* @param types The types of which one should match the listable component
52103
* @param viewMode The view mode that should match the components
53104
* @param context The context that should match the components
54105
* @param theme The theme that should match the components
55106
*/
56107
export function getListableObjectComponent(types: (string | GenericConstructor<ListableObject>)[], viewMode: ViewMode, context: Context = DEFAULT_CONTEXT, theme: string = DEFAULT_THEME) {
57-
let bestMatch;
58-
let bestMatchValue = 0;
108+
let currentBestMatch: MatchRelevancy = null;
59109
for (const type of types) {
60110
const typeMap = map.get(type);
61111
if (hasValue(typeMap)) {
62-
const typeModeMap = typeMap.get(viewMode);
63-
if (hasValue(typeModeMap)) {
64-
const contextMap = typeModeMap.get(context);
65-
if (hasValue(contextMap)) {
66-
const match = resolveTheme(contextMap, theme);
67-
if (hasValue(match)) {
68-
return match;
69-
}
70-
if (bestMatchValue < 3 && hasValue(contextMap.get(DEFAULT_THEME))) {
71-
bestMatchValue = 3;
72-
bestMatch = contextMap.get(DEFAULT_THEME);
73-
}
74-
}
75-
if (bestMatchValue < 2 &&
76-
hasValue(typeModeMap.get(DEFAULT_CONTEXT)) &&
77-
hasValue(typeModeMap.get(DEFAULT_CONTEXT).get(DEFAULT_THEME))) {
78-
bestMatchValue = 2;
79-
bestMatch = typeModeMap.get(DEFAULT_CONTEXT).get(DEFAULT_THEME);
80-
}
112+
const match = getMatch(typeMap, [viewMode, context, theme], [DEFAULT_VIEW_MODE, DEFAULT_CONTEXT, DEFAULT_THEME]);
113+
if (hasNoValue(currentBestMatch) || currentBestMatch.isLessRelevantThan(match)) {
114+
currentBestMatch = match;
115+
}
116+
}
117+
}
118+
return hasValue(currentBestMatch) ? currentBestMatch.match : null;
119+
}
120+
121+
/**
122+
* Find an object within a nested map, matching the provided keys as best as possible, falling back on defaults wherever
123+
* needed.
124+
*
125+
* Starting off with a Map, it loops over the provided keys, going deeper into the map until it finds a value
126+
* If at some point, no value is found, it'll attempt to use the default value for that index instead
127+
* If the default value exists, the index is stored in the "level"
128+
* If no default value exists, 1 is added to "relevancy"
129+
* See {@link MatchRelevancy} what these represent
130+
*
131+
* @param typeMap a multi-dimensional map
132+
* @param keys the keys of the multi-dimensional map to loop over. Each key represents a level within the map
133+
* @param defaults the default values to use for each level, in case no value is found for the key at that index
134+
* @returns matchAndLevel a {@link MatchRelevancy} object containing the match and its level of relevancy
135+
*/
136+
function getMatch(typeMap: Map<any, any>, keys: any[], defaults: any[]): MatchRelevancy {
137+
let currentMap = typeMap;
138+
let level = -1;
139+
let relevancy = 0;
140+
for (let i = 0; i < keys.length; i++) {
141+
// If we're currently checking the theme, resolve it first to take extended themes into account
142+
let currentMatch = defaults[i] === DEFAULT_THEME ? resolveTheme(currentMap, keys[i]) : currentMap.get(keys[i]);
143+
if (hasNoValue(currentMatch)) {
144+
currentMatch = currentMap.get(defaults[i]);
145+
if (level === -1) {
146+
level = i;
81147
}
82-
if (bestMatchValue < 1 &&
83-
hasValue(typeMap.get(DEFAULT_VIEW_MODE)) &&
84-
hasValue(typeMap.get(DEFAULT_VIEW_MODE).get(DEFAULT_CONTEXT)) &&
85-
hasValue(typeMap.get(DEFAULT_VIEW_MODE).get(DEFAULT_CONTEXT).get(DEFAULT_THEME))) {
86-
bestMatchValue = 1;
87-
bestMatch = typeMap.get(DEFAULT_VIEW_MODE).get(DEFAULT_CONTEXT).get(DEFAULT_THEME);
148+
} else {
149+
relevancy++;
150+
}
151+
if (hasValue(currentMatch)) {
152+
if (currentMatch instanceof Map) {
153+
currentMap = currentMatch as Map<any, any>;
154+
} else {
155+
return new MatchRelevancy(currentMatch, level > -1 ? level : i + 1, relevancy);
88156
}
157+
} else {
158+
return null;
89159
}
90160
}
91-
return bestMatch;
161+
return null;
92162
}
93163

94164
/**

0 commit comments

Comments
 (0)