From 3e9313d500158b0c3823b90d885efde263af106e Mon Sep 17 00:00:00 2001 From: Mathieu Jaumotte Date: Thu, 20 May 2021 22:42:52 +0200 Subject: [PATCH] improve --- index3.html | 10 +++- js/index3.js | 116 ++++++++++++++++++++++++++-------------------- scss/styles3.scss | 2 +- 3 files changed, 75 insertions(+), 53 deletions(-) diff --git a/index3.html b/index3.html index e83df32..0188c84 100644 --- a/index3.html +++ b/index3.html @@ -111,7 +111,15 @@ diff --git a/js/index3.js b/js/index3.js index 52372d9..39a6a80 100644 --- a/js/index3.js +++ b/js/index3.js @@ -1,61 +1,79 @@ +/* +* Add heads elements into row +*/ +const createHeadsIntoRow = (table, row) => { + const cells = row.querySelectorAll(".td"); + cells.forEach((cell, i) => { + createHeadIntoCell(table, cell, i); + }); +}; /* -* Limites de basculements +* Add head into cell */ -const minLG = window.matchMedia("(min-width: 801px)"); -const maxMD = window.matchMedia("(max-width: 800px)"); -const maxSM = window.matchMedia("(max-width: 450px)"); +const createHeadIntoCell = (table, cell, i) => { + const elem = document.createElement("div"); + elem.classList.add("head"); + elem.innerHTML = theadContent(table, i); + cell.appendChild(elem); +} +/* +* Get text in columns thead +*/ +const theadContent = (table, i) => { + const thead = table.querySelectorAll(".thead .td"); + let content; + thead.forEach((item, j) => { + if (i === j) { + content = item.textContent; + } + }, i); + return content; +}; + +/* +* Remove heads from element +*/ +const removeHeadsFrom = (element) => { + const heads = element.querySelectorAll(".head"); + heads.forEach(head => { + head.parentNode.removeChild(head); + }); +}; + +/* +* Insert head before cell +*/ +const insertHeadBeforeCell = (row, cell) => { + let head = cell.querySelector('.head'); + head.classList.add("td"); + row.insertBefore(head, cell); +}; + +/* +* Loop on each table +*/ tables.forEach(t => { - const table = document.querySelector(t.id); + const table = document.querySelector(`#${t.id}`); /* - * Récupère le texte des entêtes de colonnes + * Responsive rules limits */ - const theadContent = i => { - const thead = table.querySelectorAll(".thead .td"); - let content; - thead.forEach((item, j) => { - if (i === j) { - content = item.textContent; - } - }, i); - return content; - }; + const minLG = window.matchMedia(`(min-width: ${t.md_lg + 1}px)`); + const maxMD = window.matchMedia(`(max-width: ${t.md_lg}px)`); + const maxSM = window.matchMedia(`(max-width: ${t.sm_md}px)`); /* - * Ajoute un nouvel élément thead dans une cellule - */ - const createHeadInto = (cell, i) => { - const elem = document.createElement("div"); - elem.classList.add("head"); - elem.innerHTML = theadContent(i); - cell.appendChild(elem); - } - - /* - * Remove heads from element - */ - const removeHeadsFrom = (element) => { - const heads = element.querySelectorAll(".head"); - heads.forEach(head => { - head.parentNode.removeChild(head); - }); - }; - - /* - * Règle pour les écrans moyens + * Medium screens rule */ const mediumScreen = rule => { if (rule.matches) { console.log('medium'); const rows = [...table.querySelectorAll(".tr")]; rows.shift(); // vire le premier rang - rows.forEach(row => {; - const cells = row.querySelectorAll(".td"); - cells.forEach((cell, i) => { - createHeadInto(cell, i); - }); + rows.forEach(row => { + createHeadsIntoRow(table, row); }); } else { console.log('not medium'); @@ -64,7 +82,7 @@ tables.forEach(t => { }; /* - * Règle pour les petits écrans + * Small screens rule */ const smallScreen = (rule) => { if (rule.matches) { @@ -73,10 +91,8 @@ tables.forEach(t => { rows.shift(); rows.forEach(row => {; const cells = row.querySelectorAll(".td"); - cells.forEach((cell, i) => { - let head = cell.querySelector('.head'); - head.classList.add("td"); - row.insertBefore(head, cell); + cells.forEach(cell => { + insertHeadBeforeCell(row, cell); }); }); } else { @@ -85,11 +101,9 @@ tables.forEach(t => { rows.shift(); rows.forEach(row => {; removeHeadsFrom(row); + // Large screen exception ! if (!minLG.matches) { - const cells = row.querySelectorAll(".td"); - cells.forEach((cell, i) => { - createHeadInto(cell, i); - }); + createHeadsIntoRow(table, row); } }); } diff --git a/scss/styles3.scss b/scss/styles3.scss index b179a36..ced9694 100644 --- a/scss/styles3.scss +++ b/scss/styles3.scss @@ -31,7 +31,7 @@ div.table { &:nth-child(1) { border-left: 1px solid grey; } - &:nth-child(1) { flex: 1; } // les largeurs de colonnes + &:nth-child(1) { flex: 1; } &:nth-child(2) { flex: 4; } &:nth-child(3) { flex: 6; } &:nth-child(4) { flex: 2; }