<commit_before>'use babel';
import pinView from './pin-view.js';

import { getConfig } from './config.js';

export function logError(error) {
  console.error(error.message, error.stack);
}

export function isChildOf(childNode, parentNode) {
  while (childNode != parentNode && childNode != document.body) {
    childNode = childNode.parentNode;
  }
  return childNode == parentNode;
}

export function getTreeView() {
  if(atom.packages.isPackageActive('tree-view'))
    return atom.packages.getActivePackage('tree-view').mainModule.createView();
}

export function getTreeViewEl() {
  return atom.views.getView(getTreeView());
}

export function getContentWidth() {
  var contentWidth = 0;
  for(let element of Array.from(getTreeViewEl().querySelectorAll('.list-tree'))) {
    if(element.clientWidth > contentWidth) contentWidth = element.clientWidth;
  }

  var pinViewWidth = pinView.clientWidth;
  var treeViewWidth = contentWidth + pinViewWidth;

  var minWidth = getConfig('minWidth');
  var maxWidth = getConfig('maxWidth');

  treeViewWidth = Math.max(minWidth, treeViewWidth);
  if(maxWidth) treeViewWidth = Math.min(maxWidth, treeViewWidth);

  return treeViewWidth + pinViewWidth;
}

var focusedElement;

// cache the element that currently has focus
export function storeFocusedElement() {
  if(!focusedElement) focusedElement = document.activeElement;
}

// clear the reference to the focusedElement. useful
// when we want to invalidate the next restoreFocus<commit_msg>fix some issues in the tree view width calculation<commit_after>'use babel';
import pinView from './pin-view.js';

import { getConfig } from './config.js';

export function logError(error) {
  console.error(error.message, error.stack);
}

export function isChildOf(childNode, parentNode) {
  while (childNode != parentNode && childNode != document.body) {
    childNode = childNode.parentNode;
  }
  return childNode == parentNode;
}

// returns the tree view model
export function getTreeView() {
  if(atom.packages.isPackageActive('tree-view')) {
    return atom.packages.getActivePackage('tree-view').mainModule.createView();
  }
}

// returns the top-level element created by the tree view package
export function getTreeViewEl() {
  return atom.views.getView(getTreeView());
}

// returns the sum of the widths of the pinView button and
// the the .list-tree with the max width, clamped between
// the minWidth and maxWidth settings
export function getContentWidth() {
  var listTrees = Array.from(getTreeViewEl().querySelectorAll('.list-tree'));
  var maxListWidth = Math.max(...listTrees.map(listTree =>
    listTree.clientWidth
  ));

  var pinViewWidth = pinView.clientWidth;
  var treeViewWidth = maxListWidth + pinViewWidth;

  var minWidth = getConfig('minWidth');
  var maxWidth = getConfig('maxWidth');

  // only apply maxWidth if it's greater than 0
  treeViewWidth = Math.min(Math.max(treeViewWidth, minWidth), maxWidth || Infinity);

  return treeViewWidth;
}

var focusedElement;

// cache the element that currently has focus
export function storeFocusedElement() {
  if(!focusedElement) focusedElement = document.activeElement;
}

// clear the reference to the focusedElement. useful
// when we want to invalidate the next restoreFocus
{'fraction_non_alphanumeric': 0.11141975308641976, 'fraction_numerical': 0.0006172839506172839, 'mean_word_length': 4.777183600713013, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1488222', 'n_tokens_mistral': 939, 'n_tokens_neox': 928, 'n_words': 262}