From 97eef5dfea4e0a593d3505b596e061e061a95f49 Mon Sep 17 00:00:00 2001 From: Christophe-Marie Duquesne Date: Sun, 6 Dec 2015 16:39:58 +0100 Subject: [PATCH] =?UTF-8?q?R=C3=A9usinage=20du=20javascript=20pour=20avoir?= =?UTF-8?q?=20les=20mots=20compl=C3=A8tement=20dans=20l'ordre=20alphab?= =?UTF-8?q?=C3=A9tique.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- creuille.js | 197 +++++++++++++++++++++------------------------------- index.html | 4 +- 2 files changed, 80 insertions(+), 121 deletions(-) diff --git a/creuille.js b/creuille.js index 0aa17c4..0767907 100644 --- a/creuille.js +++ b/creuille.js @@ -1,135 +1,94 @@ -function trouveElement(id) +function changeDeSens(traductions) { - return document.getElementById(id); -} - -function obtenirLAttribut(element, attribut) -{ - return element.getAttribute(attribut); -} - -function definirLAttribut(element, attribut, valeur) -{ - return element.setAttribute(attribut, valeur); -} - -function changeDeSens(window, traductions) -{ - var element = trouveElement('mots') - var langueDesClefsActuelle = obtenirLAttribut(element, 'data-langue'); + var langue = $( "#mots" ).attr("data-langue"); var autre = {'anglais': 'francais', 'francais': 'anglais'}; - var langueDesClefsNouvelle = autre[langueDesClefsActuelle]; - definirLAttribut(element, 'data-langue', langueDesClefsNouvelle); - construitListe(window, traductions) + $( "#mots" ).attr("data-langue", autre[langue]); + construitListe(traductions); } -function construitListe(window, traductions) +function metAJourLienChange(langue){ + var de = ' Anglais '; + var vers = 'Français'; + var fleche = ' → '; + if (langue == 'francais') { + var tmp = de; + de = vers; + vers = tmp; + } + $( "#lienChange" ).html(de + fleche + vers); +} + +function sansAccents(mot){ + // Faute d'une bibliotheque unidecode, nous nous limitons aux lettres + // accentuées du français. + // https://fr.wikipedia.org/wiki/Diacritiques_utilisés_en_français + return mot + .toLowerCase() + .replace("à", "a") + .replace("â", "a") + .replace("ç", "c") + .replace("é", "e") + .replace("è", "e") + .replace("ê", "e") + .replace("ë", "e") + .replace("î", "i") + .replace("ï", "i") + .replace("ô", "o") + .replace("ù", "u") + .replace("ü", "u"); +} + +function construitListe(traductions) { - var document = window.document; - document.creeElement = document.createElement; + var langue = $( "#mots" ).attr("data-langue"); - var vrais_mots = traductions["vrais mots"]; + metAJourLienChange(langue); - var faux_mots = traductions["faux mots"]; + traductions = traductions["vrais mots"].concat(traductions["faux mots"]); + // tri par ordre alphabétique de la langue de départ + traductions.sort(function(traduction1, traduction2){ + return (sansAccents(traduction1[langue]) > + sansAccents(traduction2[langue])); + }); - function cache(e) - { - e.style.visibility = 'hidden'; - } + $( "#mots" ).html(""); + $( "#index" ).html(""); - function montre(e) - { - e.style.visibility = 'visible'; - } + var lettreAlphabet = ''; + for (var i=0; i < traductions.length; i++) { + var mot = traductions[i]; + var l = sansAccents(mot[langue]).charAt(0).toUpperCase(); - var tous_les_mots = vrais_mots.concat(faux_mots); - - var i; - var lien = trouveElement('lienChange'); - var index = trouveElement('index'); - var mots = trouveElement('mots'); - var langueDesClefs = obtenirLAttribut(mots, 'data-langue'); - var lettres = new Array(26); - var noeud; - - function enleveLesAccents(s) { - s = s.replace("é", "e"); - return s; - } - - var lienFR = 'Français'; - var lienANG = 'Anglais'; - var lienSource = lienFR; - var lienDestination = lienANG; - if (langueDesClefs === 'anglais') { - lienSource = lienANG; - lienDestination = lienFR; - } - lien.innerHTML = lienSource + ' → ' + lienDestination; - - mots.innerHTML = ''; - index.innerHTML = ''; - - - for (i = 0; i < 26; ++i) { - - var lettre = String.fromCharCode(65 + i); - - lettres[i] = noeud = document.creeElement('div'); - noeud.className = 'groupe-lettre'; - noeud.enfants = 0; - var ancre = document.creeElement('a'); - ancre.name = lettre; - noeud.appendChild(ancre); - var titre = document.creeElement('h3'); - titre.innerHTML = lettre; - noeud.appendChild(titre); - } - - // renvoie un nombre entre 0 et 25 - function indiceDeLaPremiereLettre(m) { - - var motSansAccent = enleveLesAccents(m); - var motEnMinuscule = motSansAccent.toLowerCase(); - var resultat = motEnMinuscule.charCodeAt(0) - "a".charCodeAt(0); - return resultat; - } - - for (i = 0; i < tous_les_mots.length; ++i) { - - var mot = tous_les_mots[i]; - - noeud = document.createElement('div'); - noeud.className = 'definition'; - - var spanAnglais = ' ' + mot.anglais + ' '; - var spanFrancais = '' + mot.francais + ''; - - var spanClef = spanFrancais; - var spanValeur = spanAnglais; - var indice = indiceDeLaPremiereLettre(mot.francais); - if (langueDesClefs === 'anglais') { - spanClef = spanAnglais; - spanValeur = spanFrancais; - indice = indiceDeLaPremiereLettre(mot.anglais); + if (l != lettreAlphabet) { + lettreAlphabet = l; + $( "#index" ).append( + $("") + .attr("href", "#" + lettreAlphabet) + .html(lettreAlphabet) + ); + $( "#mots" ).append( + $("
") + .attr("class", "groupe-lettre") + .append($("").attr("name", lettreAlphabet)) + .append($("

").html(lettreAlphabet)) + ); } - noeud.innerHTML = '· ' + spanClef + ' : ' + spanValeur; - - lettres[indice].appendChild(noeud); - lettres[indice].enfants++; - } - - for (i = 0; i < 26; ++i) { - var lettre = String.fromCharCode(65 + i); - noeud = lettres[i]; - if (noeud.enfants > 1) { - mots.appendChild(noeud); - var lettreIndex = document.creeElement('a'); - lettreIndex.href = '#' + lettre; - lettreIndex.innerHTML = lettre; - index.appendChild(lettreIndex); + var cle = ' ' + mot.anglais + ' '; + var val = '' + mot.francais + ''; + if (langue == "francais") { + var tmp = cle; + cle = val; + val = tmp; } + $( "#mots" ) + .children() + .last() + .append( + $("
") + .attr("class", "definition") + .html('· ' + cle + ' : ' + val) + ); } } diff --git a/index.html b/index.html index 81e63a8..acd7273 100644 --- a/index.html +++ b/index.html @@ -11,8 +11,8 @@