File tree 4 files changed +49
-14
lines changed
4 files changed +49
-14
lines changed Original file line number Diff line number Diff line change 1
- const registry = new Map ( ) ;
2
- const iconCollectionPromises = new Map ( ) ;
1
+ import getSharedResource from "./getSharedResource.js" ;
2
+
3
+ const registry = getSharedResource ( "SVGIcons.registry" , new Map ( ) ) ;
4
+ const iconCollectionPromises = getSharedResource ( "SVGIcons.promises" , new Map ( ) ) ;
3
5
4
6
const ICON_NOT_FOUND = "ICON_NOT_FOUND" ;
5
7
const DEFAULT_COLLECTION = "SAP-icons" ;
Original file line number Diff line number Diff line change 1
- const getStaticAreaInstance = ( ) => {
2
- let staticArea = document . querySelector ( "ui5-static-area" ) ;
1
+ import getSingletonElementInstance from "./util/getSingletonElementInstance.js" ;
3
2
4
- if ( staticArea ) {
5
- return staticArea ;
6
- }
7
-
8
- // Create static area if it is not present
9
- const bodyElement = document . body ;
10
- staticArea = document . createElement ( "ui5-static-area" ) ;
11
-
12
- return bodyElement . insertBefore ( staticArea , bodyElement . firstChild ) ;
13
- } ;
3
+ const getStaticAreaInstance = ( ) => getSingletonElementInstance ( "ui5-static-area" ) ;
14
4
15
5
const removeStaticArea = ( ) => {
16
6
getStaticAreaInstance ( ) . destroy ( ) ;
Original file line number Diff line number Diff line change
1
+ import getSingletonElementInstance from "./util/getSingletonElementInstance.js" ;
2
+
3
+ const getSharedResourcesInstance = ( ) => getSingletonElementInstance ( "ui5-shared-resources" , document . head ) ;
4
+
5
+ /**
6
+ * Use this method to initialize/get resources that you would like to be shared among UI5 Web Components runtime instances.
7
+ * The data will be accessed via a singleton "ui5-shared-resources" HTML element in the "head" element of the page.
8
+ *
9
+ * @public
10
+ * @param namespace Unique ID of the resource, may contain "." to denote hierarchy
11
+ * @param initialValue Object or primitive that will be used as an initial value if the resource does not exist
12
+ * @returns {* }
13
+ */
14
+ const getSharedResource = ( namespace , initialValue ) => {
15
+ const parts = namespace . split ( "." ) ;
16
+ let current = getSharedResourcesInstance ( ) ;
17
+
18
+ for ( let i = 0 ; i < parts . length ; i ++ ) {
19
+ const part = parts [ i ] ;
20
+ const lastPart = i === parts . length - 1 ;
21
+ if ( ! Object . prototype . hasOwnProperty . call ( current , part ) ) {
22
+ current [ part ] = lastPart ? initialValue : { } ;
23
+ }
24
+ current = current [ part ] ;
25
+ }
26
+
27
+ return current ;
28
+ } ;
29
+
30
+ export default getSharedResource ;
Original file line number Diff line number Diff line change
1
+ const getSingletonElementInstance = ( tag , parentElement = document . body ) => {
2
+ let el = document . querySelector ( tag ) ;
3
+
4
+ if ( el ) {
5
+ return el ;
6
+ }
7
+
8
+ el = document . createElement ( tag ) ;
9
+
10
+ return parentElement . insertBefore ( el , parentElement . firstChild ) ;
11
+ } ;
12
+
13
+ export default getSingletonElementInstance ;
You can’t perform that action at this time.
0 commit comments