|
| 1 | +class DataOperator { |
| 2 | + constructor(){ |
| 3 | + } |
| 4 | + |
| 5 | + insertDataRow(dataObj, n=1, pos){ |
| 6 | + const row = Array(dataObj.cols).fill(0); |
| 7 | + dataObj.data.splice(n, 0, row); |
| 8 | + dataObj.rows++; |
| 9 | + } |
| 10 | + |
| 11 | + removeDataRow(dataObj, n=1, pos){ |
| 12 | + dataObj.data.splice(n, 1); |
| 13 | + dataObj.rows--; |
| 14 | + } |
| 15 | + |
| 16 | + insertDataCol(dataObj, n=1){ |
| 17 | + const cols = Array(dataObj.rows).fill(0); |
| 18 | + for (let r = 0; r < dataObj.data.length; r++) { |
| 19 | + dataObj.data[r].splice(n, 0, cols[r]); |
| 20 | + //dataObj.weights[r].splice(n,0,0); |
| 21 | + } |
| 22 | + dataObj.cols++; |
| 23 | + } |
| 24 | + |
| 25 | + removeDataCol(dataObj, n=1, pos){ |
| 26 | + for (let r = 0; r < dataObj.data.length; r++) { |
| 27 | + dataObj.data[r].splice(n, 1); |
| 28 | + //dataObj.weights[r].splice(n, 1); |
| 29 | + } |
| 30 | + dataObj.cols--; |
| 31 | + } |
| 32 | + |
| 33 | + createBinaryData(dim) { |
| 34 | + let binaryArray = []; |
| 35 | + for (let i = 0; i < 2**dim; i++){ |
| 36 | + binaryArray[i] = []; |
| 37 | + let binaryString = i.toString(2); |
| 38 | + while (binaryString.length < dim) { |
| 39 | + binaryString = "0" + binaryString; |
| 40 | + } |
| 41 | + for (let j = 0; j < binaryString.length; j++ ) { |
| 42 | + binaryArray[i][j] = Number(binaryString.substring(j, j+1)); |
| 43 | + } |
| 44 | + } |
| 45 | + return binaryArray; |
| 46 | + } |
| 47 | + |
| 48 | + updateDataFromTableNoDisplay(dataObj, tableObj) { |
| 49 | + let table = tableObj.table; |
| 50 | + // skip header row and button column of table, start from 1 |
| 51 | + let start = (table.id === 'output-table')? 1 : 2; |
| 52 | + let startCol = (table.id === 'output-table')? 0 : 1; |
| 53 | + let row = start; |
| 54 | + if(dataObj.data.length !== table.rows.length) |
| 55 | + { |
| 56 | + for (let i = 0; i < table.rows.length; i++) { |
| 57 | + dataObj.data[i] = Array(table.rows[0].cells.length).fill(0); |
| 58 | + } |
| 59 | + } |
| 60 | + for (let n = table.rows.length; row < n; row++) { |
| 61 | + for (let c = startCol, m = table.rows[row].cells.length; c < m; c++) { |
| 62 | + const cell = table.rows[row].cells[c]; |
| 63 | + const rawValue = cell.innerText; |
| 64 | + let [parsedValue, isValid] = demo.stringToValidFloat(rawValue); |
| 65 | + if (table.id === 'output-table' && c === DESIRED_OUTPUT_COLUMN && (parsedValue !== 1 && parsedValue !== 0) ) { |
| 66 | + isValid = false; |
| 67 | + } |
| 68 | + dataObj.data[row-start][c-startCol] = parsedValue; |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + // update data from table when user changes entry |
| 73 | + updateDataFromTable(dataObj, tableObj){ //TODO: make more efficient by passing specific location to update |
| 74 | + let table = tableObj.table; |
| 75 | + // skip header row and button column of table, start from 1 |
| 76 | + let start = (table.id === 'output-table')? 1 : 2; |
| 77 | + let startCol = (table.id === 'output-table')? 0 : 1; |
| 78 | + let row = start; |
| 79 | + for (let n = table.rows.length; row < n; row++) { |
| 80 | + for (let c = startCol, m = table.rows[row].cells.length; c < m; c++) { |
| 81 | + const cell = table.rows[row].cells[c]; |
| 82 | + const rawValue = cell.innerText; |
| 83 | + let [parsedValue, isValid] = demo.stringToValidFloat(rawValue); |
| 84 | + if (table.id === 'output-table' && c === DESIRED_OUTPUT_COLUMN && (parsedValue !== 1 && parsedValue !== 0) ) { |
| 85 | + isValid = false; |
| 86 | + } |
| 87 | + const regex = '^-?0+$'; |
| 88 | + if (new RegExp(regex).test(rawValue)) { |
| 89 | + isValid = false; |
| 90 | + } |
| 91 | + const regex1 = '^00+1$'; |
| 92 | + if (new RegExp(regex1).test(rawValue)) { |
| 93 | + isValid = false; |
| 94 | + } |
| 95 | + if(!isValid) { |
| 96 | + if(parsedValue > 0) |
| 97 | + parsedValue = 1 |
| 98 | + else |
| 99 | + parsedValue = 0 |
| 100 | + } |
| 101 | + display.highlightInvalidText(cell, isValid); |
| 102 | + dataObj.data[row-start][c-startCol] = parsedValue; |
| 103 | + } |
| 104 | + } |
| 105 | + display.createOutputTableColors() |
| 106 | + } |
| 107 | + |
| 108 | + updateTableFromData(dataObj, tableObj){ //TODO: make more efficient by passing specific location to update |
| 109 | + let table = tableObj.table; |
| 110 | + // skip header row and button column of table, start from 1 |
| 111 | + let start = table === outputTable? 1 : 2; |
| 112 | + //let row = start; |
| 113 | + for (let row = start; row < table.rows.length; row++) { |
| 114 | + for (let c = 1 ; c < table.rows[row].cells.length; c++) { |
| 115 | + const cell = table.rows[row].cells[c]; |
| 116 | + //cell.innerHTML = `<span>`+dataObj.data[row-start][c-1]+`</span>`; |
| 117 | + cell.innerText = dataObj.data[row-start][c-1]; |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + updateTableFromDesired(desiredOutput, tableObj){ //TODO: make more efficient by passing specific location to update |
| 123 | + let table = tableObj.table; |
| 124 | + // skip header row and button column of table, start from 1 |
| 125 | + let start = 1; |
| 126 | + //let row = start; |
| 127 | + for (let row = start; row < table.rows.length; row++) { |
| 128 | + const cell = table.rows[row].cells[DESIRED_OUTPUT_COLUMN]; |
| 129 | + cell.innerText = desiredOutput.data[row-1]; |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + // make editable and update demo on edit |
| 134 | + makeEditable(textbox, editable = true, setFocusoutHandler = true){ |
| 135 | + if (textbox.id === "activation" || textbox.id === "output" || textbox.id === "desired") |
| 136 | + return |
| 137 | + textbox.contentEditable = editable; |
| 138 | + // add event listener to update demo with table changes |
| 139 | + // add a class to textbox to keep track of an eventlistener already being added |
| 140 | + if (editable && !textbox.classList.contains("edit-handler")) { |
| 141 | + textbox.classList.add("edit-handler"); |
| 142 | + if (textbox.id.startsWith("tblinput")) { |
| 143 | + if (!textbox.classList.contains("input-table-th")) |
| 144 | + textbox.classList.add("input-table-th"); |
| 145 | + } |
| 146 | + if (textbox.innerText.length === 0) |
| 147 | + { |
| 148 | + if (editable) |
| 149 | + textbox.innerHTML = `<span class="editable-border">` + 0 + `</span>` |
| 150 | + else |
| 151 | + textbox.innerHTML = `<span>` + 0 + `</span>` |
| 152 | + } |
| 153 | + |
| 154 | + if (document.getElementById('OutputToggle').checked) |
| 155 | + demo.hasNoSolution(); |
| 156 | + |
| 157 | + //console.trace() |
| 158 | + if(setFocusoutHandler) { |
| 159 | + textbox.addEventListener("focusout", function (event) { |
| 160 | + demo.update(this); |
| 161 | + display.checkForSuccess(); |
| 162 | + let identify = this?.id |
| 163 | + if (identify !== "th1" && !(new RegExp('^w[0-9]+$', 'gm').test(identify))) { //checks if not threshold, or any of the weight textboxes |
| 164 | + if (this?.tagName !== 'TH' && this.parentNode?.tagName !== 'TH') { |
| 165 | + if (!textbox.innerHTML) |
| 166 | + textbox.innerHTML = 0; |
| 167 | + } |
| 168 | + } |
| 169 | + }); |
| 170 | + } |
| 171 | + } |
| 172 | + if (editable) { |
| 173 | + if (textbox.nodeName !== "TH" && !textbox.classList.contains("editable-border")) |
| 174 | + textbox.classList.add("editable-border"); |
| 175 | + if (textbox.nodeName !== "TH" && !textbox.classList.contains("edit-handler")) |
| 176 | + textbox.classList.add("edit-handler"); |
| 177 | + } |
| 178 | + else { |
| 179 | + if (textbox.classList.contains("editable-border")) |
| 180 | + textbox.classList.remove("editable-border"); |
| 181 | + if (textbox.classList.contains("edit-handler")) |
| 182 | + textbox.classList.remove("edit-handler"); |
| 183 | + } |
| 184 | + textbox.onkeydown = function(event){ |
| 185 | + if (event.which === 13 || event.which === 27 || event.keyCode === 13 || event.keyCode === 27) { |
| 186 | + event.target.blur(); // focus out of text box |
| 187 | + demo.update(this); |
| 188 | + } |
| 189 | + }; |
| 190 | + |
| 191 | + } |
| 192 | + |
| 193 | + setFocusoutEventListener(textbox) { |
| 194 | + if(textbox && !textbox.classList.contains("focus-listener-installed")) |
| 195 | + { |
| 196 | + textbox.classList.add("focus-listener-installed"); |
| 197 | + textbox.addEventListener("focusout", function (event) { |
| 198 | + demo.update(this); |
| 199 | + display.checkForSuccess(); |
| 200 | + let identify = this?.id |
| 201 | + if (identify !== "th1" && !(new RegExp('^w[0-9]+$', 'gm').test(identify))) { //checks if not threshold, or any of the weight textboxes |
| 202 | + if (this?.tagName !== 'TH' && this.parentNode?.tagName !== 'TH') { |
| 203 | + if (!textbox.innerHTML) |
| 204 | + textbox.innerHTML = 0; |
| 205 | + } |
| 206 | + } |
| 207 | + }); |
| 208 | + } |
| 209 | + } |
| 210 | +} |
0 commit comments