Skip to content

Commit 7f5a198

Browse files
authored
feat(framework): Create a global shared resources repo, share SVG Icons (#1869)
1 parent 7a68dd2 commit 7f5a198

File tree

4 files changed

+49
-14
lines changed

4 files changed

+49
-14
lines changed

packages/base/src/SVGIconRegistry.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
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());
35

46
const ICON_NOT_FOUND = "ICON_NOT_FOUND";
57
const DEFAULT_COLLECTION = "SAP-icons";

packages/base/src/StaticArea.js

+2-12
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,6 @@
1-
const getStaticAreaInstance = () => {
2-
let staticArea = document.querySelector("ui5-static-area");
1+
import getSingletonElementInstance from "./util/getSingletonElementInstance.js";
32

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");
144

155
const removeStaticArea = () => {
166
getStaticAreaInstance().destroy();
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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;

0 commit comments

Comments
 (0)