aboutsummaryrefslogtreecommitdiff
path: root/public/js/qcm.js
blob: 3ce572495b796eccb6ed440c00827bb7c7f219e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
'use strict';

/* eslint-env browser */
/* exported check */

/*
 * Écrit par Hédy GIRAUDEAU
 *
 */

const check = (id => {
  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 '=':
        label.style.color = '#00BB00';
        break;
      case '~':
        label.style.color = '#FFAA00';
        break;
      default:
        // Empty
    }
  });
});

function shuffle(array) {
  let currentIndex = array.length;
  let temporaryValue;
  let randomIndex;

  // While there remain elements to shuffle...
  while (currentIndex !== 0) {
    // Pick a remaining element...
    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;
  }

  return array;
}

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);

document.check = check;