aboutsummaryrefslogtreecommitdiffstats
path: root/src/js/utils.js
diff options
context:
space:
mode:
authorSam Potts <me@sampotts.me>2017-11-18 19:33:01 +1100
committerSam Potts <me@sampotts.me>2017-11-18 19:33:01 +1100
commit5a244b7fedf185842b04f817eb07f49ae589d4a6 (patch)
tree86bcff6d5b6f9843df0d4f1ad3da926569066662 /src/js/utils.js
parent4dca4bf93c5cfcb8adb40b78528867de271b8361 (diff)
parent6984d6fb1606a71edd35ac043ac1116b6de8e98b (diff)
downloadplyr-5a244b7fedf185842b04f817eb07f49ae589d4a6.tar.lz
plyr-5a244b7fedf185842b04f817eb07f49ae589d4a6.tar.xz
plyr-5a244b7fedf185842b04f817eb07f49ae589d4a6.zip
Merge branch 'develop' of https://github.com/Selz/plyr into develop
# Conflicts: # dist/plyr.js # dist/plyr.js.map # src/js/controls.js
Diffstat (limited to 'src/js/utils.js')
-rw-r--r--src/js/utils.js95
1 files changed, 47 insertions, 48 deletions
diff --git a/src/js/utils.js b/src/js/utils.js
index a635e021..bb576576 100644
--- a/src/js/utils.js
+++ b/src/js/utils.js
@@ -31,6 +31,9 @@ const utils = {
htmlElement(input) {
return !this.undefined(input) && input instanceof HTMLElement;
},
+ textNode(input) {
+ return this.getConstructor(input) === Text;
+ },
event(input) {
return !this.undefined(input) && input instanceof Event;
},
@@ -49,8 +52,8 @@ const utils = {
return (
input === null ||
typeof input === 'undefined' ||
- ((this.string(input) || this.array(input) || this.nodeList(input)) && input.length === 0) ||
- (this.object(input) && Object.keys(input).length === 0)
+ ((this.string(input) || this.array(input) || this.nodeList(input)) && !input.length) ||
+ (this.object(input) && !Object.keys(input).length)
);
},
getConstructor(input) {
@@ -100,12 +103,12 @@ const utils = {
// Load an external SVG sprite
loadSprite(url, id) {
- if (typeof url !== 'string') {
+ if (!utils.is.string(url)) {
return;
}
const prefix = 'cache-';
- const hasId = typeof id === 'string';
+ const hasId = utils.is.string(id);
let isCached = false;
function updateSprite(data) {
@@ -134,34 +137,30 @@ const utils = {
if (isCached) {
const data = JSON.parse(cached);
updateSprite.call(container, data.content);
+ return;
}
}
- // ReSharper disable once InconsistentNaming
- const xhr = new XMLHttpRequest();
-
- // XHR for Chrome/Firefox/Opera/Safari
- if ('withCredentials' in xhr) {
- xhr.open('GET', url, true);
- } else {
- return;
- }
-
- // Once loaded, inject to container and body
- xhr.onload = () => {
- if (support.storage) {
- window.localStorage.setItem(
- prefix + id,
- JSON.stringify({
- content: xhr.responseText,
- })
- );
- }
+ // Get the sprite
+ fetch(url)
+ .then(response => (response.ok ? response.text() : null))
+ .then(text => {
+ if (text === null) {
+ return;
+ }
- updateSprite.call(container, xhr.responseText);
- };
+ if (support.storage) {
+ window.localStorage.setItem(
+ prefix + id,
+ JSON.stringify({
+ content: text,
+ })
+ );
+ }
- xhr.send();
+ updateSprite.call(container, text);
+ })
+ .catch(() => {});
}
},
@@ -210,22 +209,6 @@ const utils = {
});
},
- // Remove an element
- removeElement(element) {
- if (!utils.is.htmlElement(element) || !utils.is.htmlElement(element.parentNode)) {
- return null;
- }
-
- element.parentNode.removeChild(element);
-
- return element;
- },
-
- // Inaert an element after another
- insertAfter(element, target) {
- target.parentNode.insertBefore(element, target.nextSibling);
- },
-
// Create a DocumentFragment
createElement(type, attributes, text) {
// Create a new <element>
@@ -245,12 +228,28 @@ const utils = {
return element;
},
+ // Inaert an element after another
+ insertAfter(element, target) {
+ target.parentNode.insertBefore(element, target.nextSibling);
+ },
+
// Insert a DocumentFragment
insertElement(type, parent, attributes, text) {
// Inject the new <element>
parent.appendChild(utils.createElement(type, attributes, text));
},
+ // Remove an element
+ removeElement(element) {
+ if (!utils.is.htmlElement(element) || !utils.is.htmlElement(element.parentNode)) {
+ return null;
+ }
+
+ element.parentNode.removeChild(element);
+
+ return element;
+ },
+
// Remove all child elements
emptyElement(element) {
let { length } = element.childNodes;
@@ -442,9 +441,9 @@ const utils = {
// Trap focus inside container
trapFocus() {
- const tabbables = utils.getElements.call(this, 'button:not(:disabled), input:not(:disabled), [tabindex]');
- const first = tabbables[0];
- const last = tabbables[tabbables.length - 1];
+ const focusable = utils.getElements.call(this, 'button:not(:disabled), input:not(:disabled), [tabindex]');
+ const first = focusable[0];
+ const last = focusable[focusable.length - 1];
utils.on(
this.elements.container,
@@ -525,7 +524,7 @@ const utils = {
},
// Trigger event
- dispatchEvent(element, type, bubbles, properties) {
+ dispatchEvent(element, type, bubbles, detail) {
// Bail if no element
if (!element || !type) {
return;
@@ -534,7 +533,7 @@ const utils = {
// Create and dispatch the event
const event = new CustomEvent(type, {
bubbles: utils.is.boolean(bubbles) ? bubbles : false,
- detail: Object.assign({}, properties, {
+ detail: Object.assign({}, detail, {
plyr: this instanceof Plyr ? this : null,
}),
});