2019-02-22 23:04:55 +01:00
|
|
|
export const getRandomNumber = (min, max) =>
|
|
|
|
Math.floor(Math.random() * (max - min) + min);
|
2018-05-28 17:22:22 +02:00
|
|
|
|
2019-02-22 23:04:55 +01:00
|
|
|
export const generateChars = length => {
|
2017-08-16 10:47:22 +02:00
|
|
|
let chars = '';
|
2017-06-02 22:34:07 +02:00
|
|
|
|
2017-08-16 10:47:22 +02:00
|
|
|
for (let i = 0; i < length; i++) {
|
|
|
|
const randomChar = getRandomNumber(0, 36);
|
2017-06-02 22:34:07 +02:00
|
|
|
chars += randomChar.toString(36);
|
|
|
|
}
|
|
|
|
|
|
|
|
return chars;
|
|
|
|
};
|
|
|
|
|
2019-02-22 23:04:55 +01:00
|
|
|
export const generateId = (element, prefix) => {
|
2018-05-28 16:50:16 +02:00
|
|
|
let id =
|
|
|
|
element.id ||
|
|
|
|
(element.name && `${element.name}-${generateChars(2)}`) ||
|
|
|
|
generateChars(4);
|
2017-06-02 22:34:07 +02:00
|
|
|
id = id.replace(/(:|\.|\[|\]|,)/g, '');
|
2018-05-28 18:56:36 +02:00
|
|
|
id = `${prefix}-${id}`;
|
2017-06-02 22:34:07 +02:00
|
|
|
|
|
|
|
return id;
|
|
|
|
};
|
|
|
|
|
2019-02-22 23:04:55 +01:00
|
|
|
export const getType = obj => Object.prototype.toString.call(obj).slice(8, -1);
|
2016-04-04 22:44:32 +02:00
|
|
|
|
2019-02-22 23:04:55 +01:00
|
|
|
export const isType = (type, obj) =>
|
|
|
|
obj !== undefined && obj !== null && getType(obj) === type;
|
2016-04-04 22:44:32 +02:00
|
|
|
|
2019-02-22 23:04:55 +01:00
|
|
|
export const isElement = element => element instanceof Element;
|
2016-03-16 21:24:11 +01:00
|
|
|
|
2019-02-22 23:04:55 +01:00
|
|
|
export const wrap = (element, wrapper = document.createElement('div')) => {
|
2016-09-05 23:04:15 +02:00
|
|
|
if (element.nextSibling) {
|
|
|
|
element.parentNode.insertBefore(wrapper, element.nextSibling);
|
|
|
|
} else {
|
|
|
|
element.parentNode.appendChild(wrapper);
|
|
|
|
}
|
|
|
|
return wrapper.appendChild(element);
|
2016-03-16 21:24:11 +01:00
|
|
|
};
|
|
|
|
|
2019-02-22 23:04:55 +01:00
|
|
|
export const findAncestorByAttrName = (el, attr) => {
|
2017-03-12 14:17:46 +01:00
|
|
|
let target = el;
|
|
|
|
|
|
|
|
while (target) {
|
|
|
|
if (target.hasAttribute(attr)) {
|
|
|
|
return target;
|
|
|
|
}
|
|
|
|
|
|
|
|
target = target.parentElement;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
2016-05-02 14:22:53 +02:00
|
|
|
export const getAdjacentEl = (startEl, className, direction = 1) => {
|
2019-02-22 23:04:55 +01:00
|
|
|
if (!startEl || !className) {
|
|
|
|
return;
|
|
|
|
}
|
2016-05-02 14:22:53 +02:00
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
const parent = startEl.parentNode.parentNode;
|
|
|
|
const children = Array.from(parent.querySelectorAll(className));
|
2016-05-02 14:22:53 +02:00
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
const startPos = children.indexOf(startEl);
|
|
|
|
const operatorDirection = direction > 0 ? 1 : -1;
|
2016-08-14 23:14:37 +02:00
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
return children[startPos + operatorDirection];
|
2016-05-02 14:22:53 +02:00
|
|
|
};
|
|
|
|
|
2016-05-02 22:39:33 +02:00
|
|
|
export const isScrolledIntoView = (el, parent, direction = 1) => {
|
2019-02-22 23:04:55 +01:00
|
|
|
if (!el) {
|
|
|
|
return;
|
|
|
|
}
|
2016-05-02 22:39:33 +02:00
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
let isVisible;
|
2016-05-02 22:39:33 +02:00
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
if (direction > 0) {
|
|
|
|
// In view from bottom
|
2018-05-28 16:50:16 +02:00
|
|
|
isVisible =
|
|
|
|
parent.scrollTop + parent.offsetHeight >= el.offsetTop + el.offsetHeight;
|
2016-09-05 23:04:15 +02:00
|
|
|
} else {
|
|
|
|
// In view from top
|
|
|
|
isVisible = el.offsetTop >= parent.scrollTop;
|
|
|
|
}
|
2016-08-14 23:14:37 +02:00
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
return isVisible;
|
2016-08-14 23:14:37 +02:00
|
|
|
};
|
2016-05-02 16:29:05 +02:00
|
|
|
|
2019-02-22 23:04:55 +01:00
|
|
|
export const sanitise = value => {
|
|
|
|
if (!isType('String', value)) {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return value
|
2018-05-28 16:50:16 +02:00
|
|
|
.replace(/&/g, '&')
|
2018-03-23 16:51:50 +01:00
|
|
|
.replace(/>/g, '&rt;')
|
|
|
|
.replace(/</g, '<')
|
|
|
|
.replace(/"/g, '"');
|
2019-02-22 23:04:55 +01:00
|
|
|
};
|
2016-03-16 21:24:11 +01:00
|
|
|
|
2019-02-22 23:04:55 +01:00
|
|
|
export const strToEl = (() => {
|
2017-08-16 10:47:22 +02:00
|
|
|
const tmpEl = document.createElement('div');
|
2019-02-22 23:04:55 +01:00
|
|
|
return str => {
|
2017-08-16 10:47:22 +02:00
|
|
|
const cleanedInput = str.trim();
|
2017-06-27 13:42:01 +02:00
|
|
|
tmpEl.innerHTML = cleanedInput;
|
2019-02-22 23:04:55 +01:00
|
|
|
const firldChild = tmpEl.children[0];
|
2016-09-05 23:04:15 +02:00
|
|
|
|
|
|
|
while (tmpEl.firstChild) {
|
|
|
|
tmpEl.removeChild(tmpEl.firstChild);
|
|
|
|
}
|
2016-04-04 00:07:10 +02:00
|
|
|
|
2019-02-22 23:04:55 +01:00
|
|
|
return firldChild;
|
2016-09-05 23:04:15 +02:00
|
|
|
};
|
2018-05-28 16:50:16 +02:00
|
|
|
})();
|
2016-04-07 20:44:16 +02:00
|
|
|
|
2016-08-14 23:14:37 +02:00
|
|
|
/**
|
2018-05-22 14:14:02 +02:00
|
|
|
* Determines the width of a passed input based on its value and passes
|
|
|
|
* it to the supplied callback function.
|
2016-04-07 20:44:16 +02:00
|
|
|
*/
|
2018-05-22 14:14:02 +02:00
|
|
|
export const calcWidthOfInput = (input, callback) => {
|
2016-09-05 23:04:15 +02:00
|
|
|
const value = input.value || input.placeholder;
|
|
|
|
let width = input.offsetWidth;
|
|
|
|
|
|
|
|
if (value) {
|
2019-02-22 23:04:55 +01:00
|
|
|
const testEl = strToEl(`<span>${sanitise(value)}</span>`);
|
2016-09-05 23:04:15 +02:00
|
|
|
testEl.style.position = 'absolute';
|
|
|
|
testEl.style.padding = '0';
|
|
|
|
testEl.style.top = '-9999px';
|
|
|
|
testEl.style.left = '-9999px';
|
|
|
|
testEl.style.width = 'auto';
|
|
|
|
testEl.style.whiteSpace = 'pre';
|
|
|
|
|
2017-08-02 15:05:26 +02:00
|
|
|
if (document.body.contains(input) && window.getComputedStyle) {
|
|
|
|
const inputStyle = window.getComputedStyle(input);
|
|
|
|
|
|
|
|
if (inputStyle) {
|
|
|
|
testEl.style.fontSize = inputStyle.fontSize;
|
|
|
|
testEl.style.fontFamily = inputStyle.fontFamily;
|
|
|
|
testEl.style.fontWeight = inputStyle.fontWeight;
|
|
|
|
testEl.style.fontStyle = inputStyle.fontStyle;
|
|
|
|
testEl.style.letterSpacing = inputStyle.letterSpacing;
|
|
|
|
testEl.style.textTransform = inputStyle.textTransform;
|
2019-10-02 10:03:38 +02:00
|
|
|
testEl.style.paddingLeft = inputStyle.paddingLeft;
|
|
|
|
testEl.style.paddingRight = inputStyle.paddingRight;
|
2017-08-02 15:05:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
document.body.appendChild(testEl);
|
|
|
|
|
2018-05-22 14:14:02 +02:00
|
|
|
requestAnimationFrame(() => {
|
|
|
|
if (value && testEl.offsetWidth !== input.offsetWidth) {
|
|
|
|
width = testEl.offsetWidth + 4;
|
|
|
|
}
|
2016-04-16 18:06:27 +02:00
|
|
|
|
2018-05-22 14:14:02 +02:00
|
|
|
document.body.removeChild(testEl);
|
2016-09-05 23:04:15 +02:00
|
|
|
|
2018-05-22 14:14:02 +02:00
|
|
|
callback.call(this, `${width}px`);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
callback.call(this, `${width}px`);
|
|
|
|
}
|
2016-06-29 15:47:58 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export const sortByAlpha = (a, b) => {
|
2019-02-22 23:04:55 +01:00
|
|
|
const labelA = `${a.label || a.value}`.toLowerCase();
|
|
|
|
const labelB = `${b.label || b.value}`.toLowerCase();
|
2016-06-29 15:47:58 +02:00
|
|
|
|
2018-05-28 18:56:36 +02:00
|
|
|
if (labelA < labelB) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (labelA > labelB) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
return 0;
|
2016-08-03 15:23:23 +02:00
|
|
|
};
|
|
|
|
|
2017-08-16 10:47:22 +02:00
|
|
|
export const sortByScore = (a, b) => a.score - b.score;
|
2017-01-01 16:32:09 +01:00
|
|
|
|
2017-10-18 10:08:27 +02:00
|
|
|
export const dispatchEvent = (element, type, customArgs = null) => {
|
2017-12-20 13:38:16 +01:00
|
|
|
const event = new CustomEvent(type, {
|
2017-01-01 16:32:09 +01:00
|
|
|
detail: customArgs,
|
|
|
|
bubbles: true,
|
2017-08-16 10:47:22 +02:00
|
|
|
cancelable: true,
|
2017-01-01 16:32:09 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
return element.dispatchEvent(event);
|
|
|
|
};
|
2017-08-10 12:57:17 +02:00
|
|
|
|
2017-12-10 19:00:57 +01:00
|
|
|
export const getWindowHeight = () => {
|
2019-10-21 21:03:57 +02:00
|
|
|
const { body } = document;
|
2017-12-10 19:00:57 +01:00
|
|
|
const html = document.documentElement;
|
|
|
|
return Math.max(
|
|
|
|
body.scrollHeight,
|
|
|
|
body.offsetHeight,
|
|
|
|
html.clientHeight,
|
|
|
|
html.scrollHeight,
|
|
|
|
html.offsetHeight,
|
|
|
|
);
|
2017-12-18 13:06:38 +01:00
|
|
|
};
|
|
|
|
|
2018-05-28 18:56:36 +02:00
|
|
|
export const fetchFromObject = (object, path) => {
|
|
|
|
const index = path.indexOf('.');
|
2018-03-12 21:42:13 +01:00
|
|
|
|
2018-05-28 16:50:16 +02:00
|
|
|
if (index > -1) {
|
|
|
|
return fetchFromObject(
|
2018-05-28 18:56:36 +02:00
|
|
|
object[path.substring(0, index)],
|
|
|
|
path.substr(index + 1),
|
2018-05-28 16:50:16 +02:00
|
|
|
);
|
2018-03-12 21:42:13 +01:00
|
|
|
}
|
|
|
|
|
2018-05-28 18:56:36 +02:00
|
|
|
return object[path];
|
2018-05-28 16:13:55 +02:00
|
|
|
};
|
|
|
|
|
2018-05-28 17:22:22 +02:00
|
|
|
export const isIE11 = () =>
|
|
|
|
!!(
|
2018-05-28 16:50:16 +02:00
|
|
|
navigator.userAgent.match(/Trident/) &&
|
|
|
|
navigator.userAgent.match(/rv[ :]11/)
|
|
|
|
);
|
2018-05-27 18:22:58 +02:00
|
|
|
|
2018-05-28 18:56:36 +02:00
|
|
|
export const existsInArray = (array, value, key = 'value') =>
|
2018-05-28 17:22:22 +02:00
|
|
|
array.some(item => {
|
2018-05-27 18:22:58 +02:00
|
|
|
if (isType('String', value)) {
|
2018-05-28 18:56:36 +02:00
|
|
|
return item[key] === value.trim();
|
2018-05-27 18:22:58 +02:00
|
|
|
}
|
|
|
|
|
2018-05-28 18:56:36 +02:00
|
|
|
return item[key] === value;
|
2018-05-28 16:50:16 +02:00
|
|
|
});
|
2018-05-28 17:22:22 +02:00
|
|
|
|
|
|
|
export const cloneObject = obj => JSON.parse(JSON.stringify(obj));
|
2018-10-09 14:16:58 +02:00
|
|
|
|
2019-02-12 19:35:46 +01:00
|
|
|
export const diff = (a, b) => {
|
2018-10-09 14:16:58 +02:00
|
|
|
const aKeys = Object.keys(a).sort();
|
|
|
|
const bKeys = Object.keys(b).sort();
|
2019-02-12 19:35:46 +01:00
|
|
|
|
2019-02-22 23:04:55 +01:00
|
|
|
return aKeys.filter(i => bKeys.indexOf(i) < 0);
|
|
|
|
};
|