]> git.sesse.net Git - remoteglot/blobdiff - www/js/remoteglot.js
Fix double-forwarding when clicking along a hash-probe line.
[remoteglot] / www / js / remoteglot.js
index c96335feb62baeacd88435fcc65c91c6375920b2..bac1ef9711cb882dc196c451912bb678c484d180 100644 (file)
@@ -839,6 +839,8 @@ let update_refutation_lines = function() {
        tbl.replaceChildren();
 
        if (display_lines.length < 2) {
+               // Update the move highlight, as we've rewritten all the HTML.
+               update_move_highlight();
                return;
        }
 
@@ -897,8 +899,11 @@ let update_refutation_lines = function() {
                        continue;
                }
 
-               let move = "<a class=\"move\" href=\"javascript:show_line(" + display_lines.length + ", " + 0 + ");\">" + line['move'] + "</a>";
-               move_td.innerHTML = move;
+               let move_link = document.createElement("a");
+               move_link.classList.add("move");
+               move_link.setAttribute("href", "javascript:show_line(" + display_lines.length + ", 0)");
+               move_link.textContent = line['move'];
+               move_td.appendChild(move_link);
 
                let score_td = document.createElement("td");
                tr.appendChild(score_td);
@@ -1223,6 +1228,9 @@ let update_board = function() {
                return;
        }
 
+       if (clock_timer !== null) {
+               clearTimeout(clock_timer);
+       }
        update_clock();
 
        // The score.
@@ -1395,7 +1403,10 @@ let update_sparkline = function(data) {
                                scores.push(last_score);
                        }
                        if (data['score']) {
-                               scores.push(compute_plot_score(data['score']));
+                               let score = compute_plot_score(data['score']);
+                               scores.push(score);
+                               if (score < min_score) min_score = score;
+                               if (score > max_score) max_score = score;
                        }
 
                        const h = scorespark.getBoundingClientRect().height;
@@ -1441,7 +1452,7 @@ let update_sparkline = function(data) {
                                rect.addEventListener('mouseenter', (e) => draw_hover(e, hlcolor, tooltip));
                                rect.addEventListener('mousemove', (e) => draw_hover(e, hlcolor, tooltip));
                                rect.addEventListener('mouseleave', (e) => hide_hover(e, color));
-                               rect.addEventListener('click', (e) => show_line(0, i + first_move_num - 1));
+                               rect.addEventListener('click', (e) => { hide_hover(e, color); show_line(0, i + first_move_num - 1) });
                                scorespark.appendChild(rect);
                        }
                }
@@ -1491,7 +1502,7 @@ let update_num_viewers = function(num_viewers) {
 }
 
 let update_clock = function() {
-       clearTimeout(clock_timer);
+       clock_timer = null;
 
        let data = displayed_analysis_data || current_analysis_data;
        if (!data) return;
@@ -1827,20 +1838,38 @@ let update_imbalance = function(fen) {
                        }
                }
        }
-       let white_imbalance = '';
-       let black_imbalance = '';
+       let white_imbalance = document.getElementById('whiteimbalance');
+       let black_imbalance = document.getElementById('blackimbalance');
+       white_imbalance.textContent = '';
+       black_imbalance.textContent = '';
        for (let piece in imbalance) {
                for (let i = 0; i < imbalance[piece]; ++i) {
-                       white_imbalance += '<img src="' + svg_pieces['w' + piece.toUpperCase()] + '" alt="" style="width: 15px;height: 15px;" class="imbalance-piece">';
-                       white_imbalance += '<img src="' + svg_pieces['b' + piece.toUpperCase()] + '" alt="" style="width: 15px;height: 15px;" class="imbalance-inverted-piece">';
+                       let i1 = document.createElement('img');
+                       i1.src = svg_pieces['w' + piece.toUpperCase()];
+                       i1.setAttribute('alt', piece.toUpperCase());
+                       i1.classList.add('imbalance-piece');
+                       white_imbalance.appendChild(i1);
+
+                       let i2 = document.createElement('img');
+                       i2.src = svg_pieces['b' + piece.toUpperCase()];
+                       i2.setAttribute('alt', piece.toUpperCase());
+                       i2.classList.add('imbalance-inverted-piece');
+                       white_imbalance.appendChild(i2);
                }
                for (let i = 0; i < -imbalance[piece]; ++i) {
-                       black_imbalance += '<img src="' + svg_pieces['b' + piece.toUpperCase()] + '" alt="" style="width: 15px;height: 15px;" class="imbalance-piece">';
-                       black_imbalance += '<img src="' + svg_pieces['w' + piece.toUpperCase()] + '" alt="" style="width: 15px;height: 15px;" class="imbalance-inverted-piece">';
+                       let i1 = document.createElement('img');
+                       i1.src = svg_pieces['b' + piece.toUpperCase()];
+                       i1.setAttribute('alt', piece.toUpperCase());
+                       i1.classList.add('imbalance-piece');
+                       black_imbalance.appendChild(i1);
+
+                       let i2 = document.createElement('img');
+                       i2.src = svg_pieces['w' + piece.toUpperCase()];
+                       i2.setAttribute('alt', piece.toUpperCase());
+                       i2.classList.add('imbalance-inverted-piece');
+                       black_imbalance.appendChild(i2);
                }
        }
-       document.getElementById('whiteimbalance').innerHTML = white_imbalance;
-       document.getElementById('blackimbalance').innerHTML = black_imbalance;
 }
 
 /** Mark the currently selected move in red.
@@ -1993,7 +2022,11 @@ let explore_hash = function(fen) {
        fetch(backend_hash_url + "?fen=" + fen, { signal })
                .then((response) => response.json())
                .then((data) => { show_explore_hash_results(data, fen); })
-               .catch((err) => {});
+               .catch((err) => {
+                       // Truncate the lines, since we already cleared the display.
+                       display_lines = [ display_lines[0], display_lines[1] ];
+                       update_move_highlight();
+               });
 }
 
 /** Process the JSON response from a hash probe request.
@@ -2175,6 +2208,8 @@ let onSnapEnd = function(source, target) {
                promotion: 'q' // NOTE: always promote to a queen for example simplicity
        });
 
+       // Move ahead on the line we're on -- this includes history if we've
+       // gone backwards.
        if (current_display_line &&
            current_display_move < current_display_line.pv.length - 1 &&
            current_display_line.pv[current_display_move + 1] === move.san) {
@@ -2184,8 +2219,9 @@ let onSnapEnd = function(source, target) {
 
        // Walk down the displayed lines until we find one that starts with
        // this move, then select that. Note that this gives us a good priority
-       // order (history first, then PV, then multi-PV lines).
-       for (let i = 0; i < display_lines.length; ++i) {
+       // order (PV, then multi-PV lines; history was already dealt with above,
+       // as it's the only line that originates backwards).
+       for (let i = 1; i < display_lines.length; ++i) {
                if (i == 1 && current_display_line) {
                        // Do not choose PV if not on it.
                        continue;