]> git.cworth.org Git - lmno-server/commitdiff
Refactor admin page a bit
authorCarl Worth <cworth@cworth.org>
Thu, 25 Jun 2020 03:19:18 +0000 (20:19 -0700)
committerCarl Worth <cworth@cworth.org>
Fri, 26 Jun 2020 14:37:59 +0000 (07:37 -0700)
Using a new filter to map an array to a new array of just a single
property value from each object of the original array.

This lets us avoid using the "for" directive in the template, as well
as use the "join" filter to get commas between each item in the list,
(which is hard to do with the "for" directive in the template).

lmno.js
templates/admin.html

diff --git a/lmno.js b/lmno.js
index 0db678957f0d0e09717fd9e6257d5c3d57189dff..569d33d620244371488a64bcdca10b8804602b72 100644 (file)
--- a/lmno.js
+++ b/lmno.js
@@ -59,14 +59,21 @@ njx.addFilter('active', function(list) {
   if (list)
     return list.filter(e => e.active === true);
   else
-    return null;
+    return [];
 });
 
 njx.addFilter('idle', function(list) {
   if (list)
     return list.filter(e => e.active === false);
   else
-    return null;
+    return [];
+});
+
+njx.addFilter('map_prop', function(list, prop) {
+  if (list)
+    return list.map(e => e[prop]);
+  else
+    return [];
 });
 
 /* Load each of our game mini-apps.
index 3a25e608d2562c0c44e9fb367014b9857982f240..e2ed1b7664a94d1c4de97f3aa2156fd804b466e8 100644 (file)
 <ul>
   {% for game in games.active %}
   <li>
-    {{game.meta.name}}: {{ game.id }} {{ game.players.length }} players
-    <ul>
-      <li>
-        Active players:
-        {% for player in game.players|active %}
-          {{ player.name }}
-        {% endfor %}
-      </li>
-      <li>
-        Idle players:
-        {% for player in game.players|idle %}
-          {{ player.name }}
-        {% endfor %}
-      </li>
-    </ul>
+    {{game.meta.name}}: {{ game.id }} ({{ game.players|active|length }}/{{ game.players.length }})
+    <strong>
+      Active:
+      {{ game.players|active|map_prop("name")|join(", ") }}
+    </strong>
+    Idle:
+    {{ game.players|idle|map_prop("name")|join(", ") }}
   </li>
   {% endfor %}
 </ul>
 <ul>
   {% for game in games.idle %}
   <li>
-    {{game.meta.name}}: {{ game.id }} {{ game.players.length }} players
-    <ul>
-      <li>
-        {% for player in game.players %}
-          {{ player.name }}
-        {% endfor %}
-      </li>
-    </ul>
+    {{game.meta.name}}: {{ game.id }} ({{ game.players.length }}):
+    {{ game.players|map_prop("name")|join(", ") }}
   </li>
   {% endfor %}
 </ul>