|
| 1 | +<script> |
| 2 | + |
| 3 | +/* update total correct if #webex-total_correct exists */ |
| 4 | +update_total_correct = function() { |
| 5 | + console.log("webex: update total_correct"); |
| 6 | + |
| 7 | + var t = document.getElementsByClassName("webex-total_correct"); |
| 8 | + for (var i = 0; i < t.length; i++) { |
| 9 | + p = t[i].parentElement; |
| 10 | + var correct = p.getElementsByClassName("webex-correct").length; |
| 11 | + var solvemes = p.getElementsByClassName("webex-solveme").length; |
| 12 | + var radiogroups = p.getElementsByClassName("webex-radiogroup").length; |
| 13 | + var selects = p.getElementsByClassName("webex-select").length; |
| 14 | + |
| 15 | + t[i].innerHTML = correct + " of " + (solvemes + radiogroups + selects) + " correct"; |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +/* webex-solution button toggling function */ |
| 20 | +b_func = function() { |
| 21 | + console.log("webex: toggle hide"); |
| 22 | + |
| 23 | + var cl = this.parentElement.classList; |
| 24 | + if (cl.contains('open')) { |
| 25 | + cl.remove("open"); |
| 26 | + } else { |
| 27 | + cl.add("open"); |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +/* check answers */ |
| 32 | +check_func = function() { |
| 33 | + console.log("webex: check answers"); |
| 34 | + |
| 35 | + var cl = this.parentElement.classList; |
| 36 | + if (cl.contains('unchecked')) { |
| 37 | + cl.remove("unchecked"); |
| 38 | + this.innerHTML = "Hide Answers"; |
| 39 | + } else { |
| 40 | + cl.add("unchecked"); |
| 41 | + this.innerHTML = "Show Answers"; |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +/* function for checking solveme answers */ |
| 46 | +solveme_func = function(e) { |
| 47 | + console.log("webex: check solveme"); |
| 48 | + |
| 49 | + var real_answers = JSON.parse(this.dataset.answer); |
| 50 | + var my_answer = this.value; |
| 51 | + var cl = this.classList; |
| 52 | + if (cl.contains("ignorecase")) { |
| 53 | + my_answer = my_answer.toLowerCase(); |
| 54 | + } |
| 55 | + if (cl.contains("nospaces")) { |
| 56 | + my_answer = my_answer.replace(/ /g, "") |
| 57 | + } |
| 58 | + |
| 59 | + if (my_answer == "") { |
| 60 | + cl.remove("webex-correct"); |
| 61 | + cl.remove("webex-incorrect"); |
| 62 | + } else if (real_answers.includes(my_answer)) { |
| 63 | + cl.add("webex-correct"); |
| 64 | + cl.remove("webex-incorrect"); |
| 65 | + } else { |
| 66 | + cl.add("webex-incorrect"); |
| 67 | + cl.remove("webex-correct"); |
| 68 | + } |
| 69 | + |
| 70 | + // match numeric answers within a specified tolerance |
| 71 | + if(this.dataset.tol > 0){ |
| 72 | + var tol = JSON.parse(this.dataset.tol); |
| 73 | + var matches = real_answers.map(x => Math.abs(x - my_answer) < tol) |
| 74 | + if (matches.reduce((a, b) => a + b, 0) > 0) { |
| 75 | + cl.add("webex-correct"); |
| 76 | + } else { |
| 77 | + cl.remove("webex-correct"); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + // added regex bit |
| 82 | + if (cl.contains("regex")){ |
| 83 | + answer_regex = RegExp(real_answers.join("|")) |
| 84 | + if (answer_regex.test(my_answer)) { |
| 85 | + cl.add("webex-correct"); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + update_total_correct(); |
| 90 | +} |
| 91 | + |
| 92 | +/* function for checking select answers */ |
| 93 | +select_func = function(e) { |
| 94 | + console.log("webex: check select"); |
| 95 | + |
| 96 | + var cl = this.classList |
| 97 | + |
| 98 | + /* add style */ |
| 99 | + cl.remove("webex-incorrect"); |
| 100 | + cl.remove("webex-correct"); |
| 101 | + if (this.value == "answer") { |
| 102 | + cl.add("webex-correct"); |
| 103 | + } else if (this.value != "blank") { |
| 104 | + cl.add("webex-incorrect"); |
| 105 | + } |
| 106 | + |
| 107 | + update_total_correct(); |
| 108 | +} |
| 109 | + |
| 110 | +/* function for checking radiogroups answers */ |
| 111 | +radiogroups_func = function(e) { |
| 112 | + console.log("webex: check radiogroups"); |
| 113 | + |
| 114 | + var checked_button = document.querySelector('input[name=' + this.id + ']:checked'); |
| 115 | + var cl = checked_button.parentElement.classList; |
| 116 | + var labels = checked_button.parentElement.parentElement.children; |
| 117 | + |
| 118 | + /* get rid of styles */ |
| 119 | + for (i = 0; i < labels.length; i++) { |
| 120 | + labels[i].classList.remove("webex-incorrect"); |
| 121 | + labels[i].classList.remove("webex-correct"); |
| 122 | + } |
| 123 | + |
| 124 | + /* add style */ |
| 125 | + if (checked_button.value == "answer") { |
| 126 | + cl.add("webex-correct"); |
| 127 | + } else { |
| 128 | + cl.add("webex-incorrect"); |
| 129 | + } |
| 130 | + |
| 131 | + update_total_correct(); |
| 132 | +} |
| 133 | + |
| 134 | +window.onload = function() { |
| 135 | + console.log("webex onload"); |
| 136 | + /* set up solution buttons */ |
| 137 | + var buttons = document.getElementsByTagName("button"); |
| 138 | + |
| 139 | + for (var i = 0; i < buttons.length; i++) { |
| 140 | + if (buttons[i].parentElement.classList.contains('webex-solution')) { |
| 141 | + buttons[i].onclick = b_func; |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + var check_sections = document.getElementsByClassName("webex-check"); |
| 146 | + console.log("check:", check_sections.length); |
| 147 | + for (var i = 0; i < check_sections.length; i++) { |
| 148 | + check_sections[i].classList.add("unchecked"); |
| 149 | + |
| 150 | + let btn = document.createElement("button"); |
| 151 | + btn.innerHTML = "Show Answers"; |
| 152 | + btn.classList.add("webex-check-button"); |
| 153 | + btn.onclick = check_func; |
| 154 | + check_sections[i].appendChild(btn); |
| 155 | + |
| 156 | + let spn = document.createElement("span"); |
| 157 | + spn.classList.add("webex-total_correct"); |
| 158 | + check_sections[i].appendChild(spn); |
| 159 | + } |
| 160 | + |
| 161 | + /* set up webex-solveme inputs */ |
| 162 | + var solveme = document.getElementsByClassName("webex-solveme"); |
| 163 | + |
| 164 | + for (var i = 0; i < solveme.length; i++) { |
| 165 | + /* make sure input boxes don't auto-anything */ |
| 166 | + solveme[i].setAttribute("autocomplete","off"); |
| 167 | + solveme[i].setAttribute("autocorrect", "off"); |
| 168 | + solveme[i].setAttribute("autocapitalize", "off"); |
| 169 | + solveme[i].setAttribute("spellcheck", "false"); |
| 170 | + solveme[i].value = ""; |
| 171 | + |
| 172 | + /* adjust answer for ignorecase or nospaces */ |
| 173 | + var cl = solveme[i].classList; |
| 174 | + var real_answer = solveme[i].dataset.answer; |
| 175 | + if (cl.contains("ignorecase")) { |
| 176 | + real_answer = real_answer.toLowerCase(); |
| 177 | + } |
| 178 | + if (cl.contains("nospaces")) { |
| 179 | + real_answer = real_answer.replace(/ /g, ""); |
| 180 | + } |
| 181 | + solveme[i].dataset.answer = real_answer; |
| 182 | + |
| 183 | + /* attach checking function */ |
| 184 | + solveme[i].onkeyup = solveme_func; |
| 185 | + solveme[i].onchange = solveme_func; |
| 186 | + |
| 187 | + solveme[i].insertAdjacentHTML("afterend", " <span class='webex-icon'></span>") |
| 188 | + } |
| 189 | + |
| 190 | + /* set up radiogroups */ |
| 191 | + var radiogroups = document.getElementsByClassName("webex-radiogroup"); |
| 192 | + for (var i = 0; i < radiogroups.length; i++) { |
| 193 | + radiogroups[i].onchange = radiogroups_func; |
| 194 | + } |
| 195 | + |
| 196 | + /* set up selects */ |
| 197 | + var selects = document.getElementsByClassName("webex-select"); |
| 198 | + for (var i = 0; i < selects.length; i++) { |
| 199 | + selects[i].onchange = select_func; |
| 200 | + selects[i].insertAdjacentHTML("afterend", " <span class='webex-icon'></span>") |
| 201 | + } |
| 202 | + |
| 203 | + update_total_correct(); |
| 204 | +} |
| 205 | + |
| 206 | +</script> |
0 commit comments