All files / pagination/utils dom-dimensions.js

8.2% Statements 5/61
0% Branches 0/45
0% Functions 0/8
8.62% Lines 5/58

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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166                  1x                                         1x               1x                                                                             1x                                                                                                           1x                                                                    
import { findKey, reduce } from 'lodash';
 
/**
 * Gets browser computed style of element
 *
 * @param { DOM element } element-
 * @param { array } attributes - list of attributes you want to get from the element
 *  @return { object } of attributes & their values
 */
export const getComputedStyles = (element, attributes = []) => {
  if (!window) return null;
  const computedStyles = window.getComputedStyle(element);
  const parsedStyles = reduce(attributes, (allAttributes, attr, key) => {
    allAttributes = allAttributes || {};
 
    const thisAttr = computedStyles[attr] || null;
    if (thisAttr) {
      allAttributes[attr] = parseFloat((thisAttr.split('px'))[0]);
    }
    return allAttributes;
  }, {});
 
  return parsedStyles;
};
 
/**
 * Gets full calculated width of element, including left & right margins
 *
 * @param { DOM elememnt } element
 */
export const calculateElementWidth = (element) => {
  if (!window) return null;
  const { offsetWidth } = element;
  const { marginLeft, marginRight } = getComputedStyles(element, ['marginLeft', 'marginRight']);
 
  return marginLeft + offsetWidth + marginRight;
};
 
export const getPageToShow = ({
  currentPage,
  scrollThresholds,
  viewportFlush
}) => {
  const matchingThresholds = reduce(scrollThresholds,
    (matches, threshold, key) => {
      matches = matches || {};
      const { low, high } = threshold;
      const isInThreshold = viewportFlush >= low && viewportFlush <= high;
      if (isInThreshold) {
        matches[key] = threshold;
      }
      return matches;
    },
    {});
 
  const isOnSamePage = !!matchingThresholds[currentPage];
  const pageToShow = isOnSamePage
    ? currentPage
    : findKey(
      scrollThresholds,
      th => viewportFlush >= th.low && viewportFlush <= th.high
    );
 
  return pageToShow;
};
 
/**
 * Creates a K:V dictionary of the scroll range of each page
 *
 * @param { number } numberOfColumns
 * @param { number } numberOfPages
 * @param { number } clientWidth
 * @param { boolean } columnsAreOdd
 * @param { number } childWidth
 *
 * @return { object } scrollThreshold
 */
export const getScrollThresholdsByPage = ({
  numberOfColumns,
  numberOfPages,
  clientWidth,
  columnsAreOdd,
  childWidth,
}) => {
  const scrollThresholds = {};
 
  for (let i = 0; i < numberOfPages; i++) {
    const page = i + 1;
    const previousThreshold = scrollThresholds[page - 1] || {};
    const { high: prevHigh } = previousThreshold;
    const currentLowThreshold = prevHigh && prevHigh + 1;
    let low = currentLowThreshold || clientWidth * (page - 1);
    let high = clientWidth * page;
 
    if (columnsAreOdd) {
      // odd number of columns need threshold overlap between the last 2 pages
 
      const columnsMultiplier = Math.ceil(numberOfColumns / numberOfPages);
      const cw = Math.ceil(childWidth);
 
      const isSecondToLastPage = page === numberOfPages - 1;
      const isLastPage = page === numberOfPages;
 
      if (isSecondToLastPage && columnsAreOdd) {
        high = Math.ceil((clientWidth * page) + (cw * columnsMultiplier));
      }
 
      if (isLastPage && columnsAreOdd && page > 1) {
        low = prevHigh - 2 - (cw * (columnsMultiplier + 1));
      }
    }
 
    scrollThresholds[page] = {
      low,
      high
    };
  }
 
  return scrollThresholds;
};
 
/**
 * Takes the flexbox column wrap pagination container and
 * calculates the pertinent information. we need like:
 *
 * @param { DOM element} element
 * @return { object }
 *  - numberOfColumns - number of columns created by flexbox
 *  - numberOfPages - numberOfPages we have
 *  - scrollThresholds - K:V dictionary of the scroll range of each page
 */
export const calculateDimensions = (paginationContainer) => {
  const {
    scrollWidth, clientWidth, offsetWidth, firstElementChild
  } = paginationContainer;
  const numberOfColumns = Math.floor(
    scrollWidth / firstElementChild.offsetWidth
  );
 
  const columnsAreOdd = !!(numberOfColumns % 2);
  const widthCanAccept3Columns = clientWidth >= 1168;
  const extraWideViewHasOverflow = widthCanAccept3Columns && (numberOfColumns > 3) && (numberOfColumns < 6);
 
  const numberOfPages = columnsAreOdd || extraWideViewHasOverflow
    ? Math.ceil((scrollWidth / clientWidth).toFixed(1))
    : Math.round((scrollWidth / clientWidth).toFixed(1));
 
  const childWidth = calculateElementWidth(firstElementChild);
 
  const scrollThresholds = getScrollThresholdsByPage({
    numberOfColumns,
    numberOfPages,
    clientWidth,
    columnsAreOdd,
    childWidth,
    offsetWidth
  });
 
  const dimensionsToReturn = {
    numberOfColumns,
    numberOfPages,
    scrollThresholds,
  };
  return dimensionsToReturn;
};