diff options
Diffstat (limited to 'src/js/utils.js')
-rw-r--r-- | src/js/utils.js | 31 |
1 files changed, 18 insertions, 13 deletions
diff --git a/src/js/utils.js b/src/js/utils.js index d46a7601..a58d8555 100644 --- a/src/js/utils.js +++ b/src/js/utils.js @@ -3,15 +3,13 @@ // ========================================================================== import loadjs from 'loadjs'; +import Storage from './storage'; import support from './support'; import { providers } from './types'; const utils = { // Check variable types is: { - plyr(input) { - return this.instanceof(input, window.Plyr); - }, object(input) { return this.getConstructor(input) === Object; }, @@ -31,22 +29,22 @@ const utils = { return !this.nullOrUndefined(input) && Array.isArray(input); }, weakMap(input) { - return this.instanceof(input, window.WeakMap); + return this.instanceof(input, WeakMap); }, nodeList(input) { - return this.instanceof(input, window.NodeList); + return this.instanceof(input, NodeList); }, element(input) { - return this.instanceof(input, window.Element); + return this.instanceof(input, Element); }, textNode(input) { return this.getConstructor(input) === Text; }, event(input) { - return this.instanceof(input, window.Event); + return this.instanceof(input, Event); }, cue(input) { - return this.instanceof(input, window.TextTrackCue) || this.instanceof(input, window.VTTCue); + return this.instanceof(input, TextTrackCue) || this.instanceof(input, VTTCue); }, track(input) { return this.instanceof(input, TextTrack) || (!this.nullOrUndefined(input) && this.string(input.kind)); @@ -159,6 +157,8 @@ const utils = { // Only load once if ID set if (!hasId || !exists()) { + const useStorage = Storage.supported; + // Create container const container = document.createElement('div'); utils.toggleHidden(container, true); @@ -168,7 +168,7 @@ const utils = { } // Check in cache - if (support.storage) { + if (useStorage) { const cached = window.localStorage.getItem(prefix + id); isCached = cached !== null; @@ -187,7 +187,7 @@ const utils = { return; } - if (support.storage) { + if (useStorage) { window.localStorage.setItem( prefix + id, JSON.stringify({ @@ -250,7 +250,7 @@ const utils = { // Add text node if (utils.is.string(text)) { - element.textContent = text; + element.innerText = text; } // Return built element @@ -547,7 +547,7 @@ const utils = { const event = new CustomEvent(type, { bubbles, detail: Object.assign({}, detail, { - plyr: utils.is.plyr(this) ? this : null, + plyr: this, }), }); @@ -583,7 +583,7 @@ const utils = { return input; } - return input.toString().replace(/{(\d+)}/g, (match, i) => utils.is.string(args[i]) ? args[i] : ''); + return input.toString().replace(/{(\d+)}/g, (match, i) => (utils.is.string(args[i]) ? args[i] : '')); }, // Get percentage @@ -706,6 +706,11 @@ const utils = { return array.filter((item, index) => array.indexOf(item) === index); }, + // Clone nested objects + cloneDeep(object) { + return JSON.parse(JSON.stringify(object)); + }, + // Get the closest value in an array closest(array, value) { if (!utils.is.array(array) || !array.length) { |