Advertisement
petar_bonov

Transpose banner table

Mar 19th, 2024
633
0
326 days
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5.01 KB | None | 0 0
  1. <style>
  2. table {
  3.     padding-bottom: 100px;
  4. }
  5. table td:first-child {
  6.     white-space: nowrap;    
  7. }
  8.    
  9. table tr:first-child td {
  10.     writing-mode: vertical-lr;
  11.     transform: rotate(-180deg);
  12.     text-align: left;
  13. }
  14. table tr:first-child td b {
  15.     display: inline-block;
  16.     font-weight: normal;
  17.     letter-spacing: 0.1px;
  18. }
  19. table tr:first-child td b a {
  20.     padding: 5px 2px 0 2px;
  21. }
  22.  
  23. table .banner-preview {
  24.     display: none;
  25.     position: absolute;
  26.     bottom: 108%;
  27.  
  28.     writing-mode: horizontal-tb;
  29.     transform: rotate(-180deg);
  30.     left: calc(100% + 0.4em);
  31.  
  32.     background: white;
  33.     border: 1px solid #888;
  34.     padding: 5px;
  35. }
  36. table .move-right .banner-preview {
  37.     left: unset;
  38.     right: calc(100% + 0.6em);
  39. }
  40.  
  41. table .banner-preview span {
  42.     display: inline-block;
  43.     color: black;
  44.     margin-top: 5px;
  45.     font-size: 14px;
  46. }
  47.  
  48. table a:hover .banner-preview {
  49.     display: block;
  50. }
  51.  
  52. /* transposed table */
  53. table.transposed {
  54.     margin-bottom: 250px;
  55. }
  56. table.transposed tr:first-child td {
  57.     transform: unset;
  58.     writing-mode: unset;
  59. }
  60. table.transposed tr:first-child td b {
  61.     font-weight: bold;
  62.     white-space: nowrap;
  63. }
  64. table.transposed td:nth-child(n+2) {
  65.     text-align: right;
  66. }
  67. table.transposed .banner-preview {
  68.     left: 250px;
  69.     right: unset;
  70.     bottom: unset;
  71.     transform: translateY(3px);
  72.     writing-mode: unset;
  73. }
  74. table.transposed .banner-preview img {
  75.     display: block;
  76. }
  77. </style>    
  78.  
  79. <script>
  80. const BASE_FOLDER = "https://st.gsmarena.com/imgroot/static/banners/self/";
  81.  
  82. window.addEventListener("DOMContentLoaded", function () {
  83.     const table = document.querySelector("table");
  84.     const links = table.querySelectorAll("a");
  85.     for (const link of links) {
  86.         const match = link.innerText.match(/(?:(?:\[[^\]]+\])|(?:[dma]-))(.+)/);
  87.         if (!match) continue;
  88.  
  89.         const container = document.createElement("div");
  90.         container.classList = "banner-preview";
  91.  
  92.         const image = document.createElement("img");
  93.         image.src = BASE_FOLDER + match[1];
  94.  
  95.         const text = document.createElement("span");
  96.         text.innerText = link.innerText;
  97.  
  98.         container.appendChild(image);
  99.         container.appendChild(text);
  100.         link.appendChild(container);
  101.  
  102.         link.onmouseenter = function () {
  103.             const banner = this.querySelector(".banner-preview");
  104.             const rect = banner.getBoundingClientRect();
  105.             const image = banner.querySelector("img")
  106.            
  107.             setTimeout(function () {
  108.                 if (rect.right > screen.width) {
  109.                     banner.style.left = (rect.right - screen.width + 30) + "px";
  110.                 } else if (rect.left < 0) {
  111.                    banner.style.left = (rect.left) + "px";
  112.                } else {
  113.                    banner.style.left = "";
  114.                }
  115.            }, 0);
  116.        };
  117.        link.onmouseleave = function () {
  118.            const banner = this.querySelector(".banner-preview");
  119.            banner.style.left = "";
  120.        };
  121.        if (link.getBoundingClientRect().left < 500) link.classList.add("move-right");
  122.    }
  123.  
  124.    function transposeTable() {
  125.        const isTransposed = table.classList.contains("transposed");
  126.        const tbody = table.tBodies[0];
  127.        const rows = tbody.querySelectorAll("tr");
  128.        const rowColors = [];
  129.        const data = [];
  130.        for (let i = 0; i < rows.length; i++) {
  131.            data[i] = [];
  132.            const cells = rows[i].querySelectorAll("td");
  133.            for (let j = 0; j < cells.length; j++) {
  134.                data[i][j] = rows[i].removeChild(cells[j]);
  135.            }
  136.            if (!isTransposed) rowColors[i] = rows[i].bgColor || rows[i].style.background;
  137.            rows[i].remove();
  138.        }
  139.  
  140.        if (isTransposed) {
  141.            const colgroup = table.querySelector("colgroup");
  142.            for (const col of colgroup.querySelectorAll("col")) {
  143.                rowColors.push(col.style.background);
  144.            }
  145.            colgroup.remove();
  146.        }
  147.  
  148.        const newRows = [];
  149.        for (let i = 0; i < data[0].length; i++) {
  150.            const newRow = document.createElement("tr");
  151.            if (isTransposed) newRow.style.background = rowColors[i];
  152.            tbody.appendChild(newRow);
  153.            newRows.push(newRow);
  154.        }
  155.  
  156.        for (let i = 0; i < newRows.length; i++) {
  157.            const row = newRows[i];
  158.            for (let j = 0; j < data.length; j++) {
  159.                row.appendChild(data[j][i]);
  160.            }
  161.        }
  162.  
  163.        if (!isTransposed) {
  164.            const colgroup = document.createElement("colgroup");
  165.            table.appendChild(colgroup);
  166.            for (let c of rowColors) {
  167.                const col = document.createElement("col");
  168.                col.style.background = c;
  169.                colgroup.appendChild(col);
  170.            }
  171.        }
  172.  
  173.        table.classList.toggle("transposed");
  174.    }
  175.    table.onclick = transposeTable;
  176. });
  177. </script>
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement