refactor with bind

This commit is contained in:
Adrien MALINGREY 2023-11-25 21:40:35 +01:00
parent 1365ef65dd
commit 80bc9f083d
2 changed files with 18 additions and 17 deletions

View File

@ -32,11 +32,12 @@ table input {
table td.table-primary input, table td.table-primary input,
table td.table-active input, table td.table-active input,
table.table-success input, table.table-success input,
td.table-danger input:disabled,
table input:not([disabled]) { table input:not([disabled]) {
background: transparent !important; background: transparent !important;
} }
table input[disabled] { table input:disabled {
background-position: center !important; background-position: center !important;
} }

View File

@ -178,18 +178,14 @@ function checkBox(box) {
checkCandidates(box.andNeighbourhood) checkCandidates(box.andNeighbourhood)
if (box.value) { if (box.value) {
for (let [areaName, neighbours] of Object.entries({ for (let [areaName, area] of Object.entries({
région: regions[box.regionId], région: regions[box.regionId],
ligne: rows[box.rowId], ligne: rows[box.rowId],
colonne: columns[box.columnId], colonne: columns[box.columnId],
})) }))
for (neighbour of neighbours) for (neighbour of area)
if (box != neighbour && box.value == neighbour.value) { if (box != neighbour)
for (neighbour of [box, neighbour]) { showDuplicates(areaName, box, neighbour)
neighbour.setCustomValidity(`Il y a un autre ${box.value} dans cette ${areaName}.`)
neighbour.classList.add("is-invalid")
}
}
} }
checkSuccess() checkSuccess()
@ -204,14 +200,7 @@ function checkBoxes() {
colonne: columns, colonne: columns,
})) }))
for (area of areas) for (area of areas)
area.sort((box, neighbour) => { area.filter(box => box.value).sort(showDuplicates.bind(null, areaName))
if (box.value && box.value == neighbour.value) {
for (neighbour of [box, neighbour]) {
neighbour.setCustomValidity(`Il y a un autre ${box.value} dans cette ${areaName}.`)
neighbour.classList.add("is-invalid")
}
}
})
checkSuccess() checkSuccess()
} }
@ -220,6 +209,7 @@ function checkCandidates(area) {
area.forEach(box => { area.forEach(box => {
box.setCustomValidity("") box.setCustomValidity("")
box.classList.remove("is-invalid") box.classList.remove("is-invalid")
box.parentElement.classList.remove("table-danger")
searchCandidatesOf(box) searchCandidatesOf(box)
if (box.candidates.size == 0) { if (box.candidates.size == 0) {
box.setCustomValidity("Aucun chiffre possible !") box.setCustomValidity("Aucun chiffre possible !")
@ -228,6 +218,16 @@ function checkCandidates(area) {
}) })
} }
function showDuplicates(areaName, box, neighbour) {
if(box.value == neighbour.value) {
area.forEach(neighbour => neighbour.parentElement.classList.add("table-danger"))
for (neighbour of [box, neighbour]) {
neighbour.setCustomValidity(`Il y a un autre ${box.value} dans cette ${areaName}.`)
neighbour.classList.add("is-invalid")
}
}
}
function checkSuccess() { function checkSuccess() {
if (sudokuForm.checkValidity()) { // Correct grid if (sudokuForm.checkValidity()) { // Correct grid
if (boxes.filter(box => box.value == "").length == 0) { if (boxes.filter(box => box.value == "").length == 0) {