<commit_before>/* global wagtailConfig */

const BULK_ACTION_PAGE_CHECKBOX_INPUT = 'bulk-action-checkbox';
const BULK_ACTION_SELECT_ALL_CHECKBOX_TH = 'bulk-actions-filter-checkbox';
const BULK_ACTION_FILTERS_CLASS = `${BULK_ACTION_SELECT_ALL_CHECKBOX_TH} .c-dropdown__item a`;
const BULK_ACTION_CHOICES_DIV = 'bulk-actions-choices';
const BULK_ACTION_NUM_PAGES_SPAN = 'num-pages';
const BULK_ACTION_NUM_PAGES_IN_LISTING = 'num-pages-in-listing';
const TABLE_HEADERS_TR = 'table-headers';

const checkedState = {
  checkedPages: new Set(),
  numPages: 0,
  selectAllInListing: false
};

/* Event listener for the `Select All` checkbox */
function SelectBulkActionsFilter(e) {
  const changeEvent = new Event('change');
  for (const el of document.querySelectorAll(`.${BULK_ACTION_PAGE_CHECKBOX_INPUT}`)) {
    if (el.checked === e.target.checked) continue;
    el.checked = e.target.checked;
    el.dispatchEvent(changeEvent);
  }
}


/* Event listener for individual page checkbox */
function SelectBulkActionsCheckboxes(e) {
  if (checkedState.selectAllInListing) checkedState.selectAllInListing = false;
  const prevLength = checkedState.checkedPages.size;
  if (e.target.checked) checkedState.checkedPages.add(+e.target.dataset.pageId);
  else {
    /* unchecks `Select all` checkbox as soon as one page is unchecked */
    document.querySelector(`.${BULK_ACTION_SELECT_ALL_CHECKBOX_TH} input`).checked = false;
    checkedState.checkedPages.delete(+e.target.dataset.pageId);
  }

  if (checkedState.checkedPages.size === 0) {
    /* when all checkboxes are unchecked */
    document.querySelectorAll(`.${TABLE_HEADERS_TR} > th`).forEach(el => el.classList.remove('u-hidden'));
    document.querySelector(`.${BULK_ACTION_CHOICES_DIV}`).classList.add('u-hidden');
    document.querySelectorAll(`.${BULK_ACTION_PAGE_CHECKBOX_INPUT}`).forEach(el => el.classList.remove('show'));
    document.querySelector(`.${BULK_ACTION_SELECT_ALL_CHECKBOX_TH}`).setAttribute('colspan', '1');
  } else if (checkedState.checkedPages.size === 1 && prevLength === 0) {
    /* when 1 checkbox is checked for the first time */
    document.querySelectorAll(`.${BULK_ACTION_PAGE_CHECKBOX_INPUT}`).forEach(el => {
      el.classList.add('show');
    });
    document.querySelectorAll(`.${TABLE_HEADERS_TR} > th`).forEach(el => el.classList.add('u-hidden'));
    document.querySelector(`.${BULK_ACTION_SELECT_ALL_CHECKBOX_TH}`).classList.remove('u-hidden');
    document.querySelector(`.${BULK_ACTION_CHOICES_DIV}`).classList.remove('u-hidden');
    document.querySelector(`.${BULK_ACTION_SELECT_ALL_CHECKBOX_TH}`).setAttribute('colspan', '6');
  }

  if (checkedState.checkedPages.size === checkedState.numPages) {
    /* when all checkboxes in the page are checked */
    document.querySelector(`.${BULK_ACTION_SELECT_ALL_CHECKBOX_TH} input`).checked = true;
  }

  if (checkedState.checkedPages.size > 0) {
    /* Update text on number of pages */
    let numPagesSelected = '';
    if (checkedState.checkedPages.size === checkedState.numPages) {
      document.querySelector(`.${BULK_ACTION_NUM_PAGES_IN_LISTING}`).classList.remove('u-hidden');
      if (checkedState.numPages === 1) {
        numPagesSelected = wagtailConfig.STRINGS.NUM_PAGES_SELECTED_SINGULAR;
      } else {
        numPagesSelected = wagtailConfig.STRINGS.NUM_PAGES_SELECTED_ALL.replace('{0}', checkedState.checkedPages.size);
      }
    } else {
      document.querySelector(`.${BULK_ACTION_NUM_PAGES_IN_LISTING}`).classList.add('u-hidden');
      if (checkedState.checkedPages.size === 1) {
        numPagesSelected = wagtailConfig.STRINGS.NUM_PAGES_SELECTED_SINGULAR;
      } else {
        numPagesSelected = wagtailConfig.STRINGS.NUM_PAGES_SELECTED_PLURAL.replace(
          '{0}', checkedState.checkedPages.size
        );
      }
    }
    document.querySelector(`.${BULK_ACTION_NUM_PAGES_SPAN}`).textContent = numPagesSelected;
  }
}

function selectAllPageIdsInListing() {
  checkedState.selectAllInListing = true;<commit_msg>[refactor] Remove repeated logic in bulk-actions.js<commit_after>/* global wagtailConfig */

const BULK_ACTION_PAGE_CHECKBOX_INPUT = 'bulk-action-checkbox';
const BULK_ACTION_SELECT_ALL_CHECKBOX_TH = 'bulk-actions-filter-checkbox';
const BULK_ACTION_FILTERS_CLASS = `${BULK_ACTION_SELECT_ALL_CHECKBOX_TH} .c-dropdown__item a`;
const BULK_ACTION_CHOICES_DIV = 'bulk-actions-choices';
const BULK_ACTION_NUM_PAGES_SPAN = 'num-pages';
const BULK_ACTION_NUM_PAGES_IN_LISTING = 'num-pages-in-listing';
const TABLE_HEADERS_TR = 'table-headers';

const checkedState = {
  checkedPages: new Set(),
  numPages: 0,
  selectAllInListing: false
};

/* Event listener for the `Select All` checkbox */
function SelectBulkActionsFilter(e) {
  const changeEvent = new Event('change');
  for (const el of document.querySelectorAll(`.${BULK_ACTION_PAGE_CHECKBOX_INPUT}`)) {
    if (el.checked === e.target.checked) continue;
    el.checked = e.target.checked;
    el.dispatchEvent(changeEvent);
  }
}


/* Event listener for individual page checkbox */
function SelectBulkActionsCheckboxes(e) {
  if (checkedState.selectAllInListing) checkedState.selectAllInListing = false;
  const prevLength = checkedState.checkedPages.size;
  if (e.target.checked) checkedState.checkedPages.add(+e.target.dataset.pageId);
  else {
    /* unchecks `Select all` checkbox as soon as one page is unchecked */
    document.querySelector(`.${BULK_ACTION_SELECT_ALL_CHECKBOX_TH} input`).checked = false;
    checkedState.checkedPages.delete(+e.target.dataset.pageId);
  }

  if (checkedState.checkedPages.size === 0) {
    /* when all checkboxes are unchecked */
    document.querySelectorAll(`.${TABLE_HEADERS_TR} > th`).forEach(el => el.classList.remove('u-hidden'));
    document.querySelector(`.${BULK_ACTION_CHOICES_DIV}`).classList.add('u-hidden');
    document.querySelectorAll(`.${BULK_ACTION_PAGE_CHECKBOX_INPUT}`).forEach(el => el.classList.remove('show'));
    document.querySelector(`.${BULK_ACTION_SELECT_ALL_CHECKBOX_TH}`).setAttribute('colspan', '1');
  } else if (checkedState.checkedPages.size === 1 && prevLength === 0) {
    /* when 1 checkbox is checked for the first time */
    document.querySelectorAll(`.${BULK_ACTION_PAGE_CHECKBOX_INPUT}`).forEach(el => {
      el.classList.add('show');
    });
    document.querySelectorAll(`.${TABLE_HEADERS_TR} > th`).forEach(el => el.classList.add('u-hidden'));
    document.querySelector(`.${BULK_ACTION_SELECT_ALL_CHECKBOX_TH}`).classList.remove('u-hidden');
    document.querySelector(`.${BULK_ACTION_CHOICES_DIV}`).classList.remove('u-hidden');
    document.querySelector(`.${BULK_ACTION_SELECT_ALL_CHECKBOX_TH}`).setAttribute('colspan', '6');
  }

  if (checkedState.checkedPages.size === checkedState.numPages) {
    /* when all checkboxes in the page are checked */
    document.querySelector(`.${BULK_ACTION_SELECT_ALL_CHECKBOX_TH} input`).checked = true;
  }

  if (checkedState.checkedPages.size > 0) {
    /* Update text on number of pages */
    let numPagesSelected = '';
    const numCheckPages = checkedState.checkedPages.size;
    if (numCheckPages === 1) {
      numPagesSelected = wagtailConfig.STRINGS.NUM_PAGES_SELECTED_SINGULAR;
    } else {
      if (numCheckPages === checkedState.numPages) {
        numPagesSelected = wagtailConfig.STRINGS.NUM_PAGES_SELECTED_ALL.replace('{0}', numCheckPages);
      } else {
        numPagesSelected = wagtailConfig.STRINGS.NUM_PAGES_SELECTED_PLURAL.replace('{0}', numCheckPages);
      }
    }
    document.querySelector(`.${BULK_ACTION_NUM_PAGES_IN_LISTING}`).classList.add('u-hidden');
    document.querySelector(`.${BULK_ACTION_NUM_PAGES_SPAN}`).textContent = numPagesSelected;
  }
}

function selectAllPageIdsInListing() {
  checkedState.selectAllInListing = true;
{'fraction_non_alphanumeric': 0.13577057227748354, 'fraction_numerical': 0.0029711923524092495, 'mean_word_length': 5.006206361520559, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, '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': '975', 'n_tokens_mistral': 2546, 'n_tokens_neox': 2552, 'n_words': 379}