commit ea19ba0f58b20f540ff31aeedaa165ec4e8a40d8
parent 6547f507b2b7bbc377a34b9b56660f2335397a39
Author: Natasha Kerensikova <natgh@instinctive.eu>
Date:   Sun, 28 Jan 2024 10:33:04 +0000
Sample assets support topics
Diffstat:
3 files changed, 49 insertions(+), 21 deletions(-)
diff --git a/sample-assets/edit.html b/sample-assets/edit.html
@@ -3,7 +3,7 @@
   <head>
     <meta charset="utf-8" />
     <meta name="viewport" content="width=device-width">
-    <title>Plannification JDR</title>
+    <title>Planification JDR</title>
     <link rel="stylesheet" type="text/css" href="style.css" />
     <script>
 var val_class=["val0","val1","val2","val3","val4","val5"];
@@ -26,8 +26,17 @@ window.onerror = function(msg, url, line) {
   item.scrollIntoView();
 }
 
+function pathname_to_topic(str) {
+  var components = str.split("/");
+  var fileName = components[components.length - 1];
+  var lastDotIndex = fileName.lastIndexOf("-");
+  return lastDotIndex !== -1 ? fileName.substring(0, lastDotIndex) : "";
+}
+
+var topic=pathname_to_topic(window.location.pathname);
+
 async function send(){
-  var req_body = "sub=" + encodeURIComponent(subject);
+  var req_body = (topic ? "topic=" + encodeURIComponent(topic) + "&" : "") + "sub=" + encodeURIComponent(subject);
   for (const [key, value] of outbox) {
     req_body += "&" + encodeURIComponent(key) + "=" + encodeURIComponent(value);
   }
@@ -147,8 +156,9 @@ function activate_subject(){
 }
 
 async function reload_data(){
+  document.getElementById("back-link").href = (topic ? topic + "-" : "") + "view.html";
   document.getElementById("reload-spinner").style = "display:inline";
-  const response = await fetch("all.json", { cache: "no-cache" });
+  const response = await fetch((topic || "all") + ".json", { cache: "no-cache" });
   all_data = await response.json();
 
   var holder = document.getElementById("pref-table-body");
@@ -168,7 +178,7 @@ async function reload_data(){
     holder.appendChild(new_table_line(name));
   }
 
-  subject = localStorage.getItem("subject");
+  subject = localStorage.getItem("subject" + (topic ? "-" + topic : ""));
   activate_subject();
 
   document.getElementById("reload-spinner").style = "display:none";
@@ -180,7 +190,7 @@ async function create_subject(){
     method: "POST",
     cache: "no-store",
     headers: { "Content-Type": "application/x-www-form-urlencoded" },
-    body: "name=" + encodeURIComponent(subject),
+    body: (topic ? "topic=" + encodeURIComponent(topic) + "&" : "") + "name=" + encodeURIComponent(subject),
   });
   document.getElementById("create-spinner").style = "display:none";
 
@@ -192,7 +202,7 @@ async function create_subject(){
 
 function set_subject(){
   subject = document.getElementById("set-subject").value;
-  localStorage.setItem("subject", subject);
+  localStorage.setItem("subject" + (topic ? "-" + topic : ""), subject);
   activate_subject();
   return false;
 }
@@ -204,9 +214,9 @@ function reset_subject(){
     </script>
   </head>
   <body onload="reload_data()">
-    <h1>Plannification JDR</h1>
+    <h1>Planification JDR</h1>
     <p>
-      <a href="view.html">Retour à la vue d'ensemble</a>
+      <a href="view.html" id="back-link">Retour à la vue d'ensemble</a>
       <input name="test" value="Recharger" type="button" onclick="reload_data()">
       <img id="reload-spinner" class="spinner" src="spinner.svg" style="display: none">
     </p>
diff --git a/sample-assets/spa.html b/sample-assets/spa.html
@@ -27,12 +27,22 @@ window.onerror = function(msg, url, line) {
   item.scrollIntoView();
 }
 
+function pathname_to_topic(str) {
+  var components = str.split("/");
+  var fileName = components[components.length - 1];
+  var lastDotIndex = fileName.lastIndexOf('.');
+  var basename = lastDotIndex !== -1 ? fileName.substring(0, lastDotIndex) : fileName;
+  return basename;
+}
+
+var topic=pathname_to_topic(window.location.pathname);
+
 /************************************/
 /* Asynchronous Push of Preferences */
 /************************************/
 
 async function send(){
-  var req_body = "sub=" + encodeURIComponent(send_subject);
+  var req_body = "topic=" + encodeURIComponent(topic) + "&sub=" + encodeURIComponent(send_subject);
   sent = outbox.get(send_subject);
   outbox.delete(send_subject);
 
@@ -40,14 +50,12 @@ async function send(){
     req_body += "&" + encodeURIComponent(key) + "=" + encodeURIComponent(value);
   }
 
-  console.log("Start fetch");
   const response = await fetch("do/set-pref", {
     method: "POST",
     cache: "no-store",
     headers: { "Content-Type": "application/x-www-form-urlencoded" },
     body: req_body,
   });
-  console.log("End of fetch");
 
   var to = base_timeout;
 
@@ -230,6 +238,7 @@ function redisplay() {
 
     document.getElementById("overview-table").style = "";
     document.getElementById("subject-name-label").style = "display:inline";
+    document.getElementById("create-subject").style = "";
     document.getElementById("create-subject-name").type = "text";
     document.getElementById("create-subject-name").value = "";
 
@@ -264,7 +273,7 @@ function redisplay() {
 
 async function reload_data(){
   document.getElementById("reload-spinner").style = "display:inline";
-  const response = await fetch("all.json", { cache: "no-cache" });
+  const response = await fetch(topic + ".json", { cache: "no-cache" });
   all_data = await response.json();
 
   redisplay();
@@ -279,8 +288,8 @@ async function create_subject(){
     method: "POST",
     cache: "no-store",
     headers: { "Content-Type": "application/x-www-form-urlencoded" },
-    body: "name=" + encodeURIComponent(name),
-  });
+    body: "topic=" + encodeURIComponent(topic) + "&name=" + encodeURIComponent(name),
+  }) ;
   document.getElementById("create-subject-name").value = "";
   document.getElementById("create-spinner").style = "display:none";
 
@@ -309,7 +318,7 @@ function close_change_form(){
 function open_change_form(){
   document.getElementById("new-subject").value = "";
   document.getElementById("change-subject-form").style = "display:block";
-  document.getElementById("close-subject").style = "display:inline";
+  document.getElementById("close-subject").style = subject && "display:inline" || "display:none";
   document.getElementById("cur-subject").style = "display:none";
   document.getElementById("new-subject").style = "display:inline";
   document.getElementById("open-subject").style = "display:none";
diff --git a/sample-assets/view.html b/sample-assets/view.html
@@ -3,7 +3,7 @@
   <head>
     <meta charset="utf-8" />
     <meta name="viewport" content="width=device-width">
-    <title>Plannification JDR</title>
+    <title>Planification JDR</title>
     <link rel="stylesheet" type="text/css" href="style.css" />
     <script>
 var val_class=["val0","val1","val2","val3","val4","val5"];
@@ -17,6 +17,15 @@ window.onerror = function(msg, url, line) {
   item.scrollIntoView();
 }
 
+function pathname_to_topic(str) {
+  var components = str.split("/");
+  var fileName = components[components.length - 1];
+  var lastDotIndex = fileName.lastIndexOf("-");
+  return lastDotIndex !== -1 ? fileName.substring(0, lastDotIndex) : "";
+}
+
+var topic=pathname_to_topic(window.location.pathname);
+
 function append_cell(line, type, cl, text) {
   var elt = document.createElement(type);
   elt.className = cl;
@@ -35,8 +44,8 @@ function set_table(data) {
   for (const name in data[1]) {
     var elt = document.createElement("th");
     var link = document.createElement("a");
-    link.href = "edit.html";
-    link.onclick = function() { localStorage.setItem("subject", name); };
+    link.href = (topic ? topic + "-" : "") + "edit.html";
+    link.onclick = function() { localStorage.setItem("subject" + (topic ? "-" + topic : ""), name); };
     link.appendChild(document.createTextNode(name));
     elt.appendChild(link);
     line.appendChild(elt);
@@ -56,7 +65,7 @@ function set_table(data) {
 
 async function reload_table(){
   document.getElementById("reload-spinner").style = "display:inline";
-  const response = await fetch("all.json", { cache: "no-cache" });
+  const response = await fetch((topic || "all") + ".json", { cache: "no-cache" });
   const data = await response.json();
   set_table(data);
   document.getElementById("reload-spinner").style = "display:none";
@@ -68,7 +77,7 @@ async function create_subject(name){
     method: "POST",
     cache: "no-store",
     headers: { "Content-Type": "application/x-www-form-urlencoded" },
-    body: "name=" + encodeURIComponent(name),
+    body: (topic ? "topic=" + encodeURIComponent(topic) + "&" : "") + "name=" + encodeURIComponent(name),
   });
   if (response.ok) {
     reload_table();
@@ -86,7 +95,7 @@ function new_subject(){
     </script>
   </head>
   <body onload="reload_table()">
-    <h1>Plannification JDR</h1>
+    <h1>Planification JDR</h1>
     <p>Bienvenue sur la page d'organisation des soirés JdR. Cette page présente la vue d'ensemble des préférences. Les préférences peuvent être changées colonne par colonne, en suivant le lien en en-tête de la table. Une nouvelle colonne peut être créée au moyen du formulaire ci-dessous.</p>
     <form onsubmit="return new_subject()">
       <p>Nouveau pseudo :