|
| 1 | +/* eslint-disable no-inner-declarations */ |
1 | 2 | import * as ui from "/src/modules/ui.js"; |
2 | 3 | import storage from "/src/modules/storage.js"; |
3 | 4 |
|
@@ -134,6 +135,23 @@ try { |
134 | 135 | break; |
135 | 136 | }; |
136 | 137 | return values; |
| 138 | + } else if (mode === "matrix") { |
| 139 | + var matrix = []; |
| 140 | + var matrixRows = document.querySelectorAll('#matrix [data-matrix-row]'); |
| 141 | + matrixRows.forEach(row => { |
| 142 | + var matrixRow = []; |
| 143 | + row.querySelectorAll('[data-matrix-column]').forEach(input => { |
| 144 | + var value = input.value?.trim(); |
| 145 | + if (value.length > 0) { |
| 146 | + matrixRow.push(value); |
| 147 | + } else { |
| 148 | + matrixRow.push(""); |
| 149 | + } |
| 150 | + }); |
| 151 | + matrix.push(matrixRow); |
| 152 | + }); |
| 153 | + var matrixString = JSON.stringify(matrix).replaceAll('["', '[').replaceAll('","', ', ').replaceAll('"]', ']'); |
| 154 | + return matrixString; |
137 | 155 | } else if (mode === "frq") { |
138 | 156 | if (part && document.querySelector(`[data-frq-part="${part}"]`)) { |
139 | 157 | return document.querySelector(`[data-frq-part="${part}"]`).value?.trim(); |
@@ -211,6 +229,12 @@ try { |
211 | 229 | } else if (mode === "set") { |
212 | 230 | setInput.classList.add("attention"); |
213 | 231 | setInput.focus(); |
| 232 | + } else if (mode === "matrix") { |
| 233 | + document.querySelector('#matrix [data-matrix-row]:first-child [data-matrix-column]').classList.add("attention"); |
| 234 | + document.querySelector('#matrix [data-matrix-row]:first-child [data-matrix-column]').focus(); |
| 235 | + setTimeout(() => { |
| 236 | + document.getElementById("submit-button").disabled = false; |
| 237 | + }, 3000); |
214 | 238 | } else if (mode === "frq") { |
215 | 239 | if (part) { |
216 | 240 | if (document.querySelector(`[data-frq-part="${part}"]`).parentElement.nextElementSibling && (document.querySelector(`[data-frq-part="${part}"]`).parentElement.nextElementSibling.classList.contains('part'))) { |
@@ -239,6 +263,8 @@ try { |
239 | 263 | storeClick(storage.get("code"), question, mf.value, "latex"); |
240 | 264 | } else if (mode === "set" && !multipleChoice) { |
241 | 265 | storeClick(storage.get("code"), question, answer, "array"); |
| 266 | + } else if (mode === "matrix" && !multipleChoice) { |
| 267 | + storeClick(storage.get("code"), question, answer, "matrix"); |
242 | 268 | } else if (mode === "frq" && !multipleChoice) { |
243 | 269 | storeClick(storage.get("code"), question, answer, "frq"); |
244 | 270 | } else { |
@@ -306,6 +332,7 @@ try { |
306 | 332 | }); |
307 | 333 | } |
308 | 334 | document.querySelectorAll('[data-answer-mode="set"] .button-grid')[1].style.flexWrap = 'nowrap'; |
| 335 | + resetMatrix(); |
309 | 336 | frqInput.value = 4; |
310 | 337 | // Switch input mode (exit multiple choice) |
311 | 338 | answerMode(mode); |
@@ -574,13 +601,18 @@ try { |
574 | 601 | const button = document.createElement("button"); |
575 | 602 | const latex = item.type === "latex"; |
576 | 603 | const array = item.type === "array"; |
| 604 | + const matrix = item.type === "matrix"; |
577 | 605 | const frq = item.type === "frq"; |
578 | 606 | if (!latex) { |
579 | 607 | if (!array) { |
580 | | - if (!frq) { |
581 | | - button.innerHTML = `<p><b>${item.question}.</b> ${unixToTimeString(item.timestamp)} (${item.code})${item.makeup ? ` (Makeup for ${item.makeup.split(' ')[0]})` : ''}</p>\n<p>${item.answer}</p>`; |
| 608 | + if (!matrix) { |
| 609 | + if (!frq) { |
| 610 | + button.innerHTML = `<p><b>${item.question}.</b> ${unixToTimeString(item.timestamp)} (${item.code})${item.makeup ? ` (Makeup for ${item.makeup.split(' ')[0]})` : ''}</p>\n<p>${item.answer}</p>`; |
| 611 | + } else { |
| 612 | + button.innerHTML = `<p><b>${item.question}.</b> ${unixToTimeString(item.timestamp)} (${item.code})${item.makeup ? ` (Makeup for ${item.makeup.split(' ')[0]})` : ''}</p>\n<p>${item.answer}${(item.question === '1') ? '/9' : ''}</p>`; |
| 613 | + } |
582 | 614 | } else { |
583 | | - button.innerHTML = `<p><b>${item.question}.</b> ${unixToTimeString(item.timestamp)} (${item.code})${item.makeup ? ` (Makeup for ${item.makeup.split(' ')[0]})` : ''}</p>\n<p>${item.answer}${(item.question === '1') ? '/9' : ''}</p>`; |
| 615 | + button.innerHTML = `<p><b>${item.question}.</b> ${unixToTimeString(item.timestamp)} (${item.code})${item.makeup ? ` (Makeup for ${item.makeup.split(' ')[0]})` : ''}</p>\n<p>${JSON.stringify(JSON.parse(item.answer).map(innerArray => innerArray.map(numString => String(numString)))).replaceAll('["', '[').replaceAll('","', ', ').replaceAll('"]', ']')}</p>`; |
584 | 616 | } |
585 | 617 | } else { |
586 | 618 | button.innerHTML = `<p><b>${item.question}.</b> ${unixToTimeString(item.timestamp)} (${item.code})${item.makeup ? ` (Makeup for ${item.makeup.split(' ')[0]})` : ''}</p>\n<p>${item.answer.slice(1, -1)}</p>`; |
@@ -627,6 +659,37 @@ try { |
627 | 659 | i++; |
628 | 660 | if (i < item.answer.slice(1, -1).split(', ').length) addSet(); |
629 | 661 | }); |
| 662 | + } else if (matrix) { |
| 663 | + answerMode("matrix"); |
| 664 | + ui.setButtonSelectValue(document.getElementById("answer-mode-selector"), "matrix"); |
| 665 | + resetMatrix(); |
| 666 | + var rows = JSON.parse(item.answer); |
| 667 | + if (rows.length != 2) { |
| 668 | + if (rows.length === 1) { |
| 669 | + removeRow(); |
| 670 | + } else { |
| 671 | + for (let i = 0; i < rows.length - 2; i++) { |
| 672 | + addRow(); |
| 673 | + } |
| 674 | + } |
| 675 | + } |
| 676 | + var columns = rows[0].length; |
| 677 | + if (columns != 2) { |
| 678 | + if (columns === 1) { |
| 679 | + removeColumn(); |
| 680 | + } else { |
| 681 | + for (let i = 0; i < columns - 2; i++) { |
| 682 | + addColumn(); |
| 683 | + } |
| 684 | + } |
| 685 | + } |
| 686 | + var matrixRows = document.querySelectorAll('#matrix [data-matrix-row]'); |
| 687 | + for (let i = 0; i < rows.length; i++) { |
| 688 | + for (let j = 0; j < rows[i].length; j++) { |
| 689 | + matrixRows[i].querySelectorAll('[data-matrix-column]')[j].value = rows[i][j]; |
| 690 | + } |
| 691 | + } |
| 692 | + matrixRows[matrixRows.length - 1].lastChild.focus(); |
630 | 693 | } else if (frq) { |
631 | 694 | answerMode("frq"); |
632 | 695 | ui.setButtonSelectValue(document.getElementById("answer-mode-selector"), "frq"); |
@@ -752,6 +815,8 @@ try { |
752 | 815 | answerLabel.setAttribute("for", "math-input"); |
753 | 816 | } else if (mode === "set") { |
754 | 817 | answerLabel.setAttribute("for", "set-input"); |
| 818 | + } else if (mode === "matrix") { |
| 819 | + answerLabel.setAttribute("for", "matrix"); |
755 | 820 | } else if (mode === "frq") { |
756 | 821 | answerLabel.setAttribute("for", "frq-input"); |
757 | 822 | } |
@@ -865,6 +930,83 @@ try { |
865 | 930 | if (frqParts.length > 4) frqParts[frqParts.length - 1].remove(); |
866 | 931 | if (frqParts.length === 5) document.querySelector("[data-remove-frq-part]").disabled = true; |
867 | 932 | } |
| 933 | + |
| 934 | + // Add matrix column |
| 935 | + if (document.querySelector("[data-add-matrix-column]")) document.querySelector("[data-add-matrix-column]").addEventListener("click", addColumn); |
| 936 | + |
| 937 | + function addColumn() { |
| 938 | + var rows = [...document.getElementById('matrix').children]; |
| 939 | + rows.forEach(row => { |
| 940 | + var newColumn = document.createElement('input'); |
| 941 | + newColumn.setAttribute('type', 'text'); |
| 942 | + newColumn.setAttribute('autocomplete', 'off'); |
| 943 | + newColumn.setAttribute('data-matrix-column', row.children.length + 1); |
| 944 | + row.appendChild(newColumn); |
| 945 | + }); |
| 946 | + rows[0].lastElementChild.focus(); |
| 947 | + var columns = document.querySelectorAll('#matrix [data-matrix-row]:first-child [data-matrix-column]'); |
| 948 | + if (columns.length === 10) document.querySelector("[data-add-matrix-column]").disabled = true; |
| 949 | + document.querySelector("[data-remove-matrix-column]").disabled = false; |
| 950 | + } |
| 951 | + |
| 952 | + // Remove matrix column |
| 953 | + if (document.querySelector("[data-remove-matrix-column]")) document.querySelector("[data-remove-matrix-column]").addEventListener("click", removeColumn); |
| 954 | + |
| 955 | + function removeColumn() { |
| 956 | + var rows = [...document.getElementById('matrix').children]; |
| 957 | + rows.forEach(row => { |
| 958 | + var lastColumn = row.lastElementChild; |
| 959 | + if (lastColumn) lastColumn.remove(); |
| 960 | + }); |
| 961 | + if (rows[0].children.length < 10) document.querySelector("[data-add-matrix-column]").disabled = false; |
| 962 | + if (rows[0].children.length === 1) document.querySelector("[data-remove-matrix-column]").disabled = true; |
| 963 | + } |
| 964 | + |
| 965 | + // Add matrix row |
| 966 | + if (document.querySelector("[data-add-matrix-row]")) document.querySelector("[data-add-matrix-row]").addEventListener("click", addRow); |
| 967 | + |
| 968 | + function addRow() { |
| 969 | + var newRow = document.createElement('div'); |
| 970 | + newRow.classList.add('row'); |
| 971 | + newRow.setAttribute('data-matrix-row', document.querySelectorAll('[data-matrix-row]').length + 1); |
| 972 | + var columns = document.querySelectorAll('[data-matrix-row]:first-child [data-matrix-column]'); |
| 973 | + columns.forEach(column => { |
| 974 | + var newColumn = document.createElement('input'); |
| 975 | + newColumn.setAttribute('type', 'text'); |
| 976 | + newColumn.setAttribute('autocomplete', 'off'); |
| 977 | + newColumn.setAttribute('data-matrix-column', column.getAttribute('data-matrix-column')); |
| 978 | + newRow.appendChild(newColumn); |
| 979 | + }); |
| 980 | + document.getElementById('matrix').appendChild(newRow); |
| 981 | + newRow.firstElementChild.focus(); |
| 982 | + var rows = document.querySelectorAll('[data-matrix-row]'); |
| 983 | + if (rows.length === 10) document.querySelector("[data-add-matrix-row]").disabled = true; |
| 984 | + document.querySelector("[data-remove-matrix-row]").disabled = false; |
| 985 | + } |
| 986 | + |
| 987 | + // Remove matrix row |
| 988 | + if (document.querySelector("[data-remove-matrix-row]")) document.querySelector("[data-remove-matrix-row]").addEventListener("click", removeRow); |
| 989 | + |
| 990 | + function removeRow() { |
| 991 | + var rows = document.querySelectorAll('[data-matrix-row]'); |
| 992 | + if (rows.length > 1) { |
| 993 | + var lastRow = rows[rows.length - 1]; |
| 994 | + lastRow.remove(); |
| 995 | + if (rows.length < 10) document.querySelector("[data-add-matrix-row]").disabled = false; |
| 996 | + if (rows.length === 2) document.querySelector("[data-remove-matrix-row]").disabled = true; |
| 997 | + } |
| 998 | + } |
| 999 | + |
| 1000 | + function resetMatrix() { |
| 1001 | + var matrix = document.getElementById('matrix'); |
| 1002 | + matrix.innerHTML = '<div class="row" data-matrix-row="1"><input type="text" autocomplete="off" id="matrix-column" data-matrix-column="1" /><input type="text" autocomplete="off" id="matrix-column" data-matrix-column="2" /></div><div class="row" data-matrix-row="2"><input type="text" autocomplete="off" id="matrix-column" data-matrix-column="1" /><input type="text" autocomplete="off" id="matrix-column" data-matrix-column="2" /></div>'; |
| 1003 | + document.querySelectorAll('[data-answer-mode="matrix"] .button-grid')[1].innerHTML = '<button square data-add-matrix-column tooltip="Add Matrix Column"><i class="bi bi-arrow-90deg-left rotate-right"></i></button><button square data-remove-matrix-column tooltip="Remove Matrix Column"><i class="bi bi-x"></i></button>'; |
| 1004 | + document.querySelectorAll('[data-answer-mode="matrix"] .button-grid')[2].innerHTML = '<button square data-add-matrix-row tooltip="Add Matrix Row"><i class="bi bi-arrow-return-left"></i></button><button square data-remove-matrix-row tooltip="Remove Matrix Row"><i class="bi bi-x"></i></button>'; |
| 1005 | + if (document.querySelector("[data-add-matrix-column]")) document.querySelector("[data-add-matrix-column]").addEventListener("click", addColumn); |
| 1006 | + if (document.querySelector("[data-remove-matrix-column]")) document.querySelector("[data-remove-matrix-column]").addEventListener("click", removeColumn); |
| 1007 | + if (document.querySelector("[data-add-matrix-row]")) document.querySelector("[data-add-matrix-row]").addEventListener("click", addRow); |
| 1008 | + if (document.querySelector("[data-remove-matrix-row]")) document.querySelector("[data-remove-matrix-row]").addEventListener("click", removeRow); |
| 1009 | + } |
868 | 1010 | } catch (error) { |
869 | 1011 | if (storage.get("developer")) { |
870 | 1012 | alert(`Error @ clicker.js: ${error.message}`); |
|
0 commit comments