<commit_before>import {slide, calculateLayerHeight, calculateContainerHeight} from './layer-utils'

const layers = Array.from(document.querySelectorAll('.c-layer'))
const layerContainers = Array.from(document.querySelectorAll('.c-layers'))


const staggeredBackwards = (backwardsLayers, previousLayer) => {

  previousLayer = previousLayer || backwardsLayers[backwardsLayers.length - 1].previousElementSibling
  return previousLayer !== null
    ? new Promise((resolve) => {
      backwardsLayers.forEach((bLayer, index) => {
        index === 0
          ? bLayer.parentNode.insertBefore(bLayer, previousLayer)
          : bLayer.parentNode.insertBefore(bLayer, backwardsLayers[index - 1])
      })
      setTimeout(() => {
        resolve(staggeredBackwards(backwardsLayers, backwardsLayers[backwardsLayers.length - 1].previousElementSibling))
      }, 200)
    })
    : Promise.resolve(backwardsLayers)
}

const animateFrontLayers = layer => slide(layer.parentNode.lastElementChild, 480, 300)
  .then(oldFrontLayer => new Promise((resolve) => {
    oldFrontLayer.classList.add('c-layer--visited')
    oldFrontLayer.classList.remove('c-layer--selected')
    const backwardsLayers = [oldFrontLayer]
    let previousLayer = oldFrontLayer.previousElementSibling
    while (previousLayer !== layer) {
      backwardsLayers.push(previousLayer)
      previousLayer = previousLayer.previousElementSibling
    }
    setTimeout(() => {
      resolve(backwardsLayers)
    }, 150)
  }))
  .then(backwardsLayers => Promise.all(backwardsLayers.map((blayer, index) => {
    return new Promise(resolve => {
      setTimeout(() => {
        blayer.classList.add('c-layer--hide')
        resolve(blayer)
      }, 150 * index)
    })
  })))
  .then(data => new Promise(resolve => {
    setTimeout(() => {
      resolve(data)
    }, 150)
  }))
  .then(backwardsLayers => new Promise((resolve) => {
    layer.classList.add('c-layer--selected')
    resolve(layer)
    staggeredBackwards(backwardsLayers, layer)
      .then(backwardsLayers => backwardsLayers.forEach((blayer, index) => {
        setTimeout(() => {
          blayer.classList.remove('c-layer--hide')
        }, 200 * (index + 1))
      }))
  }))

const animateNewFrontLayer = layer => {
  const frontHeight = calculateLayerHeight(layer)
  const containerHeight = calculateContainerHeight(layer)
  return Promise.all([slide(layer, frontHeight, 500, true), slide(layer.parentNode, containerHeight, 500, true)])
}

const moveForward = layer => {
  layers.forEach(label => label.removeEventListener('click', moveForwardListener))

  animateFrontLayers(layer)
    .then(animateNewFrontLayer)
    .then(() => {
      layers.forEach(label => label.addEventListener('click', moveForwardListener))<commit_msg>improved performance<commit_after>import {slide, calculateLayerHeight, calculateContainerHeight} from './layer-utils'

const layers = Array.from(document.querySelectorAll('.c-layer'))
const layerContainers = Array.from(document.querySelectorAll('.c-layers'))


const staggeredBackwards = (backwardsLayers, previousLayer) => {

  previousLayer = previousLayer || backwardsLayers[backwardsLayers.length - 1].previousElementSibling
  return previousLayer !== null
    ? new Promise((resolve) => {
      backwardsLayers.forEach((bLayer, index) => {
        index === 0
          ? bLayer.parentNode.insertBefore(bLayer, previousLayer)
          : bLayer.parentNode.insertBefore(bLayer, backwardsLayers[index - 1])
      })
      setTimeout(() => {
        resolve(staggeredBackwards(backwardsLayers, backwardsLayers[backwardsLayers.length - 1].previousElementSibling))
      }, 200)
    })
    : Promise.resolve(backwardsLayers)
}

const animateFrontLayers = layer => slide(layer.parentNode.lastElementChild, 480, 300)
  .then(oldFrontLayer => new Promise((resolve) => {
    oldFrontLayer.classList.add('c-layer--visited')
    oldFrontLayer.classList.remove('c-layer--selected')
    const backwardsLayers = [oldFrontLayer]
    let previousLayer = oldFrontLayer.previousElementSibling
    while (previousLayer !== layer) {
      backwardsLayers.push(previousLayer)
      previousLayer = previousLayer.previousElementSibling
    }
    setTimeout(() => {
      resolve(backwardsLayers)
    }, 150)
  }))
  .then(backwardsLayers => Promise.all(backwardsLayers.map((blayer, index) => {
    return new Promise(resolve => {
      setTimeout(() => {
        blayer.classList.add('c-layer--hide')
        resolve(blayer)
      }, 150 * index)
    })
  })))
  .then(data => new Promise(resolve => {
    setTimeout(() => {
      resolve(data)
    }, 150)
  }))
  .then(backwardsLayers => new Promise((resolve) => {
    staggeredBackwards(backwardsLayers, layer)
      .then(backwardsLayers => {
        resolve(layer)
        return backwardsLayers
      })
      .then(backwardsLayers => backwardsLayers.forEach((blayer, index) => {
        setTimeout(() => {
          blayer.classList.remove('c-layer--hide')
        }, 200 * (index + 1))
      }))
  }))

const animateNewFrontLayer = layer => {
  const frontHeight = calculateLayerHeight(layer)
  const containerHeight = calculateContainerHeight(layer)
  layer.classList.add('c-layer--selected')
  return Promise.all([slide(layer, frontHeight, 500, true), slide(layer.parentNode, containerHeight, 500, true)])
}

const moveForward = layer => {
  layers.forEach(label => label.removeEventListener('click', moveForwardListener))

  animateFrontLayers(layer)
    .then(animateNewFrontLayer)
    .then(() => {
      layers.forEach(label => label.addEventListener('click', moveForwardListener))
{'fraction_non_alphanumeric': 0.13688554000717618, 'fraction_numerical': 0.011481880157875853, 'mean_word_length': 3.8775153105861766, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 54, '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': '3198315', 'n_tokens_mistral': 1690, 'n_tokens_neox': 1604, 'n_words': 259}