From 1c0df5f469fef57cdc9b60d24a36c9de3943dcc9 Mon Sep 17 00:00:00 2001 From: ache Date: Thu, 1 Feb 2018 20:34:58 +0100 Subject: Improving general readability --- public/js/qcm.js | 113 +++++++++++++++++++++++++------------------------------ 1 file changed, 52 insertions(+), 61 deletions(-) (limited to 'public/js') diff --git a/public/js/qcm.js b/public/js/qcm.js index a0eeee2..e6b8190 100644 --- a/public/js/qcm.js +++ b/public/js/qcm.js @@ -8,80 +8,71 @@ /* eslint-env browser */ /* exported check */ -function check(id/*, tab*/) { - const fieldQCM = document.getElementById(id); +function check(id/* , tab */) { + const fieldQCM = document.getElementById(id); - Array.from(fieldQCM.getElementsByTagName('INPUT')).forEach((input) => { - if( input.type != 'checkbox' ) - return; - - - const label = document.querySelector("label[for='" + input.id + "']"); - if (input.checked) { - Array.from(label.getElementsByClassName('hiden_block_quote')).forEach(((child) => { - child.classList.remove('hiden_block_quote'); - })); - } - switch (input.className) { - case '!': { - label.style.color = '#FF0000'; - } - break; - case '=': { - console.log("Good") - label.style.color = '#00BB00'; - } - break; - case '~': { + Array.from(fieldQCM.getElementsByTagName('INPUT')).forEach(input => { + if (input.type !== 'checkbox') { + return; + } - label.style.color = '#FFAA00'; - } - break; - default: { - console.log("Oups") - // empty - } - } - }); + const label = document.querySelector('label[for=\'' + input.id + '\']'); + if (input.checked) { + Array.from(label.getElementsByClassName('hiden_block_quote')).forEach((child => { + child.classList.remove('hiden_block_quote'); + })); + } + switch (input.className) { + case '!': + label.style.color = '#FF0000'; + break; + case '=': + label.style.color = '#00BB00'; + break; + case '~': + label.style.color = '#FFAA00'; + break; + default: + // Empty + } + }); } function shuffle(array) { - var currentIndex = array.length, temporaryValue, randomIndex; + let currentIndex = array.length; + let temporaryValue; + let randomIndex; // While there remain elements to shuffle... - while (0 !== currentIndex) { - + while (currentIndex !== 0) { // Pick a remaining element... - randomIndex = Math.floor(Math.random() * currentIndex); - currentIndex -= 1; + randomIndex = Math.floor(Math.random() * currentIndex); + currentIndex -= 1; // And swap it with the current element. - temporaryValue = array[currentIndex]; - array[currentIndex] = array[randomIndex]; - array[randomIndex] = temporaryValue; - } + temporaryValue = array[currentIndex]; + array[currentIndex] = array[randomIndex]; + array[randomIndex] = temporaryValue; + } - return array; + return array; } -document.addEventListener('DOMContentLoaded', function () { - const listQCM = Array.from(document.getElementsByClassName('qcm_field')); - listQCM.forEach((field) => { - let listInput = []; - Array.from(field.getElementsByTagName('INPUT')).forEach((input) => { - const label = document.querySelector("label[for='" + input.id + "']"); - listInput.push({'input': input, 'label': label}); - field.removeChild(input); - field.removeChild(label); - }); - listInput = shuffle(listInput); - Array.from(listInput).forEach((couple) => { - field.insertBefore(couple['label'], field.childNodes[0]); field.insertBefore(couple['input'], couple['label']); - }); +document.addEventListener('DOMContentLoaded', () => { + const listQCM = Array.from(document.getElementsByClassName('qcm_field')); + listQCM.forEach(field => { + let listInput = []; + Array.from(field.getElementsByTagName('INPUT')).forEach(input => { + const label = document.querySelector('label[for=\'' + input.id + '\']'); + listInput.push({input, label}); + field.removeChild(input); + field.removeChild(label); }); - - + listInput = shuffle(listInput); + Array.from(listInput).forEach(couple => { + field.insertBefore(couple.label, field.childNodes[0]); + field.insertBefore(couple.input, couple.label); + }); + }); }, false); - - -- cgit v1.2.3-54-g00ecf