-
Notifications
You must be signed in to change notification settings - Fork 253
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(framework): Create a global shared resources repo, share SVG Ico…
…ns (#1869)
- Loading branch information
1 parent
7a68dd2
commit 7f5a198
Showing
4 changed files
with
49 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import getSingletonElementInstance from "./util/getSingletonElementInstance.js"; | ||
|
||
const getSharedResourcesInstance = () => getSingletonElementInstance("ui5-shared-resources", document.head); | ||
|
||
/** | ||
* Use this method to initialize/get resources that you would like to be shared among UI5 Web Components runtime instances. | ||
* The data will be accessed via a singleton "ui5-shared-resources" HTML element in the "head" element of the page. | ||
* | ||
* @public | ||
* @param namespace Unique ID of the resource, may contain "." to denote hierarchy | ||
* @param initialValue Object or primitive that will be used as an initial value if the resource does not exist | ||
* @returns {*} | ||
*/ | ||
const getSharedResource = (namespace, initialValue) => { | ||
const parts = namespace.split("."); | ||
let current = getSharedResourcesInstance(); | ||
|
||
for (let i = 0; i < parts.length; i++) { | ||
const part = parts[i]; | ||
const lastPart = i === parts.length - 1; | ||
if (!Object.prototype.hasOwnProperty.call(current, part)) { | ||
current[part] = lastPart ? initialValue : {}; | ||
} | ||
current = current[part]; | ||
} | ||
|
||
return current; | ||
}; | ||
|
||
export default getSharedResource; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
const getSingletonElementInstance = (tag, parentElement = document.body) => { | ||
let el = document.querySelector(tag); | ||
|
||
if (el) { | ||
return el; | ||
} | ||
|
||
el = document.createElement(tag); | ||
|
||
return parentElement.insertBefore(el, parentElement.firstChild); | ||
}; | ||
|
||
export default getSingletonElementInstance; |