Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentesRévision précédente
Prochaine révision
Révision précédente
kb_it:javascript [2022/12/09 23:39] befekb_it:javascript [2024/12/12 21:59] (Version actuelle) befe
Ligne 126: Ligne 126:
 ==== Gestion des clics et double-clics sur un même élément ==== ==== Gestion des clics et double-clics sur un même élément ====
  
-<code ts>+<code javascript>
 let clickCounter = 0 let clickCounter = 0
 let timer: NodeJS.Timeout let timer: NodeJS.Timeout
-node.addEventListener('click', () => {+domElement.addEventListener('click', () => {
   clickCounter++   clickCounter++
   if (clickCounter === 1) {   if (clickCounter === 1) {
     timer = setTimeout(() => {     timer = setTimeout(() => {
-      clickCounter = 0;+      clickCounter = 0
       // Simple click       // Simple click
       onSvgNodeClick(node)       onSvgNodeClick(node)
Ligne 145: Ligne 145:
   }   }
 }) })
 +</code>
 +
 +==== Gestion des erreurs ====
 +
 +=== Etendre la classe Error ===
 +
 +<code javascript>
 +class DownloadError extends Error {
 +  response: Response
 +  constructor(message: string, response: Response) {
 +    super(message);
 +    this.response = response;
 +    Object.setPrototypeOf(this, DownloadError.prototype);
 +  }
 +};
 +
 +new DownloadError('Erreur au téléchargement du fichier', response);
 </code> </code>
  
Ligne 205: Ligne 222:
   return text   return text
 } }
 +</code>
 +
 +=== Fixer plusieurs lignes et/ou plusieurs colonnes d'un tableau ===
 +
 +HTML
 +<code html>
 +<table class="software-table" data-sticky-rows="2" data-sticky-cols="3">
 +    ...
 +</table>
 +</code>
 +
 +CSS
 +<code css>
 +table[data-sticky-rows] th,
 +table[data-sticky-rows] td,
 +table[data-sticky-cols] th,
 +table[data-sticky-cols] td {
 +    position: relative;
 +    z-index: -2;
 +}
 +table[data-sticky-rows] tr.sticky th,
 +table[data-sticky-rows] tr.sticky td,
 +table[data-sticky-cols] tr th.sticky,
 +table[data-sticky-rows] tr td.sticky {
 +    position: sticky;
 +    z-index: -1;
 +}
 +table[data-sticky-rows] tr.sticky th.sticky,
 +table[data-sticky-rows] tr.sticky td.sticky {
 +    z-index: 0;
 +}
 +</code>
 +
 +Javascript
 +<code javascript>
 +document.querySelectorAll('table[data-sticky-rows],table[data-sticky-cols]').forEach(table => {
 +    const options = {
 +        rows: parseInt(table.getAttribute('data-sticky-rows')) || 0,
 +        cols: parseInt(table.getAttribute('data-sticky-cols')) || 0
 +    };
 +    const tableStyle = getComputedStyle(table);
 +    let deltaV = deltaH = 0;
 +    // Si le style border-collapse est "separate", il faut en tenir compte
 +    if (tableStyle.borderCollapse === 'separate') {
 +        const borderSpacing = tableStyle.borderSpacing.split(' ');
 +        deltaV += parseFloat(borderSpacing[0]) * 2;
 +        deltaH += parseFloat(borderSpacing[1]) * 2;
 +    }
 +    let top = 0;
 +    // Pour chaque ligne du tableau
 +    table.querySelectorAll('tr').forEach((row, rowI) => {
 +        let left = 0;
 +        let totalColspan = 0;
 +        // Pour chaque cellule de la ligne
 +        [...row.querySelectorAll('th,td')].some((cell, cellI) => {
 +            const colspan = cell.getAttribute('colspan');
 +            colspan && (totalColspan += parseInt(colspan) - 1);
 +            if (rowI < options.rows) {
 +                cell.style.top = `${top}px`
 +            }
 +            if (cellI < options.cols - totalColspan) {
 +                cell.classList.add('sticky');
 +                cell.style.left = `${left}px`;
 +            }
 +            if (rowI >= options.rows && cellI >= options.cols - totalColspan) {
 +                return true;
 +            }
 +            left += cell.offsetWidth + deltaH;
 +        });
 +        if (rowI < options.rows) {
 +            row.classList.add('sticky');
 +            top += row.offsetHeight + deltaV;
 +        }
 +    });
 +});
 +</code>
 +
 +=== Télécharger des données en provenance d'une variable ===
 +
 +<code javascript>
 +function download (data, type = 'text') {
 +  const a = document.createElement('a')
 +  a.setAttribute('download', 'data')
 +  a.href = window.URL.createObjectURL(new Blob([data], { type: type }))
 +  document.body.appendChild(a)
 +  a.click()
 +  a.remove()
 +}
 +
 +const data = Array.from({ length: 256 }, (_, i) => -128 + i)
 +  .sort(() => Math.random() - 0.5)
 +  .join("\n")
 +
 +download(data)
 </code> </code>