1
0
Fork 0
why-cant-we-deploy-today/js/utils/util.js

297 lines
6.7 KiB
JavaScript
Raw Permalink Normal View History

/**
* Extend object a with the properties of object b.
* If there's a conflict, object b takes precedence.
*
* @param {object} a
* @param {object} b
*/
2023-05-23 19:50:14 +02:00
export const extend = (a, b) => {
for (let i in b) {
a[i] = b[i];
}
2023-05-23 19:50:14 +02:00
return a;
};
/**
* querySelectorAll but returns an Array.
*/
2023-05-23 19:50:14 +02:00
export const queryAll = (el, selector) => {
return Array.from(el.querySelectorAll(selector));
};
/**
* classList.toggle() with cross browser support
*/
2023-05-23 19:50:14 +02:00
export const toggleClass = (el, className, value) => {
if (value) {
el.classList.add(className);
} else {
el.classList.remove(className);
}
};
/**
* Utility for deserializing a value.
*
* @param {*} value
* @return {*}
*/
2023-05-23 19:50:14 +02:00
export const deserialize = (value) => {
if (typeof value === "string") {
if (value === "null") return null;
else if (value === "true") return true;
else if (value === "false") return false;
else if (value.match(/^-?[\d\.]+$/)) return parseFloat(value);
}
2023-05-23 19:50:14 +02:00
return value;
};
/**
* Measures the distance in pixels between point a
* and point b.
*
* @param {object} a point with x/y properties
* @param {object} b point with x/y properties
*
* @return {number}
*/
2023-05-23 19:50:14 +02:00
export const distanceBetween = (a, b) => {
let dx = a.x - b.x,
dy = a.y - b.y;
2023-05-23 19:50:14 +02:00
return Math.sqrt(dx * dx + dy * dy);
};
/**
* Applies a CSS transform to the target element.
*
* @param {HTMLElement} element
* @param {string} transform
*/
2023-05-23 19:50:14 +02:00
export const transformElement = (element, transform) => {
element.style.transform = transform;
};
2020-05-26 09:46:50 +02:00
/**
* Element.matches with IE support.
*
* @param {HTMLElement} target The element to match
* @param {String} selector The CSS selector to match
* the element against
*
* @return {Boolean}
*/
2023-05-23 19:50:14 +02:00
export const matches = (target, selector) => {
let matchesMethod =
target.matches || target.matchesSelector || target.msMatchesSelector;
2020-05-26 09:46:50 +02:00
2023-05-23 19:50:14 +02:00
return !!(matchesMethod && matchesMethod.call(target, selector));
};
2020-05-26 09:46:50 +02:00
/**
* Find the closest parent that matches the given
* selector.
*
* @param {HTMLElement} target The child element
* @param {String} selector The CSS selector to match
* the parents against
*
* @return {HTMLElement} The matched parent or null
* if no matching parent was found
*/
2023-05-23 19:50:14 +02:00
export const closest = (target, selector) => {
// Native Element.closest
if (typeof target.closest === "function") {
return target.closest(selector);
}
2023-05-23 19:50:14 +02:00
// Polyfill
while (target) {
if (matches(target, selector)) {
return target;
}
2023-05-23 19:50:14 +02:00
// Keep searching
target = target.parentNode;
}
2023-05-23 19:50:14 +02:00
return null;
};
/**
* Handling the fullscreen functionality via the fullscreen API
*
* @see http://fullscreen.spec.whatwg.org/
* @see https://developer.mozilla.org/en-US/docs/DOM/Using_fullscreen_mode
*/
2023-05-23 19:50:14 +02:00
export const enterFullscreen = (element) => {
element = element || document.documentElement;
// Check which implementation is available
let requestMethod =
element.requestFullscreen ||
element.webkitRequestFullscreen ||
element.webkitRequestFullScreen ||
element.mozRequestFullScreen ||
element.msRequestFullscreen;
if (requestMethod) {
requestMethod.apply(element);
}
};
2020-03-07 14:18:03 +01:00
2020-03-10 21:08:11 +01:00
/**
* Creates an HTML element and returns a reference to it.
* If the element already exists the existing instance will
* be returned.
*
* @param {HTMLElement} container
* @param {string} tagname
* @param {string} classname
* @param {string} innerHTML
*
* @return {HTMLElement}
*/
2023-05-23 19:50:14 +02:00
export const createSingletonNode = (
container,
tagname,
classname,
innerHTML = ""
) => {
// Find all nodes matching the description
let nodes = container.querySelectorAll("." + classname);
// Check all matches to find one which is a direct child of
// the specified container
for (let i = 0; i < nodes.length; i++) {
let testNode = nodes[i];
if (testNode.parentNode === container) {
return testNode;
}
}
// If no node was found, create it now
let node = document.createElement(tagname);
node.className = classname;
node.innerHTML = innerHTML;
container.appendChild(node);
return node;
};
2020-03-10 21:08:11 +01:00
2020-03-07 14:18:03 +01:00
/**
* Injects the given CSS styles into the DOM.
*
* @param {string} value
*/
2023-05-23 19:50:14 +02:00
export const createStyleSheet = (value) => {
let tag = document.createElement("style");
tag.type = "text/css";
2023-05-23 19:50:14 +02:00
if (value && value.length > 0) {
if (tag.styleSheet) {
tag.styleSheet.cssText = value;
} else {
tag.appendChild(document.createTextNode(value));
}
}
2023-05-23 19:50:14 +02:00
document.head.appendChild(tag);
2023-05-23 19:50:14 +02:00
return tag;
};
2020-03-10 20:40:35 +01:00
/**
* Returns a key:value hash of all query params.
*/
export const getQueryHash = () => {
2023-05-23 19:50:14 +02:00
let query = {};
2020-03-10 20:40:35 +01:00
2023-05-23 19:50:14 +02:00
location.search.replace(/[A-Z0-9]+?=([\w\.%-]*)/gi, (a) => {
query[a.split("=").shift()] = a.split("=").pop();
});
2020-03-10 20:40:35 +01:00
2023-05-23 19:50:14 +02:00
// Basic deserialization
for (let i in query) {
let value = query[i];
2020-03-10 20:40:35 +01:00
2023-05-23 19:50:14 +02:00
query[i] = deserialize(unescape(value));
}
2020-03-10 20:40:35 +01:00
2023-05-23 19:50:14 +02:00
// Do not accept new dependencies via query config to avoid
// the potential of malicious script injection
if (typeof query["dependencies"] !== "undefined")
delete query["dependencies"];
2020-03-10 20:40:35 +01:00
2023-05-23 19:50:14 +02:00
return query;
};
/**
* Returns the remaining height within the parent of the
* target element.
*
* remaining height = [ configured parent height ] - [ current parent height ]
*
* @param {HTMLElement} element
* @param {number} [height]
*/
2023-05-23 19:50:14 +02:00
export const getRemainingHeight = (element, height = 0) => {
if (element) {
let newHeight,
oldHeight = element.style.height;
2023-05-23 19:50:14 +02:00
// Change the .stretch element height to 0 in order find the height of all
// the other elements
element.style.height = "0px";
2023-05-23 19:50:14 +02:00
// In Overview mode, the parent (.slide) height is set of 700px.
// Restore it temporarily to its natural height.
element.parentNode.style.height = "auto";
2023-05-23 19:50:14 +02:00
newHeight = height - element.parentNode.offsetHeight;
2023-05-23 19:50:14 +02:00
// Restore the old height, just in case
element.style.height = oldHeight + "px";
2023-05-23 19:50:14 +02:00
// Clear the parent (.slide) height. .removeProperty works in IE9+
element.parentNode.style.removeProperty("height");
2023-05-23 19:50:14 +02:00
return newHeight;
}
2023-05-23 19:50:14 +02:00
return height;
};
2021-11-24 11:07:11 +01:00
const fileExtensionToMimeMap = {
2023-05-23 19:50:14 +02:00
mp4: "video/mp4",
m4a: "video/mp4",
ogv: "video/ogg",
mpeg: "video/mpeg",
webm: "video/webm",
};
2021-11-24 11:07:11 +01:00
/**
* Guess the MIME type for common file formats.
*/
2023-05-23 19:50:14 +02:00
export const getMimeTypeFromFile = (filename = "") => {
return fileExtensionToMimeMap[filename.split(".").pop()];
};
/**
* Encodes a string for RFC3986-compliant URL format.
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURI#encoding_for_rfc3986
*
* @param {string} url
*/
2023-05-23 19:50:14 +02:00
export const encodeRFC3986URI = (url = "") => {
return encodeURI(url)
.replace(/%5B/g, "[")
.replace(/%5D/g, "]")
.replace(
/[!'()*]/g,
(c) => `%${c.charCodeAt(0).toString(16).toUpperCase()}`
);
};