« get me outta code hell

Play on select - mtui - Music Text User Interface - user-friendly command line music player
about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFlorrie <towerofnix@gmail.com>2018-06-01 08:50:48 -0300
committerFlorrie <towerofnix@gmail.com>2018-06-01 08:50:48 -0300
commit7ca9a6a4002990e8c8e38d039a0bed9bf5b3a488 (patch)
treec35346a4cfa655ee82f6ea1b51038d874e267b61
parentb374e3ce617f4f1392f470afbb0f4982b3e8d53f (diff)
Play on select
-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)
   }
 }