diff options
author | Sam Potts <sam@potts.es> | 2018-06-09 17:03:16 +1000 |
---|---|---|
committer | Sam Potts <sam@potts.es> | 2018-06-09 17:03:16 +1000 |
commit | 7c6d4666e99f1604c28c57bec12f16bd0fb7e79c (patch) | |
tree | 2b5e3288defe5760694fe1096cc1d9c3490f3118 /src/js/utils.js | |
parent | 90c5735904354f5fde0dcdae9f8894fe9088739c (diff) | |
parent | 76bb299c68a6b5cc72729771aca2f0d51078ebc5 (diff) | |
download | plyr-7c6d4666e99f1604c28c57bec12f16bd0fb7e79c.tar.lz plyr-7c6d4666e99f1604c28c57bec12f16bd0fb7e79c.tar.xz plyr-7c6d4666e99f1604c28c57bec12f16bd0fb7e79c.zip |
Merge branch 'develop' into a11y-improvements
# Conflicts:
# demo/dist/demo.css
# dist/plyr.css
# dist/plyr.js.map
# dist/plyr.min.js
# dist/plyr.min.js.map
# dist/plyr.polyfilled.js
# dist/plyr.polyfilled.js.map
# dist/plyr.polyfilled.min.js
# dist/plyr.polyfilled.min.js.map
# src/js/captions.js
# src/js/plyr.js
Diffstat (limited to 'src/js/utils.js')
-rw-r--r-- | src/js/utils.js | 31 |
1 files changed, 17 insertions, 14 deletions
diff --git a/src/js/utils.js b/src/js/utils.js index 201c06c8..216cd341 100644 --- a/src/js/utils.js +++ b/src/js/utils.js @@ -44,7 +44,7 @@ const utils = { return this.instanceof(input, Event); }, cue(input) { - return this.instanceof(input, TextTrackCue) || this.instanceof(input, VTTCue); + return this.instanceof(input, window.TextTrackCue) || this.instanceof(input, window.VTTCue); }, track(input) { return this.instanceof(input, TextTrack) || (!this.nullOrUndefined(input) && this.string(input.kind)); @@ -151,24 +151,23 @@ const utils = { return; } - const prefix = 'cache-'; + const prefix = 'cache'; const hasId = utils.is.string(id); let isCached = false; - const exists = () => document.querySelectorAll(`#${id}`).length; + const exists = () => document.getElementById(id) !== null; + + const update = (container, data) => { + container.innerHTML = data; - function injectSprite(data) { // Check again incase of race condition if (hasId && exists()) { return; } - // Inject content - this.innerHTML = data; - // Inject the SVG to the body - document.body.insertBefore(this, document.body.childNodes[0]); - } + document.body.insertAdjacentElement('afterbegin', container); + }; // Only load once if ID set if (!hasId || !exists()) { @@ -184,13 +183,12 @@ const utils = { // Check in cache if (useStorage) { - const cached = window.localStorage.getItem(prefix + id); + const cached = window.localStorage.getItem(`${prefix}-${id}`); isCached = cached !== null; if (isCached) { const data = JSON.parse(cached); - injectSprite.call(container, data.content); - return; + update(container, data.content); } } @@ -204,14 +202,14 @@ const utils = { if (useStorage) { window.localStorage.setItem( - prefix + id, + `${prefix}-${id}`, JSON.stringify({ content: result, }), ); } - injectSprite.call(container, result); + update(container, result); }) .catch(() => {}); } @@ -706,6 +704,11 @@ const utils = { return JSON.parse(JSON.stringify(object)); }, + // Get a nested value in an object + getDeep(object, path) { + return path.split('.').reduce((obj, key) => obj && obj[key], object); + }, + // Get the closest value in an array closest(array, value) { if (!utils.is.array(array) || !array.length) { |