« get me outta code hell

Queue whole group - 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-05-31 17:10:22 -0300
committerFlorrie <towerofnix@gmail.com>2018-05-31 17:10:22 -0300
commit1542bb95af86bfd3aeda7919f7abde6a699e5198 (patch)
tree37d4d442ac463eb4906920e6837eef205360f3e5
parentf633034befb44ba0465271c464b063ddaa10c552 (diff)
Queue whole group
-rw-r--r--index.js2
-rw-r--r--todo.txt13
-rw-r--r--ui.js54
3 files changed, 49 insertions, 20 deletions
diff --git a/index.js b/index.js
index 676a5f1..a345301 100644
--- a/index.js
+++ b/index.js
@@ -55,7 +55,7 @@ async function main() {
     ]
   }
 
-  // grouplike = require('./library.json')
+  grouplike = require('./library.json')
 
   grouplike = updatePlaylistFormat(grouplike)
 
diff --git a/todo.txt b/todo.txt
new file mode 100644
index 0000000..58b25f9
--- /dev/null
+++ b/todo.txt
@@ -0,0 +1,13 @@
+TODO: A way to see the path of the currently selected item in any grouplike
+      element. Each item in the path should be a button that, when pressed,
+      makes the MAIN grouplike view navigate there.
+
+TODO: A "shuffle queue" button!
+
+TODO: A "clear queue" button.
+
+TODO: Don't immediately queue an entire group if it has more than 100 items -
+      ask first.
+
+TODO: A way to jump to an item with a particular name. Probably press "/".
+      It'd be nice if the closest match got highlighted while you were typing.
diff --git a/ui.js b/ui.js
index a9c2d37..5a6a6c9 100644
--- a/ui.js
+++ b/ui.js
@@ -40,6 +40,7 @@ class AppElement extends FocusElement {
         this.queueGrouplikeItem(item)
       }
     })
+    this.grouplikeListingElement.on('queue', item => this.queueGrouplikeItem(item))
 
     this.queueGrouplike = {isTheQueue: true, items: []}
 
@@ -117,25 +118,38 @@ class AppElement extends FocusElement {
   }
 
   async queueGrouplikeItem(item, play = true) {
-    // TODO: Check if it's an item or a group
+    const newTrackIndex = this.queueGrouplike.items.length
 
-    const items = this.queueGrouplike.items
+    handleTrack: {
+      // For groups, just queue all children.
+      if (isGroup(item)) {
+        for (const child of item.items) {
+          await this.queueGrouplikeItem(child, false)
+        }
 
-    // You can't put the same track in the queue twice - we automatically
-    // remove the old entry. (You can't for a variety of technical reasons,
-    // but basically you either have the display all bork'd, or new tracks
-    // can't be added to the queue in the right order (because Object.assign
-    // is needed to fix the display, but then you end up with a new object
-    // that doesn't work with indexOf).)
-    if (items.includes(item)) {
-      items.splice(items.indexOf(item), 1)
-    }
+        break handleTrack
+      }
+
+      const items = this.queueGrouplike.items
 
-    items.push(item)
-    this.queueListingElement.buildItems()
+      // You can't put the same track in the queue twice - we automatically
+      // remove the old entry. (You can't for a variety of technical reasons,
+      // but basically you either have the display all bork'd, or new tracks
+      // can't be added to the queue in the right order (because Object.assign
+      // is needed to fix the display, but then you end up with a new object
+      // that doesn't work with indexOf).)
+      if (items.includes(item)) {
+        items.splice(items.indexOf(item), 1)
+      }
+
+      items.push(item)
+      this.queueListingElement.buildItems()
+    }
 
-    if (play && !this.playingTrack) {
-      this.playGrouplikeItem(item)
+    // This is the first new track, if a group was queued.
+    const newTrack = this.queueGrouplike.items[newTrackIndex]
+    if (play && !this.playingTrack && newTrack) {
+      this.playGrouplikeItem(newTrack)
     }
   }
 
@@ -252,6 +266,7 @@ class GrouplikeListingElement extends ListScrollForm {
         const itemElement = new GrouplikeItemElement(item, this.recordStore)
         itemElement.on('download', () => this.emit('download', item))
         itemElement.on('select', () => this.emit('select', item))
+        itemElement.on('queue', () => this.emit('queue', item))
         this.addInput(itemElement)
       }
     } else if (!this.grouplike.isTheQueue) {
@@ -261,6 +276,7 @@ class GrouplikeListingElement extends ListScrollForm {
     if (wasSelected) {
       if (resetIndex) {
         this.curIndex = 0
+        this.scrollItems = 0
         this.updateSelectedElement()
       } else {
         this.root.select(this)
@@ -320,11 +336,11 @@ class GrouplikeItemElement extends Button {
 
   keyPressed(keyBuf) {
     // TODO: Helper function for this
-    if (keyBuf[0] === 'd'.charCodeAt(0) || keyBuf[0] === 'D'.charCodeAt(0)) {
+    if (telc.isCaselessLetter(keyBuf, 'd')) {
       this.emit('download')
-    }
-
-    if (telc.isSelect(keyBuf)) {
+    } else if (telc.isCaselessLetter(keyBuf, 'q')) {
+      this.emit('queue')
+    } else if (telc.isSelect(keyBuf)) {
       this.emit('select')
     }
   }