]> git.cworth.org Git - zombocom-ai/commitdiff
Add dialog for setting the user's name
authorCarl Worth <cworth@cworth.org>
Thu, 8 Dec 2022 07:00:15 +0000 (23:00 -0800)
committerCarl Worth <cworth@cworth.org>
Thu, 8 Dec 2022 07:03:05 +0000 (23:03 -0800)
And force it to be shown when clicking the comment field, (if the name
has not been set previously).

index.html
index.js

index ad53d866130fc1b683f5d1274c203c4aff2b4a79..a54a09067399e778dffebca2754e5fb421e49c82 100644 (file)
       all. The only limit is yourself!
     </p>
 
+    <dialog id="name-dialog">
+      <form method="dialog">
+        <p>
+          <label>Your name:
+            <input id="name" type="text">
+          </label>
+        </p>
+        <div>
+          <button value="cancel">Cancel</button>
+          <button id="confirm" value="default">Confirm</button>
+        </div>
+      </form>
+    </dialog>
+
     <p>
       <div id="spinner" align="center">
         <div class="animate-flicker">
@@ -102,21 +116,32 @@ mute.addEventListener("click", () => {
   <script>
     var socket = io();
 
+    const name = document.querySelector("#name");
+    const name_dialog = document.querySelector("#name-dialog");
     const images = document.querySelector("#images");
-
     const comments = document.querySelector("#comments");
     const comment_form = document.querySelector("#comment-form");
     const comment = document.querySelector("#comment");
-
     const zombo_form = document.querySelector("#zombo-form");
     const prompt = document.querySelector("#prompt");
     const code = document.querySelector("#code");
-
     const safety= document.querySelector("#safety");
-
     const spinner = document.querySelector("#spinner");
     var spinner_timeout;
 
+    comment.addEventListener('focus', () => {
+        /* Do nothing if name is already set. */
+        if (name.value)
+            return;
+
+        /* Otherwise, bring up the modal dialog to set the name. */
+        name_dialog.showModal();
+    });
+
+    name_dialog.addEventListener('close', () => {
+        socket.emit('set-name', name.value);
+    });
+
     comment_form.addEventListener('submit', function(e) {
         e.preventDefault();
         if (comment.value) {
@@ -131,6 +156,10 @@ mute.addEventListener("click", () => {
         comments.appendChild(item);
     });
 
+    socket.on('inform-name', (name) => {
+        name.value = name;
+    });
+
     socket.on('reset', () => {
         images.replaceChildren();
     });
index 9d4028fbd4cad5a1889ddadc037fd39949388946..2c980dc796d3d1992db34646ed09d46e8de64c08 100644 (file)
--- a/index.js
+++ b/index.js
@@ -65,17 +65,15 @@ process.on('SIGINT', () => {
 });
 
 app.get('/index.html', (req, res) => {
-    if (req.session.views) {
-        req.session.views++;
-    } else {
-        req.session.views = 1;
-    }
     res.sendFile(__dirname + '/index.html');
 });
 
 io.on('connection', (socket) => {
 
-    console.log("Connection from client with " + socket.request.session.views + " views.");
+    // First things first, tell the client their name (if any)
+    if (socket.request.session.name) {
+        socket.emit('inform-name', socket.request.session.name);
+    }
 
     // Replay old comments and images to a newly-joining client
     socket.emit('reset');
@@ -86,6 +84,10 @@ io.on('connection', (socket) => {
         socket.emit('image', image)
     });
 
+    socket.on('set-name', (name) => {
+        socket.request.session.name = name;
+    });
+
     // When any client comments, send that to all clients (including sender)
     socket.on('comment', (comment) => {
         io.emit('comment', comment);