From a8c4d791c95beafb34765e9ba3a0507169405927 Mon Sep 17 00:00:00 2001 From: AudricV <74829229+AudricV@users.noreply.github.com> Date: Mon, 9 Jan 2023 15:24:14 +0100 Subject: [PATCH] [Client] Add ability to copy game room link to clipboard Also rename Invite your friends button to Copy invitation link and open the room code link in a new tab by default. --- truthseeker/static/js/game_lobby.js | 72 ++++++++++++++++++++++++++++- truthseeker/templates/lobby.html | 4 +- 2 files changed, 73 insertions(+), 3 deletions(-) diff --git a/truthseeker/static/js/game_lobby.js b/truthseeker/static/js/game_lobby.js index 75c88e3..7bf293e 100644 --- a/truthseeker/static/js/game_lobby.js +++ b/truthseeker/static/js/game_lobby.js @@ -102,6 +102,7 @@ function startChallengeGame() { } // Join room functions + function joinRoom() { unsetListenerToJoinRoomButton(); if (isNickNameInvalid()) { @@ -114,6 +115,27 @@ function joinRoom() { //TODO: join the game room and handle server errors + connection errors } +// Room code functions + +/** + * Copy the room code to the clipboard. + * + *
+ * In order to not make an additional API call to get the room code, we use the value from the + * room code HTML element and generate a HTTP link from this value, copied to the clipboard using + * {@link copyTextToClipboard}. + *
+ */ +function copyCode() { + // Get the room code from the displayed text to avoid an extra API call + let roomCode = document.getElementsByClassName("room_code")[0].textContent; + if (roomCode == "") { + alert("Veuillez patientez, le code d'équipe est en cours de génération."); + } + copyTextToClipboard(window.location.protocol + "//" + window.location.hostname + ":" + + window.location.port + "/lobby/" + roomCode); +} + // Listeners functions /** @@ -139,6 +161,17 @@ function setListenerToJoinRoomButton() { document.getElementById("join_game_button").addEventListener("click", joinRoom); } +/** + * Set listeners to the copy room code button. + * + *+ * This function adds a click event listener on the copy room code button. + *
+ */ +function setListenerToCopyCodeButton() { + document.getElementById("invite_friends_button").addEventListener("click", copyCode); +} + /** * Unset listeners to game buttons. * @@ -153,7 +186,7 @@ function unsetListenersToButtons() { } /** - * Set listeners to the join room button. + * Unset listeners to the join room button. * ** This function removes the click event listener set with {@link setListenerToJoinRoomButton} on @@ -164,6 +197,18 @@ function unsetListenerToJoinRoomButton() { document.getElementById("join_game_button").removeEventListener("click", joinRoom); } +/** + * Unset listeners to the copy room code button. + * + *
+ * This function removes the click event listener set with {@link setListenerToCopyCodeButton} on + * the copy room code button. + *
+ */ +function unsetListenerToCopyCodeButton() { + document.getElementById("invite_friends_button").removeEventListener("click", copyCode); +} + // Utility functions function isRoomOwner() { @@ -176,6 +221,30 @@ function hasJoinedRoom() { return true; } +/** + * Copy the given text in the clipboard, if the browser allows it. + * + *+ * A JavaScript alert is created witn an appropriate message, regardless of whether the copy succeeded. + *
+ * + *+ * This function uses the Clipboard API. In the case it is not supported by the browser used, a JavaScript alert is shown.. + *
+ * + * @param {string}} textToCopy the text to copy to the clipboard + */ +function copyTextToClipboard(textToCopy) { + if (!navigator.clipboard) { + alert("Votre navigateur ne supporte pas l'API Clipboard. Veuillez copier le texte en ouvrant le menu contextuel de votre navigateur sur le lien et sélectionner l'option pour copier le lien."); + } + navigator.clipboard.writeText(textToCopy).then(() => { + alert("Code copié avec succès dans le presse-papiers."); + }, () => { + alert("Impossible de copier le texte. Vérifiez si vous avez donné la permission d'accès au presse-papiers pour le site de Thruth Inquiry dans les paramètres de votre navigateur."); + }); +} + /** * Determine whether a nickname is invalid. * @@ -254,6 +323,7 @@ function initLobby() { displayRoomCode(); displayMultiPlayerModeChoices(); setListenersToGameButtons(); + setListenerToCopyCodeButton(); } displayPlayerList(); diff --git a/truthseeker/templates/lobby.html b/truthseeker/templates/lobby.html index 922fd35..e3bdfdc 100644 --- a/truthseeker/templates/lobby.html +++ b/truthseeker/templates/lobby.html @@ -18,8 +18,8 @@