2020-03-07 11:50:58 +01:00
|
|
|
/**
|
|
|
|
* Extend object a with the properties of object b.
|
|
|
|
* If there's a conflict, object b takes precedence.
|
|
|
|
*
|
|
|
|
* @param {object} a
|
|
|
|
* @param {object} b
|
|
|
|
*/
|
|
|
|
export const extend = ( a, b ) => {
|
|
|
|
|
2020-03-07 11:58:18 +01:00
|
|
|
for( let i in b ) {
|
2020-03-07 11:50:58 +01:00
|
|
|
a[ i ] = b[ i ];
|
|
|
|
}
|
|
|
|
|
|
|
|
return a;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts the target object to an array.
|
|
|
|
*
|
|
|
|
* @param {object} o
|
|
|
|
* @return {object[]}
|
|
|
|
*/
|
|
|
|
export const toArray = ( o ) => {
|
|
|
|
|
|
|
|
return Array.prototype.slice.call( o );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Utility for deserializing a value.
|
|
|
|
*
|
|
|
|
* @param {*} value
|
|
|
|
* @return {*}
|
|
|
|
*/
|
|
|
|
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 );
|
|
|
|
}
|
|
|
|
|
|
|
|
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}
|
|
|
|
*/
|
|
|
|
export const distanceBetween = ( a, b ) => {
|
|
|
|
|
2020-03-07 11:58:18 +01:00
|
|
|
let dx = a.x - b.x,
|
2020-03-07 11:50:58 +01:00
|
|
|
dy = a.y - b.y;
|
|
|
|
|
|
|
|
return Math.sqrt( dx*dx + dy*dy );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Applies a CSS transform to the target element.
|
|
|
|
*
|
|
|
|
* @param {HTMLElement} element
|
|
|
|
* @param {string} transform
|
|
|
|
*/
|
|
|
|
export const transformElement = ( element, transform ) => {
|
|
|
|
|
|
|
|
element.style.transform = transform;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
export const closestParent = ( target, selector ) => {
|
|
|
|
|
|
|
|
let parent = target.parentNode;
|
|
|
|
|
|
|
|
while( parent ) {
|
|
|
|
|
|
|
|
// There's some overhead doing this each time, we don't
|
|
|
|
// want to rewrite the element prototype but should still
|
|
|
|
// be enough to feature detect once at startup...
|
|
|
|
let matchesMethod = parent.matches || parent.matchesSelector || parent.msMatchesSelector;
|
|
|
|
|
|
|
|
// If we find a match, we're all set
|
|
|
|
if( matchesMethod && matchesMethod.call( parent, selector ) ) {
|
|
|
|
return parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Keep searching
|
|
|
|
parent = parent.parentNode;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
export const enterFullscreen = () => {
|
|
|
|
|
|
|
|
let 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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Injects the given CSS styles into the DOM.
|
|
|
|
*
|
|
|
|
* @param {string} value
|
|
|
|
*/
|
2020-03-09 14:44:57 +01:00
|
|
|
export const createStyleSheet = ( value ) => {
|
2020-03-07 14:18:03 +01:00
|
|
|
|
|
|
|
let tag = document.createElement( 'style' );
|
|
|
|
tag.type = 'text/css';
|
2020-03-09 14:44:57 +01:00
|
|
|
|
|
|
|
if( value && value.length > 0 ) {
|
|
|
|
if( tag.styleSheet ) {
|
|
|
|
tag.styleSheet.cssText = value;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
tag.appendChild( document.createTextNode( value ) );
|
|
|
|
}
|
2020-03-07 14:18:03 +01:00
|
|
|
}
|
2020-03-09 14:44:57 +01:00
|
|
|
|
|
|
|
document.head.appendChild( tag );
|
|
|
|
|
|
|
|
return tag;
|
2020-03-07 14:18:03 +01:00
|
|
|
|
2020-03-10 20:40:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a key:value hash of all query params.
|
|
|
|
*/
|
|
|
|
export const getQueryHash = () => {
|
|
|
|
|
|
|
|
let query = {};
|
|
|
|
|
|
|
|
location.search.replace( /[A-Z0-9]+?=([\w\.%-]*)/gi, a => {
|
|
|
|
query[ a.split( '=' ).shift() ] = a.split( '=' ).pop();
|
|
|
|
} );
|
|
|
|
|
|
|
|
// Basic deserialization
|
|
|
|
for( let i in query ) {
|
|
|
|
let value = query[ i ];
|
|
|
|
|
|
|
|
query[ i ] = deserialize( unescape( value ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do not accept new dependencies via query config to avoid
|
|
|
|
// the potential of malicious script injection
|
|
|
|
if( typeof query['dependencies'] !== 'undefined' ) delete query['dependencies'];
|
|
|
|
|
|
|
|
return query;
|
|
|
|
|
2020-03-07 11:50:58 +01:00
|
|
|
}
|