fetch_analysis();
}
+var get_history_url = function() {
+ var history = game.history({ verbose: true }).map(function(x) { return x.san; });
+ history.length = move_override;
+ return '/?' + history.join(',');
+}
+
var fetch_analysis = function() {
var fen = current_display_fen();
$.ajax({
}
window['set_includetransp'] = set_includetransp;
-var make_move = function(move) {
+var make_move = function(move, do_update) {
var history = game.history({ verbose: true });
if (move_override < history.length && history[move_override].san == move) {
// User effectively only moved forward in history.
fens.push(game.fen());
++move_override;
}
- update();
+
+ if (do_update !== false) {
+ update();
+ window.history.pushState(null, null, get_history_url());
+ }
}
window['make_move'] = make_move;
if (move_override > 0) {
--move_override;
update();
+ window.history.replaceState(null, null, get_history_url());
}
}
window['prev_move'] = prev_move;
if (move_override < game.history().length) {
++move_override;
update();
+ window.history.replaceState(null, null, get_history_url());
}
}
window['next_move'] = next_move;
-var set_move = function(n) {
+var set_move = function(n, do_update) {
move_override = n;
- update();
+ if (do_update !== false) {
+ update();
+ window.history.replaceState(null, null, get_history_url());
+ }
}
window['set_move'] = set_move;
make_move(pseudogame.history({ verbose: true }).pop().san);
}
+var onpopstate = function() {
+ var old_moves = game.history({ verbose: true }).map(function(x) { return x.san; });
+ var new_moves = document.location.search.replace(/^\?/, "").split(",");
+
+ if (new_moves.length == 1 && new_moves[0] == "") {
+ new_moves = [];
+ }
+
+ var num_shared_moves;
+ for (num_shared_moves = 0; num_shared_moves < Math.min(old_moves.length, new_moves.length); ++num_shared_moves) {
+ if (old_moves[i] != new_moves[i]) {
+ break;
+ }
+ }
+
+ set_move(num_shared_moves, false);
+ for (var i = num_shared_moves; i < new_moves.length; ++i) {
+ make_move(new_moves[i], false);
+ }
+ update();
+ window.history.replaceState(null, null, get_history_url());
+}
+
var init = function() {
// Create board.
board = new window.ChessBoard('board', {
onDrop: onDrop,
onSnapEnd: onSnapEnd
});
- update();
+
+ window.onpopstate = onpopstate;
+ onpopstate();
$(window).keyup(function(event) {
if (event.which == 39) {