]> littlesong.place Git - littlesongplace.git/commitdiff
Refactor song lists to use macro
authorChris Fulljames <christianfulljames@gmail.com>
Sun, 13 Jul 2025 17:44:44 +0000 (13:44 -0400)
committerChris Fulljames <christianfulljames@gmail.com>
Sun, 13 Jul 2025 17:44:44 +0000 (13:44 -0400)
src/littlesongplace/static/player.js
src/littlesongplace/templates/base.html
src/littlesongplace/templates/index.html
src/littlesongplace/templates/jam-event.html
src/littlesongplace/templates/playlist.html
src/littlesongplace/templates/profile.html
src/littlesongplace/templates/song-list.html [deleted file]
src/littlesongplace/templates/song-macros.html
src/littlesongplace/templates/songs-by-tag.html

index d1da4433e7433406a290b6208a9558e576e8dc4c..e48bfa59220088727abb3a049145170e050f939f 100644 (file)
@@ -175,6 +175,58 @@ function songUpdate() {
     document.getElementById("player-total-time").textContent = getTimeString(audio.duration);
 }
 
+// Shown song details when the "..." button is clicked in a song list
+function showSongDetails(event) {
+    var songElement = event.target.closest(".song");
+    var songDetails = songElement.querySelector(".song-details");
+    var detailsToggle = songElement.querySelector(".details-toggle img");
+    if (songDetails.hidden) {
+        // Show details
+        songDetails.hidden = false;
+        detailsToggle.alt = "Hide Details";
+        detailsToggle.className = "lsp_btn_hide02";
+        detailsToggle.src = customImage(document.getElementById("lsp_btn_hide02"), detailsToggle);
+    }
+    else {
+        // Hide details
+        songDetails.hidden = true;
+        detailsToggle.alt = "Show Details";
+        detailsToggle.className = "lsp_btn_show02";
+        detailsToggle.src = customImage(document.getElementById("lsp_btn_show02"), detailsToggle);
+    }
+    return false;
+}
+
+// Shuffle the songs in a song list
+function shuffleSongList(event) {
+    var songList = event.target.closest(".song-list");
+    var songs = songList.querySelector(".song-list-songs");
+    if (event.target.checked) {
+        // Store original list so it can be restored later
+        songList.dataset.original = songs.innerHTML;
+
+        // Shuffle
+        var songElements = [];
+        while (songs.firstElementChild) {
+            songElements.push(songs.lastElementChild);
+            songs.removeChild(songs.lastElementChild);
+        }
+        for (let i = songElements.length - 1; i >= 0; i --) {
+            const j = Math.floor(Math.random() * (i + 1));
+            [songElements[i], songElements[j]] = [songElements[j], songElements[i]];
+        }
+        for (const child of songElements) {
+            songs.appendChild(child);
+        }
+    }
+    else {
+        // Unshuffle
+        if (songList.dataset.original) {
+            songs.innerHTML = songList.dataset.original;
+        }
+    }
+}
+
 // Add event listeners
 var m_firstLoadPlayer = true;
 document.addEventListener("DOMContentLoaded", (event) => {
index e3a3fdc43dce93cf6bfe6f24a007243a80f3a46a..253763b3674b8f6ed7a0f540397dd44459bd5583 100644 (file)
@@ -4,7 +4,7 @@
         <title>{% block title %}{% endblock %}</title>
         <link rel="stylesheet" href="/static/styles.css?v=5"/>
         <link rel="icon" type="image/x-icon" href="/static/lsp_notes.png?v=1"/>
-        <script src="/static/player.js?v=4"></script>
+        <script src="/static/player.js?v=5"></script>
         <script src="/static/nav.js?v=4"></script>
         <meta name="viewport" content="width=device-width, initial-scale=1">
 
index 587ff5ddc31790e74bec81aefafe2a0bfe8c6a95..1fc7d0c47bd49ffe26526c7181aaab9b1b5213ff 100644 (file)
@@ -33,6 +33,7 @@
 </div>
 
 <h2>hot new tunes</h2>
-{% include "song-list.html" %}
+{%- from "song-macros.html" import song_list %}
+{{ song_list(songs, current_user_playlists) }}
 
 {% endblock %}
index 44a3f95e33f701412c1c653de2a3dc09d5c559ef..e891a849619b77318f5baefa5f91c9f36bb58d48 100644 (file)
@@ -52,7 +52,9 @@
     <br/>
     <br/>
     {% if songs %}<p><small>This event has received {{ songs|length }} {% if songs|length > 1 %}entries{% else %}entry{% endif %}</small></p>{% endif %}
-    {% include "song-list.html" %}
+
+    {%- from "song-macros.html" import song_list %}
+    {{ song_list(songs, current_user_playlists) | indent(4) }}
     {%- endif %}
 
     <h2>Comments</h2>
index f7a8a60c8d00eaba0d9d39c364f395b2a9a00ab2..d9b5ad3f359433dc58f346522b05ebafa4dfa85f 100644 (file)
@@ -43,7 +43,8 @@ function hidePlaylistEditor() {
 
 {%- endif %}
 
-{% include "song-list.html" %}
+{%- from "song-macros.html" import song_list -%}
+{{ song_list(songs, current_user_playlists) }}
 
 {% if session["userid"] == userid -%}
 <!-- Drag-and-drop playlist editor -->
index 21dc231421df2c5468f5cf5176d093810df8d206..1e71c9a08cda2771ba292271611c0992c33130c9 100644 (file)
     {% endif %}
 
     <!-- Song List -->
-    {% include "song-list.html" %}
+    {%- from "song-macros.html" import song_list -%}
+    {{ song_list(songs, current_user_playlists) | indent(4) }}
 </div>
 
 {% endif %}
diff --git a/src/littlesongplace/templates/song-list.html b/src/littlesongplace/templates/song-list.html
deleted file mode 100644 (file)
index b333980..0000000
+++ /dev/null
@@ -1,93 +0,0 @@
-{% from "song-macros.html" import song_info, song_details %}
-
-<div class="song-list">
-    {% if songs|length > 1 %}
-    <div class="song-list-controls">
-        <label><input type="checkbox" name="shuffle" onchange="shuffleSongList(event)">Shuffle</label>
-    </div>
-    {% endif %}
-
-    <div class="song-list-songs">
-        {% for song in songs %}
-        {%- if not (song.hidden and session['userid'] != song.userid) -%}
-        <div class="song" data-song="{{ song.json() }}">
-            <div class="song-main">
-                <div class="song-list-pfp-container">
-                    {%- if song.user_has_pfp %}
-                    <!-- Profile Picture -->
-                    <img class="small-pfp" src="/pfp/{{ song.userid }}" onerror="this.style.display = 'none'" width="32" height="32" />
-                    {%- endif %}
-                </div>
-
-                {{ song_info(song) | indent(12) }}
-
-                <div class="song-buttons">
-                    <!-- Details Button -->
-                    <button onclick="return showDetails(event)" class="song-list-button details-toggle" title="Toggle Details">
-                        <img class="lsp_btn_show02" alt="Show Details">
-                    </button>
-
-                    <!-- Play Button -->
-                    <button onclick="return play(event)" class="song-list-button" title="Play">
-                        <img class="lsp_btn_play02" alt="Play">
-                    </button>
-                </div>
-            </div>
-            {{ song_details(song, current_user_playlists) | indent(8) }}
-        </div>
-        {%- endif -%}
-        {% endfor %}
-    </div>
-</div>
-
-<script>
-function showDetails(event) {
-    var songElement = event.target.closest(".song");
-    var songDetails = songElement.querySelector(".song-details");
-    var detailsToggle = songElement.querySelector(".details-toggle img");
-    if (songDetails.hidden) {
-        // Show details
-        songDetails.hidden = false;
-        detailsToggle.alt = "Hide Details";
-        detailsToggle.className = "lsp_btn_hide02";
-        detailsToggle.src = customImage(document.getElementById("lsp_btn_hide02"), detailsToggle);
-    }
-    else {
-        // Hide details
-        songDetails.hidden = true;
-        detailsToggle.alt = "Show Details";
-        detailsToggle.className = "lsp_btn_show02";
-        detailsToggle.src = customImage(document.getElementById("lsp_btn_show02"), detailsToggle);
-    }
-    return false;
-}
-
-function shuffleSongList(event) {
-    var songList = event.target.closest(".song-list");
-    var songs = songList.querySelector(".song-list-songs");
-    if (event.target.checked) {
-        // Store original list so it can be restored later
-        songList.dataset.original = songs.innerHTML;
-
-        // Shuffle
-        var songElements = [];
-        while (songs.firstElementChild) {
-            songElements.push(songs.lastElementChild);
-            songs.removeChild(songs.lastElementChild);
-        }
-        for (let i = songElements.length - 1; i >= 0; i --) {
-            const j = Math.floor(Math.random() * (i + 1));
-            [songElements[i], songElements[j]] = [songElements[j], songElements[i]];
-        }
-        for (const child of songElements) {
-            songs.appendChild(child);
-        }
-    }
-    else {
-        // Unshuffle
-        if (songList.dataset.original) {
-            songs.innerHTML = songList.dataset.original;
-        }
-    }
-}
-</script>
index d8b604c089674e2ee578ad0aa05774abddf58832..0b8ddf88ed86c6cff72e0e7deb5af7e4e82cbdb6 100644 (file)
     {{ comment_thread(song.threadid, session["userid"], song.userid, song.get_comments()) }}
 </div>
 {% endmacro %}
+
+{% macro song_list_entry(song, current_user_playlists) -%}
+{%- if not (song.hidden and session['userid'] != song.userid) -%}
+<div class="song" data-song="{{ song.json() }}">
+    <div class="song-main">
+        <div class="song-list-pfp-container">
+            {%- if song.user_has_pfp %}
+            <!-- Profile Picture -->
+            <img class="small-pfp" src="/pfp/{{ song.userid }}" onerror="this.style.display = 'none'" width="32" height="32" />
+            {%- endif %}
+        </div>
+        {{ song_info(song) | indent(8) }}
+        <div class="song-buttons">
+            <!-- Details Button -->
+            <button onclick="return showSongDetails(event)" class="song-list-button details-toggle" title="Toggle Details">
+                <img class="lsp_btn_show02" alt="Show Details">
+            </button>
+
+            <!-- Play Button -->
+            <button onclick="return play(event)" class="song-list-button" title="Play">
+                <img class="lsp_btn_play02" alt="Play">
+            </button>
+        </div>
+    </div>
+    {{ song_details(song, current_user_playlists) | indent(4) }}
+</div>
+{%- endif -%}
+{%- endmacro %}
+
+{% macro song_list(songs, current_user_playlists) -%}
+<div class="song-list">
+    {% if songs|length > 1 %}
+    <div class="song-list-controls">
+        <label><input type="checkbox" name="shuffle" onchange="shuffleSongList(event)">Shuffle</label>
+    </div>
+    {% endif %}
+
+    <div class="song-list-songs">
+        {% for song in songs -%}
+        {{ song_list_entry(song, current_user_playlists) | indent(8) }}
+        {%- endfor %}
+    </div>
+</div>
+{%- endmacro %}
+
index 7a3e1c518a606b601efb30a87df9fc7b65e238fd..6e03269be69f18efd30d2c9d50a5cea275f7b476 100644 (file)
@@ -18,6 +18,7 @@
     </div>
 {% endif %}
 
-{% include "song-list.html" %}
+{% from "song-macros.html" import song_list %}
+{{ song_list(songs, current_user_playlists) }}
 
 {% endblock %}