This commit is contained in:
Adrien MALINGREY 2020-10-09 18:25:59 +02:00
parent cfc3c712fc
commit aaf988dde1
2 changed files with 45 additions and 14 deletions

View File

@ -52,17 +52,18 @@
echo " <button type='button' onclick='showValue(\"$value\")'>$value</button>\n"; echo " <button type='button' onclick='showValue(\"$value\")'>$value</button>\n";
} }
?> ?>
<label for="colorPicker">🎨</label>
<input id="colorPicker" type="color" value="#00008b"/>
</div> </div>
<div> <div>
<button type="reset">Tout effacer</button> <button type="reset">Tout effacer</button>
<button id="undoButton" type="button" onclick="undo()" disabled>Annuler Ctrl+Z</button>
<label for="colorPicker">🎨</label>
<input id="colorPicker" type="color" value="#00008b"/>
</div> </div>
</form> </form>
<div id=help> <div>
Remplissez la grille de sorte que chaque ligne, colonne et région (carré de 3×3 cases) contienne tous les chiffres de 1 à 9. Remplissez la grille de sorte que chaque ligne, colonne et région (carré de 3×3 cases) contienne tous les chiffres de 1 à 9.
</div> </div>
<div id=links> <div>
<a href="">Lien vers cette grille</a><br/> <a href="">Lien vers cette grille</a><br/>
<a href=".">Nouvelle grille</a> <a href=".">Nouvelle grille</a>
</div> </div>

View File

@ -6,6 +6,7 @@ let columns = Array.from(Array(9), x => [])
let regions = Array.from(Array(9), x => []) let regions = Array.from(Array(9), x => [])
let suggestionTimer= null let suggestionTimer= null
let highlightedValue = "" let highlightedValue = ""
let history = []
window.onload = function() { window.onload = function() {
let rowId = 0 let rowId = 0
@ -13,9 +14,11 @@ window.onload = function() {
let columnId = 0 let columnId = 0
for (box of row.getElementsByTagName('input')) { for (box of row.getElementsByTagName('input')) {
let regionId = rowId - rowId%3 + Math.floor(columnId/3) let regionId = rowId - rowId%3 + Math.floor(columnId/3)
if (!box.readOnly) box.onfocus = box.select if (!box.readOnly) {
box.oninput = oninput box.onfocus = onfocus
box.oninvalid = oninvalid box.oninput = oninput
box.oninvalid = oninvalid
}
box.onkeydown = keyboardBrowse box.onkeydown = keyboardBrowse
box.rowId = rowId box.rowId = rowId
box.columnId = columnId box.columnId = columnId
@ -45,6 +48,13 @@ window.onload = function() {
suggestionTimer = setTimeout(showSuggestion, 30000) suggestionTimer = setTimeout(showSuggestion, 30000)
} }
document.onkeydown = function(event) {
if (event.ctrlKey == true && event.key == "z") {
event.preventDefault()
undo()
}
}
function searchAllowedValuesOf(box) { function searchAllowedValuesOf(box) {
box.allowedValues = new Set(VALUES) box.allowedValues = new Set(VALUES)
box.neighbourhood.forEach(neighbour => box.allowedValues.delete(neighbour.value)) box.neighbourhood.forEach(neighbour => box.allowedValues.delete(neighbour.value))
@ -62,10 +72,21 @@ function showAllowedValuesOn(box) {
} }
} }
function oninput() { function onfocus() {
this.style.color = colorPicker.value this.oldValue = this.value
this.select()
}
this.neighbourhood.concat([this]).forEach(box => { function oninput() {
history.push({input: this, value: this.oldValue})
undoButton.disabled = false
refresh(this)
}
function refresh(input) {
input.style.color = colorPicker.value
input.neighbourhood.concat([input]).forEach(box => {
box.setCustomValidity("") box.setCustomValidity("")
searchAllowedValuesOf(box) searchAllowedValuesOf(box)
box.pattern = `[${Array.from(box.allowedValues).join("")}]?` box.pattern = `[${Array.from(box.allowedValues).join("")}]?`
@ -73,9 +94,9 @@ function oninput() {
enableButtons() enableButtons()
refreshShowValue() refreshShowValue()
this.neighbourhood.concat([this]).forEach(neighbour => showAllowedValuesOn(neighbour)) input.neighbourhood.concat([input]).forEach(neighbour => showAllowedValuesOn(neighbour))
if (this.form.checkValidity()) { // Correct grid if (input.form.checkValidity()) { // Correct grid
if (boxes.filter(box => box.value == "").length == 0) { if (boxes.filter(box => box.value == "").length == 0) {
alert(`Bravo ! Vous avez résolu la grille.`) alert(`Bravo ! Vous avez résolu la grille.`)
} else { } else {
@ -83,8 +104,17 @@ function oninput() {
suggestionTimer = setTimeout(showSuggestion, 30000) suggestionTimer = setTimeout(showSuggestion, 30000)
} }
} else { // Errors on grid } else { // Errors on grid
this.select() input.select()
this.reportValidity() input.reportValidity()
}
}
function undo() {
if (history.length) {
previousState = history.pop()
previousState.input.value = previousState.value
refresh(previousState.input)
if (history.length < 1) undoButton.disabled = true
} }
} }