github.com/munnerz/test-infra@v0.0.0-20190108210205-ce3d181dc989/gubernator/static/pr.js (about)

     1  const upArrow = '▲';
     2  const downArrow = '▼';
     3  
     4  function sortRows(rows, col, colName, dir) {
     5    // customize sorting functions for different columns
     6    let extract = el => el.innerText.replace(/^\s+|\s+$/g, '');
     7    let fn = x => x;
     8    switch (colName.trim()) {
     9      case 'Number':
    10        fn = Number;
    11        break;
    12      case 'Size':
    13        fn = x => ['XS', 'S', 'M', 'L', 'XL', 'XXL'].indexOf(x);
    14        break;
    15      case 'Updated':
    16        extract = el => Number(el.firstChild.dataset.epoch);
    17        break;
    18      case 'Author':
    19      case 'Assignees':
    20        fn = x => x.toLowerCase();
    21        break;
    22    }
    23  
    24    for (let i = 0; i < rows.length; i++) {
    25      rows[i].index = i;
    26    }
    27  
    28    rows.sort(function(a, b) {
    29      let valA = fn(extract(a.children[col]));
    30      let valB = fn(extract(b.children[col]));
    31      if (valA === valB) {
    32        return a.index - b.index;  // make the sort stable
    33      } else if (valA < valB) {
    34        return -dir;
    35      } else {
    36        return dir;
    37      }
    38    });
    39  }
    40  
    41  function sortColumn(evt) {
    42    const target = evt.target;
    43    const reverse = target.innerText[0] == upArrow;
    44  
    45    let col, i = 0;
    46  
    47    // Remove all directional arrows.
    48    for (let sibling of target.parentElement.children) {
    49      const first = sibling.innerText[0];
    50      if (first == upArrow || first == downArrow) {
    51        sibling.innerText = sibling.innerText.slice(2);
    52      }
    53      if (sibling === target) {
    54        col = i;
    55      }
    56      i++;
    57    }
    58  
    59    const table = target.closest('table');
    60    let tbody = table.tBodies[0];
    61    const rows = Array.prototype.slice.call(tbody.children);
    62    tbody.remove();               // clear old rows
    63    tbody = table.createTBody();  // create a new body
    64  
    65    sortRows(rows, col, target.innerText, reverse ? -1 : 1);
    66  
    67    for (let row of rows) {
    68      tbody.appendChild(row);
    69    }
    70  
    71    // Add arrow.
    72    target.innerText = (reverse ? downArrow : upArrow) + ' ' + target.innerText;
    73  
    74    return true;
    75  }
    76  
    77  window.addEventListener('load', function() {
    78    for (let th of document.getElementsByTagName('TH')) {
    79      th.addEventListener('click', sortColumn, false);
    80    }
    81  });