« 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.js19
1 files changed, 14 insertions, 5 deletions
diff --git a/ui.js b/ui.js
index 740b7f9..5541d81 100644
--- a/ui.js
+++ b/ui.js
@@ -37,7 +37,7 @@ class AppElement extends FocusElement {
       if (isGroup(item)) {
         this.grouplikeListingElement.loadGrouplike(item)
       } else {
-        this.queueGrouplikeItem(item)
+        this.playGrouplikeItem(item)
       }
     })
     this.grouplikeListingElement.on('queue', item => this.queueGrouplikeItem(item))
@@ -117,7 +117,7 @@ class AppElement extends FocusElement {
     this.player.togglePause()
   }
 
-  async queueGrouplikeItem(item, play = true) {
+  async queueGrouplikeItem(item, play = true, afterItem = null) {
     const newTrackIndex = this.queueGrouplike.items.length
 
     handleTrack: {
@@ -142,7 +142,12 @@ class AppElement extends FocusElement {
         items.splice(items.indexOf(item), 1)
       }
 
-      items.push(item)
+      if (afterItem && items.includes(afterItem)) {
+        items.splice(items.indexOf(afterItem) + 1, 0, item)
+      } else {
+        items.push(item)
+      }
+
       this.queueListingElement.buildItems()
     }
 
@@ -164,7 +169,7 @@ class AppElement extends FocusElement {
     }
   }
 
-  async playGrouplikeItem(item) {
+  async playGrouplikeItem(item, shouldQueue = true) {
     if (this.player === null) {
       throw new Error('Attempted to play before a player was loaded')
     }
@@ -175,6 +180,10 @@ class AppElement extends FocusElement {
       playingThisTrack = false
     })
 
+    if (shouldQueue) {
+      this.queueGrouplikeItem(item, false, this.playingTrack)
+    }
+
     // TODO: Check if it's an item or a group
 
     const downloadFile = await this.downloadGrouplikeItem(item)
@@ -221,7 +230,7 @@ class AppElement extends FocusElement {
       queueIndex = queue.items.length - 1
     }
 
-    this.playGrouplikeItem(queue.items[queueIndex])
+    this.playGrouplikeItem(queue.items[queueIndex], false)
   }
 }