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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
| <!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Arbre de décision dynamique</title>
<meta name="Author" content="NoSmoking">
<meta name="DVP-discussion" content="d2116259">
<meta name="description" content="Réalisation d'un arbre de décision dynamique construit à la volée.">
<style>
html, body {margin: 0;padding: 0;font: 1em/1.5 Verdana,sans-serif;}
h1, h2, h3 {margin: .25em 0;color: #069;}
time {float: right;margin: .5em;font-size: 0.9em;color: #888;}
main {display: block;margin: auto;max-width: 60em;}
section {position: relative;margin: 0 2em 1em; padding: 1px;}
#arbre-decision {font-size: .9em;}
#arbre-decision section {top: 0;padding: .5em;border: 1px solid #ABC;background-color: #FEFEFE;transition: all .5s;transition-timing-function: cubic-bezier(0,0,.1,1.25);}
#arbre-decision section.animated {top: -100vh;}
#arbre-decision span {position: absolute;top: .5em;right: .5em;font-style: italic;color: #BBB;}
.btn-next {display: flex;justify-content: center;margin-bottom: 0;}
.btn-next button {display: block;margin: .5em 1em;padding: .25em 1em;border: 1px solid #888;font: inherit;line-height: 2;background: #FFF;cursor: pointer;}
.btn-next button:first-letter {text-transform: uppercase;}
.btn-next .actif-btn {border: 1px solid #080;outline: 3px solid #0C0;outline-offset: -4px;background-color: #F0FFF0;}
</style>
</head>
<body>
<main>
<header>
<time datetime="2021-09-08">Sept. 2021</time>
<h1>Création arbre de décision dynamique</h1>
</header>
<section id="arbre-decision"></section>
<footer>
<p>Voir la <a href="https://www.developpez.net/forums/showthread.php?t=2116259">discussion sur Developpez.com</a>
</footer>
</main>
<script>
"use strict";
const DATAS = {
"depart": {
"titre": "Arbre de décision",
"text": "Sélectionner votre contexte",
"action": [{
"label": "Contexte #1",
"goto": "ctx1"
},
{
"label": "Contexte #2",
"goto": "ctx2"
}
]
},
"ctx1": {
"text": "C'est parti ...<br> faites votre choix",
"action": [{
"label": "choix #1",
"goto": "ctx1_c1"
},
{
"label": "choix #2",
"goto": "ctx1_c2"
},
{
"label": "choix #3",
"goto": "ctx1_c3"
}
]
},
"ctx2": {
"text": "Pas plus d'info !",
"action": [{
"label": "Merci d'être venu !",
"goto": "the_end"
}]
},
"ctx1_c1": {
"text": "Choix #1<br> faites votre super choix",
"action": [{
"label": "super choix #1",
"goto": "ctx1_c1_c1"
},
{
"label": "super choix #2",
"goto": "ctx1_c1_c2"
}
]
},
"ctx1_c2": {
"text": "Choix #2<br> faites votre super choix",
"action": [{
"label": "super choix #1",
"goto": "etape_05"
},
{
"label": "super choix #2",
"goto": "ctx1_c1_c2"
}
]
},
"ctx1_c3": {
"text": "Mauvais choix, à bientôt !",
"action": []
},
"ctx1_c1_c1": {
"text": "Votre choix final est on ne peut plus judicieux !",
"action": [{
"label": "Merci d'être venu !",
"goto": "the_end"
}]
},
"ctx1_c1_c2": {
"text": "J'ai rien compris !",
"action": [{
"label": "Bye !",
"goto": "the_end"
}]
},
"the_end": {
"titre": "The end !",
"text": "A bientôt !",
"action": []
}
};
/**
* Supprime les éléments qui ne sont plus concernés
* et affecte la classe acive à l'élément
* @param {HTMLElement} btn - le bouton cliqué
*/
function removeNextEtape(btn) {
const parent = btn.closest("section");
const btnCde = parent.querySelector(".actif-btn");
btnCde?.classList.remove("actif-btn");
while (parent.nextElementSibling) {
parent.nextElementSibling.remove();
}
}
/**
* function unique de création d'étape
* @param {string} ref - étiquette de l'objet action
*/
function createNextEtape(ref) {
const oDest = document.getElementById("arbre-decision");
if (DATAS[ref]) {
// création conteneur
const oSection = document.createElement("SECTION");
// pour animation éventuelle
oSection.classList.add("animated");
// info where on est
const oSpan = document.createElement("SPAN");
oSpan.textContent = "[" + ref + "]";
oSection.append(oSpan);
// création des éléments
if (DATAS[ref].titre) {
const oTitre = document.createElement("H2");
oTitre.textContent = DATAS[ref].titre;
oSection.append(oTitre);
}
// la question
const oText = document.createElement("P");
oText.innerHTML = DATAS[ref].text || "Oups !!";
oSection.append(oText);
// création des boutons
const oCdeBtn = document.createElement("P");
oCdeBtn.classList.add("btn-next");
const actions = DATAS[ref].action || [];
actions.forEach((act) => {
const oBtn = document.createElement("BUTTON");
oBtn.textContent = act.label;
// ajout événement click
oBtn.addEventListener("click", (e) => {
removeNextEtape(oBtn);
oBtn.classList.add("actif-btn");
createNextEtape(act.goto);
});
// facultatif !
oBtn.disabled = !DATAS[act.goto];
// ajout des éléments crées
oCdeBtn.append(oBtn);
});
// ajout des éléments crées
oSection.append(oCdeBtn);
oDest.append(oSection);
// applique animation
setTimeout(() => oSection.classList.remove("animated"), 10);
}
}
// lancement questionnaire
createNextEtape("depart");
</script>
</body>
</html> |