All files / coral-component-table/src/scripts TableUtil.js

98.39% Statements 61/62
91.18% Branches 31/34
100% Functions 17/17
98.39% Lines 61/62

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 167 168 169 170 171 172 173 174 175 176 177 178                          2x 1052x         2x 258x         2x 1884x         2x 4117x         2x 5706x   5706x       5706x         2x 566x   566x 132x 264x   264x 270x   270x 270x         434x   434x 62x 72x 58x     14x     62x 52x     372x 64x 32x   32x 20x         308x 308x       258x         2x 11110x   11110x 20678x     11110x         2x 1986x         2x 4432x 4432x 4494x 4494x     4432x         2x 1398x         2x 1312x         2x 1920x         2x 1366x 3682x                                     2x                
/**
 * Copyright 2019 Adobe. All rights reserved.
 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. You may obtain a copy
 * of the License at http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under
 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
 * OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */
 
/** @ignore */
const isTableHeaderCell = node => node.nodeName === 'TH' && node.getAttribute('is') === 'coral-table-headercell';
 
/** @ignore */
const isTableCell = node => node.nodeName === 'TD' && node.getAttribute('is') === 'coral-table-cell';
 
/** @ignore */
const isTableRow = node => node.nodeName === 'TR' && node.getAttribute('is') === 'coral-table-row';
 
/** @ignore */
const isTableBody = node => node.nodeName === 'TBODY' && node.getAttribute('is') === 'coral-table-body';
 
/** @ignore */
const getIndexOf = (el) => {
  const parent = el.parentNode;
  if (!parent) {
    return -1;
  }
 
  return Array.prototype.indexOf.call(parent.children, el);
};
 
/** @ignore */
const getSiblingsOf = (el, selector, type) => {
  const stack = [];
 
  // Returns siblings of el
  if (!type) {
  I  ['previousElementSibling', 'nextElementSibling'].forEach((direction) => {
      let sibling = el;
      while (sibling[direction]) {
        sibling = sibling[direction];
        if (sibling.matches(selector)) {
          stack.push(sibling);
        }
      }
    });
  } else {
    const direction = type.indexOf('next') === 0 ? 'nextElementSibling' : 'previousElementSibling';
 
    // All following siblings of el up to but not including the element matched by the selector
    if (type.indexOf('Until') !== -1) {
      const matches = function () {
        if (typeof selector === 'string') {
          return el[direction].matches(selector);
        }
 
        Ereturn el[direction] === selector;
      };
 
      while (el[direction] && !matches()) {
        stack.push(el = el[direction]);
      }
    }
    // All following siblings of el filtered by a selector.
    else if (type.indexOf('All') !== -1) {
      while (el[direction]) {
        el = el[direction];
        if (el.matches(selector)) {
          stack.push(el);
        }
      }
    }
    // Returns the sibling only if it matches that selector.
    else {
      const sibling = el[direction];
      return sibling && sibling.matches(selector) ? sibling : null;
    }
  }
 
  return stack;
};
 
/** @ignore */
const listToArray = (list) => {
  const res = [];
  for (let i = 0, listCount = res.length = list.length ; i < listCount ; i++) {
    res[i] = list[i];
  }
  return res;
};
 
/** @ignore */
const getColumns = (colgroup) => listToArray(colgroup.querySelectorAll('col[is="coral-table-column"]'));
 
/** @ignore */
const getRows = (sections) => {
  let rows = [];
 
  sections.forEach((section) => {
    if (section) {
      rows = rows.concat(listToArray(section.querySelectorAll('tr[is="coral-table-row"]')));
    }
  });
 
  return rows;
};
 
/** @ignore */
const getCells = (row) => listToArray(row.querySelectorAll('td[is="coral-table-cell"], th[is="coral-table-headercell"]'));
 
/** @ignore */
const getContentCells = (row) => listToArray(row.querySelectorAll('td[is="coral-table-cell"]'));
 
/** @ignore */
const getHeaderCells = (row) => listToArray(row.querySelectorAll('th[is="coral-table-headercell"]'));
 
/** @ignore */
const getCellByIndex = (row, index) => getCells(row).filter(cell => getIndexOf(cell) === index)[0] || null;
E
/**
 Enumeration for {@link TableHead}, {@link TableBody} and {@link TableFoot} divider values.
 
 @typedef {Object} TableSectionDividerEnum
 
 @property {String} NONE
 No divider.
 @property {String} ROW
 Row divider.
 @property {String} COLUMN
 Column divider.
 @property {String} CELL
 Row and Column divider.
 */
const divider = {
  NONE: 'none',
  ROW: 'row',
  COLUMN: 'column',
  CELL: 'cell'
};
 
/**
 Enumeration for {@link TableColumn} alignment options.
 
 @typedef {Object} TableColumnAlignmentEnum
 
 @property {String} LEFT
 Left alignment.
 @property {String} CENTER
 Center alignment.
 @property {String} RIGHT
 Right alignment.
 */
const alignment = {
  LEFT: 'left',
  CENTER: 'center',
  RIGHT: 'right'
};
 
export {
  divider,
  alignment,
  getColumns,
  getCells,
  getContentCells,
  getHeaderCells,
  getCellByIndex,
  getIndexOf,
  getSiblingsOf,
  getRows,
  isTableHeaderCell,
  isTableCell,
  isTableRow,
  isTableBody
};