diff options
Diffstat (limited to 'src/js/utils/loadSprite.js')
-rw-r--r-- | src/js/utils/loadSprite.js | 75 |
1 files changed, 0 insertions, 75 deletions
diff --git a/src/js/utils/loadSprite.js b/src/js/utils/loadSprite.js deleted file mode 100644 index fe4add00..00000000 --- a/src/js/utils/loadSprite.js +++ /dev/null @@ -1,75 +0,0 @@ -// ========================================================================== -// Sprite loader -// ========================================================================== - -import Storage from '../storage'; -import fetch from './fetch'; -import is from './is'; - -// Load an external SVG sprite -export default function loadSprite(url, id) { - if (!is.string(url)) { - return; - } - - const prefix = 'cache'; - const hasId = is.string(id); - let isCached = false; - const exists = () => document.getElementById(id) !== null; - - const update = (container, data) => { - // eslint-disable-next-line no-param-reassign - container.innerHTML = data; - - // Check again incase of race condition - if (hasId && exists()) { - return; - } - - // Inject the SVG to the body - document.body.insertAdjacentElement('afterbegin', container); - }; - - // Only load once if ID set - if (!hasId || !exists()) { - const useStorage = Storage.supported; - // Create container - const container = document.createElement('div'); - container.setAttribute('hidden', ''); - - if (hasId) { - container.setAttribute('id', id); - } - - // Check in cache - if (useStorage) { - const cached = window.localStorage.getItem(`${prefix}-${id}`); - isCached = cached !== null; - - if (isCached) { - const data = JSON.parse(cached); - update(container, data.content); - } - } - - // Get the sprite - fetch(url) - .then(result => { - if (is.empty(result)) { - return; - } - - if (useStorage) { - window.localStorage.setItem( - `${prefix}-${id}`, - JSON.stringify({ - content: result, - }), - ); - } - - update(container, result); - }) - .catch(() => {}); - } -} |