aboutsummaryrefslogtreecommitdiffstats
path: root/src/js/utils/load-sprite.js
diff options
context:
space:
mode:
authorSam Potts <sam@potts.es>2020-04-11 16:23:14 +1000
committerSam Potts <sam@potts.es>2020-04-11 16:23:14 +1000
commit502d5977d79148957828cbf313b7ef4c9f31973f (patch)
tree71bbffcffc9745c5b672a122f3937519ba67c1c4 /src/js/utils/load-sprite.js
parent8f5b59c18cc7837bde9af55d24d12e1fd939043d (diff)
downloadplyr-502d5977d79148957828cbf313b7ef4c9f31973f.tar.lz
plyr-502d5977d79148957828cbf313b7ef4c9f31973f.tar.xz
plyr-502d5977d79148957828cbf313b7ef4c9f31973f.zip
Converted to 2 space indentation
Diffstat (limited to 'src/js/utils/load-sprite.js')
-rw-r--r--src/js/utils/load-sprite.js104
1 files changed, 52 insertions, 52 deletions
diff --git a/src/js/utils/load-sprite.js b/src/js/utils/load-sprite.js
index fe4add00..0a4eff99 100644
--- a/src/js/utils/load-sprite.js
+++ b/src/js/utils/load-sprite.js
@@ -8,68 +8,68 @@ import is from './is';
// Load an external SVG sprite
export default function loadSprite(url, id) {
- if (!is.string(url)) {
- return;
+ 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;
}
- const prefix = 'cache';
- const hasId = is.string(id);
- let isCached = false;
- const exists = () => document.getElementById(id) !== null;
+ // Inject the SVG to the body
+ document.body.insertAdjacentElement('afterbegin', container);
+ };
- const update = (container, data) => {
- // eslint-disable-next-line no-param-reassign
- container.innerHTML = data;
+ // Only load once if ID set
+ if (!hasId || !exists()) {
+ const useStorage = Storage.supported;
+ // Create container
+ const container = document.createElement('div');
+ container.setAttribute('hidden', '');
- // Check again incase of race condition
- if (hasId && exists()) {
- return;
- }
+ if (hasId) {
+ container.setAttribute('id', id);
+ }
- // Inject the SVG to the body
- document.body.insertAdjacentElement('afterbegin', container);
- };
+ // Check in cache
+ if (useStorage) {
+ const cached = window.localStorage.getItem(`${prefix}-${id}`);
+ isCached = cached !== null;
- // Only load once if ID set
- if (!hasId || !exists()) {
- const useStorage = Storage.supported;
- // Create container
- const container = document.createElement('div');
- container.setAttribute('hidden', '');
+ if (isCached) {
+ const data = JSON.parse(cached);
+ update(container, data.content);
+ }
+ }
- if (hasId) {
- container.setAttribute('id', id);
+ // Get the sprite
+ fetch(url)
+ .then(result => {
+ if (is.empty(result)) {
+ return;
}
- // 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);
- }
+ window.localStorage.setItem(
+ `${prefix}-${id}`,
+ JSON.stringify({
+ content: result,
+ }),
+ );
}
- // 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(() => {});
- }
+ update(container, result);
+ })
+ .catch(() => {});
+ }
}