« get me outta code hell

mtui - Music Text User Interface - user-friendly command line music player
about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--ui.js14
1 files changed, 12 insertions, 2 deletions
diff --git a/ui.js b/ui.js
index 15c0d34..cf45a4b 100644
--- a/ui.js
+++ b/ui.js
@@ -441,6 +441,14 @@ class AppElement extends FocusElement {
   async queueGrouplikeItem(topItem, afterItem = null, {movePlayingTrack = true} = {}) {
     const newTrackIndex = this.queueGrouplike.items.length
 
+    // The position which new tracks should be added at, if afterItem is
+    // passed.
+    const afterIndex = afterItem && this.queueGrouplike.items.indexOf(afterItem)
+
+    // Keeps track of how many tracks have been added; this is used so that
+    // a whole group can be queued in order after a given item.
+    let grouplikeOffset = 0
+
     const recursivelyAddTracks = item => {
       // For groups, just queue all children.
       if (isGroup(item)) {
@@ -471,11 +479,13 @@ class AppElement extends FocusElement {
 
       if (afterItem === 'FRONT') {
         items.unshift(item)
-      } else if (afterItem && items.includes(afterItem)) {
-        items.splice(items.indexOf(afterItem) + 1, 0, item)
+      } else if (afterItem) {
+        items.splice(afterIndex + 1 + grouplikeOffset, 0, item)
       } else {
         items.push(item)
       }
+
+      grouplikeOffset++
     }
 
     recursivelyAddTracks(topItem)