]> git.sesse.net Git - remoteglot/commitdiff
Use a slightly more normal function syntax.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Thu, 29 Dec 2022 08:49:28 +0000 (09:49 +0100)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Thu, 29 Dec 2022 08:49:28 +0000 (09:49 +0100)
www/js/remoteglot.js

index 5c769f4c05b50a94bd4f214513fdd08fe2b6d104..5d19116ee1927ebc82a989e0aab19a0dc646cf5b 100644 (file)
@@ -233,7 +233,7 @@ let current_hash_xhr = null;
  */
 let current_hash_display_timer = null;
 
-let supports_html5_storage = function() {
+function supports_html5_storage() {
        try {
                return 'localStorage' in window && window['localStorage'] !== null;
        } catch (e) {
@@ -243,7 +243,7 @@ let supports_html5_storage = function() {
 
 // Make the unique token persistent so people refreshing the page won't count twice.
 // Of course, you can never fully protect against people deliberately wanting to spam.
-let get_unique = function() {
+function get_unique() {
        let use_local_storage = supports_html5_storage();
        if (use_local_storage && window['localStorage']['unique']) {
                return window['localStorage']['unique'];
@@ -255,7 +255,7 @@ let get_unique = function() {
        return unique;
 }
 
-let request_update = function() {
+function request_update() {
        current_analysis_request_timer = null;
 
        let handle_err = () => {
@@ -301,7 +301,7 @@ let request_update = function() {
 
 }
 
-let process_update_response = function(data, headers) {
+function process_update_response(data, headers) {
        sync_server_clock(headers.get('Date'));
        ims = headers.get('X-RGLM');
        let num_viewers = headers.get('X-RGNV');
@@ -342,7 +342,7 @@ let process_update_response = function(data, headers) {
        }
 }
 
-let possibly_play_sound = function(old_data, new_data) {
+function possibly_play_sound(old_data, new_data) {
        if (!enable_sound) {
                return;
        }
@@ -363,7 +363,7 @@ let possibly_play_sound = function(old_data, new_data) {
 /**
  * @type {!string} server_date_string
  */
-let sync_server_clock = function(server_date_string) {
+function sync_server_clock(server_date_string) {
        let server_time_ms = new Date(server_date_string).getTime();
        let client_time_ms = new Date().getTime();
        let estimated_offset_ms = server_time_ms - client_time_ms;
@@ -377,7 +377,7 @@ let sync_server_clock = function(server_date_string) {
        }
 }
 
-let clear_arrows = function() {
+function clear_arrows() {
        for (let i = 0; i < arrows.length; ++i) {
                if (arrows[i].svg) {
                        arrows[i].svg.remove();
@@ -392,7 +392,7 @@ let clear_arrows = function() {
        }
 }
 
-let redraw_arrows = function() {
+function redraw_arrows() {
        for (let i = 0; i < arrows.length; ++i) {
                position_arrow(arrows[i]);
        }
@@ -401,7 +401,7 @@ let redraw_arrows = function() {
 /** @param {!number} x
  * @return {!number}
  */
-let sign = function(x) {
+function sign(x) {
        if (x > 0) {
                return 1;
        } else if (x < 0) {
@@ -416,7 +416,7 @@ let sign = function(x) {
  * @param {!string} to The square the arrow is to (e.g. e4).
  * @return {boolean}
  */
-let interfering_arrow = function(from, to) {
+function interfering_arrow(from, to) {
        let from_col = from.charCodeAt(0) - "a1".charCodeAt(0);
        let from_row = from.charCodeAt(1) - "a1".charCodeAt(1);
        let to_col   = to.charCodeAt(0) - "a1".charCodeAt(0);
@@ -457,7 +457,7 @@ let interfering_arrow = function(from, to) {
  * @param {!number} u
  * @return {!string} The point in "x y" form, suitable for SVG paths.
  */
-let point_from_start = function(x1, y1, x2, y2, t, u) {
+function point_from_start(x1, y1, x2, y2, t, u) {
        let dx = x2 - x1;
        let dy = y2 - y1;
 
@@ -480,7 +480,7 @@ let point_from_start = function(x1, y1, x2, y2, t, u) {
  * @param {!number} u
  * @return {!string} The point in "x y" form, suitable for SVG paths.
  */
-let point_from_end = function(x1, y1, x2, y2, t, u) {
+function point_from_end(x1, y1, x2, y2, t, u) {
        let dx = x2 - x1;
        let dy = y2 - y1;
 
@@ -493,7 +493,7 @@ let point_from_end = function(x1, y1, x2, y2, t, u) {
        return x + " " + y;
 }
 
-let position_arrow = function(arrow) {
+function position_arrow(arrow) {
        if (arrow.svg) {
                if (arrow.svg.parentElement) {
                        arrow.svg.parentElement.removeChild(arrow.svg);
@@ -584,7 +584,7 @@ let position_arrow = function(arrow) {
  * @param {number} line_width
  * @param {number} arrow_size
  */
-let create_arrow = function(from_square, to_square, fg_color, line_width, arrow_size) {
+function create_arrow(from_square, to_square, fg_color, line_width, arrow_size) {
        let from_col = from_square.charCodeAt(0) - "a1".charCodeAt(0);
        let from_row = from_square.charCodeAt(1) - "a1".charCodeAt(1);
        let to_col   = to_square.charCodeAt(0) - "a1".charCodeAt(0);
@@ -605,7 +605,7 @@ let create_arrow = function(from_square, to_square, fg_color, line_width, arrow_
        arrows.push(arrow);
 }
 
-let compare_by_score = function(refutation_lines, invert, a, b) {
+function compare_by_score(refutation_lines, invert, a, b) {
        let sa = compute_score_sort_key(refutation_lines[b]['score'], refutation_lines[b]['depth'], invert);
        let sb = compute_score_sort_key(refutation_lines[a]['score'], refutation_lines[a]['depth'], invert);
        return sa - sb;
@@ -622,7 +622,7 @@ let compare_by_score = function(refutation_lines, invert, a, b) {
  * @return {Array.<string>} The FEN representation (e.g. Ne4) of all
  *     moves, in score order.
  */
-let find_nonstupid_moves = function(data, margin, invert) {
+function find_nonstupid_moves(data, margin, invert) {
        // First of all, if there are any moves that are more than 0.5 ahead of
        // the primary move, the refutation lines are probably bunk, so just
        // kill them all. 
@@ -666,7 +666,7 @@ let find_nonstupid_moves = function(data, margin, invert) {
  * @param {number} x
  * @return {!string}
  */
-let thousands = function(x) {
+function thousands(x) {
        return String(x).split('').reverse().join('').replace(/(\d{3}\B)/g, '$1,').split('').reverse().join('');
 }
 
@@ -680,7 +680,7 @@ let thousands = function(x) {
  * @param {number=} opt_limit
  * @param {boolean=} opt_showlast
  */
-let add_pv = function(start_fen, pv, move_num, toplay, scores, start_display_move_num, opt_limit, opt_showlast) {
+function add_pv(start_fen, pv, move_num, toplay, scores, start_display_move_num, opt_limit, opt_showlast) {
        display_lines.push({
                start_fen: start_fen,
                pv: pv,
@@ -706,7 +706,7 @@ let add_pv = function(start_fen, pv, move_num, toplay, scores, start_display_mov
  * @param {number=} opt_limit If set, show at most this number of moves.
  * @param {boolean=} opt_showlast If limit is set, show the last moves instead of the first ones.
  */
-let print_pv = function(line_num, splicepos, opt_limit, opt_showlast) {
+function print_pv(line_num, splicepos, opt_limit, opt_showlast) {
        let display_line = display_lines[line_num];
        let pv = display_line.pv;
        let move_num = display_line.move_num;
@@ -807,7 +807,7 @@ let print_pv = function(line_num, splicepos, opt_limit, opt_showlast) {
 /** Update the highlighted to/from squares on the board.
  * Based on the global "highlight_from" and "highlight_to" letiables.
  */
-let update_board_highlight = function() {
+function update_board_highlight() {
        document.getElementById("board").querySelectorAll('.square-55d63').forEach((square) => square.classList.remove('nonuglyhighlight'));
        if ((current_display_line === null || current_display_line_is_history) &&
            highlight_from !== undefined && highlight_to !== undefined) {
@@ -816,7 +816,7 @@ let update_board_highlight = function() {
        }
 }
 
-let update_history = function() {
+function update_history() {
        let history = document.getElementById('history');
        if (display_lines[0] === null || display_lines[0].pv.length == 0) {
                history.textContent = 'No history';
@@ -837,7 +837,7 @@ let update_history = function() {
 /**
  * @param {!boolean} truncate_history
  */
-let collapse_history = function(truncate_history) {
+function collapse_history(truncate_history) {
        truncate_display_history = truncate_history;
        update_history();
 }
@@ -847,7 +847,7 @@ window['collapse_history'] = collapse_history;
  *
  * Also recreates the global "display_lines".
  */
-let update_refutation_lines = function() {
+function update_refutation_lines() {
        if (base_fen === null) {
                return;
        }
@@ -962,7 +962,7 @@ let update_refutation_lines = function() {
  * @param {Array.<string>} moves
  * @param {number} last_move
  */
-let chess_from = function(fen, moves, last_move) {
+function chess_from(fen, moves, last_move) {
        let hiddenboard = new Chess();
        if (fen !== null && fen !== undefined) {
                hiddenboard.load(fen);
@@ -979,7 +979,7 @@ let chess_from = function(fen, moves, last_move) {
        return hiddenboard;
 }
 
-let update_game_list = function(games) {
+function update_game_list(games) {
        document.getElementById("games").textContent = "";
        if (games === null) {
                return;
@@ -1029,7 +1029,7 @@ let update_game_list = function(games) {
  * Try to find a running game that matches with the current hash,
  * and switch to it if we're not already displaying it.
  */
-let possibly_switch_game_from_hash = function() {
+function possibly_switch_game_from_hash() {
        let history_match = window.location.hash.match(/^#history=([a-zA-Z0-9_-]+)/);
        if (history_match !== null) {
                let game_id = history_match[1];
@@ -1061,7 +1061,7 @@ let possibly_switch_game_from_hash = function() {
  * If this is a Chess960 castling which doesn't move the king,
  * move the rook instead.
 */
-let patch_move = function(move) {
+function patch_move(move) {
        if (move === null) return null;
        if (move.from !== move.to) return move;
 
@@ -1085,7 +1085,7 @@ let patch_move = function(move) {
 
 /** Update all the HTML on the page, based on current global state.
  */
-let update_board = function() {
+function update_board() {
        document.body.style.opacity = null;
 
        let data = displayed_analysis_data || current_analysis_data;
@@ -1389,7 +1389,7 @@ let update_board = function() {
        update_sparkline(data);
 }
 
-let update_sparkline = function(data) {
+function update_sparkline(data) {
        let scorespark = document.getElementById('scoresparkcontainer');
        scorespark.textContent = '';
        if (data && data['score_history']) {
@@ -1489,7 +1489,7 @@ let update_sparkline = function(data) {
        }
 }
 
-let draw_hover = function(e, color, tooltip) {
+function draw_hover(e, color, tooltip) {
        e.target.style.fill = 'rgb(' + color[0]*100.0 + '%, ' + color[1]*100.0 + '%, ' + color[2]*100.0 + '%)';
 
        let hover = document.getElementById('sparklinehover');
@@ -1504,7 +1504,7 @@ let draw_hover = function(e, color, tooltip) {
        hover.style.top = top + 'px';
 
 }
-let hide_hover = function(e, color) {
+function hide_hover(e, color) {
        e.target.style.fill = 'rgb(' + color[0]*100.0 + '%, ' + color[1]*100.0 + '%, ' + color[2]*100.0 + '%)';
        document.getElementById('sparklinehover').style.display = 'none';
 }
@@ -1512,7 +1512,7 @@ let hide_hover = function(e, color) {
 /**
  * @param {number} num_viewers
  */
-let update_num_viewers = function(num_viewers) {
+function update_num_viewers(num_viewers) {
        let text = "";
        if (num_viewers === null) {
                text = "";
@@ -1531,7 +1531,7 @@ let update_num_viewers = function(num_viewers) {
        document.getElementById("numviewers").textContent = text;
 }
 
-let update_clock = function() {
+function update_clock() {
        clock_timer = null;
 
        let data = displayed_analysis_data || current_analysis_data;
@@ -1627,7 +1627,7 @@ let update_clock = function() {
  * @param {Number} remaining_ms
  * @param {boolean} show_seconds
  */
-let format_clock = function(remaining_ms, show_seconds) {
+function format_clock(remaining_ms, show_seconds) {
        if (remaining_ms <= 0) {
                if (show_seconds) {
                        return "00:00:00";
@@ -1652,7 +1652,7 @@ let format_clock = function(remaining_ms, show_seconds) {
 /**
  * @param {Number} x
  */
-let format_2d = function(x) {
+function format_2d(x) {
        if (x >= 10) {
                return x;
        } else {
@@ -1665,7 +1665,7 @@ let format_2d = function(x) {
  * @param {Number} move_num Move number of this move.
  * @param {boolean} white_to_play Whether white is to play this move.
  */
-let format_move_with_number = function(move, move_num, white_to_play) {
+function format_move_with_number(move, move_num, white_to_play) {
        let ret;
        if (white_to_play) {
                ret = move_num + '. ';
@@ -1681,7 +1681,7 @@ let format_move_with_number = function(move, move_num, white_to_play) {
  * @param {Number} halfmove_num Half-move number that is to be played,
  *   starting from 0.
  */
-let format_halfmove_with_number = function(move, halfmove_num) {
+function format_halfmove_with_number(move, halfmove_num) {
        return format_move_with_number(
                move,
                Math.floor(halfmove_num / 2) + 1,
@@ -1692,7 +1692,7 @@ let format_halfmove_with_number = function(move, halfmove_num) {
  * @param {Object} data
  * @param {Number} halfmove_num
  */
-let format_tooltip = function(data, halfmove_num) {
+function format_tooltip(data, halfmove_num) {
        if (data['score_history'][halfmove_num + 1] ||
            (halfmove_num + 1) === data['position']['history'].length) {
                // Position is in the history, or it is the current position
@@ -1729,7 +1729,7 @@ let format_tooltip = function(data, halfmove_num) {
 /**
  * @param {boolean} truncate_history
  */
-let set_truncate_history = function(truncate_history) {
+function set_truncate_history(truncate_history) {
        truncate_display_history = truncate_history;
        update_refutation_lines();
 }
@@ -1739,7 +1739,7 @@ window['set_truncate_history'] = set_truncate_history;
  * @param {number} line_num
  * @param {number} move_num
  */
-let show_line = function(line_num, move_num) {
+function show_line(line_num, move_num) {
        if (line_num == -1) {
                current_display_line = null;
                current_display_move = null;
@@ -1765,7 +1765,7 @@ let show_line = function(line_num, move_num) {
 }
 window['show_line'] = show_line;
 
-let prev_move = function() {
+function prev_move() {
        if (current_display_line &&
            current_display_move >= current_display_line.start_display_move_num) {
                --current_display_move;
@@ -1776,7 +1776,7 @@ let prev_move = function() {
 }
 window['prev_move'] = prev_move;
 
-let next_move = function() {
+function next_move() {
        if (current_display_line &&
            current_display_move < current_display_line.pv.length - 1) {
                ++current_display_move;
@@ -1787,7 +1787,7 @@ let next_move = function() {
 }
 window['next_move'] = next_move;
 
-let next_game = function() {
+function next_game() {
        if (current_games === null) {
                return;
        }
@@ -1805,7 +1805,7 @@ let next_game = function() {
        // Couldn't find it; give up.
 }
 
-let update_historic_analysis = function() {
+function update_historic_analysis() {
        if (!current_display_line_is_history) {
                return;
        }
@@ -1850,7 +1850,7 @@ let update_historic_analysis = function() {
 /**
  * @param {string} fen
  */
-let update_imbalance = function(fen) {
+function update_imbalance(fen) {
        let imbalance = {'k': 0, 'q': 0, 'r': 0, 'b': 0, 'n': 0, 'p': 0};
        for (const c of fen) {
                if (c === ' ') {
@@ -1902,7 +1902,7 @@ let update_imbalance = function(fen) {
  * Also replaces the PV with the current displayed line if it's not shown
  * anywhere else on the screen.
  */
-let update_move_highlight = function() {
+function update_move_highlight() {
        if (highlighted_move !== null) {
                highlighted_move.classList.remove('highlight'); 
        }
@@ -1934,7 +1934,7 @@ let update_move_highlight = function() {
  *
  * @return {?number}
  */
-let find_display_line_matching_num = function() {
+function find_display_line_matching_num() {
        for (let i = 0; i < display_lines.length; ++i) {
                let line = display_lines[i];
                if (line.start_display_move_num > 0) continue;
@@ -1959,7 +1959,7 @@ let find_display_line_matching_num = function() {
  * TODO: This should really be called only whenever something changes,
  * instead of all the time.
  */
-let update_displayed_line = function() {
+function update_displayed_line() {
        if (current_display_line === null) {
                document.getElementById("linenav").style.display = 'none';
                document.getElementById("linemsg").style.display = 'revert';
@@ -1994,7 +1994,7 @@ let update_displayed_line = function() {
        update_imbalance(hiddenboard.fen());
 }
 
-let set_board_position = function(new_fen) {
+function set_board_position(new_fen) {
        board_is_animating = true;
        let old_fen = board.fen();
        board.position(new_fen);
@@ -2006,7 +2006,7 @@ let set_board_position = function(new_fen) {
 /**
  * @param {boolean} param_enable_sound
  */
-let set_sound = function(param_enable_sound) {
+function set_sound(param_enable_sound) {
        enable_sound = param_enable_sound;
        if (enable_sound) {
                document.getElementById("soundon").innerHTML = "<strong>On</strong>";
@@ -2032,7 +2032,7 @@ window['set_sound'] = set_sound;
 /** Send off a hash probe request to the backend.
  * @param {string} fen
  */
-let explore_hash = function(fen) {
+function explore_hash(fen) {
        // If we already have a backend response going, abort it.
        if (current_hash_xhr) {
                current_hash_xhr.abort();
@@ -2059,7 +2059,7 @@ let explore_hash = function(fen) {
  * @param {!Object} data
  * @param {string} fen
  */
-let show_explore_hash_results = function(data, fen) {
+function show_explore_hash_results(data, fen) {
        if (board_is_animating) {
                // Updating while the animation is still going causes
                // the animation to jerk. This is pretty crude, but it will do.
@@ -2072,7 +2072,7 @@ let show_explore_hash_results = function(data, fen) {
 }
 
 // almost all of this stuff comes from the chessboard.js example page
-let onDragStart = function(source, piece, position, orientation) {
+function onDragStart(source, piece, position, orientation) {
        let pseudogame = new Chess(display_fen);
        if (pseudogame.game_over() === true ||
            (pseudogame.turn() === 'w' && piece.search(/^b/) !== -1) ||
@@ -2088,7 +2088,7 @@ let onDragStart = function(source, piece, position, orientation) {
        return true;
 }
 
-let mousedownSquare = function(e) {
+function mousedownSquare(e) {
        if (!e.target || !e.target.closest('.square-55d63')) {
                return;
        }
@@ -2118,7 +2118,7 @@ let mousedownSquare = function(e) {
        }
 }
 
-let mouseupSquare = function(e) {
+function mouseupSquare(e) {
        if (!e.target || !e.target.closest('.square-55d63')) {
                return;
        }
@@ -2136,7 +2136,7 @@ let mouseupSquare = function(e) {
        });
 }
 
-let get_best_move = function(game, source, target, invert) {
+function get_best_move(game, source, target, invert) {
        let moves = game.moves({ verbose: true });
        if (source !== null) {
                moves = moves.filter(function(move) { return move.from == source; });
@@ -2196,7 +2196,7 @@ let get_best_move = function(game, source, target, invert) {
        return best_move;
 }
 
-let onDrop = function(source, target) {
+function onDrop(source, target) {
        if (source === target) {
                if (recommended_move === null) {
                        return 'snapback';
@@ -2221,7 +2221,7 @@ let onDrop = function(source, target) {
        if (move === null) return 'snapback';
 }
 
-let onSnapEnd = function(source, target) {
+function onSnapEnd(source, target) {
        if (source === target && recommended_move !== null) {
                source = recommended_move.from;
                target = recommended_move.to;
@@ -2267,7 +2267,7 @@ let onSnapEnd = function(source, target) {
 }
 // End of dragging-related code.
 
-let fmt_cp = function(v) {
+function fmt_cp(v) {
        if (v === 0) {
                return "0.00";
        } else if (v > 0) {
@@ -2278,7 +2278,7 @@ let fmt_cp = function(v) {
        }
 }
 
-let format_short_score = function(score) {
+function format_short_score(score) {
        if (!score) {
                return "???";
        }
@@ -2311,7 +2311,7 @@ let format_short_score = function(score) {
        return null;
 }
 
-let format_long_score = function(score) {
+function format_long_score(score) {
        if (!score) {
                return "???";
        }
@@ -2347,7 +2347,7 @@ let format_long_score = function(score) {
        return null;
 }
 
-let compute_plot_score = function(score) {
+function compute_plot_score(score) {
        if (score[0] === 'M' || score[0] === 'T') {
                return 500;
        } else if (score[0] === 'm' || score[0] === 't') {
@@ -2372,7 +2372,7 @@ let compute_plot_score = function(score) {
  * @param {boolean} invert Whether black is to play.
  * @return {number}
  */
-let compute_score_sort_key = function(score, depth, invert) {
+function compute_score_sort_key(score, depth, invert) {
        let s;
        if (!score) {
                return -10000000;
@@ -2405,7 +2405,7 @@ let compute_score_sort_key = function(score, depth, invert) {
 /**
  * @param {Object} game
  */
-let switch_backend = function(game) {
+function switch_backend(game) {
        // Stop looking at historic data.
        current_display_line = null;
        current_display_move = null;
@@ -2465,11 +2465,11 @@ const svg_pieces = {
        'bP': 'data:image/svg+xml,<?xml version=%221.0%22 encoding=%22UTF-8%22 standalone=%22no%22?>%0A<!DOCTYPE svg PUBLIC %22-//W3C//DTD SVG 1.1//EN%22 %22http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd%22>%0A<svg xmlns=%22http://www.w3.org/2000/svg%22 version=%221.1%22 width=%2245%22 height=%2245%22><path d=%22m 22.5,9 c -2.21,0 -4,1.79 -4,4 0,0.89 0.29,1.71 0.78,2.38 C 17.33,16.5 16,18.59 16,21 c 0,2.03 0.94,3.84 2.41,5.03 C 15.41,27.09 11,31.58 11,39.5 H 34 C 34,31.58 29.59,27.09 26.59,26.03 28.06,24.84 29,23.03 29,21 29,18.59 27.67,16.5 25.72,15.38 26.21,14.71 26.5,13.89 26.5,13 c 0,-2.21 -1.79,-4 -4,-4 z%22 style=%22opacity:1;fill:%23000000;fill-opacity:1;fill-rule:nonzero;stroke:%23000000;stroke-width:1.5;stroke-linecap:round;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1%22/></svg>',
 };
 
-let svg_piece_theme = function(piece) {
+function svg_piece_theme(piece) {
        return svg_pieces[piece];
 }
 
-let init = function() {
+function init() {
        unique = get_unique();
 
        // Load settings from HTML5 local storage if available.