@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
330 lines (329 loc) • 11.5 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.PLATFORM_WIN = exports.PLATFORM_MAC = exports.PLATFORM_LINUX = exports.PLATFORM_IOS = exports.PLATFORM_ANDROID = exports.IS_WIN = exports.IS_SAFARI = exports.IS_MAC = exports.IS_LINUX = exports.IS_IOS = exports.IS_EDGE = exports.IS_ANDROID = void 0;
exports.applyPageFocus = applyPageFocus;
exports.copyToClipboard = copyToClipboard;
Object.defineProperty(exports, "debounce", {
enumerable: true,
get: function () {
return _debounce.debounce;
}
});
Object.defineProperty(exports, "debounceAsync", {
enumerable: true,
get: function () {
return _debounce.debounceAsync;
}
});
exports.emptySelectedText = emptySelectedText;
exports.getColor = getColor;
exports.getOffsetLeft = getOffsetLeft;
exports.getOffsetTop = getOffsetTop;
exports.getSelectedElement = getSelectedElement;
exports.getSelectedText = getSelectedText;
exports.hasSelectedText = hasSelectedText;
exports.insertElementBeforeSelection = insertElementBeforeSelection;
exports.isiOS = exports.isWin = exports.isSafari = exports.isMac = exports.isLinux = exports.isEdge = exports.isAndroid = void 0;
exports.scrollToLocationHashId = scrollToLocationHashId;
exports.setPageFocusElement = setPageFocusElement;
exports.warn = void 0;
var _debounce = require("./helpers/debounce.js");
const PLATFORM_MAC = exports.PLATFORM_MAC = 'Mac|iPad|iPhone|iPod';
const PLATFORM_WIN = exports.PLATFORM_WIN = 'Win';
const PLATFORM_ANDROID = exports.PLATFORM_ANDROID = 'Android';
const PLATFORM_LINUX = exports.PLATFORM_LINUX = 'Linux';
const PLATFORM_IOS = exports.PLATFORM_IOS = 'iOS|iPhone|iPad|iPod';
let IS_EDGE = exports.IS_EDGE = false;
let IS_IOS = exports.IS_IOS = false;
let IS_SAFARI = exports.IS_SAFARI = false;
let IS_WIN = exports.IS_WIN = false;
let IS_MAC = exports.IS_MAC = false;
let IS_ANDROID = exports.IS_ANDROID = false;
let IS_LINUX = exports.IS_LINUX = false;
const isMac = () => {
var _navigator;
return exports.IS_MAC = IS_MAC = typeof navigator !== 'undefined' && new RegExp(PLATFORM_MAC, 'i').test((_navigator = navigator) === null || _navigator === void 0 ? void 0 : _navigator.platform);
};
exports.isMac = isMac;
const isWin = () => {
var _navigator2;
return exports.IS_WIN = IS_WIN = typeof navigator !== 'undefined' && new RegExp(PLATFORM_WIN, 'i').test((_navigator2 = navigator) === null || _navigator2 === void 0 ? void 0 : _navigator2.platform);
};
exports.isWin = isWin;
const isAndroid = () => {
var _navigator3;
return exports.IS_ANDROID = IS_ANDROID = typeof navigator !== 'undefined' && new RegExp(PLATFORM_ANDROID, 'i').test((_navigator3 = navigator) === null || _navigator3 === void 0 ? void 0 : _navigator3.userAgent);
};
exports.isAndroid = isAndroid;
const isLinux = () => {
var _navigator4;
return exports.IS_LINUX = IS_LINUX = typeof navigator !== 'undefined' && new RegExp(PLATFORM_LINUX, 'i').test((_navigator4 = navigator) === null || _navigator4 === void 0 ? void 0 : _navigator4.platform);
};
exports.isLinux = isLinux;
const isiOS = () => {
var _navigator5;
return exports.IS_IOS = IS_IOS = typeof navigator !== 'undefined' && new RegExp(PLATFORM_IOS, 'i').test((_navigator5 = navigator) === null || _navigator5 === void 0 ? void 0 : _navigator5.platform);
};
exports.isiOS = isiOS;
const isSafari = () => {
var _navigator6, _navigator7;
return exports.IS_SAFARI = IS_SAFARI = typeof navigator !== 'undefined' && /safari/i.test((_navigator6 = navigator) === null || _navigator6 === void 0 ? void 0 : _navigator6.userAgent) && !/chrome/i.test((_navigator7 = navigator) === null || _navigator7 === void 0 ? void 0 : _navigator7.userAgent);
};
exports.isSafari = isSafari;
const isEdge = () => {
var _navigator8;
return exports.IS_EDGE = IS_EDGE = typeof navigator !== 'undefined' && /edge/i.test((_navigator8 = navigator) === null || _navigator8 === void 0 ? void 0 : _navigator8.userAgent);
};
exports.isEdge = isEdge;
isEdge();
isiOS();
isSafari();
isWin();
isAndroid();
isMac();
isLinux();
const pageFocusElements = {};
function setPageFocusElement(selectorOrElement, key = 'default') {
return pageFocusElements[key] = selectorOrElement;
}
function applyPageFocus(selector = 'default', callback = null) {
try {
let element = /^[.#]/.test(selector) ? selector : pageFocusElements[selector];
if (typeof element === 'string' && typeof document !== 'undefined') {
element = document.querySelector(element);
} else if (!element && typeof document !== 'undefined') {
element = document.querySelector('.dnb-no-focus');
}
if (!(element instanceof HTMLElement)) {
return;
}
const role = element.getAttribute('role');
const list = ['a', 'button', 'input', 'textarea', 'select', 'label', 'menu'];
const isInteractive = list.includes(String(element.nodeName).toLowerCase()) || list.includes(String(role).toLowerCase());
const hasTabIndex = element.hasAttribute('tabindex');
const hasNoFocus = element.classList.contains('dnb-no-focus');
if (!isInteractive) {
if (!hasTabIndex) {
element.setAttribute('tabindex', '-1');
}
if (!hasNoFocus) {
element.classList.add('dnb-no-focus');
}
}
element.focus();
const onBlur = () => {
if (!isInteractive) {
if (!hasTabIndex) {
element.removeAttribute('tabindex');
}
if (!hasNoFocus) {
element.classList.remove('dnb-no-focus');
}
}
};
element.addEventListener('blur', onBlur, {
once: true
});
if (typeof callback === 'function') {
callback(element);
}
} catch (e) {
warn('Error on applyPageFocus:', e);
}
}
function getOffsetTop(elem) {
let offsetTop = 0;
if (elem) {
do {
if (!isNaN(elem.offsetTop)) {
offsetTop += elem.offsetTop;
}
} while (elem = elem.offsetParent);
}
return offsetTop;
}
function getOffsetLeft(elem) {
let offsetLeft = 0;
if (elem) {
do {
if (!isNaN(elem.offsetLeft)) {
offsetLeft += elem.offsetLeft;
}
} while (elem = elem.offsetParent);
}
return offsetLeft;
}
function scrollToLocationHashId({
offset = 0,
delay = null,
onCompletion = null
} = {}) {
if (typeof document !== 'undefined' && typeof window !== 'undefined' && window.location) {
try {
let _timeout;
const id = String(window.location.hash).replace('#', '');
if (id.length > 0) {
const handleScroll = () => {
const runScroll = () => {
const totalOffset = getOffsetTop(elem);
if (totalOffset <= 0) {
return;
}
const top = totalOffset - offset;
try {
if (typeof IntersectionObserver !== 'undefined') {
const intersectionObserver = new IntersectionObserver(entries => {
const [entry] = entries;
if (entry.isIntersecting) {
intersectionObserver.unobserve(elem);
if (typeof onCompletion === 'function') {
onCompletion(elem);
}
}
});
intersectionObserver.observe(elem);
}
if (window.scrollTo) {
window.scrollTo({
top,
behavior: 'smooth'
});
} else {
window.scrollTop = top;
}
} catch (e) {
warn('Error on scrollToLocationHashId:', e);
}
};
if (delay > 0) {
clearTimeout(_timeout);
_timeout = setTimeout(runScroll, delay);
} else {
runScroll();
}
};
const elem = document.getElementById(id);
if (elem instanceof HTMLElement) {
window.addEventListener('beforeunload', () => clearTimeout(_timeout));
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', handleScroll);
} else {
handleScroll();
}
}
return elem;
}
} catch (e) {
warn('Error on scrollToLocationHashId:', e);
}
}
}
function insertElementBeforeSelection(elem) {
try {
const selection = window.getSelection();
const range = selection.getRangeAt(0);
range.cloneRange().insertNode(elem);
selection.addRange(range);
} catch (e) {}
}
function getSelectedText() {
try {
return window.getSelection().toString();
} catch (e) {}
}
function emptySelectedText() {
try {
if (window.getSelection && window.getSelection().empty) {
window.getSelection().empty();
}
} catch (e) {}
}
function hasSelectedText() {
return getSelectedText().length > 0;
}
function getSelectedElement() {
try {
const selection = window.getSelection();
if (selection.rangeCount > 0) {
let elem = selection.getRangeAt(0).startContainer;
if (elem && typeof elem === 'object') {
elem = elem.parentNode;
}
return elem;
}
} catch (e) {}
return null;
}
async function copyToClipboard(string) {
var _navigator9;
if (typeof window === 'undefined' || typeof document === 'undefined') {
return false;
}
const selection = window.getSelection();
const range = selection.rangeCount > 0 ? selection.getRangeAt(0) : false;
const resetSelection = () => {
try {
selection.removeAllRanges();
selection.addRange(range);
} catch (e) {}
};
const copyFallback = () => {
try {
const elem = document.createElement('textarea');
elem.value = String(string);
elem.contentEditable = true;
elem.readOnly = false;
elem.style.position = 'fixed';
elem.style.top = '-1000px';
document.body.appendChild(elem);
elem.select();
const success = document.execCommand('copy');
document.body.removeChild(elem);
resetSelection();
if (success) {
return true;
}
} catch (e) {
return e;
}
return `Could not copy! Unknown reason. ${string}`;
};
let success;
if (typeof navigator !== 'undefined' && (_navigator9 = navigator) !== null && _navigator9 !== void 0 && _navigator9.clipboard) {
try {
await navigator.clipboard.writeText(String(string));
success = true;
resetSelection();
} catch (e) {
success = e;
const newTry = copyFallback();
if (newTry === true) {
success = newTry;
}
}
} else {
success = copyFallback();
}
return success;
}
const warn = (...params) => {
if (typeof process !== 'undefined' && process.env.NODE_ENV !== 'production' && typeof console !== 'undefined' && typeof console.log === 'function') {
const isBrowser = typeof window !== 'undefined' && process.env.NODE_ENV !== 'test';
if (isBrowser) {
const styles = [`padding: 0.125rem 0.5rem ${IS_SAFARI ? '' : '0'}`, 'font-weight: bold', 'color: #00343E', 'background: #A5E1D2'].join(';');
console.log('%cEufemia', styles, ...params);
} else {
console.log('\u001b[0m\u001b[1m\u001b[38;5;23m\u001b[48;5;152mEufemia\u001b[49m\u001b[39m\u001b[22m\u001b[0m', ...params);
}
}
};
exports.warn = warn;
function getColor(value) {
if (String(value).includes('--')) {
return value;
}
return value ? !/#|var/.test(value) ? `var(--color-${value})` : value : undefined;
}
//# sourceMappingURL=helpers.js.map