All files / src/utils BrowserUtils.ts

96% Statements 24/25
92.85% Branches 13/14
100% Functions 5/5
95.23% Lines 20/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 4767x 2x 2x                 67x 1x           67x 1x     67x 5x 5x 5x 5x 5x 5x                 67x 7x 7x 5x 5x 2x        
const openWindowWithTarget = (url: string, target: "_blank" | "_self"): void => {
  const newWindow = window.open(url, target, 'noopener,noreferrer');
  Iif (newWindow) {
    newWindow.opener = null;
  }
};
 
/**
 * Open a url in new browser link
 * https://stackoverflow.com/questions/45046030/maintaining-href-open-in-new-tab-with-an-onclick-handler-in-react
 */
export const openInNewTab = (url: string): void => {
  openWindowWithTarget(url, '_blank');
};
 
/**
 * Open a url in current browser tab
 */
export const openInCurrentTab = (url: string): void => {
  openWindowWithTarget(url, '_self');
};
 
export const getOperatingSystem = (): string => {
  let operatingSystem = 'Not known';
  if (window.navigator.appVersion.indexOf('Win') !== -1) { operatingSystem = 'Windows OS'; }
  if (window.navigator.appVersion.indexOf('Mac') !== -1) { operatingSystem = 'MacOS'; }
  if (window.navigator.appVersion.indexOf('X11') !== -1) { operatingSystem = 'UNIX OS'; }
  if (window.navigator.appVersion.indexOf('Linux') !== -1) { operatingSystem = 'Linux OS'; }
  return operatingSystem;
};
 
/**
 * Sets focus on the first textarea element with the specified className,
 * if it is not currently focused.
 * @param {string} className - The class name of the textarea element(s) to target.
 * @returns {void}
 */
export const autoFocusTextarea = (className: string): void => {
  const elements = Array.from(document.getElementsByClassName(className));
  if (elements.length > 0) {
    const inputElem = (elements[0] as HTMLTextAreaElement);
    if (inputElem !== document.activeElement) {
      inputElem.focus();
    }
  }
};