1
1
import getSharedResource from "../getSharedResource.js" ;
2
2
import type { I18nText } from "../i18nBundle.js" ;
3
+ import { getTheme } from "../config/Theme.js" ;
3
4
4
5
type IllustrationLoader = ( illustrationName : string ) => Promise < IllustrationData > ;
5
6
@@ -13,14 +14,69 @@ type IllustrationProperties = {
13
14
14
15
type IllustrationData = IllustrationProperties & {
15
16
set : string ,
17
+ collection : string ,
16
18
} ;
17
19
20
+ enum IllustrationCollections {
21
+ "sap_horizon" = "V5" ,
22
+ "sap_horizon_dark" = "V5" ,
23
+ "sap_horizon_hcb" = "V5/HC" ,
24
+ "sap_horizon_hcw" = "V5/HC" ,
25
+ }
26
+
27
+ const FALLBACK_COLLECTION = "V4" ;
28
+
18
29
const loaders = new Map < string , IllustrationLoader > ( ) ;
19
30
const registry = getSharedResource < Map < string , IllustrationProperties > > ( "SVGIllustration.registry" , new Map ( ) ) ;
20
31
const illustrationPromises = getSharedResource < Map < string , Promise < IllustrationData > > > ( "SVGIllustration.promises" , new Map ( ) ) ;
21
32
33
+ const getCollection = ( ) => {
34
+ const theme = getTheme ( ) as keyof typeof IllustrationCollections ;
35
+
36
+ if ( IllustrationCollections [ theme ] ) {
37
+ return IllustrationCollections [ theme ] ;
38
+ }
39
+
40
+ return FALLBACK_COLLECTION ;
41
+ } ;
42
+
43
+ /**
44
+ * Processes the name of the illustration
45
+ * The name is used to generate the registry key and the loader key
46
+ * The registry key is used to store and get the illustration data from the registry
47
+ * The loader key is used to store and get the illustration loader from the loaders map
48
+ * The function generates the correct registry key and loader key based on whether an loader exists for the illustration
49
+ * If there is no loader registered for the collection, it falls back to the default collection
50
+ */
51
+ const processName = ( name : string ) => {
52
+ let collection = getCollection ( ) ;
53
+ const isTnt = name . startsWith ( "Tnt" ) ;
54
+ const set = isTnt ? "tnt" : "fiori" ;
55
+
56
+ let registryKey = `${ set } /${ collection } /${ name } ` ;
57
+ let loaderKey = `${ collection } /${ name } ` ;
58
+
59
+ if ( ! loaders . has ( loaderKey ) && collection !== FALLBACK_COLLECTION ) {
60
+ collection = FALLBACK_COLLECTION ;
61
+ loaderKey = `${ collection } /${ name } ` ;
62
+ registryKey = `${ set } /${ collection } /${ name } ` ;
63
+ }
64
+
65
+ if ( isTnt ) {
66
+ name = name . replace ( / ^ T n t / , "" ) ;
67
+ registryKey = `${ set } /${ collection } /${ name } ` ;
68
+ }
69
+
70
+ return {
71
+ registryKey,
72
+ loaderKey,
73
+ collection,
74
+ } ;
75
+ } ;
76
+
22
77
const registerIllustration = ( name : string , data : IllustrationData ) => {
23
- registry . set ( `${ data . set } /${ name } ` , {
78
+ const collection = data . collection || FALLBACK_COLLECTION ;
79
+ registry . set ( `${ data . set } /${ collection } /${ name } ` , {
24
80
dialogSvg : data . dialogSvg ,
25
81
sceneSvg : data . sceneSvg ,
26
82
spotSvg : data . spotSvg ,
@@ -33,38 +89,31 @@ const registerIllustrationLoader = (illustrationName: string, loader: Illustrati
33
89
loaders . set ( illustrationName , loader ) ;
34
90
} ;
35
91
36
- const _loadIllustrationOnce = async ( illustrationName : string ) => {
37
- if ( ! illustrationPromises . has ( illustrationName ) ) {
38
- if ( ! loaders . has ( illustrationName ) ) {
92
+ const _loadIllustrationOnce = ( illustrationName : string ) => {
93
+ const { loaderKey } = processName ( illustrationName ) ;
94
+
95
+ if ( ! illustrationPromises . has ( loaderKey ) ) {
96
+ if ( ! loaders . has ( loaderKey ) ) {
39
97
const illustrationPath = illustrationName . startsWith ( "Tnt" ) ? `tnt/${ illustrationName . replace ( / ^ T n t / , "" ) } ` : illustrationName ;
40
98
throw new Error ( `No loader registered for the ${ illustrationName } illustration. Probably you forgot to import the "@ui5/webcomponents-fiori/dist/illustrations/${ illustrationPath } .js" module. Or you can import the "@ui5/webcomponents-fiori/dist/illustrations/AllIllustrations.js" module that will make all illustrations available, but fetch only the ones used.` ) ;
41
99
}
42
100
43
- const loadIllustrations = loaders . get ( illustrationName ) ! ;
44
- illustrationPromises . set ( illustrationName , loadIllustrations ( illustrationName ) ) ;
101
+ const loadIllustrations = loaders . get ( loaderKey ) ! ;
102
+ illustrationPromises . set ( loaderKey , loadIllustrations ( loaderKey ) ) ;
45
103
}
46
- return illustrationPromises . get ( illustrationName ) ;
104
+ return illustrationPromises . get ( loaderKey ) ;
47
105
} ;
48
106
49
107
const getIllustrationDataSync = ( illustrationName : string ) => {
50
- let set = "fiori" ;
51
-
52
- if ( illustrationName . startsWith ( "Tnt" ) ) {
53
- set = "tnt" ;
54
- illustrationName = illustrationName . replace ( / ^ T n t / , "" ) ;
55
- }
56
- return registry . get ( `${ set } /${ illustrationName } ` ) ;
108
+ const { registryKey } = processName ( illustrationName ) ;
109
+ return registry . get ( registryKey ) ;
57
110
} ;
58
111
59
112
const getIllustrationData = async ( illustrationName : string ) => {
60
- let set = "fiori" ;
113
+ const { registryKey } = processName ( illustrationName ) ;
61
114
62
115
await _loadIllustrationOnce ( illustrationName ) ;
63
- if ( illustrationName . startsWith ( "Tnt" ) ) {
64
- set = "tnt" ;
65
- illustrationName = illustrationName . replace ( / ^ T n t / , "" ) ;
66
- }
67
- return registry . get ( `${ set } /${ illustrationName } ` ) ;
116
+ return registry . get ( registryKey ) ;
68
117
} ;
69
118
70
119
export {
0 commit comments