<commit_before>const noop = function () {
  return undefined;
};

const inheritsFrom = function (child, parent) {
  child.prototype = Object.create(parent.prototype);
  child.prototype.super = parent.prototype;
};

const AutoComplete = function (el, item, elem) {
  this.type = el;
  this.el = document.querySelector('#' + el + '-autocomplete');
  this.content = this.el.parentElement;
  this.filter = document.querySelector('#toggl-button-' + el + '-filter');
  this.field = this.el.closest('.TB__Dialog__field');
  this.overlay = this.field.querySelector('.TB__Popdown__overlay');
  this.placeholderItem = document.querySelector(
    '#toggl-button-' + el + '-placeholder'
  );
  this.placeholderDiv = this.placeholderItem.querySelector('div');
  this.clearSelected = this.field.querySelector('.' + el + '-clear');
  this.addLink = this.field.querySelector('.add-new-' + el);
  this.elem = elem;
  this.item = item;
  this.lastFilter = '';
  this.listItems = [];
  this.exactMatch = false;

  this.addEvents();
};

AutoComplete.prototype.addEvents = function () {
  const that = this;

  const bindedToggleDropdown = that.toggleDropdown.bind(that);

  this.field.addEventListener('focus', function (e) {
    that.openDropdown();

    setTimeout(() => {
      that.placeholderItem.addEventListener('click', bindedToggleDropdown);
    }, 150);
  });

  this.field.addEventListener('focusout', function (e) {
    if (that.field.contains(e.relatedTarget)) return;
    that.placeholderItem.removeEventListener('click', bindedToggleDropdown);
    that.closeDropdown();
  });

  that.filter.addEventListener('keydown', function (e) {
    if (e.code === 'Tab') {
      that.closeDropdown();
    }
    if (e.code === 'Enter') {
      if (
        that.field.classList.contains('open') &&
        !!that.saveSelected
      ) {
        that.saveSelected();
        e.stopPropagation();
        e.preventDefault();
      }
      that.closeDropdown();
    }
    if (
      e.code === 'Escape' &&
      that.field.classList.contains('open')
    ) {
      that.closeDropdown();
      e.stopPropagation();
      e.preventDefault();
    }
  });

  that.filter.addEventListener('keyup', function (e) {
    that.filterSelection();
  });

  that.overlay.addEventListener('click', function (e) {
    if (!that.content.contains(e.target) && !that.placeholderItem.contains(e.target)) {
      e.stopPropagation();
      that.closeDropdown();
    }
  });
};

AutoComplete.prototype.clearFilters = function () {
  this.el.classList.remove('filtered');
  let i;

  const a = this.el.querySelectorAll('.filter');

  const b = this.el.querySelectorAll('.tasklist-opened');

  for (i = 0; i < a.length; i++) {
    a[i].classList.remove('filter');
  }

  for (i = 0; i < b.length; i++) {
    b[i].classList.remove('tasklist-opened');
  }

  this.updateAddLink && this.updateAddLink();
};

AutoComplete.prototype.toggleDropdown = function () {
  if (this.field.classList.contains('open')) {
    this.closeDropdown();
  } else {
    this.openDropdown();
  }
};

AutoComplete.prototype.openDropdown = function () {
  if (this.field.classList.contains('open')) {
    return;
  }
  this.field.classList.toggle('open', true);
  // Avoid trapping focus inside the dropdown field while tabbing around
  this.field.setAttribute('tabindex', '-1');
  this.filter.focus();
  this.listItems = this.el.querySelectorAll(this.item);
  this.visibleItems = this.el.querySelectorAll('.' + this.type + '-row');
  this.updateHeight();
};

AutoComplete.prototype.closeDropdown = function (t) {
  const that = t || this;
  that.filter.value = '';
  that.el.classList.remove('filtered');
  that.field.classList.toggle('open', false);
  if (that.addLink) that.addLink.parentNode.classList.remove('add-allowed');
  that.clearFilters();

  // Delay enabling of tabbing again to avoid trapping focus inside this dropdown
  // completely when using SHIFT+TAB.
  setTimeout(() => that.field.setAttribute('tabindex', '0'));
};

AutoComplete.prototype.updateHeight = function () {
  const bodyRect = document.body.getBoundingClientRect();
  const elRect = this.el.getBoundingClientRect();
  let popdownStyle = '';
  let listStyle = 'max-height:auto;';
  let calc;

  if (bodyRect.bottom > 0 && elRect.bottom + 25 >= bodyRect.bottom) {
    calc = window.scrollY + bodyRect.bottom - elRect.top - 10;
    if (calc < 55) {
      calc = 55;
    }
    popdownStyle = 'max-height: ' + calc + 'px;';
    // Not sure, but probably: 55=filter, 25=??, 24=clear-tags
    listStyle = 'max-height: ' + (calc - 55 - 25 - 24) + 'px;';
  } else {
    return;
  }

  this.el.closest('.TB__Popdown__content').style = popdownStyle;
  if (this.type === 'tag') {
    document.querySelector('.tag-list').style = listStyle;
  }
};

//* Project autocomplete *//

export const ProjectAutoComplete = function (el, item, elem) {
  AutoComplete.call(this, el, item, elem);
  this.onChangeHandler = noop;
  this.selectedItem = -1;<commit_msg>fix(autocomplete): Fix autocomplete height to be calculated based on window's innerHeight<commit_after>const noop = function () {
  return undefined;
};

const inheritsFrom = function (child, parent) {
  child.prototype = Object.create(parent.prototype);
  child.prototype.super = parent.prototype;
};

const AutoComplete = function (el, item, elem) {
  this.type = el;
  this.el = document.querySelector('#' + el + '-autocomplete');
  this.content = this.el.parentElement;
  this.filter = document.querySelector('#toggl-button-' + el + '-filter');
  this.field = this.el.closest('.TB__Dialog__field');
  this.overlay = this.field.querySelector('.TB__Popdown__overlay');
  this.placeholderItem = document.querySelector(
    '#toggl-button-' + el + '-placeholder'
  );
  this.placeholderDiv = this.placeholderItem.querySelector('div');
  this.clearSelected = this.field.querySelector('.' + el + '-clear');
  this.addLink = this.field.querySelector('.add-new-' + el);
  this.elem = elem;
  this.item = item;
  this.lastFilter = '';
  this.listItems = [];
  this.exactMatch = false;

  this.addEvents();
};

AutoComplete.prototype.addEvents = function () {
  const that = this;

  const bindedToggleDropdown = that.toggleDropdown.bind(that);

  this.field.addEventListener('focus', function (e) {
    that.openDropdown();

    setTimeout(() => {
      that.placeholderItem.addEventListener('click', bindedToggleDropdown);
    }, 150);
  });

  this.field.addEventListener('focusout', function (e) {
    if (that.field.contains(e.relatedTarget)) return;
    that.placeholderItem.removeEventListener('click', bindedToggleDropdown);
    that.closeDropdown();
  });

  that.filter.addEventListener('keydown', function (e) {
    if (e.code === 'Tab') {
      that.closeDropdown();
    }
    if (e.code === 'Enter') {
      if (
        that.field.classList.contains('open') &&
        !!that.saveSelected
      ) {
        that.saveSelected();
        e.stopPropagation();
        e.preventDefault();
      }
      that.closeDropdown();
    }
    if (
      e.code === 'Escape' &&
      that.field.classList.contains('open')
    ) {
      that.closeDropdown();
      e.stopPropagation();
      e.preventDefault();
    }
  });

  that.filter.addEventListener('keyup', function (e) {
    that.filterSelection();
  });

  that.overlay.addEventListener('click', function (e) {
    if (!that.content.contains(e.target) && !that.placeholderItem.contains(e.target)) {
      e.stopPropagation();
      that.closeDropdown();
    }
  });
};

AutoComplete.prototype.clearFilters = function () {
  this.el.classList.remove('filtered');
  let i;

  const a = this.el.querySelectorAll('.filter');

  const b = this.el.querySelectorAll('.tasklist-opened');

  for (i = 0; i < a.length; i++) {
    a[i].classList.remove('filter');
  }

  for (i = 0; i < b.length; i++) {
    b[i].classList.remove('tasklist-opened');
  }

  this.updateAddLink && this.updateAddLink();
};

AutoComplete.prototype.toggleDropdown = function () {
  if (this.field.classList.contains('open')) {
    this.closeDropdown();
  } else {
    this.openDropdown();
  }
};

AutoComplete.prototype.openDropdown = function () {
  if (this.field.classList.contains('open')) {
    return;
  }
  this.field.classList.toggle('open', true);
  // Avoid trapping focus inside the dropdown field while tabbing around
  this.field.setAttribute('tabindex', '-1');
  this.filter.focus();
  this.listItems = this.el.querySelectorAll(this.item);
  this.visibleItems = this.el.querySelectorAll('.' + this.type + '-row');
  this.updateHeight();
};

AutoComplete.prototype.closeDropdown = function (t) {
  const that = t || this;
  that.filter.value = '';
  that.el.classList.remove('filtered');
  that.field.classList.toggle('open', false);
  if (that.addLink) that.addLink.parentNode.classList.remove('add-allowed');
  that.clearFilters();

  // Delay enabling of tabbing again to avoid trapping focus inside this dropdown
  // completely when using SHIFT+TAB.
  setTimeout(() => that.field.setAttribute('tabindex', '0'));
};

AutoComplete.prototype.updateHeight = function () {
  const windowHeight = window.innerHeight;
  const elRect = this.el.getBoundingClientRect();
  let popdownStyle = '';
  let listStyle = 'max-height:auto;';
  let calc;

  if (windowHeight > 0 && elRect.bottom + 25 >= windowHeight) {
    calc = window.scrollY + windowHeight - elRect.top - 10;
    if (calc < 55) {
      calc = 55;
    }
    popdownStyle = 'max-height: ' + calc + 'px;';
    // Not sure, but probably: 55=filter, 25=??, 24=clear-tags
    listStyle = 'max-height: ' + (calc - 55 - 25 - 24) + 'px;';
  } else {
    return;
  }

  this.el.closest('.TB__Popdown__content').style = popdownStyle;
  if (this.type === 'tag') {
    document.querySelector('.tag-list').style = listStyle;
  }
};

//* Project autocomplete *//

export const ProjectAutoComplete = function (el, item, elem) {
  AutoComplete.call(this, el, item, elem);
  this.onChangeHandler = noop;
  this.selectedItem = -1;
{'fraction_non_alphanumeric': 0.16576250753465943, 'fraction_numerical': 0.005826803295157726, 'mean_word_length': 3.5106479383778884, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, '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': '5372971', 'n_tokens_mistral': 3204, 'n_tokens_neox': 3151, 'n_words': 589}