Add button to download list of game moves
This commit is contained in:
parent
7efde29810
commit
4616143ffa
51
takhta.html
51
takhta.html
|
@ -177,6 +177,7 @@
|
|||
<form id="actions">
|
||||
<input type="button" value="Undo" onclick="undoMove()">
|
||||
<input type="button" value="Flip board" onclick="flipBoard()">
|
||||
<input type="button" value="Download moves" onclick="download()">
|
||||
</form>
|
||||
</p>
|
||||
</section>
|
||||
|
@ -437,6 +438,56 @@
|
|||
// Replace the old board with the new one
|
||||
document.getElementById('board').replaceChild(newBoard, oldBoard);
|
||||
}
|
||||
|
||||
function download() {
|
||||
let actions = document.getElementById("actions");
|
||||
let moves = document.getElementById("movelist");
|
||||
let link = document.createElement("a");
|
||||
|
||||
link.setAttribute("download", "moves.text");
|
||||
link.style.display = "none";
|
||||
actions.appendChild(link);
|
||||
|
||||
var pos = 0;
|
||||
var game = [];
|
||||
var longest = 0;
|
||||
|
||||
for(let move = 0; move < moves.children.length; move += 1) {
|
||||
let record = moves.children[move];
|
||||
|
||||
if(move % 2 === 0) {
|
||||
if(record.innerHTML.length > longest) {
|
||||
longest = record.innerHTML.length;
|
||||
}
|
||||
|
||||
game[pos] = [record.innerHTML];
|
||||
} else {
|
||||
game[pos].push(record.innerHTML);
|
||||
pos += 1;
|
||||
}
|
||||
}
|
||||
|
||||
let file = [];
|
||||
let padding = Math.floor(Math.log10(game.length));
|
||||
|
||||
for(let entry = 0; entry < game.length; entry += 1) {
|
||||
var line = `${entry + 1}`.padStart(padding + 1, " ") + ": ";
|
||||
|
||||
if(game[entry][1] !== undefined) {
|
||||
line += `${game[entry][0].padEnd(longest, " ")} ${game[entry][1]}`;
|
||||
} else {
|
||||
line += game[entry][0];
|
||||
}
|
||||
|
||||
file.push(line);
|
||||
}
|
||||
|
||||
let contents = encodeURIComponent(file.join("\n"));
|
||||
|
||||
link.setAttribute("href", "data:text/plain;charset=utf-8," + contents);
|
||||
link.click();
|
||||
actions.removeChild(link);
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
|
Loading…
Reference in New Issue