]> git.sesse.net Git - remoteglot/blobdiff - www/js/remoteglot.js
Support a ding sound per move. Off by default, of course.
[remoteglot] / www / js / remoteglot.js
index cdf8a5670e4b48971270d3d31da125564e5d1735..cd2ffa5ccb04c548aa706cf05be73f6ad2c7c4d9 100644 (file)
@@ -65,6 +65,9 @@ var highlighted_move = null;
 /** @type {?number} @private */
 var unique = null;
 
 /** @type {?number} @private */
 var unique = null;
 
+/** @type {boolean} @private */
+var enable_sound = false;
+
 /** The current position on the board, represented as a FEN string.
  * @type {?string}
  * @private
 /** The current position on the board, represented as a FEN string.
  * @type {?string}
  * @private
@@ -121,6 +124,7 @@ var request_update = function() {
        }).done(function(data, textstatus, xhr) {
                ims = xhr.getResponseHeader('X-Remoteglot-Last-Modified');
                var num_viewers = xhr.getResponseHeader('X-Remoteglot-Num-Viewers');
        }).done(function(data, textstatus, xhr) {
                ims = xhr.getResponseHeader('X-Remoteglot-Last-Modified');
                var num_viewers = xhr.getResponseHeader('X-Remoteglot-Num-Viewers');
+               possibly_play_sound(current_analysis_data, data);
                current_analysis_data = data;
                update_board(current_analysis_data, displayed_analysis_data);
                update_num_viewers(num_viewers);
                current_analysis_data = data;
                update_board(current_analysis_data, displayed_analysis_data);
                update_num_viewers(num_viewers);
@@ -133,6 +137,24 @@ var request_update = function() {
        });
 }
 
        });
 }
 
+var possibly_play_sound = function(old_data, new_data) {
+       if (!enable_sound) {
+               return;
+       }
+       if (old_data === null) {
+               return;
+       }
+       var ding = document.getElementById('ding');
+       if (ding && ding.play !== undefined) {
+               if (old_data['position'] && old_data['position']['fen'] &&
+                   new_data['position'] && new_data['position']['fen'] &&
+                   (old_data['position']['fen'] !== new_data['position']['fen'] ||
+                    old_data['position']['move_num'] !== new_data['position']['move_num'])) {
+                       ding.play();
+               }
+       }
+}
+
 var clear_arrows = function() {
        for (var i = 0; i < arrows.length; ++i) {
                if (arrows[i].svg) {
 var clear_arrows = function() {
        for (var i = 0; i < arrows.length; ++i) {
                if (arrows[i].svg) {
@@ -671,6 +693,7 @@ var update_board = function(current_data, display_data) {
                refutation_lines = [];
                update_refutation_lines();
                clear_arrows();
                refutation_lines = [];
                update_refutation_lines();
                clear_arrows();
+               update_displayed_line();
                return;
        }
 
                return;
        }
 
@@ -929,8 +952,24 @@ var update_displayed_line = function() {
        board.position(hiddenboard.fen());
 }
 
        board.position(hiddenboard.fen());
 }
 
+/**
+ * @param {boolean} param_enable_sound
+ */
+var set_sound = function(param_enable_sound) {
+       enable_sound = param_enable_sound;
+       if (enable_sound) {
+               $("#soundon").html("<strong>On</strong>");
+               $("#soundoff").html("<a href=\"javascript:set_sound(false)\">Off</a>");
+       } else {
+               $("#soundon").html("<a href=\"javascript:set_sound(true)\">On</a>");
+               $("#soundoff").html("<strong>Off</strong>");
+       }
+}
+window['set_sound'] = set_sound;
+
 var init = function() {
        unique = get_unique();
 var init = function() {
        unique = get_unique();
+       set_sound(false);
 
        // Create board.
        board = new window.ChessBoard('board', 'start');
 
        // Create board.
        board = new window.ChessBoard('board', 'start');